星期三, 6月 29, 2005

MCS51 8051 Use the Timer

Timer 0,Mode0 : 13 bit counter, not auto reload
clock source可選擇internal crystal (freq/12)或是external trigger (Pin: T0).

TMOD Reg (Not bit addressable)

bit 0 - 3 control Timer0
  • bit 0 : M0 (w M1, mode select)
  • bit 1 : M1 ( --)
  • bit 2 : C/T (Counter or Timer ? Select clock source)
  • bit 3 : GATE, Timer controllerd by Pin: INT0 ?
bit 4 - 7 are the same as bit 0 - 3, but used to control Timer1

TCON Reg ( bit addressable)
  • bit 0 : IT0, external interupt 0 pin, trigger mode (rising or falling )
  • bit 1 : IE0, external interrupt 0 pin (INT0) enable
  • bit 2 : IT1, external interrupt 1 pin, trigger mode (rising or falling )
  • bit 3 : IE1, external interrupt 1 pin (INT1) enable
  • bit 4 : TR0, Timer 0 enable
  • bit 5 : TF0, Timer 0 overflow flag
  • bit 6 : TR1, Timer 1 enable
  • bit 7 : TF1, Timer 1 overflow flag
TH0.TL0 Reg (Counter Register)
  • TH0 使用8 bit,代表13bit counter的hi 8 bits
  • TL0使用5 bits,代表13bit counter的lo 5 bits
一般的設定code是 (在main)
TH0 = value/32;
TL0 = value%32;Crystal = 12MHz.
因為timer用overflow trigger,所以value = (8192-要counter的次數)。

Example : 使用Timer0 作 5 ms的timer count。

TMOD = 0x00; //Mode0, C/T 0: use internal, GATE 0: no relation w INT0
TH0=(8192-5000)/32;
TL0=(8192-5000)%32;
IE = 0x82; // enable interrupt
TR0=1; // start gtimer

interrupt function

void tmr5ms(void) interrupt 1
{
TH0=5000/32;
TL0=5000%32;

tmrcounter ++;
}

沒有留言:

網誌存檔