Microcontroller › 8051 › Interrupt of 1s -Atmel 89C51
- This topic has 3 replies, 2 voices, and was last updated 9 years, 1 month ago by Mike.
-
AuthorPosts
-
November 27, 2015 at 10:10 am #4130MikeParticipant
How to fix timer0 to have one second interrupt ?
I have idea but dont work..
i tried to put 20 interruptes for 50ms because that is equal to 1s and that didnt work.I need one second to put some numbers on display every second..
Any other idea?
I mustn use delay(), only counter which i using for counting 20 interruptes !!!!!!!!!!!!!!!!!!
November 27, 2015 at 10:58 am #13527MikeParticipantSo, I need to write only numbers (every second) in string on display (memory mapped on 0x8001 adress) which are divding with 2 .
I must use counter not delay () for interrupt …
Below is code which i imagine but dont work correctly…
#include <reg51.h>
#include <math.h>
typedef unsigned char byte;
byte a[3]= {2,1,8};byte counter, frequency,displ;
int i=0;
byte xdata display _at_ 0x8001;sbit switcher=P0^0;
void Inic(void) {
EA=1;
ET0=1;
TMOD=1;
TH0=0x3C;
TL0=0xB0;
TR0=1;
counter=1;
frequency=0;
}void timer0(void) interrupt 1 using 2 {
TR0=0;
TH0=0x3C;
TL0=0xB0;
TR0=1;if(switcher) {
if(!(–counter)) {
counter=frequency;
display=displ;
}} }
void main(void) {
Inic();
while(1) {
if(switcher) {
for (i=0;i<3;i++) {
if(a/2) {
displ=a;
frequency=20;
} } } } }
December 3, 2015 at 11:04 am #13551Ashutosh BhattParticipantur logic is perfect. count 20 timer interruts of 50 ms. ur code might be wrong. post ur code
December 3, 2015 at 11:09 am #13553MikeParticipantNow 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
Code:#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
}
}
} -
AuthorPosts
- You must be logged in to reply to this topic.