Microcontroller › 8051 › Interrupt of 1s -Atmel 89C51 › Now I tried like this below
Now I tried like this below but stil dont have 1 second per number which is divided with 3 . .. He put all numbers which are divided with 3 in one second
#include <reg51.h>
typedef unsigned char byte;
byte a[9]= {7,5,16,3,1,4,11,9,6};
byte counter;
byte frequency;
byte displ;
byte xdata display _at_ 0x8001;
byte started = 0;
int i=0;
sbit switcher=P0^0;
void Inic(void)
{
EA=1; // enable global interrupt
TMOD=1; // configure timer0 to 16 bit timer
TH0=0x3C; // set timer0 to 0xC350
TL0=0xAF; // 50,000 decimal
counter=1; // set counter to 1
}
void timer0(void) interrupt 1 using 2
{
TH0 = 0x3C; // set timer0 to C350 = 50,000
TL0 = 0xAF; // after each interrupt
counter–; // count the interrupt
if(counter == 0) // count 20 interrupts for 1 sec delay
{
counter = 20; // reset counter to 20
if(started == 1) // if timer is started
{
while(i < 9) // go through array "a" to find a multiple of 3
{
if( (a[i] % 3) == 0) // multiple of 3 has remainder = 0
{
display = (a[i]); // display a matching number
i++; // increment i to the next number in "a"
break; // break out of the while loop after displaying a number
}
i++; // increment i to the next number in "a"
} // end while i < 9
if(i == 9) // if 9, all numbers have been tested
{
ET0 = 0; // disable timer 0 interrupt
TR0 = 0; // stop timer0 – all of "a" has been checked
started = 0; // started = 0, stop until switch pressed again
}
} // end if started == 1
} // end if counter == 0
}
void main(void)
{
Inic(); // initialize timer0
switcher = 1; // write 1 to switcher to use as an input
// it should have a pullup resistor to pull it high
while(1) // continuous loop
{ // the switch should pull P0^0 low
if(switcher == 0) // using switcher as a start switch
if(started == 0) // if counter is stopped
{
started = 1; // started goes 1 when switch pressed
i = 0; // set i for first byte in "a" array
counter = 1; // counter to 1 to get first number
// quicker (or 20 to wait 1 second)
TH0=0x3C; // set timer0 to 0xC350
TL0=0xAF; // 50,000 decimal
ET0=1; // enable timer0 interrupt
TR0=1; // start timer0 running
}
}
}