Timer in real time


Closed Thread
Results 1 to 8 of 8
  1. #1
    martarse's Avatar
    martarse Guest

    Unhappy Timer in real time

    hello,

    One of my friend ask me to post the question here:

    Marc, My friend, want to know how he can make a timer in real time (24 hours/day) to make a specific task.

    In the bottom I have paste his example code that he want to start with. Marc want to know which specific crystal and prescaler are required? and if this method is good to get a second, minutes and hours equal to a real to the real world time.

    Let us know if you are a better ways to get the result.

    Thanks!
    Martin and Marc!



    He want to make his program starting with this code example, here is the code :

    ' LCD clock program using On Interrupt
    ' Uses TMR0 and prescaler. Watchdog Timer should be
    ' set to off at program time and Nap and Sleep should not be used.
    ' Buttons may be used to set hours and minutes

    Define LCD_DREG PORTb ' Define LCD connections
    Define LCD_DBIT 4
    Define LCD_RSREG PORTb
    Define LCD_RSBIT 3
    Define LCD_EREG PORTb
    Define LCD_EBIT 0
    define LCD_BIT 4
    DEFINE LCD_LINES 1

    hour var byte ' Define hour variable
    dhour var byte ' Define display hour variable
    minute var byte ' Define minute variable
    second var byte ' Define second variable
    ticks var byte ' Define pieces of seconds variable
    update var byte ' Define variable to indicate update of LCD
    i var byte ' Debounce loop variable


    Pause 100 ' Wait for LCD to startup

    hour = 0 ' Set initial time to 00:00:00
    minute = 0
    second = 0
    ticks = 0

    update = 1 ' Force first display

    ' Set TMR0 to interrupt every 1:64 16.384 milliseconds
    OPTION_REG = $15 ' Set TMR0 configuration and 1:64
    INTCON = $a0 ' Enable TMR0 interrupts
    On Interrupt Goto tickint


    ' Main program loop - in this case, it only updates the LCD with the time
    mainloop:
    'PORTA = 0 ' PORTB lines low to read buttons
    'rtfecs*TRISA = $f0 ' Enable all buttons

    ' Check any button pressed to set time
    If PORTA.0 = 1 Then decmin
    If PORTA.1 = 1 Then incmin ' Last 2 buttons set minute
    If PORTA.2 = 1 Then dechr
    If PORTA.3 = 1 Then inchr ' First 2 buttons set hour

    ' Check for time to update screen
    chkup: If update = 1 Then
    Lcdout $fe, 1 ' Clear screen ,l

    ' Display time as hh:mm:ss
    dhour = hour ' Change hour 0 to 12
    If (hour // 12) = 0 Then
    dhour = dhour + 12
    Endif

    ' Check for AM or PM
    If hour < 12 Then
    Lcdout dec2 dhour, ":", dec2 minute, ":", dec2
    second, " AM"
    Else
    Lcdout dec2 (dhour - 12), ":", dec2 minute, ":",
    dec2 second, " PM"
    Endif

    update = 0 ' Screen updated
    Endif

    Goto mainloop ' Do it all forever


    ' Increment minutes
    incmin: minute = minute + 1
    If minute >= 60 Then
    minute = 0
    Endif
    Goto debounce

    ' Increment hours
    inchr: hour = hour + 1
    If hour >= 24 Then
    hour = 0
    Endif
    Goto debounce

    ' Decrement minutes
    decmin: minute = minute - 1
    If minute >= 60 Then
    minute = 59
    Endif
    Goto debounce

    ' Decrement hours
    dechr: hour = hour - 1
    If hour >= 24 Then
    hour = 23
    Endif

    ' Debounce and delay for 250ms
    debounce: For i = 1 to 25
    Pause 10 ' 10ms at a time so no interrupts are lost
    Next i

    update = 1 ' Set to update screen

    Goto chkup


    ' Interrupt routine to handle each timer tick
    disable ' Disable interrupts during interrupt handler
    tickint: ticks = ticks + 1 ' Count pieces of seconds
    If ticks < 61 Then tiexit ' 61 ticks per second (16.384ms per
    tick)

    ' One second elasped - update time
    ticks = 0
    second = second + 1
    If second >= 60 Then
    second = 0
    minute = minute + 1
    If minute >= 60 Then
    minute = 0
    hour = hour + 1
    If hour >= 24 Then
    hour = 0
    Endif
    Endif
    Endif

    update = 1 ' Set to update LCD

    tiexit: INTCON.2 = 0 ' Reset timer interrupt flag
    Resume

    End

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Salut Martin

    There's a simple way to do it, but require a bit of extra hardware. An RTC, something Like DS1307. Really easy to use. That way, no need to care about the timing and blah blah blah. Just read the RTC when you want and do the according stuff.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    I would recommend the use of an RTC

    (DS1307 or similar.)
    Search the forum for RTC*
    and you'll find many examples.

    EDIT: Steve, you have won (as usual)
    Last edited by NavMicroSystems; - 25th July 2005 at 23:53.
    regards

    Ralph

    _______________________________________________
    There are only 10 types of people:
    Those who understand binary, and those who don't ...
    _______________________________________________



  4. #4
    martarse's Avatar
    martarse Guest


    Did you find this post helpful? Yes | No

    Default

    Hello,

    thanks for your reply. my friend has already analyzed the RTC solution. He wants something software that is included in a PIC also for further development, very low cost and very simple.

    anything else ?

  5. #5
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Do a search for the Olympic Timer thread...

  6. #6
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    There's a bunch of other idea that spring to mind. Is the unit will be battery powered or AC powered?

    If it's AC powered, i would prefer to use the AC as timebase, use an internal timer interrupt to get the 1 Second or 1 minute. Once in the interrupt do the math and check for Alarm or Events.

    BUT you can do it with the internal Tick FOSC/4, in this case use one of the lower Crystal frequency... let's say 4 MHZ => internal = 1MHZ => ticks every 1 uSec => internal timer interrupt every 1000 counts to have a time base of 1 Sec => you need a 16 bits Timer.

    Wich PIC Marc will use ???

    Process is almost the same than with an RTC but you'll have to deal with a bunch of more code line to take care of the days of week/month/year/Leap Year/hours/minutes... BLAH BLAH BLAH, tsé veux dire !!!

    NOT impossible, a bit tricky, i agree.

    What the unit will also do?
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  7. #7
    martarse's Avatar
    martarse Guest


    Did you find this post helpful? Yes | No

    Default

    HEllo

    Marc make an automatic food system for Deer hunting. It's a little contract that ones of his neighboor ask for.

    Marc use a 16F84A-4MHz. Unit will be DC powered.

    He will get back to me soon. He actualy looking at some tricks mentionned here.

    I Will let you know how is going with his tests.

    thanks again!

    Martin et Marc

  8. #8
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Smile

    Martin & Marc,

    I just posted in the “Code Examples” area two examples of how to create easy clocks that track time in an HH:MM format. The examples do not require Real Time Clock (RTC) ICs and are based on 4MHz Crystals. They are highly accurate at < 2 sec /day error with a standard 4.000MHz crystal (with +/- 20ppm tolerance.)

    Both examples use TMRO with prescalers set to 256. This method requires only about 30 lines of code to maintain accurate time.

    The programs are named EZClock1.bas and EZClock2.bas. These files are mostly comment - the code is at the end.

    Maybe these can help your friend -

    Good Luck,
    Paul Borgmeier
    Salt Lake City, Utah
    www.cruxanalysis.com

Similar Threads

  1. Real Time Clock
    By in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 2nd June 2012, 04:52
  2. Real time clock ICs
    By Eng4444 in forum mel PIC BASIC Pro
    Replies: 66
    Last Post: - 20th October 2008, 16:05
  3. Real time the right way?
    By Michael in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 1st March 2007, 18:38
  4. Real time clock... what a headache!
    By Eng4444 in forum mel PIC BASIC
    Replies: 2
    Last Post: - 8th June 2006, 21:56
  5. please read it ( real time)
    By moud_man in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 20th September 2005, 14:38

Members who have read this thread : 1

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