Menu

PIC16F1936 Timers

Help
2014-10-15
2014-10-21
  • Daniel Cioba

    Daniel Cioba - 2014-10-15

    Hi , how can i use timer 4/6 and their interrupts ? It doesn't seems to be implemented in GCB . In file timer.h i could add this timers but i don't know how to add interrupts

     
  • William Roth

    William Roth - 2014-10-16

    Hi Daniel,

    As I read the help file under Timers/Timer Overview there are functions for timer0 through timer5.

    As far as interrupts go you can refer to the help file under Interrupts/ON Interrupt and see that Timer0 = Timer5 are supported.

    What PIC are you using ?

     

    Last edit: William Roth 2014-10-16
  • Daniel Cioba

    Daniel Cioba - 2014-10-16

    I use PIC16F1936 as in my post title. It has Timer0 , 1 , 2 , 4 and 6. Only Timer0 , 1 and 2 are implemented for PIC microcontrollers in GCB. For the rest you must use manual register settings. I figured out from help file how to use Timer4 / 6 interrupts by creating a subrutine called "interrupts" for all interrupts and manually checking interrupts flags .
    So my problem is solved for now ... but it would be nice to improve GCB by adding all available timers and interrupts

     
  • Anobium

    Anobium - 2014-10-16

    Hi, what release of GCB are your using? The date of the timer.h would also be useful.

    Thank you.

    Anobium

     
  • Daniel Cioba

    Daniel Cioba - 2014-10-16

    Hi , i use last GCB stable release ,i think, that i downloaded from official site http://gcbasic.sourceforge.net/ Intermediate and Advanced GCB@Syn.zip 25/05/2014
    All files including timer.h are from that release , i didn't used GCB before

     

    Last edit: Daniel Cioba 2014-10-16
  • William Roth

    William Roth - 2014-10-16

    I modified the example code code in the help file to use PIC16F1936, timer4, and interrupt on timer4 overflow. It compiles fine. HOwever I don have the Chip to test. Here is the code. I am using Timer.h from the code repository dated 2014-08-23.

    ~~~~~

    chip 16F1936, 8

    config osc = int

    define MOTOR PORTB.0

    'Call the initialisation routine
    InitMotorControl

    'Main routine
    Do
    'Increase speed to full over 2.5 seconds
    For Speed = 0 to 100
    MotorSpeed = Speed
    Wait 25 ms
    Next
    'Hold speed
    Wait 1 s
    'Decrease speed to zero over 2.5 seconds
    For Speed = 100 to 0
    MotorSpeed = Speed
    Wait 25 ms
    Next
    'Hold speed
    Wait 1 s
    Loop

    'Setup routine
    Sub InitMotorControl
    'Clear variables
    MotorSpeed = 0
    PWMCounter = 0

    'Add a handler for the interrupt
    On Interrupt Timer4Overflow Call PWMHandler
    
    'Set up the timer
    InitTimer4 Osc, PS0_2
    'Timer 0 starts automatically on a PIC
    

    End Sub

    'PWM sub
    'This will be called when Timer 4 overflows
    Sub PWMHandler
    If MotorSpeed > PWMCounter Then
    Set MOTOR On
    Else
    Set MOTOR Off
    End If
    PWMCounter += 1
    If PWMCounter = 100 Then PWMCounter = 0
    End Sub
    ~~~~~~

     

    Last edit: William Roth 2014-10-16
  • Daniel Cioba

    Daniel Cioba - 2014-10-16

    Sorry , i made a mistake , Timer4 is working fine , only Timer6 is not
    Compiler error at InitTimer6 ... and On Interrupt Timer6Overflow ...

    Other question , timers2/4/6 all have a prescaler and a postscaler , it is possible to set the postscaler value ? InitTimer0 source, prescaler

     

    Last edit: Daniel Cioba 2014-10-16
  • Anobium

    Anobium - 2014-10-16

    I have just updated Timer.h. I have asked Hugh to complete a code review. I will post when completed.

    Revised file now additionally supports:

    InitTimer4
    InitTimer6
    Starttimer (4|6)
    Stoptimer (4|6)
    Cleartimer (4|6)
    Stoptimer (4|6)
    and, 
    Timer 4 prescales
    #define PS4_1/1 0
    #define PS4_1/4 1
    #define PS4_1/16 2
    #define PS4_1/64 3
    #define PS4_1 0
    #define PS4_4 1
    #define PS4_16 2
    #define PS4_64 3
    and,
    'Timer 6 prescales
    #define PS6_1/1 0
    #define PS6_1/4 1
    #define PS6_1/16 2
    #define PS6_1/64 3
    #define PS6_1 0
    #define PS6_4 1
    #define PS6_16 2
    #define PS6_64 3
    

    Anobium

     

    Last edit: Anobium 2014-10-16
  • William Roth

    William Roth - 2014-10-16

    So the problem is restricted to timer6?

    Looking at Timer.h, it seems that timer6 is not implemented. Also according to the 16F1936.dat file, only "On Interrupt timer6match is supported."

     
  • William Roth

    William Roth - 2014-10-16

    Anobium is on top of it.

    What about the 16F1936 dat file [Interrupt] that does not support timer6 overflow? There will still be a syntax error if this is not remedied.

     

    Last edit: William Roth 2014-10-16
  • Anobium

    Anobium - 2014-10-16

    @William. On Interrupt Timer6Overflow Call {method} will not compile, but, of course On Interrupt Timer6Match Call {method} will. Constraints of the chip can be removed. :-)

     
  • Anobium

    Anobium - 2014-10-21

    @All. I have just posted an updated timer.h.

    Please see https://sourceforge.net/p/gcbasic/code/HEAD/tree/GCBASIC/trunk/include/lowlevel/timer.h

    This will be part of the next release of GCB and GCGB.

     
    • Anobium

      Anobium - 2014-10-21

      This has all the normal commands for timers, plus the following scalers.

      ~~~~~
      Timer 4 prescales
      PS4_1/1
      PS4_1/4
      PS4_1/16
      PS4_1/64
      PS4_1
      PS4_4
      PS4_16
      PS4_64

      Timer 6 prescales
      PS6_1/1
      PS6_1/4
      PS6_1/16
      PS6_1/64
      PS6_1
      PS6_4
      PS6_16
      PS6_64

       

Log in to post a comment.