1

I cannot understand why my code won't work. It works when I set the condition PORTA == 0x00 but not when PORTA == 0x01. How do you check if a bit is high? Below is my code and my schematic (Crystal frequency is 4MHz).

#include<xc.h>

void main(){
    int cnt;
    int delay_cnt;
    TRISA = 1;                 // PortA as input
    TRISB = 0;                // PortB as output
    PORTB = 0x00;            // Initialize LED as off


    for(;;){                // Infinite loop
        if(PORTA == 0x01){
            for(cnt=0;cnt<3;cnt++){
                PORTB = 0x01; // Turns on LED
                for(delay_cnt=0;delay_cnt<10000;delay_cnt++); 
                PORTB = 0x00; // Turns off LED
                for(delay_cnt=0;delay_cnt<10000;delay_cnt++); 
            }
        }
    }
}

enter image description here

1 Answers1

1

In this case if (PORTA == 0x01), you are checking the whole port (8 bit).
If you want to check just bit 0 from the port use this:

if (PORTAbits.RA0 == 1){

or

if (PORTA & 0x01){
Mike
  • 4,041
  • 6
  • 20
  • 37