PDA

View Full Version : RTC & timer1 help required.



Mark J
- 17th December 2010, 23:19
Hi guys,

Im working on a high power LED lighting unit and using a RTC (PC8583) proto board kindly lent to me by Malc_c and basically this is what im trying to do (bear with me!):

Pic used: 18F2455 as use USB functionality (not related to the current problem)

In controlling the LED's I am having them fade in and out (ie dawn to dusk) and in the program, I take the time I have set for the fade in duration and depending upon the start and end intensity of the LED's I calculate the delay in seconds and milliseconds for the delay inbetween adding 1 to the PWM value, so that over the space of eg. 2 hours , it pauses for 37.69 seconds between each PWM increase based on a PWM end value of 191 starting at 0.
So as you can see to get good time keeping so that the fade keeps as close as possible to the set fade in time I use milliseconds.

I have set a timer1 interupt (using DT's excellent instant interupt includes etc) and set it to prescale 1:1 (ie T1CON %00000001) and all I have in the interupt is a simple counter that adds ticks, and when they reach >180 it resets to 0 and adds one to a counter.
Now this is where the problem lies, as displaying the counter that is incremented in the timer when it goes over 180 on an LCD above the seconds from the RTC it 'seems' to keep in time with the seconds, but over time you can see that the increase goes out of sync with the RTC seconds, and ive fiddled with the 180 either way and can see the differences, but just cant seem to lock it in so its bang on with the RTC seconds counter on the LCD

Anyone know why it might be going out of sync as I thought that things were pretty constant regarding timing (using a 4Mhz xtal btw)

Can post code bits if needed, just thought id put a 'small'? lol post asking if anyone could throw some light onto my problem if possible.

Many thanks.

Kamikaze47
- 24th December 2010, 15:16
It's hard to say what's happening without seeing the code.

Keep in mind that crystals do not keep perfect time. They have a rated accuracy - usually in ppm (parts per million). They are also prone to changing frequency with temperature and time.

Mark J
- 28th December 2010, 22:24
Yeah I had thought about the crystals etc, and decided I didn't need to be quite so picky on the timing etc, so decided to simply round up or down the results which keeps the times within acceptable timing etc, as was speaking to Malc_c and realised I was been too an@l LOL!!!