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

Timing on a two speed program

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



Joined: 13 Apr 2011
Posts: 417

View user's profile Send private message

Timing on a two speed program
PostPosted: Mon Apr 22, 2024 12:50 pm     Reply with quote

I have a PIC16LF1827 and, since is a low power battery device, I change the device speed from 31kHz to 4MHz when needed but I need a time base and since all the timers must run from the internal oscillator the clock timing will change a lot.

I don't need super precision, the 4MHZ phase last around 2 seconds but it depends on the user.

I was thinking in to run the timer only when is in low power mode(31kHz) and add 2 seconds for every time the device is wake up before goes in low speed mode.

Do you have a better approach?
_________________
Electric Blue
newguy



Joined: 24 Jun 2004
Posts: 1907

View user's profile Send private message

PostPosted: Mon Apr 22, 2024 12:57 pm     Reply with quote

When high speed is invoked, implement some sort of timer ASAP. Clock source will be the 4MHz clock.

Simply use that timer to count.

When it's time to go back to sleep, calculate how many 31kHz ticks have elapsed. Update your second clock and then use the remainder to preset the 31kHz timer. For example, if 2.7 seconds have elapsed, add 2 to your second counter and preset the 31kHz timer to expire in 0.3 seconds.
E_Blue



Joined: 13 Apr 2011
Posts: 417

View user's profile Send private message

PostPosted: Mon Apr 22, 2024 1:05 pm     Reply with quote

Cool, nice approach, I wasn't thinking in to use a second timer on HiSpeed mode. I was thinking in a way to read the WDT that always run with the same clock but yours is better.

I will try your approach. thanks.
_________________
Electric Blue
temtronic



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

View user's profile Send private message

PostPosted: Mon Apr 22, 2024 5:00 pm     Reply with quote

'adding 2 seconds' sounds like you have a 'real time clock' as part of the PIC project ?

If so add an external RTC module, maybe splurge for the high accurate one ! I use them in every PIC project and the one I use has an EEPROM as well.
Using the external RTC also gives you the option of having it send an interrupt.
Mine are set for 1 second to 'trigger' main() to update the LCD, trigger the DS temperature sensors, and a few other 'things'.
Being battery backed up, no loss of real time either.
E_Blue



Joined: 13 Apr 2011
Posts: 417

View user's profile Send private message

PostPosted: Mon Apr 22, 2024 6:08 pm     Reply with quote

Nope. Is an RF PIR, so is interrupted every time a person walk in front of it.
I need to send a test signal to the alarm panel every 12 hours to know that the device is there.

I can't add any external crystal or base time, is something super cheap.
_________________
Electric Blue
temtronic



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

View user's profile Send private message

PostPosted: Tue Apr 23, 2024 5:20 am     Reply with quote

random thoughts....

hmm, remember the internal RC OSC isn't 'great' at accurate timing and varies on temperature.

Only sending once every 12 hrs is 'scary' to me. My luck it'd fail 12.3 seconds after the last 'I'm OK'......My remote energy systems talked every 15 seconds, even when on batteries.

Is your communications 2 way ? If so,send the remote the current time.

batteries lose a lot of power when cold,so use bigger batteries.....or play every 'trick' Microchip has to reduce power consumption.Obvious ones are turnin goff peripherals, 10meg pullups, largecap across battery, etc.
Ttelmah



Joined: 11 Mar 2010
Posts: 19495

View user's profile Send private message

PostPosted: Tue Apr 23, 2024 7:08 am     Reply with quote

Just have a timer interrupt for the clock. Perhaps use Timer0, with /32, and
then this will interrupt every 0.946 second when running at 31KHz. Remember
this is quite a low accuracy oscillator, so +/-5%. Simply have a global
count variable. Set this to '1' when you are running 31KHz, and to 122
when you switch to 4MHz. In the interrupt have a static variable counter,
and increment your time whenever this matches or exceeds the global
variable (and resets to zero when this happens). When your clock is 4Mhz,
you will get your time incrementing every 122*256*32 = 999424 clocks
(= 0.999 seconds), while when in 31KHz mode it will increment every
256*32 = 8192 clocks = 0.946 seconds.
E_Blue



Joined: 13 Apr 2011
Posts: 417

View user's profile Send private message

PostPosted: Tue Apr 23, 2024 10:50 am     Reply with quote

temtronic wrote:
random thoughts....

hmm, remember the internal RC OSC isn't 'great' at accurate timing and varies on temperature.

Only sending once every 12 hrs is 'scary' to me. My luck it'd fail 12.3 seconds after the last 'I'm OK'......My remote energy systems talked every 15 seconds, even when on batteries.

Is your communications 2 way ? If so,send the remote the current time.

batteries lose a lot of power when cold,so use bigger batteries.....or play every 'trick' Microchip has to reduce power consumption.Obvious ones are turnin goff peripherals, 10meg pullups, largecap across battery, etc.


I know that has very low accuracy but my boss doesn't want to add anything else.
The batteries are 2 AAA and must last at least a year, so, is not viable to send data every 15 seconds.
The device is one way, only has an RF TX.
_________________
Electric Blue
temtronic



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

View user's profile Send private message

PostPosted: Wed Apr 24, 2024 5:28 am     Reply with quote

Redesign using ONE lithium battery (18650) instead of two AAA cells.
yes, a little more money but WILL last over a year. Also no worry about corrosion(leaky ) AAA batteries and fewer connections.
I'm assuming some type of plastic AAA cell holder, so you could hardwire battery onto PCB.

If possible, cut code to send battery condition to 'central command'. Most if not all 'burglar alarm' battery powered sensors do this, for,well, obvious reasons.

Using 'cheap' batteries in any product is a false economy. If the 18650 is 'too big' or 'costs too much', look for a physically smaller, less expensive battery.
Do the 'math' to see how many electrons you need for 12 months, add 25% more. I spent about 3 MONTHS figuring out the best battery. NOT being called at 2AM in the middle of Winter due to 'battery low alarm' as NICE !
jeremiah



Joined: 20 Jul 2010
Posts: 1345

View user's profile Send private message

PostPosted: Wed Apr 24, 2024 7:56 am     Reply with quote

You probably don't have to redesign, they make AAA lithium batteries. Most I've seen have been in the 1100-1200 mAH range, which is a good 50% more shelf life / power budge than a 750 mAH battery.
E_Blue



Joined: 13 Apr 2011
Posts: 417

View user's profile Send private message

PostPosted: Wed Apr 24, 2024 10:06 am     Reply with quote

Lithium batteries doesn't have a bigger self discharge index?

I'm setting T1 this

Code:
set_timer1(0xFFFF-(31000/4));


This setting is giving me 1.08s base time. Doing some math gives me around 28.7kHZ. That's around -7% of deviation from 31kHz.

This is normal?

I know you said UP TO 5% but was expecting, at most 3%, but this is beyond.
_________________
Electric Blue
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