View previous topic :: View next topic |
Author |
Message |
hoa35ktxd
Joined: 23 Nov 2014 Posts: 27 Location: Vietnam
|
How to create accurate time in the CCS for PIC |
Posted: Sat Apr 16, 2016 7:55 am |
|
|
My code:
Code: |
#include <16F883.h>
#FUSES WDT //Watch Dog Timer
#FUSES HS
#use delay(crystal=20000000)
int16 time_stick;
#INT_RTCC
void RTCC_isr(void)
{
set_timer0(5);
time_stick++;
}
void main()
{
setup_wdt(WDT_ON);
setup_wdt(WDT_18MS|WDT_TIMES_128); //~2,3 s reset
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1|RTCC_8_bit); //51,2 us overflow
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);
while(TRUE)
{
restart_wdt();
if (time_stick>=10000)
{
output_toggle(PIN_A1);
time_stick=time_stick%10000;
}
}
}
|
It runs slower than actual. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Sat Apr 16, 2016 11:25 am |
|
|
PCM P is right !! I've used this code on 4 different PICs and it's bang on !!
on the plus side, no I/O pins used !!
Though for any 'serious' timekeeping it's best to use an RTC chip like the DS1307 with coin cell battery unless your PIC power supply IS battery backed up.
Jay |
|
|
hoa35ktxd
Joined: 23 Nov 2014 Posts: 27 Location: Vietnam
|
|
Posted: Sun Apr 17, 2016 1:47 am |
|
|
Many thank! |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Sun Apr 17, 2016 8:49 pm |
|
|
I read this and wonder: How many seconds / year (+/-) of absolute error is "accurate"???
There is a big difference if we are talking astronomy or navigation vs. a personal alarm clock... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Mon Apr 18, 2016 1:50 am |
|
|
Yes.
This is a very important (and often misunderstood) question.
It's like the thing of people asking for an 'average'. Any one with some mathematical background will want to know that there are a very large number of different types of 'average'.
Then the one of using an old steel tape measure versus a 'modern' ultrasonic measure. Many people will say 'of course' the latter is 'more accurate'. "Look it says the distance is 4.384m". However read the fine print and you see that it says it's reading is +/-5mm, while the steel tape is actually a class 1 tape, and at the correct tension, for distances up to 10m, is quoted at +/-1.1mm....
Then (of course) the question of the relative 'accuracy' of integers versus floating point.
In this case, the poster is probably talking about long term 'drift' in the timing. This is going to depend on a huge range of factors, including the supply stability, the temperature range involved etc..
In the original code, setting a timer 'to' a value, is always going to result in lost ticks. The version from Ckielstra is a much better solution, and will be as accurate as the clock source for the PIC. If you need better than this, then things get relatively complex!.... |
|
|
|