CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

How to create accurate time in the CCS for PIC

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
hoa35ktxd



Joined: 23 Nov 2014
Posts: 27
Location: Vietnam

View user's profile Send private message

How to create accurate time in the CCS for PIC
PostPosted: Sat Apr 16, 2016 7:55 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Apr 16, 2016 10:03 am     Reply with quote

It's right there in the Code Library, near the top:
http://www.ccsinfo.com/forum/viewtopic.php?t=26177
temtronic



Joined: 01 Jul 2010
Posts: 9226
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Apr 16, 2016 11:25 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Apr 17, 2016 1:47 am     Reply with quote

Many thank!
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Sun Apr 17, 2016 8:49 pm     Reply with quote

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: 19515

View user's profile Send private message

PostPosted: Mon Apr 18, 2016 1:50 am     Reply with quote

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!....
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group