timer1 help


Closed Thread
Results 1 to 4 of 4

Thread: timer1 help

  1. #1
    Join Date
    Aug 2009
    Posts
    63

    Default timer1 help

    hey sorry need some help on this... been working with timer1 to make leds blink from someone code example in the past. unforunately im a bit stuck

    Code:
    define OSC 20
    
    TRISA = 0
    TRISB = 0
    
    
    PORTA = 0
    PORTB = 0
    
    T1CON = %00110001       ' 0.524ms
    TMR1IF VAR PIR1.0       ' Overflow bit of Timer1.
    
    Output1 VAR PORTB.1     ' Blink 6secs.
    Output2 VAR PORTB.0     ' Blink 0.5secs.
    
    PreLoad     VAR WORD
    TimeCount   VAR BYTE
    TimetoBlink VAR BYTE
    
    PreLoad = 0 ' to get 500ms.
    TMR1H = PreLoad.HighByte
    
    TimeCount = 0
    TimetoBlink = 12 '12 = 6secs.
    
    PAUSE 50         ' OSC to Settle.
    
    
    Start:
    
    
       IF TMR1IF THEN Blink
    
       GOTO Start
        
        
    
    Blink: 'Each int is 0.500 sec.
        TMR1L = PreLoad.LowByte   ' Load the timer with preload value.
        TMR1H = PreLoad.HighByte
        TimeCount = TimeCount + 1 ' Count interrupts.
        Output2 = Output2 ^ 1     ' Toggle fast blinking output.    
    
        IF TimeCount = TimetoBlink THEN '6Secs ON, 6secs OFF.
          TimeCount = 0                   
          Output1 = Output1 ^ 1 
        ENDIF
       
        TMR1IF = 0                ' Clear TMR1 int. flag.  
        
        GOTO Start    
        
    END
    unfortunately the blinks are far too fast.... about 8 to 10x too fast.

    my pic is oscillating at 20mhz so i thought id just use that internal oscialltion. when u choose that oscillator it does Fosc/4 so its at 5mhz. with a word size timer1 i can measure up to 65536 ticks; with a 1:8 prescaler on that only takes about 0.7 seconds or something to complete. how do i get it so it will be 6 seconds? cant change prescaler anymore... and preloader wont make a difference since it only speeds up the cycle. i think anyway

    any help on this? thanks

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    If you run out of Ticks then you simply run out of Ticks... because somebody will come along and say "How can I have Timer1 count an HOUR or a DAY or a WEEK"?

    Eventually you run out of capacity for the Registers for the Time period you wish to count. There is nothing you can directly do about that...

    So the usual solution is to preset the Timer to count say 0.5 Seconds (which is within range), and when the period has elapsed you simply increment a BYTE. When that BYTE has incremented to 12 (for 12 x 0.5 Seconds = your desired 6 Seconds), zero the byte (for the next 6 Second timing period) and toggle your LED. So basically you are supplimenting Timer1 with another eight or sixteen bits, but those you have to increment yourself rather than have it done for you.

  3. #3
    Join Date
    Aug 2009
    Posts
    63


    Did you find this post helpful? Yes | No

    Default

    ok thank you mel,

    additionally i just wanna check my calcs are along right lines...

    ive timed the blinks and it takes 19 seconds for 10 "on and offs". on is 6 cycles and off is 6 cycles, so total is 120 cycles.

    each cycle is 65536 bits.. so 120x65536/19 = 410000 hz

    Fosc/4 means that its 410,000x4 = 1,640,000 hz

    this seems a bit of a random clock frequency? especially when the deault internal oscialltor is 4mhz and im running the actual pic itself on an external 20mhz oscillator.

    maybe i have missed something, which is the most likely thing but i cant see it at the moment

  4. #4
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Well, I was going to go through your code and verify your Timer settings, but not knowing what chip you are using that makes it kinda difficult.

    So, 'assuming' you are genuinely running at 20MHz (this now depends on your OSC and Configuration setting for your chosen PIC - and if you don't get that correct then everything is screwed thereafter...)...

    I tick = 200nS (FOsc/4), so an empty 16-bit Timer will execute in 65536 x 200nS = 13.1mS then multiplied by any scalers you have previously enabled.

    If you think your PIC speed is running oddly, then a simple blinky with PAUSE 10000 and your wristwatch should verify that your speed settings (which includes the DEFINE OSC 20) are all correct give or take.

Similar Threads

  1. Software PWM using Timer1
    By muqasim in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 18th December 2009, 11:49
  2. Help with TIMER1 + SLEEP
    By Yodoad in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 22nd May 2009, 15:07
  3. Time Period of Timer1
    By arnol34 in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 4th May 2007, 00:31
  4. Timer1 and Interupt wierdness
    By mind in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 23rd August 2005, 01:24
  5. Use of Timer1 with 16F819
    By Barry Johnson in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 25th January 2005, 16:25

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