How to write the long delay code ???

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

How to write the long delay code ???

Postby engkiang » Fri Jan 13, 2012 10:31 am

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.
engkiang
Newbie
 
Posts: 15
Joined: Mon Aug 03, 2009 11:42 pm

Re: How to write the long delay code ???

Postby ABSF » Fri Jan 13, 2012 11:02 am

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
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: How to write the long delay code ???

Postby engkiang » Fri Jan 13, 2012 11:33 am

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.
engkiang
Newbie
 
Posts: 15
Joined: Mon Aug 03, 2009 11:42 pm

Re: How to write the long delay code ???

Postby ABSF » Fri Jan 13, 2012 12:09 pm

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
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: How to write the long delay code ???

Postby shahrul » Fri Jan 13, 2012 7:56 pm

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.
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 7 guests