## Real time Hourly Device Control

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 8.

Moderator: Benj

Derrihj
Posts: 277
Joined: Mon Jul 09, 2018 12:43 pm
Contact:

### Real time Hourly Device Control

Hi every one,is it advisable to use internal oscillator while designing a real time clock? my issue is,i wanted to control some device and turn it off when set hours have reached so what i did i got my PIC16f886, set the internal frequency to 8MHZ, enabled Timer2 int with Prescaler Rate= 1:16,
Postscaler Rate=1:10, Rollover= 250 and got a clean 50.000Hz interrupt Frequency.The ka project tested fine both software and hardware the only thing i realised was when i compared my seconds movement with a real clock set up to run at the same time my seconds where some how slower than all the real clocks i tested with so my question is, can't we use internal oscillators when designing real time projects or controls? why the slow movement yet i had no delays used in the timing loop. Here is my flow-chat.
Attachments
Device Time Cutoff.fcfx

medelec35
Valued Contributor
Posts: 9014
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Contact:

### Re: Real time Hourly Device Control

Hi Derrih,
Derrihj wrote:is it advisable to use internal oscillator while designing a real time clock?
No, internal oscillator is not suitable.
Lets do some calculations to prove this.
Suppose internal osc has a tolerance of 1%.
Since there are 86400 seconds in a day.
The maximum error is 86400*1/100 = 864 secs = 14.4 minutes!
As your project is running at 8MHz, The alternative could be to use a 8MHz crystal which could have a tolerance of range of about 25 to 100 ppm (parts per million) depending on choice.
Suppose you choose a tol of 25ppm then the maximum error is 86400*25/1000000 = 2.16 secs per day.
The disadvantage is crystals accuracy depends on both temperature and associated load capacitors.
For the best accuracy, there are RTC (Real Time clock) components that are not affected by temperature changes.
E.g DS3231/DS3232 by Maxim. They have a tolerance of 2PPM.
The maximum error is 86400*2/1000000 = 0.17 secs per day = 63 seconds per year!
Some people use a 32.768KHz watch crystal connected to timer1 external pins.
If that was the case the error calculations are the same as parts per million (depending on crystal choice), but you then can use internal oscillator.

So you have some choices there.
Martin

Derrihj
Posts: 277
Joined: Mon Jul 09, 2018 12:43 pm
Contact:

### Re: Real time Hourly Device Control

Thanks allot medelec35 its educative to always have you around and that is why its always good to ask the professionals before u act now i have to remake the PCB with an external oscillator this one with internal oscillator was one second behind when i was testing it this went on increasing to 2 seconds and more as time went on.harass am beginning to hate internal oscillators.

medelec35
Valued Contributor
Posts: 9014
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Contact:

### Re: Real time Hourly Device Control

Hi Derrihj,
Thanks you & Your'e welcome.
Internal oscillators help to keep component count down, and accuracy is OK for lower baud communication purposes and other non very precision timing and e.g UART, SPI, I2C and even servos etc.
When is come to very precision timing like TIME, UART with higher baud rates and very short duration pulses etc then internal oscillator is not the way to go.
Martin

medelec35
Valued Contributor
Posts: 9014
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Contact:

### Re: Real time Hourly Device Control

One other thing to mention.
I believe 8 MHz is a good choice in frequency as you can enable PLL (if your microcontroller supports it) to multiply actual clock frequency to 4 X 8 = 32MHz.
The higher frequency will have much less of an impact and will allow higher comms rate to be used.

Looking at your flowchart, I would suggest the following within interrupt:
2020_01_18_15_54_37_Window.png
Martin

Derrihj
Posts: 277
Joined: Mon Jul 09, 2018 12:43 pm
Contact:

### Re: Real time Hourly Device Control

Thanks medelec35 yes i had copied that into interrupt when i was trying out any way of making my counting work better and yes it improved somehow
as seconds were easily added up within the int not to be interrupted by the next beat of int count while in the loop outside INT.Dont know if ive sad it right but anyway another thing PIC16f886 has no PLL but what u are trying to say that PLL would solve the problem sothat i dont have to switch to external oscillator?

medelec35
Valued Contributor
Posts: 9014
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Contact:

### Re: Real time Hourly Device Control

Derrihj wrote: another thing PIC16f886 has no PLL but what u are trying to say that PLL would solve the problem sothat i dont have to switch to external oscillator?
No not at all the PLL is only as accurate as the clock.
So if chip did have PLL and using 8MHz internal clock at 1% tolerance, then if use PLL the clock will be 8 * 4 = 32MHz, also with a tolerance of 1%.
Martin

Derrihj
Posts: 277
Joined: Mon Jul 09, 2018 12:43 pm
Contact:

### Re: Real time Hourly Device Control

I see some circuits with an external crystal oscillator with its body grounded whats the point in that does it do any good in stability? Whats so important in doing that?

Derrihj
Posts: 277
Joined: Mon Jul 09, 2018 12:43 pm
Contact:

### Re: Real time Hourly Device Control

Can i use the RTC component (DS3231 ic) to extract out only the seconds and use them in my project for accuracy only seconds from DS3231 ic then i use them to give me minutes and hours then i use the hours to control a relay? Check out the flowchat below and guide me on this.
Attachments
RTC test.fcfx

Benj
Matrix Staff
Posts: 14929
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Contact:

### Re: Real time Hourly Device Control

Hello,
I see some circuits with an external crystal oscillator with its body grounded whats the point in that does it do any good in stability? Whats so important in doing that?
Maybe only partially to do with electrical shielding and probably more to stop the high frequency from being radiated out rather then improving internal stability though it likely will help a little. Likely mainly used for mechanical stability to avoid stress to the connection pins.
Can i use the RTC component (DS3231 ic) to extract out only the seconds and use them in my project for accuracy only seconds from DS3231 ic then i use them to give me minutes and hours then i use the hours to control a relay?
Sounds like it should be possible. I'll have a look at your code.

Benj
Matrix Staff
Posts: 14929
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Contact:

### Re: Real time Hourly Device Control

Looking at your program seconds will never be equal to 60 as the RTC will wrap the value from 59 to 0.

I've made a small change for you to instead check for seconds being less then the previous value.
RTC test.fcfx
This way the program should work even if you don't read the RTC every second, an absolute minimum of 2 times a minute should be enough for reliable operation.

Derrihj
Posts: 277
Joined: Mon Jul 09, 2018 12:43 pm
Contact:

### Re: Real time Hourly Device Control

Thanks Benj will have to build a hardware with LCD, test and see how the seconds accurately move in line with a physical normal clock seconds.The baud rate for DS3231 here should be set to what 100KHz,400KHz or 1MHz for the Ds3231 to work perfectly?

Benj
Matrix Staff
Posts: 14929
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Contact:

### Re: Real time Hourly Device Control

Hello,
The baud rate for DS3231 here should be set to what 100KHz,400KHz or 1MHz for the Ds3231 to work perfectly?
If you're not bothered about throughput then the slower you clock the I2C the more reliable and immune to noise it should be so for your application I would go for 100KHz. However reading the datasheet it only mentions 400KHz so I would use that setting

Also for added stability make sure you have a ceramic decoupling cap as close as possible to the IC. 100nF is my goto value but anything from 1nf to 10uf should work pretty well. If you're using a module then it likely already has the cap in place.

Derrihj
Posts: 277
Joined: Mon Jul 09, 2018 12:43 pm
Contact:

### Re: Real time Hourly Device Control

Many thanks goes out to medelec35, i always say its Educative to have you around and Yes it is also thanks to Benj,my project was a success.After i remade the PCB this time with an external oscillator it turned out perfect, you can hardly see a difference if compared with a real physical clock in-fact you can not see any difference ive run it for hours and its wonderful.I gave this device a name as SSD (System Service Dailer) ok You enter the hours on a keypad that you want a system to run and after those hours the system will be cutoff from power and request for servicing.I used the MCU eeprom to store hours used to keep that record even if power to the system is swtched off.There is a password requested before you can enter new service run time, you can enter hours from 1 hour to 9000 hours there is an option of timing only Minutes from 1 minute to 60 minutes also entered using a keypad after you have provided a correct password,you can also change the password as needed.I did the password thing in this project so that only the operator or engineer can change the service run time of any system. Check out the Pictures of my finished project will upload a video of it in action soon if possible and show how it works, guys Flowcode is intresting.
Attachments
20200125_202848.jpg (140.44 KiB) Viewed 1445 times
20200125_202838.jpg (142.61 KiB) Viewed 1445 times
20200125_202759.jpg (136.57 KiB) Viewed 1445 times
Last edited by Derrihj on Sat Jan 25, 2020 8:13 pm, edited 1 time in total.

Derrihj
Posts: 277
Joined: Mon Jul 09, 2018 12:43 pm
Contact:

### Re: Real time Hourly Device Control

One more
Attachments
20200125_203006.jpg (141.7 KiB) Viewed 1444 times

medelec35
Valued Contributor
Posts: 9014
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Contact:

### Re: Real time Hourly Device Control

Hi Derrihj,
Derrihj wrote:Many thanks goes out to medelec35, i always say its Educative to have you around
It's a pleasure & glad I have helped.
Your project sounds interesting and would love to see a video if it working and perhaps compared to a clock.
What tolerance crystal did you opt for?
Martin

Derrihj
Posts: 277
Joined: Mon Jul 09, 2018 12:43 pm
Contact:

### Re: Real time Hourly Device Control

Its a 8MHz crystal, i got these from china but i even dont know its tolorence coz ive learned this tolorence thing about crystal oscillators from you so when i was buying them i had no idea about tolorence but when i measured it with a crystal tester it was showing 7.997MHz
Last edited by Derrihj on Mon Jan 27, 2020 10:58 pm, edited 1 time in total.

Derrihj
Posts: 277
Joined: Mon Jul 09, 2018 12:43 pm
Contact:

### Re: Real time Hourly Device Control

There is something i want to add in this project and that is a GSM modem,when you look at my project there are 4 pins next to the LCD connector two are for 5v out and the other two are TX and RX pins but at the moment are grounded thru two pull down resistors.When the GSM is connected i would like it to work in this way: Take for example there is a system installed somewhere may be some organisation's AC generator with my device in place,so when i make a phone call to the GSm connected to my device the GSM cancels the call and in the next 2 seconds or so the GSM sends a message to my phone asking to type password then i send the numbers in the exact order of the saved password in my device and the GSM recieves them,sends it to the MCU to compare it with the correct password then if the two are a perfect match, the GSM sends the system info to my phone like;
SHr = 250 (Set Hours)
SRHr = 10 (System Run Hours)
SRM = 2 (System Run Minutes)
SRS = 15 (System Run Seconds)

Just like that which shows that the system has so far run for 10 hours,2 minutes and 15 seconds out of the set "250" hours for example. Then i can be able to change the run time,stop the operation of the system or run the system with just a simple message from my phone to the Device installed miles away.
Last edited by Derrihj on Mon Jan 27, 2020 11:14 pm, edited 1 time in total.

Derrihj
Posts: 277
Joined: Mon Jul 09, 2018 12:43 pm
Contact:

### Re: Real time Hourly Device Control

Thanks guys
Last edited by Derrihj on Mon Jan 27, 2020 3:47 pm, edited 1 time in total.

Derrihj
Posts: 277
Joined: Mon Jul 09, 2018 12:43 pm
Contact:

### Re: Real time Hourly Device Control

Thanks guys for your response, again guys i will need your help with the GSM part as ive explained above any kind of help it can be a flow chat any, i can start and build on that.