dear all, my stopwatch seems like not working. as for timing, it should be inside the interrupt loop together with my external interrupt (infrared sensor), * once the infrared sensor detect signal, the stopwatch will start to count, once detect again the stop watch will stop.
based on my current code, it seems like when the infrared detect, the stopwatch will run , but when a second interrupt occur, the stopwatch cannot stop. May some one help to edit my code. i had already cleared all possible flag after each interrupt.
#include <pic.h>
#define _XTAL_FREQ 4000000L
//__CONFIG (FOSC_INTRCIO & WDTE_OFF & PWRTE_ON & MCLRE_OFF & CP_OFF & BOREN_OFF);
unsigned int n,g,p,q,r,s,t,a,b,c,d,start,x,counter=0;
unsigned char display []={0x3F, 0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; //0 1 2 3 4 5 6 7 8 9
void main (void)
{
ANSEL=0x00;
TRISC=0b00000000;
TRISB=0x00;
TRISA=0b00000100;
nRABPU=0;
WPUA=0b00000100;
PORTC=0X00;
PORTB=0b00000000;
OPTION_REG=0x07;
TMR0=0xd8;
IOCA=0x04;//ENABLE RA2 INTERRUPT
RABIE=1;// PROTA/PORTB change interrupt enable
//PEIE=1;
T0IE=0;// Timer0 overflow interrupt enable
GIE=1; // Global interrupt enable
while(1)
{
}
}
static void interrupt isr(void)
{
if(RABIF)
{
if (x==1)
{
x=0;
}
else
{
x=1;
TMR0=0;
T0IE=0;
T0IF = 0;
RABIF = 0;
}
if (x==0)
{
T0IE=1;
TMR0=0xd0;
PORTB=0b11100000;
PORTC=display[d];
__delay_ms(1);
PORTB=0b11010000;
PORTC=display[c];
__delay_ms(1);
PORTB=0b10110000;
PORTC=display;
__delay_ms(1);
PORTB=0b01110000;
PORTC=display[a];
__delay_ms(1);
d++;
if(d>9)
{
d=0;
if (c<10)
c++;
else c=0;
}
if(c>9)
{
c=0;
if(b<10)
b++;
else b=0;
}
if(b>9)
{
b=0;
if(a<5)
a++;
else a=0;
}
TMR0=0xd8;
T0IF = 0;
RABIF = 0;
}
}
}
based on my current code, it seems like when the infrared detect, the stopwatch will run , but when a second interrupt occur, the stopwatch cannot stop. May some one help to edit my code. i had already cleared all possible flag after each interrupt.
#include <pic.h>
#define _XTAL_FREQ 4000000L
//__CONFIG (FOSC_INTRCIO & WDTE_OFF & PWRTE_ON & MCLRE_OFF & CP_OFF & BOREN_OFF);
unsigned int n,g,p,q,r,s,t,a,b,c,d,start,x,counter=0;
unsigned char display []={0x3F, 0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; //0 1 2 3 4 5 6 7 8 9
void main (void)
{
ANSEL=0x00;
TRISC=0b00000000;
TRISB=0x00;
TRISA=0b00000100;
nRABPU=0;
WPUA=0b00000100;
PORTC=0X00;
PORTB=0b00000000;
OPTION_REG=0x07;
TMR0=0xd8;
IOCA=0x04;//ENABLE RA2 INTERRUPT
RABIE=1;// PROTA/PORTB change interrupt enable
//PEIE=1;
T0IE=0;// Timer0 overflow interrupt enable
GIE=1; // Global interrupt enable
while(1)
{
}
}
static void interrupt isr(void)
{
if(RABIF)
{
if (x==1)
{
x=0;
}
else
{
x=1;
TMR0=0;
T0IE=0;
T0IF = 0;
RABIF = 0;
}
if (x==0)
{
T0IE=1;
TMR0=0xd0;
PORTB=0b11100000;
PORTC=display[d];
__delay_ms(1);
PORTB=0b11010000;
PORTC=display[c];
__delay_ms(1);
PORTB=0b10110000;
PORTC=display;
__delay_ms(1);
PORTB=0b01110000;
PORTC=display[a];
__delay_ms(1);
d++;
if(d>9)
{
d=0;
if (c<10)
c++;
else c=0;
}
if(c>9)
{
c=0;
if(b<10)
b++;
else b=0;
}
if(b>9)
{
b=0;
if(a<5)
a++;
else a=0;
}
TMR0=0xd8;
T0IF = 0;
RABIF = 0;
}
}
}
Last edited: