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)

{
}
}


  • Hi Selma!

    I think, I know where the problem is.

    Your routine:

    [...]
    T2CON = 0x00;
    EA = 1;
    ET2 = 1;
    TR2 = 1;
    
    while (1) 
    
    {
    }
    [...]
    

    BTW: I am refering to the documentation of the philips 80C552. So some of the bits might have different names. Maybe you want to consult the maual for the philips 80c552 too, because I think, timer2 is described there pretty well.

    You want to trigger overflow on 16 bit overflows and your clock source is fosc/12. So you should set TSIS1 (bit7 of TM2COM) and [T2MS1 T2MS0] should be [0 1] (thats bit #1 and #0 of that register) to select the clock source being fosc/12. So you get a value 0x81 to select the proper mode for timer2.

    You didn't set the mode of the timer 2 right. you write 0x00 to TM2CON (Adr: 0xEA). This way, it is always halted.

    I don't know the processor you are using, have never played with it. So the register and flag names might be different. But I think, if you take a look at your processors manual, you will find out what I am talking about.

    Take care
    Sven