This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

could not get counter2/timer2 working

Sven
thanks for the great info. For now i wrote the code for timer0 to get the
interrupt every 4.6ms (i have 12Mhz) clk and it seems to be working
and i get the port pin to toggle every 4.6ms. However when i use timer2
to do the same thing, i get the port pin high all the time. I am using the
87c51FA version chip which has timer0,1 and 2.
if anyone can check the code and see what is wrong, i would appreciate it.

the following is the timer 0 code to toggle every 4.6ms and it works.but
after this code i have timer2 code that is not working.


#include <REG52.H>
#include <stdio.h>
#include <string.h>

sbit P1_0 = P1^0;
#define timercount 0xee11
int i;
void timer0_ISR (void) interrupt 1
{
P1_0 ^= 1;
TR0 =0;
TL0 = (timercount & 0x00FF);
TH0 = (timercount >> 8);
TR0 = 1;
}


void main (void) {

/*------------------------------------------------
Setup the serial port for 2400 baud at 12MHz.
------------------------------------------------*/
SCON = 0x52;
TMOD = 0x20; /* TMOD: timer 1, mode 2, 8-bit reload */
TH1 = 0xf3; /* TH1: reload value for 2400 baud @ 12MHz */
TR1 = 1; /* TR1: timer 1 run */
TI = 1; /* TI: set TI to send first char of UART */
P1 = 0;


TMOD = (TMOD & 0xF0 ) | 0x01;
ET0 = 1;
TR0 = 1;
EA = 1;

while (1)

{
}
}


NOW ..THIS is the code for Timer2 and i couldn't get it working to get
the timer2 to do exactly as timer 0.

#include <REG52.H>

#include <stdio.h>
#include <string.h>
#define timercount 0xee11
sbit P1_0 = P1^0;

void timer2_ISR (void) interrupt 5
{
P1_0 ^= 1;
TR2 =0;
TL2 = (timercount & 0x00FF);
TH2 = (timercount >> 8);
TR2 = 1;

}


void main (void) {

/*------------------------------------------------
Setup the serial port for 2400baud at 12MHz.
------------------------------------------------*/
SCON = 0x52;
TMOD = 0x21;
TH1 = 0xf3;
TR1 = 1;
TI = 1;
P1 = 0;



T2CON = 0x00;
EA = 1;
ET2 = 1;
TR2 = 1;

while (1)

{
}
}


0