Timer problem


Closed Thread
Results 1 to 3 of 3

Thread: Timer problem

  1. #1
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429

    Default Timer problem

    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:

    Code:
    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:

    Code:
            __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
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Kamikaze47 View Post
    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

  3. #3
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429


    Did you find this post helpful? Yes | No

    Default

    *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
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

Similar Threads

  1. Elapsed Timer Demo
    By Darrel Taylor in forum Code Examples
    Replies: 111
    Last Post: - 29th October 2012, 17:39
  2. Timer + rc5
    By naga in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 19th November 2009, 07:56
  3. Timer interrupt frequency
    By Samoele in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 17th January 2009, 23:49
  4. Interesting delay problem
    By Kreston in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 1st September 2008, 14:41
  5. Timer problem
    By Tastature in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 31st July 2008, 19:38

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts