Question) This is the problem of a pic microcontroller. In this problem, PIC16f877A Microcontroller Pin 0-5 of port B, indicates the value from 1 to 6 of a ludo dice. PIN-0 of PORTC is taken as a pushbutton input. when the push button is pressed At that moment the LEDs connected to Pin 0-5 of port B will display the indicated value of the ludo dice.
My problem is when I press the push button (in Proteus simulation) only one LED is on. But the LED is not displaying the value indicated by the LED at that moment. What's wrong with my code?
Code:
void main()
{
static unsigned int var;
TRISC.F0=1;
TRISB=0x00;
PORTB=0x00;
while(1)
{
for ( var =1 ; var <=63; var = (var<<1) +1)
{
PORTB = var;
delay_ms(500);
PORTB = 0;
delay_ms(500);
if( !PORTC.F0)
{
PORTB = var;
break;
}
else
continue;
}
}
}
My guess is that you are expecting the LEDs to display as long as you have the push button pushed.
However what will happen is, when the push button is low and your 'if' statement becomes true, then you will display the value of 'var' on the PORTB pins but the next 'break' statement will take you out of the 'for' loop and the outer 'while loop' will take you straight back into the start of the 'for' loop with 'var' set back to 1.
What you need to do is to detect when the button is pushed (making sure that you have debounced it first), display the 'var' number on the LEDs and then wait until the button is released.
Also the 'else continue' is not doing anything for you as you are at the end of the loop and it will 'continue' anyway.
Susan
Now that you were given the answer, do you understand WHY that works and yours didn't?
If you have not taken this step then you have not learned anything.
Susan