PDA

View Full Version : Timer problem



Kamikaze47
- 21st February 2008, 13:19
I'm having a problem with the timers on the 18F4550. I wrote the below code to test the timer, but im getting unexpected results:


Define OSC 48

DEFINE LCD_DREG PORTD
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTC
DEFINE LCD_RSBIT 1
DEFINE LCD_EREG PORTC
DEFINE LCD_EBIT 2
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50

temp var byte

' Timer 0: 8-Bit, 1:256 prescaler, 128uS/tick
T0CON=%11010111

TMR0L=0
pauseus 2048
temp=TMR0L
lcdout $FE,1,$FE,2,#temp

loop: goto loop

At 128uS per tick of the timer, i'd expect a result of about 8, however I get 95.

I have a 4Mhz crystal, and heres my config:


__CONFIG _CONFIG1L, _PLLDIV_1_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
__CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H
__CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
__CONFIG _CONFIG3H, _PBADEN_OFF_3H
__CONFIG _CONFIG4L, _LVP_OFF_4L & _ICPRT_OFF_4L & _XINST_OFF_4L

skimask
- 21st February 2008, 13:47
At 128uS per tick of the timer, i'd expect a result of about 8, however I get 95.


At 48Mhz...
Each MCU instruction cycle = .083us ( 1/ (48mhz/4) )
.083us * 256 for the Timer 0 prescaler = 21.3us
2048 us pause / 21.3 us per T0 tick = 96

Kamikaze47
- 21st February 2008, 13:54
*slaps forehead*

I'm not sure how I ended up with 128uS/tick... I must have worked it out on a 8Mhz clock instead of 48Mhz.

thanks skimask