Menu

Timer 2 issues

2016-03-29
2016-04-02
  • tony golding

    tony golding - 2016-03-29

    hi,

    i have recently been looking at using a timer as an alternative to using pause or wait delays in my code.

    currently i am just trying to get things working correctly using timer 2 set for approximately a 25ms timeout interrupt and increment a counter nnn times for my required delay for a simple visual display of 2 leds to cycle on and off a few times for each chosen delay period.

    i have tried to set registers and pr2 preload for the timer to get the required delay but i only get 1 cycle of on and off and thats it.

    this is a different route for me as an alternative to wait or delay commands so i feel im clearly missing something.

                  ;*******************************************************
                  ;************** Timer 2 pause alternative  *************
                  ;*******************************************************
    
      #chip 12f1840, 8
    
      #config MCLRE_OFF, CPD_ON, CP_ON, OSC_INTOSC, WRT_ALL
    
      ;Input Pin and Direction
      TRISA = b'00001000'
      LATA = b'00000000'
    
      '#define PORTA.0 - not used
      #define out_relay PORTA.1
      #define in_relay PORTA.2
      #define rx_in PORTA.3
      #define in_led PORTA.4
      #define out_led PORTA.5
    
      ;variables
      Dim led_loop_count as Byte
      Dim led_timer_count as Byte
      Dim led_flag as bit
      Dim led_state as bit
    
      ;set alternate pin locations:
      APFCON = b'00001000'
    
      setupTimer2
      set_up_delay
    
     ; Main Program starts here
     Do
    
     Loop
    
    sub Interrupt
    
     INTCON = 0x00
    
        if TMR2IF = 1 Then
    
          TMR2IF = 0
          led_flag = 1
    
        end if
    
     INTCON = 0xC0
    
     end sub
    
    sub set_up_delay
    
     led_loop_count = 3
    
     for count = 1 to led_loop_count
    
       set in_led on
       set out_led on
       led_timer_count = 0
       led_state = 1
    
        do until led_timer_count = 20
    
           if led_flag = 1 Then
    
            led_flag = 0
            led_timer_count += 1
    
           end if
    
        loop
    
        set in_led off
        set out_led off
        led_state = 0
    
        do until led_timer_count = 20
    
           if led_flag = 1 Then
    
            led_flag = 0
            led_timer_count += 1
    
           end if
    
        loop
    
     next count
    
     led_loop_count = 4
    
     for count = 1 to led_loop_count
    
       set in_led on
       set out_led on
       led_timer_count = 0
       led_state = 0
    
        do until led_timer_count = 10
    
           if led_flag = 1 Then
    
            led_flag = 0
            led_timer_count += 1
    
           end if
    
        loop
    
        set in_led off
        set out_led off
        led_state = 0
    
        do until led_timer_count = 10
    
           if led_flag = 1 Then
    
            led_flag = 0
            led_timer_count += 1
    
           end if
    
        loop
    
     next count
    
    end sub
    
    sub setupTimer2  '25ms TMR2 interrupt approx
    
      T2CON = 0x66
      PR2 = 240
      TMR2IE = 1
      INTCON = 0xC0
    
    end sub
    

    all that is clear is that i have missed something obviously and need some guidance.

    currently using gcb V0.95

    tony

     

    Last edit: tony golding 2016-03-30
  • tony golding

    tony golding - 2016-03-29

    thanks evan, have got a little rusty from lack of project time lately, just downloaded and will have a look.

     
  • tony golding

    tony golding - 2016-03-29

    the main thing for me is to set the registers manually rather than use the in built routines as the overall code when done will need every last piece of available space, my last project for a 12f1840 used 94% program memory as it was so big and that was only using absolute bare bones gcb routines for pwm, everything else was done setting registers manually.

    timer0 is already allocated to something else that will be part of this code, timer1 is used for the gate feature so timer2 has to be used for this function but i cant get it to seemingly work correctly.

    tony

     
  • Anobium

    Anobium - 2016-03-29

    Totally understand regarding optimisation. We are now optmising code. Do try the libraries and can then work with you to optimise the specific library. We have saving a large amount of memory in the libraries we have optmised.

     
  • tony golding

    tony golding - 2016-03-29

    thanks, but i specifically dont want to use the libraries/routines if i can avoid it, i would prefer to manually use/set the individual registers.

    tony

     
  • tony golding

    tony golding - 2016-03-30

    well it seems the issue is the timer calculator found online from mikroe, apparently the calculated 25ms pause is nowhere even close!!

     
    • Anobium

      Anobium - 2016-03-30

      I had the same issue. When you get this fixed let us know.

      Regarding optimisation: We have optimised PWM and ADC so far - we are saving many 100s of bytes per optimisation. We are making a patch release soon. It will include a number of these changes to improve Great Cow BASIC.

       
  • William Roth

    William Roth - 2016-04-02

    This is not really an issue with timer2 as it is working just fine. It is an issue with how youn are implementing the delay timer.

    I would just create a sub that generates a 25 ms delay with an input parameter for loop count. Then incorporate that into my source code. I would not bother with an interrupt since it is unnecessary.

    Here is an example using a 16F1829. Modify your code as necessary for 16F1840.

    #chip 16f1829, 8
    
    #config MCLRE_OFF, OSC_INTOSC
    
    #define out_led PORTB.7
    DIR PORTB.7 OUT '- WMR
    
    'Config timer 2
    T2CON = 0b01100010  '// Prescale 16:  Postscale 1:13 / OFF
    TMR2 = 0            '// Clear to zero
    PR2 = 240           '//  Match Value for 25Ms Timer
    
    ; Main Program starts here
    
    Do
        Delay_25ms 4    '// 4 x 25 ms  = 100 ms
        Set OUT_LED ON
        Delay_25ms 4     '// 4 x 25 ms = 100 ms
        SET OUT_LED OFF
    
    Loop
    
    Sub delay_25ms (in iterations)
        TMR2IF = 0
        TMR2 = 0
        SET TMR2ON ON
    
        Repeat iterations
        'TMR2IF will go high after 25 ms
            Do while TMR2IF <> 1
            loop
            TMR2IF = 0
        End repeat
        SET TMR2ON OFF
    End Sub
    
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.