Menu

Is My Chip Alive ? How to Generate a 1 Second Heartbeat Using No Interrupt and No Processor Resources ( PIC)

2016-12-26
2024-10-27
  • William Roth

    William Roth - 2016-12-26

    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
  • Steve Scott

    Steve Scott - 2024-10-24

    William, this is pretty cool, unfortunately, I have a 18F67K40 and the PPS tool does not seem to work. Would like to try it.

     
    • Kato

      Kato - 2024-10-24

      Not sure what you mean PPS tool does not work. Works for me.

          // PIC PPS Tool version: 0.0.6.5
      
          #startup InitPPS, 85
          #define PPSToolPart 18F67K40
      
          Sub InitPPS
      
                  // Module: TMR0
                  RC3PPS = 0x001D    // TMR0 > RC3
      
              End Sub
          // Add this code segment to your GCBASIC source program
      

      But, the timer setup is different. But, the demos will have 18Fxxk40 examples.

       
  • Steve Scott

    Steve Scott - 2024-10-24

    Yes, it DOES work, was fooled by the 'Cannot display pinout of this device' so I did not hit 'Add'.
    My apologies!

     
  • Steve Scott

    Steve Scott - 2024-10-24

    Now to get the registers setup so it will work with ext 64mHz clock!

     
  • Steve Scott

    Steve Scott - 2024-10-25

    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?

     
  • GCBASIC

    GCBASIC - 2024-10-25

    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.

     
  • Steve Scott

    Steve Scott - 2024-10-26

    Thanks GCBasic-
    I do have a few Do Loops and I am using an external oscillator; 64MHz.
    Here is some of the setup code:

    ' ----- Configuration
    #CHIP 18F67K40, 64
    #option Explicit
    ' ----- Main body of program commences here.
     // Generated for 18lf67k40
    #startup InitPPS, 85
    #define PPSToolPart 18lf67k40
    #config MCLR=ON                                                             'Uses the RESET pushbutton
        Sub InitPPS
            'Module: EUSART1
            RX1PPS = 0x0011    'RC1 > RX1
            RC0PPS = 0x000C    'TX1 > RC0
            'Module: EUSART2
            RX2PPS = 0x0031    'RG1 > RX2
            RG0PPS = 0x000E    'TX2 > RG0
            'Module: EUSART3
            RB4PPS = 0x0010    'TX3 > RB4
            'Module: EUSART5
            RX5PPS = 0x0032    'RG2 > RX5
            RG7PPS = 0x0014    'TX5 > RG7
            RC0PPS = 0x001D                                                     'Timer0 to output on PORTC.0
        End Sub
    'Heartbeat stuff here
    #Define HB PORTC.0                                                          'Heartbeat id PortF.0
    Dir PORTC.0 OUT                                                             'Sets direction
    InitTimer0 Ext, PRE0_16384, POST0_1                                         'Timer0 set to EXT clock source (64MHz), prescale=16384, no Postscaler
    StartTimer 0                                                                'Starts Timer0
    

    The LED just stays lit

     
  • Kato

    Kato - 2024-10-26

    I have to ask. Back to basic.

    Does the program below work? No other code. To prove the LED flashes.

    #CHIP 18F67K40, 64
    #option explicit 
    Do
    PULSEOUT PORTC.0, 100 ms
    wait 100 ms
    Loop
    

    What happens?

    I dont have a K40, but, then try to prove the ext osc works, and, the config is correct.

    #CHIP 18F67K40, 64
    #option explicit 
    
    
    #CONFIG CLKOUTEN=OFF
    #CONFIG CP=OFF
    #CONFIG CPD=OFF
    #CONFIG FCMEN=ON
    #CONFIG FEXTOSC=OFF
    #CONFIG LVP=OFF
    #CONFIG MCLRE=INTMCLR
    #CONFIG RSTOSC=EXTOSC
    #CONFIG WDTE=OFF
    #CONFIG WRTB=OFF
    #CONFIG WRTD=OFF
    #CONFIG XINST=OFF
    
    Do
    PULSEOUT PORTC.0, 100 ms
    wait 100 ms
    Loop
    

    What happens?

     
  • Steve Scott

    Steve Scott - 2024-10-26

    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
    • Kato

      Kato - 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.

       
  • Kato

    Kato - 2024-10-26

    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
    
     
  • Steve Scott

    Steve Scott - 2024-10-26

    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
  • Kato

    Kato - 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.

     
  • Steve Scott

    Steve Scott - 2024-10-26

    Will do as suggested, thanks again. An Adult Beverage will be had in your honor this evening!

     
  • Kato

    Kato - 2024-10-27

    A quick Google search. https://youtu.be/5FFDZyO50dI?t=186

    Shows how the Timers work in detail.

     
  • Steve Scott

    Steve Scott - 2024-10-27

    Got it and Evan made it clear. Thanks again!

     

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.