I'm beginer. I'm building a test project with Led_Blinking and Button. My code below (MCU P16F628A, 10MHz, RA0-RA3 is connecting buttons with 'Pull-up' resister):
Code: Select all
void Led_Blinking();
void OnLED();
void OffLED();
void main()
{
TRISB = 0; //PORT B is output
TRISA = 0x0f; //RA0-RA3 is input, RA4 is output
PORTA = 0xff;
PORTB = 0;
do{
if (PORTA.F0 == 0) //Key pressed?
{
Delay_ms(100); //Delay for key debounce
if (PORTA.F0 == 0) OnLED();
else OffLED();
}
else OffLED();
Led_Blinking();
}while(1);
}
void Led_Blinking()
{
PORTB.F0 = 1; // Turn on diodes(LED 0) on PORTB
// PORTA.F4 = 1;
Delay_ms(500); // Wait 1 second
PORTB.F0 = 0; // Turn off diodes(LED 0) on PORTB
// PORTA.F4 = 0;
Delay_ms(500); // Wait 1 second
}
void OnLED()
{
PORTB.F2 = 1; //LED 2 is ON
PORTB.F3 = 1; //LED 3 is ON
}
void OffLED()
{
PORTB.F2 = 0; //LED 2 is OFF
PORTB.F3 = 0; //LED 3 is OFF
}
- LED 2,3 are always ON. It don't change status when I pressed key.
- LED 0 is blinking.
Anyone tell me why my buttons don't effect?
Thank.