TMR0 Longer time


Closed Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2013
    Posts
    64

    Default TMR0 Longer time

    Hi,

    Can anyone help me with a TMR0 question please?

    I have a program with the sections below in it:

    I need a longer time than TMR0H = 0 TMR0L = 0 allows. So I want to add more TIME into the program, as 'TIME = TIME + 1' If time = 10 then do something.

    Also, I'm not sure if I have the TMR0 switching on at the correct time.

    Any suggestions welcome.

    Camerart.



    ###############################################
    'set up timer 1
    T0CON.TMR0ON = 1 'Bit7 = enables timer0
    T0CON.T016BIT = 0 'Bit6 = timer0 is configured As A 16-Bit timer/TIME
    T0CON.T0CS = 0 'Bit5 = Internal clock (FOSC/4)
    T0CON.T0SE = 0 'Bit4 = 0 = Increment on low-to-high transition on T0CKI pin
    T0CON.PSA = 0 'Bit3 = Timer0 prescaler is assigned.from prescaler output
    T0CON.T0PS2 = 0 'Bit2 = 1:2 Prescale value
    T0CON.T0PS1 = 0 'Bit1 = 1:2 Prescale value
    T0CON.T0PS0 = 0 'Bit0 = 1:2 Prescale value
    #################################################
    If INTCON.TMR0IF = 1 Then 'Switches at TMR0 rollover
    time = 1 'SIMULATE CHECK
    'INTCON.TMR0IF = 0 MOVED
    TMR0H = 0 'Watch TMR0 HIGH BYTE not TMR0H>>>>>>>>>>>>>>>>>
    TMR0L = 0
    Endif
    ################################################## ##
    If poscount = azimval Then
    If time = 0 Then
    Gosub brake
    Goto get_count '[poscnt]
    Endif
    If time = 1 Then 'added
    Gosub brake 'added
    time = 0 'added
    INTCON.TMR0IF = 0 'added
    Goto clr_azim 'Get new angle AZIMVALadded
    Endif
    Endif
    ################################################

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

    Default Re: TMR0 Longer time

    Why not use the prescaler? That will make the timer tick slower.
    Otherwise, just do it the way you're thinking, each time it overflows (indicated by the interrupflag) you increment a variable. When it reaches the desired value you do whatever.
    Just remember to claer the interrupt flag, you have that commented out in the code.

    /Henrik.

  3. #3
    Join Date
    Jan 2013
    Posts
    64


    Did you find this post helpful? Yes | No

    Default Re: TMR0 Longer time

    Quote Originally Posted by HenrikOlsson View Post
    Why not use the prescaler? That will make the timer tick slower.
    Otherwise, just do it the way you're thinking, each time it overflows (indicated by the interrupflag) you increment a variable. When it reaches the desired value you do whatever.
    Just remember to claer the interrupt flag, you have that commented out in the code.

    /Henrik.
    Hi H,

    I changed the prescaler bits:

    T0CON.T0PS2 = 0 'Bit2 = 1:2 Prescale value
    T0CON.T0PS1 = 0 'Bit1 = 1:2 Prescale value
    T0CON.T0PS0 = 0 'Bit0 = 1:2 Prescale value

    To:
    T0CON.T0PS2 = 1 'Bit2 = 1:256 Prescale value
    T0CON.T0PS1 = 1 'Bit1 = 1:256 Prescale value
    T0CON.T0PS0 = 1 'Bit0 = 1:256 Prescale value

    But I didn't see any difference.

    You can see the commented out interrupt flag farther down the code.

    Thanks C.

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

    Default Re: TMR0 Longer time

    Hi,
    Now that doesn't make sense...
    Which PIC are you using and what clock frequency?

    I don't think I've seen the individual bitnames being predefined like that...which version of PBP are you using?

    /Henrik.

  5. #5
    Join Date
    Jan 2013
    Posts
    64


    Did you find this post helpful? Yes | No

    Default Re: TMR0 Longer time

    Quote Originally Posted by HenrikOlsson View Post
    Hi,
    Now that doesn't make sense...
    Which PIC are you using and what clock frequency?

    I don't think I've seen the individual bitnames being predefined like that...which version of PBP are you using?

    /Henrik.
    Hi H,

    I'm using an 18F2431 PIC in Oshonsoft simulator.

    I just tried the 'time = time + 1' option, and also didn't get proper result, so perhaps I didn't get it correct!

    Can you confirm that if those bits are correct and they are set to 1 which should make 1:265, instead of the previous 1:2. Should it now be slower?

    C.

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

    Default Re: TMR0 Longer time

    Hi,
    Yes, TMR0CON = %10001111 should be TMR0 enabled, prescaler assigned to it with ratio 1:256. If I where you I'd try it on real hardware, I'm willing to bet it's a simulator issue...

    You do realise though that this forum is for MeLabs PBP compiler which is not the same as the Oshonsoft compiler, right? They're both BASIC, they're both for PIC but they are not the same.

    /Henrik.

  7. #7
    Join Date
    Jan 2013
    Posts
    64


    Did you find this post helpful? Yes | No

    Default Re: TMR0 Longer time

    Quote Originally Posted by HenrikOlsson View Post
    Hi,
    Yes, TMR0CON = %10001111 should be TMR0 enabled, prescaler assigned to it with ratio 1:256. If I where you I'd try it on real hardware, I'm willing to bet it's a simulator issue...

    You do realise though that this forum is for MeLabs PBP compiler which is not the same as the Oshonsoft compiler, right? They're both BASIC, they're both for PIC but they are not the same.

    /Henrik.
    Hi H,

    I am using the simulator, then putting it on the hardware, as the simulator doesn't actually support what I am doing

    I realise that this is not an Oshonsoft support forum, I have had good replies from this forum, also there isn't much on Oshonsoft.

    Thanks, C.

  8. #8
    Join Date
    Jan 2013
    Posts
    64


    Did you find this post helpful? Yes | No

    Default Re: TMR0 Longer time

    Correction. The simulator doesn't support [I]fully[I] what I'm doing.

  9. #9
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

    Default Re: TMR0 Longer time

    Hi,
    Bit 3 of TMR0CON needs to be 0 in order to assign the prescaler to TMR0 - I got that backwards in my previous post, you had it correct in your original code.
    Code:
    TMR0CON = %10000111
    The above have been tested on an 18F4431 (actual hardware) and it does work.

    /Henrik.

  10. #10
    Join Date
    Jan 2013
    Posts
    64


    Did you find this post helpful? Yes | No

    Default Re: TMR0 Longer time

    Quote Originally Posted by HenrikOlsson View Post
    Hi,
    Bit 3 of TMR0CON needs to be 0 in order to assign the prescaler to TMR0 - I got that backwards in my previous post, you had it correct in your original code.
    Code:
    TMR0CON = %10000111
    The above have been tested on an 18F4431 (actual hardware) and it does work.

    /Henrik.
    Hi H,

    Thanks very much for re-checking. I spend many hours chasing small errors like that. In fact, I couldn't get TMR0 to work because I removed the TIME = 1 with INTCON.TMR0IF = 1, and as the TMR0 is count down not up, this was also backwards. TMR0 is now working.

    Thanks, C.

Similar Threads

  1. Using TMR0 to keep time
    By BraveZero in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 23rd November 2011, 05:25
  2. ArrayRead on longer arrays
    By amgen in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 21st September 2011, 16:07
  3. Calculate TMR0 reload value at compile time?
    By HenrikOlsson in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 28th December 2010, 19:59
  4. PBP is no longer a stranger to Microchip
    By MarioC in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 12th March 2008, 22:59
  5. code needs much longer then expected
    By mischl in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 22nd March 2006, 02:03

Members who have read this thread : 4

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