----AJ----
Junior Member level 1
- Joined
- Mar 23, 2013
- Messages
- 15
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,401
Hi Guys,
I'm just starting out in the world of C and micro-controllers and I set myself a simple beginners task of using an external switch + counter + LED, all I want to do is:
When the external switch gets pressed, increment the counter by 1, when the counter gets to five blink the LED.
The code is below, and the problem seems to be that the counter is not behaving as expected, the LED does come on but at very random counts, sometimes after the button is pressed 3 times, then other times after maybe 10 times.
Can you have a look and point me in the right direction please as I think that I've tied myself up in knots somewhere,:-D
Cheers
Andy
I'm just starting out in the world of C and micro-controllers and I set myself a simple beginners task of using an external switch + counter + LED, all I want to do is:
When the external switch gets pressed, increment the counter by 1, when the counter gets to five blink the LED.
The code is below, and the problem seems to be that the counter is not behaving as expected, the LED does come on but at very random counts, sometimes after the button is pressed 3 times, then other times after maybe 10 times.
Can you have a look and point me in the right direction please as I think that I've tied myself up in knots somewhere,:-D
Code:
// PIC 12F683
// One external switch, on rising edge only count 1, when counter = 5 then blink LED
int i = 0;
void blink(void){
for(i = 0; i<4; i++)
{
GPIO.B0 = 1;
Delay_ms(100);
GPIO.B0 = 0;
Delay_ms(100);
}
}
void interrupt(void){ // ISR
if (TMR0 == 5)
{
INTCON.GIE = 0;
blink();
INTCON.GIE = 1;
INTCON.T0IE = 1;
TMR0 = 1;
INTCON.T0IF = 0;
}
}
void main(void){
TRISIO = 0x04;
CMCON0 = 0x07;
CMCON1 = 0x07;
ANSEL = 0;
ADCON0 = 0;
INTCON.GIE = 1;
INTCON = 0xB1;
INTCON.T0IE = 1;
INTCON.T0IF = 0;
OPTION_REG = 0x20;
OPTION_REG = 0b10110000;
TMR0 = 5;
IOC.IOC2 = 1;
while(1){//Do nothing
}
}
Cheers
Andy