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

Timer and serial port

I am doing a project and want to use both the serial port at 2400bps and timer 0 for timing ticking.

for the serial: my setting is that:
TMOD=0x20;
TH1=-13;
TL1=1;
SCON=0x50;


for the timer:

TMOD=0x01;
TH0=(65536-5000)/256;
TL0=(65536-5000)%256;
IE=0x82;
TR0=1;

the problem is i find the TMOD need to set in both code.

How can i make two timer work seperately and work without collide? I find my system not work and the timer0 suddenly reset.



pls help

  • moreover,
    i have recheck the program,
    i found the when i put the data into the serial buuffer

    SBUF=data;

    the timer was stopped

    pls help



  • Welcome to the C language. If you would like to set the upper nybble of TMOD without affecting the lower nybble of TMOD try:

    TMOD &= 0x0F;
    TMOD |= 0x20;
    Or if you prefer one liners
    TMOD = TMOD & 0x0F | 0x20;
    Or you could make a little TMOD macro to do this, something like
    #define WRITE_TMOD_T1(nybble) do { TMOD = TMOD & 0x0F | (nybble) << 4 } while (0)
    #define WRITE_TMOD_T0(nybble) do { TMOD = TMOD & 0xF0 | (nybble) } while (0)
    Usage as per your values (Note the shift left by four in the T1 macro):
    WRITE_TMOD_T1(2);
    WRITE_TMOD_T0(1);
    TMOD should now equal 0x21. If you need a working UART driver, see my free one at http://www.embeddedfw.com

    Enjoy!