Menu

ISO RTC pulse counter

2020-06-20
2020-06-24
  • Reginald Neale

    Reginald Neale - 2020-06-20

    Newbie ISO of code for implementation of a counter for one-second pulses, to trigger separate interrupts for each quarter hour, e.g. 900/1800/2700.3600 pulses.

     
  • Anobium

    Anobium - 2020-06-21

    @Reginald. Is this a question?

     
  • stan cartwright

    stan cartwright - 2020-06-21

    You could use the millis function. Untested code for 4 events. Just 15 minutes shown tim1 shown but same idea for tim2, etc.

    #include millis.h
    dim tim1,tim1event,tim2 etc as long
    tim1event=15 * 60 * 1000  ;15 minutes... 900000 ms
    tim1=mills
    tim2=mills
    tim3=mills
    tim4=mills
    do
    if millis-tim1=tim1event then sub tim1go
    loop
    sub tim1go
    code
    end sub
    
     

    Last edit: stan cartwright 2020-06-21
  • stan cartwright

    stan cartwright - 2020-06-21

    Using 1ms interrupt it could be for EVERY 15 minutes that uses same code every 15 minutes.

    dim millis,tim1event as long
    tim1event=15 * 60 * 1000 ;15 minutes... 900000 ms
    millis=0
    start 1ms interrupt isr
    do
    if millis=tim1event then sub tim1go
    loop
    ;
    sub tim1go
    code
    millis=0
    end sub
    ;
    sub isr
    millis=millis + 1
    end sub

     

    Last edit: stan cartwright 2020-06-21
  • Anobium

    Anobium - 2020-06-21

    @Reggie

    What do you mean by an ISO code for implemention?

     
  • kent_twt4

    kent_twt4 - 2020-06-21

    ISO, in search of?

    Here is some setup code for an interrupt on pin change, using the 1 seconcd square wave output of Dallas DS1307. I was looking to take readings every 10 sec with this example

    '*****setup IOC on RA7 with pullup*****
    #define SqWaveIn PortA.7    'DS1307 1Hz Square Wave Out
    Dir SqWaveIn In
    'wait to enable IOCIE until after startlog button press
    SET IOCAP7 ON
    ...
    ...
    Set IOCIE On
    'start logging right away
    OneSecINT = 10
    StartConv = True
    Main:
      Do While StopLog = False
        If OneSecINT = 10 Then
          OneSecINT = 0
    ...
    ...
    
    sub Interrupt
    '1Hz Square Wave Out DS1307 Event Scheduler*******
      If IOCAF7 = 1 Then
        set IOCAF7 Off
        OneSecINT += 1
        'PulseOut Led, 5 ms
      End If
    end sub
    
     
    • Reginald Neale

      Reginald Neale - 2020-06-24

      Thanks Kent. This seems closest to what I'm trying to do.

       
  • Chris Roper

    Chris Roper - 2020-06-21

    If you want an RTC this version based on the millis() function may point you in the right direction:

    '
    ' SoftRTC based on the Millis() interrupt
    '
    #chip 16f18855            ' Declare the Target Processor
    #option explicit          ' Require Explicit declaration of Variables
    #include <millis.h>       ' Include the Library
    
    ' declare working variables
      Dim SoftRTC_CurMs, SoftRTC_LstMs as word
      SoftRTC_LstMs = 0
      SoftRTC_CurMs = 0
    
      Dim SoftRTC_Hrs, SoftRTC_Min, SoftRTC_Sec as Byte
    
    ' Main
      Do
        SoftRTC_CurMs = millis()
    
        if SoftRTC_CurMs - SoftRTC_LstMs > 999 then  ' required Time has Elapsed
           SoftRTC_LstMs = SoftRTC_CurMs               ' Record Toggle Time
           SoftRTC_Update
        end if
    
      Loop
    
    END
    
    Macro SoftRTC_Update
      SoftRTC_Sec += 1
      if SoftRTC_Sec > 59 then
         SoftRTC_Sec = 0
         SoftRTC_Min += 1
      end if
      if SoftRTC_Min > 59 then
         SoftRTC_Min = 0
         SoftRTC_Hrs += 1
      end if
      if SoftRTC_Hrs > 23 then SoftRTC_Hrs = 0
    End Macro
    

    Millis() is device independent and uses Timer0 to generate an interrupt every millisecond.
    the millis() function returns the total time in mS since the last reset.

    The above code converts that elapsed time into an RTC with:
    SoftRTC_Hrs : SoftRTC_Min : SoftRTC_Sec
    containing the current elapsed time.

    User code would be needed to set the initial time and handle alarm events etc.

    This is just the basic framework but should work on any device supported by GCBASIC.

    Hope it helps.

    Cheers
    Chris

     

    Last edit: Chris Roper 2020-06-21
  • Chris Roper

    Chris Roper - 2020-06-21

    A striped down version of the SoftRTC code above may do the trick if I understand the initial question.

    This code will call a user Subroutine every 15 Min:

    '
    '  15 MIn repeating alarm based on  Millis() interrupt
    '
    #chip 16f18855            ' Declare the Target Processor and Speed
    #option explicit          ' Require Explicit declaration of Variables
    #include <millis.h>       ' Include the Library
    
    ' declare working variables
      Dim SoftRTC_CurMs, SoftRTC_LstMs, SoftRTC_Sec  as word  
      SoftRTC_LstMs = 0
      SoftRTC_CurMs = 0
      SoftRTC_Sec  = 0
    
    ' Main                    
      Do
        SoftRTC_CurMs = millis()
    
        if SoftRTC_CurMs - SoftRTC_LstMs > 999 then
           SoftRTC_LstMs = SoftRTC_CurMs              
           SoftRTC_Sec += 1
           If SoftRTC_Sec > 899 then 
             SoftRTC_Sec = 0
             DoUserCode
           end if 
        end if
    
      Loop
    
    END
    
    Macro DoUserCode
        ' Code that runs evert 15 min
      ' may be a macro, sub or function
    End Macro
    

    Cheers
    Chris

     

    Last edit: Chris Roper 2020-06-21
  • stan cartwright

    stan cartwright - 2020-06-21

    original post for 1 second pulses.
    not much code

    #chip mega328p,16
    #option Explicit
    dir portc.2 out
    dim millis,tim1event as long
    tim1event=1000 ;every second
    millis=0
    ;start 1ms interrupt isr
        On Interrupt Timer0Match1 Call isr
        Dim OCR0  AS byte alias OCR0A
        Dim TCCR0 AS  byte alias TCCR0B
        WGM01 = 1
        OCR0 = 62 ;1ms
        TCCR0 = 0x28
        TCCR0 = TCCR0 or 0x03
    '---------------
    do
      if millis=tim1event then tim1go
    loop
    ;
    sub tim1go ;every second
    portc.2 = not portc.2
    millis=0
    end sub
    ;
    sub isr
    millis=millis + 1
    end sub
    
     

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.