How to write the long delay code ???

Posted:
Fri Jan 13, 2012 10:31 am
by engkiang
Hi All,
I'm newbies on programming in PIC C, I'm looking some help on creating the code that looping two tasks where one task will be execute after 3 days, another one task will be execute after 3 hours. The ideal like below:
Loop3d:
Reach to 3 days?
no, go to Loop3d.
yes, set the portA to high.
Loop3h:
Reach 3 hours?
no, go to Loop3h
yes, set portA to low
go to Loop3d
Thanks.
Re: How to write the long delay code ???

Posted:
Fri Jan 13, 2012 11:02 am
by ABSF
engkiang WROTE:- CODE: SELECT_ALL_CODE
Loop3d:
Reach to 3 days?
no, go to Loop3d.
yes, set the portA to high.
Loop3h:
Reach 3 hours?
no, go to Loop3h
yes, set portA to low
go to Loop3d
In that case you 3-hour delay would only be run after 3 days. Something is wrong in your flowchart. It should be like this
- CODE: SELECT_ALL_CODE
Main():
call Loop3d
call Loop3h
Loop back to Main()
Loop3d:
Reach to 3 days?
no, go to Exit1
yes, set the portA to high.
Exit1: return to calling program
Loop3h:
Reach 3 hours?
no, go to Exit2
yes, set portA to low
Exit2:return to calling program
The best way to do long delay is to use a RTC (Real Time Clock)
Allen
Re: How to write the long delay code ???

Posted:
Fri Jan 13, 2012 11:33 am
by engkiang
Thanks Allen.
Kindly describe on how to use the RTC for this type of delay. Any ideal code and information that related to this can be refer?
Thanks again.
Re: How to write the long delay code ???

Posted:
Fri Jan 13, 2012 12:09 pm
by ABSF
First you have to understand what is a real time clock. The easiest way is to download the datasheet of DS1307 from internet. Then learn how to interface it to your PIC. You can also refer to the cytron PR12 here:
http://www.cytron.com.my/viewProduct.php?pid=EAo4Mh0xEQsqEzAxLi8yKqD/6h4wOeifVdvOuCrCnwY=&store=I am not telling you to make the clock but to refer to the schematics and sample code to understand how it works and how to make use of it in your project. This chip is very popular and is very small like LM555. To interface to MCU only needs 2 or 3 wires.
After reading if you still have any problem, you can always post your questions here.
Allen
Re: How to write the long delay code ???

Posted:
Fri Jan 13, 2012 7:56 pm
by shahrul
This is example code for
DS1307.
On the example, have this code
- CODE: SELECT_ALL_CODE
if(sec%5==0) LCD_LIGHT=0;
else LCD_LIGHT=1;
This is example to execute some instruction every 5 seconds. You must try yourself to do loop 3 hours or 3 days.