PDA

View Full Version : TMR0 Longer time



camerart
- 20th January 2016, 11:07
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
################################################

HenrikOlsson
- 20th January 2016, 11:39
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.

camerart
- 20th January 2016, 13:55
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.

HenrikOlsson
- 20th January 2016, 14:07
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.

camerart
- 20th January 2016, 14:30
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.

HenrikOlsson
- 20th January 2016, 15:52
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.

camerart
- 20th January 2016, 16:16
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.

camerart
- 21st January 2016, 10:24
Correction. The simulator doesn't support [I]fully[I] what I'm doing.

HenrikOlsson
- 21st January 2016, 11:25
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.

TMR0CON = %10000111

The above have been tested on an 18F4431 (actual hardware) and it does work.

/Henrik.

camerart
- 21st January 2016, 15:42
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.

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.