internetuser2k12
Banned
In the code
It checks if PORTA.F0 == 1 i.e., it checks if RA0 is 1, then it gives a delay of 30 ms then again it checks if RA0 and sw0 both == to 1.
If yes, sw0 == 0 means that switch was not previously pressed. So, It makes RB0 = 1 and also sw0 = 1. sw0 = 1 means switch has been pressed.
In the else if condition... It checks if RA0 == 1 then gives a delay of 30 ms. Then again it checks if RA0 == 1 and sw0 == 1. If it is true, i.e., if switch was previously pressed and RB0 was ON, then it turns off RB0 and also makes switch pressed flag i.e., sw0 = 0.
Instead, you can also use PORTB.F0 = ~PORTB.F0 for button press.
The code will be
Code:
if(PORTA.F0 == 1) {
Delay_ms(30);
if((PORTA.F0 == 1) && (sw0 == 0)) { //here sw=0 means switch off knw ??
PORTB.F0 = 1;
sw0 = 1;
}
else if ((PORTA.F0 == 1) && (sw0 == 1)) {
PORTB.F0 = 0;
sw0 = 0;
}
It checks if PORTA.F0 == 1 i.e., it checks if RA0 is 1, then it gives a delay of 30 ms then again it checks if RA0 and sw0 both == to 1.
If yes, sw0 == 0 means that switch was not previously pressed. So, It makes RB0 = 1 and also sw0 = 1. sw0 = 1 means switch has been pressed.
In the else if condition... It checks if RA0 == 1 then gives a delay of 30 ms. Then again it checks if RA0 == 1 and sw0 == 1. If it is true, i.e., if switch was previously pressed and RB0 was ON, then it turns off RB0 and also makes switch pressed flag i.e., sw0 = 0.
Instead, you can also use PORTB.F0 = ~PORTB.F0 for button press.
The code will be
Code:
if(PORTA.F0 == 1) {
Delay_ms(30);
if((PORTA.F0 == 1) {
PORTB.F0 = ~PORTB.F0;
}
Last edited: