enable ports RB4 and RB3 on pic 18F4550
I have written this program in mplab v8.63 with c compiler C18 on the pic 18F4550. if I press the button on my picdem (S3) and there is a led connected on RB5 (with a resistor) then the led goes on. When i pressed on (S3) and there is a led on RB4, the the led will not be on (while I expected this) the same with RB3. Thit I forgot something to set?
The goal is for the red, green and blue LED separately measured with a LDR. but first I obviously must enable the ports RB5, RB4 and RB3.
#pragma code
/******************************************************************************/
void main (void)
{
TRISD = 0x00; // PORTD as output
TRISB = 0b00110000; // RB4 en RB5 as input
TRISA = 0x00; // RA output
RCONbits.IPEN = 0; // priority
INTCONbits.GIE = 1; // enable interrupt
INTCONbits.RBIE = 1; // interrupt portB on
while(1)
{
_asm sleep _endasm
}
}
#pragma interrupt ISR
void ISR (void)
{
//int red= 01110010011011110110111101100100;
int on = 1;
int off = 0;
if (INTCONbits.RBIF==1)
{
if(PORTBbits.RB5==0) 开发者_运维百科 // S3 pressed?
{
LATDbits.LATD1 ^= 1; // D2 toggle
LATAbits.LATA2 ^= on;
}
if(PORTBbits.RB4==0)
{
LATDbits.LATD1 ^= 1; // D2 toggle
LATAbits.LATA2 ^= on;
}
if(PORTBbits.RB3==0)
{
LATDbits.LATD1 ^= 1; // D2 toggle
LATAbits.LATA2 ^= on;
}
}
INTCONbits.RBIF = 0;
}
In the PIC TRIS registers, a bit set to 1 means input and 0 means output. You set TRISB = 0b00110000 which is all outputs on port B apart from RB5 and 4 which are inputs. If you need RB3 as a digital input as well, you need to set TRISB = 0b00111000. The bits are counted b7,b6,b5,b4,b3,b2,b1,b0.
However, you are expecting RB3 to trigger the RBIF which it does not as I explained here. So as you have it, only RB5 and 4 would trigger RBIF, but if RB3 was low at that time it would toggle D1 and A2. So move this code to main loop as I suggested until you get it working, then make it work on interrupt.
As ChrisJ says, some pins default to analog input unless you disable it so you wont get the digital input and interrupt on pin change that you are expecting using ADCON1
If the PBADEN
bit is set, PORTB is initialized with RB4:RB0 set as analog inputs. If so you must use ADCON1
to disable analog inputs on RB4:RB0. The example program at the beginning of Section 10.2 of the manual wites 0x0E to ADCON1
to properly initialize PORTB.
See also Section 21.0 for an explanation of ADCON1
bits.
精彩评论