Some newer PIC chips with Core Independent Peripherals and PPS capability have a new, more flexible Timer0 module. This module can be either 8-Bit or 16-Bit and supports multiple clock sources and a wide range of prescale & postscale settings. On these chips, the timer output can be mappped to any I/O pin.
Some PIC Chips that support this new timer0 are:
16F188xx
16F183xx
16F153xx
These features can be exploited to generate a heartbeat signal using no CPU resources and no interrupt.
The code below shows how to easily output a "heartbeat" with a 1 second time period. Connect an LED on PORTC.3 ( PIN14) to see the signal.
'''
''' This demonstation shows a how to generate a "heartbeat" using
''' Timer0 on PIC16F18855 and other related chips with the same
''' type of timer0. Uses no processor resources and no interrupt.
'''
''' PIC: 16F18855
''' Compiler: GCB
''' IDE: GCB@SYN
'''
''' William R.
''' Date: 26.12.2016
'''
#Chip 16F18855,32
#Config FEXTOSC_OFF, RSTOSC_HFINT32
#Config MCLRE_ON
#Option Explicit
DIR PORTC.3 OUT ;Set RC3 (Pin 14) as output
'Timer0 output on RC3 (PIN 14)
RC3PPS = 0x18
'Inittimer0 source = FOSC/4 : Prescale = 1:16384
INITTIMER0 (OSC, PRE0_16384 + TMR0_FOSC4, POST0_1)
'Start the timer
Starttimer 0
MAIN:
'---------------------------------
DO
'//Main Program goes here
Loop
'-----------------------------------
Last edit: William Roth 2016-12-26
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Not sure what you mean PPS tool does not work. Works for me.
// PIC PPS Tool version: 0.0.6.5#startupInitPPS,85#definePPSToolPart18F67K40SubInitPPS// Module: TMR0RC3PPS=0x001D// TMR0 > RC3EndSub// Add this code segment to your GCBASIC source program
But, the timer setup is different. But, the demos will have 18Fxxk40 examples.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Well, I have studied the data for the 18F67K40 as well as the Timer0 for GCB but LED does not flash.
Code below:
'Heartbeat stuff here
#Define HB PORTC.0 'Heartbeat id PortF.0
Dir PORTC.0 OUT 'Sets direction
RC0PPS = 0x001D 'Timer0 to output on PORTC.0
InitTimer0 Ext, PRE0_16384, POST0_1 'Timer0 set to EXT clock source (64MHz), prescale=16384, no Postscaler
StartTimer 0 'Starts Timer0
What am I doing wrong for a 1 second on 1 second off LED?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Kato-
Thanks for your interest. The 'Basic' does flash the LED. I have not done the 2nd one as the chip seems to work fine - I have used this board in other configurations and the LCD, 7 segment I2C is working fine.
It is something in the setting up of Timer0 that I have missed I suspect.
I like the simplicity of this Heartbeat approach and would like to understand it and use it.
Last edit: Steve Scott 2024-10-26
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
These are two very different tests and the second test will confirm the ext osc configuration and the external clock are operating. Only when that is confirmed can you use the ext osc on the initimer instruction.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This is issue is the difference between the 16F timer and the 18F timer.
The are different with the 18F needing a PostScaler to get 1s. Works on 27K40 on PORTC.3 - I an input on PORTC.0 but change the PPS to RC0PPS and the PORTC.3 to PORTC.0.
I used the spreadsheet in demos\Interrupt_and_Timer_Solutions. See my piccy attached.
#CHIP 18F27K40, 64
#OPTION EXPLICIT
DIR PORTC.3 OUT // Set RC3 as output
// PIC PPS Tool version: 0.0.6.5
#startup InitPPS, 85
#define PPSToolPart 18F27K40
Sub InitPPS
// Module: TMR0
RC3PPS = 0x0013 // TMR0 > RC3
End Sub
// Add this code segment to your GCBASIC source program
//Inittimer0 source = FOSC/4 : Prescale = 16384, Postscale = 6
//This is an 18F init, 8 bit time mode for 1.007 s.
Inittimer0 (OSC, PRE0_16384 + TMR0_FOSC4, POST0_6)
//Start the timer
Starttimer 0
MAIN:
'---------------------------------
DO
//Main Program goes here
Loop
Kato-
Thanks! I have made the changes but still no LED activity.
You had:
RC3PPS = 0x0013 // TMR0 > RC3
But when I ran PPSTool, I got:
RC0PPS = 0x001D
I changed to what I got - and by golly it worked!
Where can I go to get an explanation of 'PRE0_16384 + TMR0_FOSC4' - I read why it was a good idea to add the source to Timer0 but I do not understand FOSC4 as I am using an external source??
Also, why is the 'POST0_6' needed?
(EDIT) In playing with the POST0-x values I see the difference. I do understand the prescaler and have modified that one.
I am learning.....
My already designed board uses F.0 as the Heartbeat LED, wish PPS (and the chip) would allow me to use F.0 instead of C.0 but there is no mapping.... oh Well.
BTW, like your spreadsheet - is that open? Shareable?
Last edit: Steve Scott 2024-10-26
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Some newer PIC chips with Core Independent Peripherals and PPS capability have a new, more flexible Timer0 module. This module can be either 8-Bit or 16-Bit and supports multiple clock sources and a wide range of prescale & postscale settings. On these chips, the timer output can be mappped to any I/O pin.
Some PIC Chips that support this new timer0 are:
16F188xx
16F183xx
16F153xx
These features can be exploited to generate a heartbeat signal using no CPU resources and no interrupt.
The code below shows how to easily output a "heartbeat" with a 1 second time period. Connect an LED on PORTC.3 ( PIN14) to see the signal.
Last edit: William Roth 2016-12-26
William, this is pretty cool, unfortunately, I have a 18F67K40 and the PPS tool does not seem to work. Would like to try it.
Not sure what you mean PPS tool does not work. Works for me.
But, the timer setup is different. But, the demos will have 18Fxxk40 examples.
Yes, it DOES work, was fooled by the 'Cannot display pinout of this device' so I did not hit 'Add'.
My apologies!
Now to get the registers setup so it will work with ext 64mHz clock!
Well, I have studied the data for the 18F67K40 as well as the Timer0 for GCB but LED does not flash.
Code below:
What am I doing wrong for a 1 second on 1 second off LED?
Does the LED work? Remove the code.
try PULSEOUT with in the DO-LOOP.
Do
PULSEOUT PORTC.0, 100 ms
wait 100 ms
Loop
If that then works,
Do you have a Do: Loop at the bottom? Otherwise the chip will go to SLEEP.
Also, why do you have EXT as the oscillator source ? Should be OSC.
Thanks GCBasic-
I do have a few Do Loops and I am using an external oscillator; 64MHz.
Here is some of the setup code:
The LED just stays lit
I have to ask. Back to basic.
Does the program below work? No other code. To prove the LED flashes.
What happens?
I dont have a K40, but, then try to prove the ext osc works, and, the config is correct.
What happens?
Kato-
Thanks for your interest. The 'Basic' does flash the LED. I have not done the 2nd one as the chip seems to work fine - I have used this board in other configurations and the LCD, 7 segment I2C is working fine.
It is something in the setting up of Timer0 that I have missed I suspect.
I like the simplicity of this Heartbeat approach and would like to understand it and use it.
Last edit: Steve Scott 2024-10-26
These are two very different tests and the second test will confirm the ext osc configuration and the external clock are operating. Only when that is confirmed can you use the ext osc on the initimer instruction.
This is issue is the difference between the 16F timer and the 18F timer.
The are different with the 18F needing a PostScaler to get 1s. Works on 27K40 on PORTC.3 - I an input on PORTC.0 but change the PPS to RC0PPS and the PORTC.3 to PORTC.0.
I used the spreadsheet in demos\Interrupt_and_Timer_Solutions. See my piccy attached.
Kato-
Thanks! I have made the changes but still no LED activity.
You had:
RC3PPS = 0x0013 // TMR0 > RC3
But when I ran PPSTool, I got:
RC0PPS = 0x001D
I changed to what I got - and by golly it worked!
Where can I go to get an explanation of 'PRE0_16384 + TMR0_FOSC4' - I read why it was a good idea to add the source to Timer0 but I do not understand FOSC4 as I am using an external source??
Also, why is the 'POST0_6' needed?
(EDIT) In playing with the POST0-x values I see the difference. I do understand the prescaler and have modified that one.
I am learning.....
My already designed board uses F.0 as the Heartbeat LED, wish PPS (and the chip) would allow me to use F.0 instead of C.0 but there is no mapping.... oh Well.
BTW, like your spreadsheet - is that open? Shareable?
Last edit: Steve Scott 2024-10-26
Great news.
The spreadsheet in the DEMO folders. Install the DEMO pack. There are more timers in the folder I mentioned above.
Full details of the timers PRE and POST are in the datasheet but just use the timer XLS.
Will do as suggested, thanks again. An Adult Beverage will be had in your honor this evening!
A quick Google search. https://youtu.be/5FFDZyO50dI?t=186
Shows how the Timers work in detail.
Got it and Evan made it clear. Thanks again!