PDA

View Full Version : Using Pic as a digital clock/timer. How accurate?



rocky79
- 22nd January 2007, 00:21
Hello,
I am trying to use the pic as a clock timer that displays the time hour and minutes but also i would like it to have a featured timer that the user can set. The timer will activate a motor.
will the time be accurate enough?
I am using the pic basic pro and the usb programmer.
What's the easiest way to get this done? any suggestions is appreciated.
Thanks

SteveB
- 22nd January 2007, 01:41
Bottom line - It will only be as accurate as the oscillator used. The PIC is not the limiting factor.

SteveB

fazan83
- 22nd January 2007, 02:51
Hello,
I am trying to use the pic as a clock timer that displays the time hour and minutes but also i would like it to have a featured timer that the user can set. The timer will activate a motor.
will the time be accurate enough?
I am using the pic basic pro and the usb programmer.
What's the easiest way to get this done? any suggestions is appreciated.
Thanks

I give u example of simple timer and clock where user can set when they want to high certain port of the PIC. THere is only two buttons need to be used. First button to toggle from one menu to other menu. For example if we press this button then we can change from setting minute to the hour setting menu. Other button function is to set(increase) the value of second, minute, hour and etc. But how accurate this timer is depend on the oscillator u use as Steve B had said before. Attached herewith is the code: Please advise if you have some input on this code. Thanks.

symbol second=2000
symbol second2=1000

second1 VAR BYTE
minute VAR BYTE
hour VAR BYTE
day VAR BYTE
time1 VAR BYTE
time2 VAR BYTE

second1=0
minute=0
hour=0
day=0
time1=0
time2=0
masa3=0


hourset:
Pause 200
LCDOut $FE,1
LCDOut "Set Hour"
LCDOut $FE,$C0
LCDOut "TIME:",DEC2 day,".",DEC2 hour,".",DEC2 minute,".",DEC2 second

IF PORTB.5=1 Then GoSub addhour
IF hour>23 Then hour=0
IF PORTB.6=1 Then GoSub time1set
GoTo hourset

hour1set:
Pause second2
LCDOut $FE,1
LCDOut "Set Time MotorOn1"
LCDOut $FE,$C0
LCDOut "Minute:",DEC2 time1

IF PORTB.5=1 Then GoSub addtime1
IF time1>59 Then time1=0
IF PORTB.6=1 Then GoSub time2set
GoTo time1set

time2set:
Pause second2
LCDOut $FE,1
LCDOut "Set Time MotorOn2"
LCDOut $FE,$C0
LCDOut "Day:",DEC2 time2

IF PORTB.5=1 Then GoSub addtime2
IF time4>99 Then time2=0
IF PORTB.6=1 Then GoSub changeday
GoTo time2set
End

addhour:
IF PORTB.5=1 Then GoSub timer
Return

timer:
second1=0
minute=minute+1
IF minute > 59 Then
minute=0
hour=hour+1
IF hour >23 Then
hour=0
day=day+1
IF day>30 Then
dayi=0
EndIF
EndIF
EndIF
Pause 500
Return

addtime1
IF PORTB.5=1 Then time1=time1+1
Pause 500
Return

addtime2
IF PORTB.5=1 Then time2=time2+1
Pause 500
Return

changeday:
IF PORTB.6=1 Then GoTo day


day:
Pause second
saat=saat+1
IF second1 >59 Then GoSub timer


LCDOut $FE,2
LCDOut "ON:",DEC2 time1,">",DEC2 time2,">"
LCDOut $FE,$C0
LCDOut "TIME:",DEC2 day,".",DEC2 hour,".",DEC2 minute,".",DEC2 second1


IF (minute=time1 OR minute=time2) Then High PORTB.7
IF (minute=time1+1 OR minute=time2+1) Then Low PORTB.7
Goto day

rocky79
- 22nd January 2007, 03:38
Thanks Fazan for the code but one thing i didn't understand is how do i get the actual seconds by simply counting? if you count from 1 to 60 does it mean its 1 to 60seconds? can you clarify how you convert from the simple execution time for the count to actual seconds.
Thanks a lot

paul borgmeier
- 22nd January 2007, 05:27
Another Option - might help get you started
http://www.picbasic.co.uk/forum/showthread.php?t=2129

fazan83
- 22nd January 2007, 06:05
Thanks Fazan for the code but one thing i didn't understand is how do i get the actual seconds by simply counting? if you count from 1 to 60 does it mean its 1 to 60seconds? can you clarify how you convert from the simple execution time for the count to actual seconds.
Thanks a lot

How accurate it is depend on the speed of oscillator that u use.

sayzer
- 22nd January 2007, 07:38
Also, the ambient temp is the most effective factor.

You can get a slow running or fast running clock ticks depending on the temperature of the environment in which your PIC is running. (ambient temp)

--------------------------

Acetronics2
- 22nd January 2007, 09:49
Hi, Rocky

I just built Thermo-baro-clock with a MS5534 sensor.( 16F876 ) the clock is driven by a 32.7... khz "clock crystal" connected to RC0,RC1 pins and using TMR1 ovf interrupt.

The only way to adjust reading is to trim the TMR1 preset ( 32768 ...in theory ). an adjustable capacitor parallelled to one of the xtal load caps could also help a bit.

Possible also to use two pushbuttons to inc/dec the TMR preset value, you then store in the EEPROM ... but do not forget to limit the trim range !!!

But as always ... no simple automatic trimming.

Absolute references will always be our main headaches ... cause there's none naturally existing !!!

best regards
Alain

rocky79
- 23rd January 2007, 02:02
Thanks guys for all the help and links. I found a chip that does all the hard work for me on top of that it is featured with 2 alarm settings.
http://pdfserv.maxim-ic.com/en/ds/DS3232.pdf

I will give it try and will see how it goes.
Thanks again

SteveB
- 23rd January 2007, 04:25
I've used the clock only version of the chip (a temp. compensated oscilattor) and can attest to it's accuracy. It was hard to get a hold of at the time, though supply might be better now.

SteveB

rocky79
- 11th February 2007, 20:44
I've used the clock only version of the chip (a temp. compensated oscilattor) and can attest to it's accuracy. It was hard to get a hold of at the time, though supply might be better now.

SteveB

Hello Steve,
I am using the RTC DS3232 from dallas. you said you have used it before. Can you share your picbasic code? I am having trouble setting the time.
when i read the time and date. I get them all as 4.
Here is mine:
'Using the pic 16f630

include "modedefs.bas"
define OSCCAL_1k 1
CMCON=%0000011' Turn off comparator module


minutes var byte
hours var byte
day var byte
date var byte
month var byte
year var byte

reset Var PORTC.0

rtcyear var byte
rtcday var byte
rtcmonth var byte
rtcdate var byte
rtchour var byte
rtcmin var byte
rtcsec var byte

' Set initial time to 8:00:00AM 07/16/99
rtcyear = $99
rtcday = $06
rtcmonth = $07
rtcdate = $16
rtchour = $08
rtcmin =0
rtcsec =0

startbigNums con 2
endBigNums con 3
clrLCD CON 12
backLightOn con 14
backLightOff con 15
cursorPos con 16
enter con 10
bell con 7
b2 var byte
CONTL CON %01101000


Low reset 'reset the RTC
input reset ' undo reset..basically it puts pin reset at high impedance

'Set the time
I2cwrite PORTC.2,PORTC.1,CONTL,$01,[rtcmin] 'minutes
I2cwrite PORTC.2,PORTC.1,CONTL,$02,[rtchour]'hours
I2cwrite PORTC.2,PORTC.1,CONTL,$03,[rtcday] 'day
I2cwrite PORTC.2,PORTC.1,CONTL,$04,[rtcdate]'date
I2cwrite PORTC.2,PORTC.1,CONTL,$05,[rtcmonth]'month
I2cwrite PORTC.2,PORTC.1,CONTL,$06,[rtcyear] 'year


pause 10
'Low reset
'input reset
I2cread PORTC.2,PORTC.1,CONTL,$01,[minutes]
I2cread PORTC.2,PORTC.1,CONTL,$02,[hours]
I2cread PORTC.2,PORTC.1,CONTL,$03,[day]
I2cread PORTC.2,PORTC.1,CONTL,$04,[date]
I2cread PORTC.2,PORTC.1,CONTL,$05,[month]
I2cread PORTC.2,PORTC.1,CONTL,$06,[year]

serout2 0,16780,[clrLCD,"Time",hex hours,":",hex minutes,enter,_
"date:",hex date,"/",hex month,"/",hex year]
end

SteveB
- 11th February 2007, 22:01
Bad wording on my part. What I ment was that I have used one of the temperature compensated oscillators (the DS32kHz, not the DS3232 RTC) with other RTCs and as an input to the PIC timers, and they worked well. I'll have a glance at you code and the Datasheet to see if I can spot anything.

With other DSXXXX RTCs, I have seen the need to set some of the registers to appropriate setting before things work correctly. So that is one place to start.

I'll get back if I see anything.

SteveB

BobK
- 11th February 2007, 23:13
Hi Rocky,

I think your code is missing the clock register constants. I use the DS1337 RTC from Maxim. The code for this chip was posted here either in 2004 or 2005 by Tonigalea. This chip also has two alarms. The key to an accurate clock is as was mentioned before. But you must also use the correct crystal. The DS1337, for example, must have a crystal with a 12.5pf capacitance load. When you look in a parts catalog, DigiKey for example, look for the capacitance load for 32,768 crystals. They are around $.50 or less.

We also just covered RTC's with jcleaver using a DS1307. It looked like he also didn't list the RTC register constants. I sent him a copy of a program for that RTC and he got that going.

I would give a serious look at Paul Borgmeier's Easy Accurate Clock programs as they are very easy to work with and setting up user interface for alarms should be very easy also.

You asked a questions about the timing that no one seemed to answer. The PIC generates an internal time base which is derived from the Oscillator of the PIC divided by 4 so if you are using a 4mHz crystal then you PIC clock runs at 1mHz. Then there are programmable timers in the PIC that can be set up to count pulses then interrupt the PIC program to advance the clock program and/or do tasks at a specified time.

Hope this helps.

BobK

rocky79
- 12th February 2007, 18:29
Bob,
Can you specify what register constant i am missing? i look it up and i didn't see anything else i need to specify.Remember its the DS3232 i aim using so it might be slightly different than the other rtc's
Please let me know

thanks

rocky79
- 12th February 2007, 18:30
Bad wording on my part. What I ment was that I have used one of the temperature compensated oscillators (the DS32kHz, not the DS3232 RTC) with other RTCs and as an input to the PIC timers, and they worked well. I'll have a glance at you code and the Datasheet to see if I can spot anything.

With other DSXXXX RTCs, I have seen the need to set some of the registers to appropriate setting before things work correctly. So that is one place to start.

I'll get back if I see anything.

SteveB

Thanks steve! let me know if you find anything unusual or wrong i n my code...Thanks again

BobK
- 12th February 2007, 19:25
Hi Rocky,

Look on page 11 of the datasheet. The addresses in Hex and the register name under the function column are what you need. I have attached a listing example from a DS1337 program for an example. The datasheet had the same type of setup as this chip. You can't write any data to the RTC if it doesn't know where to put it.

Boy if I'm wrong here I really apologize but I think I'm right. Please let me know.

BobK

rocky79
- 12th February 2007, 23:30
Hi Rocky,

Look on page 11 of the datasheet. The addresses in Hex and the register name under the function column are what you need. I have attached a listing example from a DS1337 program for an example. The datasheet had the same type of setup as this chip. You can't write any data to the RTC if it doesn't know where to put it.

Boy if I'm wrong here I really apologize but I think I'm right. Please let me know.

BobK

Bob,

That's what i have on my code if you look back. Here is a sample take the minutes sample for example:
'Set the time
I2cwrite PORTC.2,PORTC.1,CONTL,$01,[rtcmin] 'minutes
the CONTL is the address of the RTC the $01 is the minutes register and the rtcmin is the value to be put in the register.
I hope you find my mistake.

Thanks

BobK
- 13th February 2007, 01:47
Hi Rocky,

Click on SEARCH and enter DS1337. Look at the post :example code for DS1337, dated 28Oct2004. The program shows the register addresses. If you don't have the addresses setup then the clock can't be set and it will not work properly.

All of the addresses on page 11 of the datasheet need to be setup as constants. Take the list of addresses on the example code I attached on my last reply. Put them into your program and try them. I do understand you also may possibly have other issues with your setup but these constants need to be a part of your program.

BobK

rocky79
- 15th February 2007, 03:02
Thanks Bob and all of you who replied to this post. My mistake was simple like usual. The RTC slave address is 7 bits so I added an extra zero to the left to make it 8 but i was wrong the zero should be to the right!