Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

giving INPUTS to PIC18F452

Status
Not open for further replies.

mora

New Member
I wrote the following simple program to output a series of short pulses from all the pins of PORTC depending on an input to PORTB. To give "0000 0000" to PORTB as input I grounded all pins. To give "0000 0001", ground connection to RB0 pin was removed and nothing was connected. Is this the way to input "1" and "0" to the PIC ?

I checked the output from a pin of PORTC using an oscilloscope. I got the proper output only once. I couldn't even see a "0" in the oscilloscope when I tested again a several times. Please tell me whether I have written the program correctly. I'm not sure about giving inputs to the PIC as well.

IS THERE ANY OTHER WAY TO OUTPUT A SERIES OF SHORT PULSES UPON A GIVEN INPUT? I tried using PWM, but CCP1 pin did not go to zero when duty cycle was made 0. Logic used for branching was same as the following program. CCPRL1 was made zero to make duty cycle zero. Could you please tell me why this method doesn't work ?

Thanks.

#include <p18f452.h> /* for TRISC and PORTC declarations */
#include <delays.h>

/* Set configuration bits for use with ICD2 / PICDEM2 PLUS Demo Board:
* - set HS oscillator
* - disable watchdog timer
* - disable low voltage programming
*/
#pragma romdata CONFIG
_CONFIG_DECL(_CONFIG1H_DEFAULT & _OSC_HS_1H,\
_CONFIG2L_DEFAULT,\
_CONFIG2H_DEFAULT & _WDT_OFF_2H,\
_CONFIG3H_DEFAULT,\
_CONFIG4L_DEFAULT & _LVP_OFF_4L,\
_CONFIG5L_DEFAULT,\
_CONFIG5H_DEFAULT,\
_CONFIG6L_DEFAULT,\
_CONFIG6H_DEFAULT,\
_CONFIG7L_DEFAULT,\
_CONFIG7H_DEFAULT);
#pragma romdata

/*#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF */

/* connect all PINS of PORTB to ground to make PORTB = 0 */


//*********************Global Variables **********************

unsigned char temp_portb;
int i;

//************************************************************

void main(void)
{
TRISB = 0xff; // set PORTB as input
TRISC = 0x00; // PORTC pins set as output
temp_portb = 0x00; // initialize "temp_portb" to enter into the loop

while(1) // polling loop
{
PORTC = 0x00;

if(PORTB == 0x00)
{
PORTC = 0x00;
}

else if(PORTB == 0x01 && temp_portb != 0x01) // 0000 0001
{
for(i = 0; i < 10; i++)
{
PORTC = 0x00;
Delay1KTCYx(10); // 2ms
PORTC = 0xff;
Delay1KTCYx(10);
}
PORTC = 0x00;
}

else if(PORTB == 0x02 && temp_portb != 0x02) // 0000 0010
{
for(i = 0; i < 20; i++)
{
PORTC = 0x00;
Delay1KTCYx(10);
PORTC = 0xff;
Delay1KTCYx(10);
}
PORTC = 0x00;
}

else if(PORTB == 0x04 && temp_portb != 0x04) // 0000 0100
{
for(i = 0; i < 30; i++)
{
PORTC = 0x00;
Delay1KTCYx(10);
PORTC = 0xff;
Delay1KTCYx(10);
}
PORTC = 0x00;
}

else if(PORTB == 0x08 && temp_portb != 0x08) // 0000 1000
{
for(i = 0; i < 40; i++)
{
PORTC = 0x00;
Delay1KTCYx(10);
PORTC = 0xff;
Delay1KTCYx(10);
}
PORTC = 0x00;
}

else if(PORTB == 0x10 && temp_portb != 0x10) // 0001 0000
{
for(i = 0; i < 50; i++)
{
PORTC = 0x00;
Delay1KTCYx(10);
PORTC = 0xff;
Delay1KTCYx(10);
}
PORTC = 0x00;
}

temp_portb = PORTB; // each "for" loop is executed only once for an i/p
}
}
 
mora said:
I wrote the following simple program to output a series of short pulses from all the pins of PORTC depending on an input to PORTB. To give "0000 0000" to PORTB as input I grounded all pins. To give "0000 0001", ground connection to RB0 pin was removed and nothing was connected. Is this the way to input "1" and "0" to the PIC ?

No, it's not the right way, you need a resistor from the input pin to +5V in order to 'pull' the pin high. PortB on most PIC's has these internally, but you need to enable them.
 
To enable PortB weak pull-up resistor, you need to clear bit7 of INTCON2 and send a all HIGH to the PortB latch, as follow:

Code:
18FXX2 Datasheet DS39564B page 76

INTCON2 <bit 7>
 
/RBPU: PORTB Pull-up Enable bit

1 = All PORTB pull-ups are disabled
0 = PORTB pull-ups are enabled by individual port latch values
 
Thanks a lot for helping me guys. eblc1388 told me to enable PORTB pull ups. Would adding following 2 lines to code solve the problem ?

INTCON2 = 0x00; // in binary 0xxxx xxxx
LATB = 0xff;

or should i change only the INTCON2 register as follows

INTCON2 = 0x00; // in binary 0xxxx xxxx

Could you please tell me whether the above code is correct ?


Nigel,

Can u check the attachment to see whether i can connect a input to the PIC as shown. If wrong could u show me how to do it ??

Thanks.
 

Attachments

  • pic_input.jpg
    pic_input.jpg
    12.1 KB · Views: 626
Thanks for helping me on this Nigel. elbc 1388 said that I need to enable PortB weak pull-up resistor (by clearing bit7 of INTCON2)and send an all High to PORTB latch. Is it done the following way in C18 ?

INTCON2 = 0x00;
LATB = 0xff;

Could you correct me if I am wrong ?

Do i still need to send inputs as in your tutorials after enabling PORTB weak pull ups as above ?

Please help me on this. Thanks.
 
mora said:
Thanks for helping me on this Nigel. elbc 1388 said that I need to enable PortB weak pull-up resistor (by clearing bit7 of INTCON2)and send an all High to PORTB latch. Is it done the following way in C18 ?

INTCON2 = 0x00;
LATB = 0xff;

Could you correct me if I am wrong ?

Sorry, but I don't do C, and I don't use the 18F series, so I can't really comment on that.

But you only need to enable the weak pull-ups if you want to use switches on the pins, and DON'T want to add external resistors - notice my tutorials use external pull-ups, so you can use any input port.

Do i still need to send inputs as in your tutorials after enabling PORTB weak pull ups as above ?

Please help me on this. Thanks.

Yes, you still need to set the pins as inputs - they 'should' be inputs by default, but it's important never to assume this, always set the ports to be as you want.
 
I do C on the 18F series.

You can (and for code clarity perhaps should) assign the RBPU bit directly:
Code:
RBPU=0;

1. The bit should be 0, not 1, the enable the pullups.
2. PortB pullups only apply to PortB pins which are inputs:
Code:
TRISB=0xff;
Then your pushbuttons should connect the pin to ground when pressed. The internal pullups only provide a resistance connection to Vdd to give each a default "1" value; you cannot reconfigure registers to make it a default pulldown to "0".

You would not assign LATB since PortB is the input port and the only port with the capability of internal pullups. I'm guessing you meant to assign LATC?

PWM can output 0 on the pin. I don't know why this did not work for you unless you provide some code. For this we need to know the contents of the TMR2 and CCP registers, the mux bit of the config register, and TRIS register for the pin it is mux'ed to.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top