PIC clock, help

Discussion in 'Embedded Systems and Microcontrollers' started by warcuz, Feb 4, 2010.

  1. warcuz

    Thread Starter New Member

    Jul 10, 2008
    7
    0
    About PIC, is there a possibility to include a timer(like a clock) inside a PIC so it will know when it will execute a command in specified time. Because Im making a project that is time based, for example : on a specific time of a day
    I need the LED to turn ON lets say 8:00AM and when it is 9:30AM the LED would turn off. how could I make the PIC conscious that its 8:00AM to turn the LED ON and 9:30AM to let it turn OFF the LED?

    Is it with the use of interrupts??, if you do know some example related to this would you kindly post it?


    I understand that the PAUSE command isn't the best answer for my problem.
    Im using PIC16F628A
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Which PIC MCUs are you using, and what kind of development equipment do you have (hardware/software).
     
  3. Zaraphrax

    Active Member

    Mar 21, 2009
    47
    3
    I have seen people program simple alarm clocks with PICs etc. I tracked down the source for one, you could probably pick the bones out of that for the timing engine. - http://www.metricmind.com/clock/clock.htm (there's a link the the source a bit further down the page).

    That should get you started. I don't know whether that will port straight onto your controller, so you might have to make some changes to it before it'll work. Once you've got a simple clock running on it, it'd be pretty easy to setup a comparison condition so the LED is on between x and y and off the rest of the time.
     
  4. Art

    Distinguished Member

    Sep 10, 2007
    785
    61
    What language are you using?
     
  5. joeyla

    New Member

    Nov 14, 2006
    2
    0
    If you require something with any great accuracy then use something like Dallas DS1302 RTC.

    Is more accurate as has 32768Hz external clock (nice and slow).

    If micro has internal clock then it is not good enough for time sensitive apps. Clocks will have ratings plus or minus X PPM ( pulses per million ). If you have a 4Mhz external clock then those pulses per million add up and could loose a couple of minutes per day.

    Another thought. If only function of PIC is to keep track of time and switch LED then could use 32768Hz crystal to clock the PIC
     
    Last edited: Feb 4, 2010
  6. warcuz

    Thread Starter New Member

    Jul 10, 2008
    7
    0
    Im using PIC BASIC...is it possible to use interrupts?

    and how can I know the time being eaten alone by mg program, I understand that it would definitely affect the result of what I intend to do

    thnx
     
  7. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015

    Yes you can. Check this thread. The sample is in C but you should be able to follow it. It uses an internal clock, you should use an external clock as suggested, but the sample will at least show how to do timer interrupts. You may need to modify it a bit for your particular PIC.
     
  8. Art

    Distinguished Member

    Sep 10, 2007
    785
    61
    PicBasic implementation based on Roman Black's One second timer:

    Code ( (Unknown Language)):
    1.  
    2. 'PROGRAM: EZCLOCK1
    3. ' Paul R. Borgmeier, PhD
    4. ' Crux analysis & design, LLC
    5. ' www.cruxanalysis.com
    6. ' June 02, 2005
    7. ' Initial Release
    8. '***********************************************************
    9. 'DESCRIPTION: This bare bones program demonstrates how to
    10. ' create a very simple yet accurate clock without the use of
    11. ' a RTC IC.  
    12. '
    13. ' This version of the code displays the time in an HH:MM format
    14. ' with a blinking colon - a common serial alphanumeric LCD is
    15. ' used for the display (Scott Edwards Electronics BPI-216). Other
    16. ' serial displays should work equally as well.  The code also
    17. ' could easily be updated to work with parallel displays since the
    18. ' instruction set is essentially the same.  For larger display
    19. ' options, GOOGLE SEARCH "Serial LCD 4 Digit"
    20. '
    21. ' This version requires a 4.000 MHz Xtal - accuracy is
    22. ' controlled 100% by Xtal tolerance: (schematic is below)
    23. '
    24. ' 100ppm tolerance gives < 9 sec/day error
    25. '  50ppm tolerance gives < 5 sec/day error
    26. '  30ppm tolerance gives < 3 sec/day error
    27. '  20ppm tolernace gives < 2 sec/day error
    28. '
    29. '***********************************************************  
    30. 'HOW IT WORKS: This clock is based on Roman Black's "Zero-error
    31. ' One Second Timer!" (www.romanblack.com) but has been optimized
    32. ' for simplicity and implementation using PicBasic Pro. This
    33. ' program keeps track of .5 second intervals so that a blinking
    34. ' colon can be used to show that the clock is active. On the
    35. ' whole second, the time is udated (as needed) and the colon
    36. ' turned on. On the half second, the colon is turned off.
    37. '
    38. ' The version here makes use of TMRO with a prescaler of 256 and
    39. ' manual polling for TMRO overflow (interrupts could easily by
    40. ' added if needed).  This makes the TMRO period equal to 65,536
    41. ' microseconds(65.536 mSec), which gives the user lots of time to
    42. ' do other stuff as well as update the clock on a serial LCD between
    43. ' TMRO overflows.
    44.  
    45. ' Note that although the overall accuracy of the clock is as accurate
    46. ' as the Xtal tolerance, individual seconds are not and vary slightly
    47. ' from second to second - however,the cummulative error of the second
    48. ' to second variation appraoches ZERO! Each half second calculated by
    49. ' the program is either 0.4588 seconds or 0.5243 seconds in length.
    50. ' The program combines these so that the total error approaches zero
    51. ' when compared to a perfect timer - the only error left is due to
    52. ' crystal tolerance. Note that the longer the program runs, the  more
    53. ' accurate the clock becomes.  For example, after 1000 seconds, the
    54. ' total error is just 0.0052% (this equates to 52mSec—not bad!).
    55. '
    56. ' The news gets even better. If you cannot live with this accuracy,
    57. ' for example in a timer application, once the timer or program has
    58. ' been stopped, the error can be determined in software and corrected.
    59. ' The correction can be determined from the values of TMRO, HzTimer,
    60. ' and Col at the time of interest.  Most will not have to deal with
    61. ' this correction because the error approaches zero the longer the
    62. ' clock runs anyway.  The value of the error from any second is always
    63. ' less than 65.5mSec regardless of the elapsed time.
    64. '
    65. ' Black's timer uses three separate byte-sized variables to track the
    66. ' instruction counts for one second periods.  His method is fast and
    67. ' clever, but requires manually watching and maintaining addition
    68. ' overflows and carries between three variables.
    69. '
    70. ' With a 4 MHz crystal, 0.5 seconds equals 500,000 instructions (h7A120),
    71. ' which is larger than a word-sized variable in PicBasic Pro. The TMRO
    72. ' time period used in the code is 65,536 (h10000). The trick used here is
    73. ' to divide both the instruction count and timer count by 16 (h10), which
    74. ' makes the instruction count trackable in the single word-sized variable,
    75. ' HzTimer.
    76. '
    77. ' This bare bones program demonstrates how to implement this clock.  
    78.  
    79. ' The program is divided into two sections:
    80. '
    81. '  (1) Input time and display on SMARD4  (@ 9600 baud) using momentary
    82. ' push button.
    83. '
    84. '   The User:  
    85. '       a) Pushes and holds button to set hours (variable HH).
    86. '       b) Pushes and holds button to set minutes (variable MM).
    87. '       c) Pushes button to start clock.
    88. '
    89. '  (2) Maintains time display on LCD including colon blink
    90. '
    91. ' The user can add code to perform tasks as noted in the code itself.
    92. '
    93. ' Most of the code below is dedicated to setting the initial time. This
    94. ' part of the code is not optimized but works well enough for most
    95. ' applications. The actual time keeping code is amazingly short—just about
    96. ' 30 lines of code, which includes colon blinking (and maintaining the
    97. ' display)!
    98. '
    99. '***********************************************************
    100. 'PIC: 12F629
    101. ' GP0: SERIAL LCD LINE (I used a SEETRON BPI-216 at 9600 baud)
    102. ' GP1: Momentary Push Button direct to GND
    103. ' GP2:
    104. ' GP3: (MCLR Internal)
    105. ' GP4: XTAL 4.0000 Hz (w/tolerance = 30 ppm) w/ cap to GND
    106. ' GP5: XTAL 4.0000 Hz (w/tolerance = 30 ppm) w/ cap to GND
    107. '
    108. '         -_-
    109. '   +5V--|   |--GND
    110. '   Xtal-|12F|-- to Serial LCD (at n9600)
    111. '   Xtal-|629|--/ --GND (momentary button direct to GND)
    112. '       -|   |-
    113. '         ---
    114. ' (Xtal caps to GND not shown)
    115. '
    116. ' CONFIGURATION SETUP
    117. '  Oscillator:          XT
    118. '  Watchdog Timer:      OFF
    119. '  Power up Timer:      OFF
    120. '  Master Clear Enable: Internal
    121. '  Brown Out Detect:    OFF
    122. '  Code Protect:        OFF
    123. '  Data EE Read Protect:OFF
    124. '**********************************************************
    125.  
    126. CMCON=7                'all digital
    127. GPIO=0
    128. TRISIO=2               'GP1 = input for button
    129. OPTION_REG=%00000111   'weak pullups on, TMRO prescale = 256
    130. INTCON=0               'interrupts off
    131.  
    132. HzTimer VAR Word       '1/2 second counter (2 Hz)
    133.        
    134. HH VAR Byte  ' Hours 1-12
    135. MM VAR ByTE  ' Minutes 0-59
    136. SS VAR Byte  ' Seconds 0-59
    137. X VAR Byte   ' temp variable
    138.  
    139. col VAR Bit  ' colon 1=on, 0=0ff
    140.  
    141. HzTimer=$7A12        'for 1/2 Sec
    142. HH=0:MM=0:SS=0:col=0 'initial conditions
    143.  
    144. Pause 1000           'settle Time for Serial LCD
    145.  
    146. ' **************************************************************
    147. 'SET INITIAL TIME WITH PUSH BUTTON (PART 1 of Program)
    148.  
    149. Serout GPIO.0,6,[254,1]
    150. Pause 100
    151. SEROUT GPIO.0,6,[72,72,58,77,77]   'Display HH:MM
    152.  
    153. WHILE GPIO.1=1    'Wait here until user pushes button
    154.     PAUSE 150
    155.     SEROUT GPIO.0,6,[254,128,72,72]
    156.     PAUSE 150
    157.     SEROUT GPIO.0,6,[254,128,32,32]  
    158. WEND
    159.  
    160. SetHH: 'Set hours, HH
    161.  
    162. HH=HH+1
    163. IF HH=13 THEN
    164.     HH=1
    165.     SEROUT GPIO.0,6,[254,128,32]
    166. ENDIF
    167. IF HH>9 THEN
    168.     X=128
    169.   ELSE
    170.     X=129
    171. ENDIF
    172. SEROUT GPIO.0,6,[254,X,#HH]    
    173. PAUSE 1000
    174. IF GPIO.1=0 THEN SetHH
    175.  
    176. PAUSE 30
    177.  
    178. WHILE GPIO.1=1    'Wait here until user pushes button
    179.     PAUSE 150
    180.     SEROUT GPIO.0,6,[254,131,77,77]
    181.     PAUSE 150
    182.     SEROUT GPIO.0,6,[254,131,32,32]  
    183. WEND
    184.  
    185. SEROUT GPIO.0,6,[254,131,48]
    186.  
    187. SETMM: 'Set Minutes, MM
    188.  
    189. MM=MM+1
    190. IF MM=60 THEN
    191.     MM=0
    192.     SEROUT GPIO.0,6,[254,131,48]
    193. ENDIF
    194. IF MM>9 THEN
    195.     X=131
    196. ELSE
    197.     X=132
    198. ENDIF
    199. SEROUT GPIO.0,6,[254,X,#MM]
    200. PAUSE 1000
    201. IF GPIO.1 = 0 THEN SetMM
    202.  
    203. StartLoop: IF GPIO.1=1 THEN StartLoop  'Push to start clock
    204.  
    205. OPTION_REG.7=1    'Turn off weak pullups
    206. TRISIO.1=0        'Make button pin output
    207. GPIO.1=0          'Set GP1 low
    208. TMR0=0            'Reset TMRO
    209. INTCON.2= 0       'Clear TMRO overflow flag
    210.  
    211. ' **************************************************************
    212. 'TIME KEEPING LOOP (PART 2 of Code)
    213.  
    214. 'Requires variables, HH for Hours 1-12, and MM for minutes 0-59
    215. '(HH and MM manually set above in Part 1; SS & col = 0 from above)
    216.  
    217. Main:
    218. ClockLoop: IF INTCON.2=0 THEN ClockLoop ' Wait for TMRO overflow
    219. INTCON.2=0 'Clear TMRO overflow flag
    220.  
    221. HzTimer = HzTimer - $1000  'decrement timer
    222.  
    223. IF HzTimer<$1000  THEN
    224.     IF Col=1 THEN 'update time'
    225.         SS=SS+1
    226.         IF SS=60 THEN
    227.             SS=0
    228.             MM=MM+1
    229.             IF MM=60 THEN
    230.                 MM=0
    231.                 HH=HH+1
    232.                 IF HH=13 THEN
    233.                     HH=1
    234.                 ENDIF
    235.             ENDIF
    236.             IF HH>9 THEN
    237.                 SEROUT GPIO.0,6,[254,128,#HH,58]
    238.             ELSE
    239.                 SEROUT GPIO.0,6,[254,128,32,#HH,58]
    240.             ENDIF
    241.             IF MM>9 THEN
    242.                 SEROUT GPIO.0,6,[254,131,#MM]
    243.             ELSE
    244.                 SEROUT GPIO.0,6,[254,131,48,#MM]
    245.             ENDIF
    246.          ELSE
    247.              Serout GPIO.0,6,[254,130,58]   'colon on
    248.          ENDIF
    249.     ELSE
    250.         Serout GPIO.0,6,[254,130,32]    ' Colon off
    251.     ENDIF
    252.     Col=Col+1
    253.     HzTimer=HzTimer+$7A12
    254. ELSE
    255.     'Do something here but must be less than 65,500 instructions
    256.     '(e.g., less than 65.5 mSec total time)
    257. ENDIF
    258.  
    259. GOTO Main
    260. ' **************************************************************
    261. END
    262.  
    263.  
     
  9. warcuz

    Thread Starter New Member

    Jul 10, 2008
    7
    0
    I cant seem to understand this, there are some variables I cant figure out
    the GPIO=0
    TRISIO=2

    and I dont need to display the clock in the LCD, I just want to have a clock in my program to have an accurate timed program. Im using 16f628a I use the above program revised it and it lead to a queer display in my hyperterminal, some character I dont recognized which shows every half a second T T, is interrupt really this hard?
     
  10. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Just substitute TRISIO and GPIO for TRISA and PORTA.
    The first 2 are what microchip calls the ports on chips with only 1 port.
     
  11. warcuz

    Thread Starter New Member

    Jul 10, 2008
    7
    0
    ok Ill try that...but can it be ports other than that you mentioned or is it a need to have that specific port
     
  12. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    You can use any port, that was just an example. You may have to make other changes because code written for 1 PIC doesn't always run on another. A thing that is often different is the banks that various SFRs are in.
     
  13. warcuz

    Thread Starter New Member

    Jul 10, 2008
    7
    0
    Thanks ,my connections are all in PORTB no connections for porta
     
Loading...