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

Wake from sleep on pin change?
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Will Reeve



Joined: 30 Oct 2003
Posts: 209
Location: Norfolk, England

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

PostPosted: Tue Oct 05, 2004 1:37 am     Reply with quote

You need to re-init you LCD everytime you power-up! I drive a 2x16 from a PIC pin for low power and on wake-up I just resend the init bytes, then the original text.

Will
ernest



Joined: 11 Feb 2004
Posts: 51

View user's profile Send private message

PostPosted: Tue Oct 05, 2004 2:29 am     Reply with quote

Yes...I have done that. Each time after power-up, I'll initialize the LCD as shown below:

if(input(PIN_C5)==0) // RC5 to detect for presence of main power
{
OUTPUT_FLOAT(PIN_C6); // if RC5=0, disable Tx & Rx as no main power
OUTPUT_FLOAT(PIN_C7);
// PORT_B_PULLUPS(TRUE);
Just_wokeup = 1; // flag to indicate that main power has just returned
}

else // if RC5==1, then ALLOW LCD DISPLAY
{

if(Just_wokeup==1) //run this ONCE only when main power just returned
{
lcd_init(); //INITIALIZE LCD
kbd_init();
lcd_putc("\f Power Returned ");
lcd_putc("\n ");
printf("\f Main Power Returned");
delay_ms(1);
Just_wokeup = 0; /ensure loop is not repeated until next main power cutoff
}


As you can see in the program, the LCD is initialized after the power to the LCD comes back.

Please comment...
asmallri



Joined: 12 Aug 2004
Posts: 1679
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Tue Oct 05, 2004 4:36 am     Reply with quote

I can think of two possible problems.

1. Hardware - are you 100% sure that every control into the LCD is powered down including for example bias? If any of control lines are active the LCD may not power down correctly - this could be a problem for two reasins but the most interesting for you at the moment is that the power on reset circuitry does not initiate a power on reset. In ths condition you are attempting to talk to a controller that has gone off in to the never-never.

2. After you reapply power to the LCD, are you waiting long enough for the LCD's power on reset process to finish?

Does your LCD have a sleep or low power mode? If so, have you checked to see what sort of current it draws in this mode? You may find there is no need to power it down.
Best Wishes
Guest







PostPosted: Tue Oct 05, 2004 5:42 am     Reply with quote

ernest wrote:
Hi...

OK..temporarily, I'll let my PIC16LF877A run continuosly without sleeping as the currect that draws is not too high as long as I turn off all the other peripherals including the LCD. I tried to turn OFF the LCD but everytime after I turn off the power to the LCD and resupply it back later, the data that is being displayed would be JUNK!. From the Hyperterminal, I can see that the PIC is working fine and PIC can display the data correctly thru Hyperterminal as well. I have tried both ways ie. driving the LCD using external power supply and supplying the power to LCD using one of the PIC I/O pin.

So, what is wrong with the LCD?

The LCD works well when I turn ON the power together with PIC but after that, it doesn't work when I turn OFF the LCD power without turning off the PIC. I have tried setting all the I/Os to the LCD to Hi-Z as well as pull-up after the LCD is powered OFF.

What can I do to solve this problem? Confused

Thanks...

Remember that when power is applied to the PIC, it waits for it's clock to stabilise, and (if the 'power on' timer is anabled), for quite a few uSec more, before starting. This does not apply if you are waking on an external signal. Most LCD's, have quite a slow 'wake up' sequence needed. Hence try delaying for a couple of mSec before doing anything.
Seperately, there is the question of supply sequencing, and rise time. The spec for the main supply rail, has quite a fast rise time required to trigger the internal wake up sequence. If your power hardware takes longer to come on, under 'PIC' control, than on the default power-up condition, this could cause a problem. Also, look carefully at the seqencing of the rails. Some displays _require_ the main power rail to rise before the display bias is applied, otherwise the CMOS latch-up can occur.

Best Wishes
ernest



Joined: 11 Feb 2004
Posts: 51

View user's profile Send private message

Hmm.. LCD still refuses to startup
PostPosted: Wed Oct 06, 2004 11:08 pm     Reply with quote

I've put in 100ms delay after the power to LCd returns before initializing the LCD but it still couldn't startup properly. The LCD still gives me JUNK display even though i've tried with 50ms, 100ms, 150ms, 200ms and 250ms delay.

Is there something wrong with my program/LCD?
Why can it work when I power up the LCD together with MCU?

if(input(PIN_C5)==0) //RC5 to check for main power (1=yes,0=no)
{
output_float(PIN_B0); //set all I/O to LCD to float
output_float(PIN_B1);
output_float(PIN_B2);
output_float(PIN_B4);
output_float(PIN_B5);
output_float(PIN_B6);
output_float(PIN_B7);
OUTPUT_FLOAT(PIN_C6); //set Tx & Rx to float
OUTPUT_FLOAT(PIN_C7);
// PORT_B_PULLUPS(TRUE);
Just_wokeup = 1; //flag to indicate power to LCD just returned
}
else // if RC5==1 ie main power back, then ALLOW LCD DISPLAY
{
delay_ms(150);
if(input(PIN_C5)==1) // check 2nd time to ensure main power is back
{
if(Just_wokeup==1)
{
delay_ms(150); //tried with 50,100,200, 250 ms to let LCD time to power up properly
lcd_init();
kbd_init();
lcd_putc("\f Power Returned ");
lcd_putc("\n ");
printf("\f Main Power Returned");
delay_ms(150);
Just_wokeup = 0;
}
}
}
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Wed Oct 06, 2004 11:49 pm     Reply with quote

Putting the device to sleep does not start the program over. Post you entire program.
ernest



Joined: 11 Feb 2004
Posts: 51

View user's profile Send private message

PostPosted: Thu Oct 07, 2004 1:44 am     Reply with quote

Mark wrote:
Putting the device to sleep does not start the program over. Post you entire program.


#include <16F877A.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#define RTC_SCLK PIN_A1 //PIN_B1
#define RTC_RST PIN_A2 //PIN_B2
#define RTC_IO PIN_A3 //PIN_B3
#include <ds1302.c>
#include <lcd.c>
#include <kbd.c>

int day_off, hour_off, min_off, sec_off, sec_old;
int inc_flag; // flag to represent FLOW ( 0 = FLOW OFF, 1 = FLOW ON)
int sec_last[11]; // create 10 elements array for storing previous SEC. data
int min_last[11]; // create 10 elements array for storing previous SEC. data
int hour_last[11]; // create 10 elements array for storing previous SEC. data
int day_last[11]; // create 10 elements array for storing previous SEC. data
int mth_last[11]; // create 10 elements array for storing previous SEC. data
int year_last[11]; // create 10 elements array for storing previous SEC. data
int i, previously_OFF, timer_sec, timer_min, timer_hour, timer_day; //
int Just_wokeup;

///////////////////////////////////////// BELOW FUNCTIONS FOR SLEEP !!!
// global flag to send processor into sleep mode
//short sleep_mode;

// external interrupt when button pushed and released
//#INT_EXT
//void ext_isr()
// {
// static short button_pressed=FALSE;
// if(!button_pressed) // if button action and was not pressed
// {
// button_pressed=TRUE; // the button is now down
// sleep_mode=TRUE; // activate sleep
// printf("The processor is now sleeping.\r\n");
// ext_int_edge(L_TO_H); // change so interrupts on release
// }
// else // if button action and was pressed
// {
// button_pressed=FALSE; // the button is now up
// sleep_mode=FALSE; // reset sleep flag
// ext_int_edge(H_TO_L); // change so interrupts on press
// }
// if(!input(PIN_B3)) // keep button action sychronized wth button flag
// button_pressed=TRUE;
// delay_ms(100); // debounce button
// } //end of ext_isr
//////////////////////////////////////// ABOVE FUNCTIONS FOR SLEEP !!!

byte get_bcd() {
char first,second;
do {
first=kbd_getc();
} while ((first<'0') || (first>'9'));
lcd_putc(first);
first-='0';
do {
second=kbd_getc();
} while ((second<'0') || (second>'9'));
lcd_putc(second);

return((first<<4)|(second-'0'));
}

void set_clock(){
byte day,mth,year,dow,hour,min;
lcd_putc("\fYear 20: "); // Year 2000 -- 2099 //
year=get_bcd();
lcd_putc("\fMonth: ");
mth=get_bcd();
lcd_putc("\fDay: ");
day=get_bcd();
lcd_putc("\fWeekday 1-7: ");
dow=get_bcd();
lcd_putc("\fHour: ");
hour=get_bcd();
lcd_putc("\fMin: ");
min=get_bcd();
rtc_set_datetime(day,mth,year,dow,hour,min);
}


//////////// CONTROL SETUP MENU ////////////////////////////////
void set_control(){
int sec_buf, min_buf, hour_buf, day_buf;
lcd_putc("\fToff Day: ");
timer_day=get_bcd();
printf("\n\r Day_DEC:%D, Day_HEX:%X, Day_CHAR:%C",timer_day,timer_day,timer_day);
lcd_putc("\fToff Hour: ");
timer_hour=get_bcd();
printf("\n\r Hour_DEC:%D, Hour_HEX:%X, Hour_CHAR:%C",timer_hour,timer_hour,timer_hour);
lcd_putc("\fToff Min: ");
timer_min=get_bcd();
printf("\n\r Min_DEC:%D, Min_HEX:%X, Min_CHAR:%C",timer_min,timer_min,timer_min);
lcd_putc("\fToff Sec: ");
timer_sec=get_bcd();
printf("\n\r Sec_DEC:%D, Sec_HEX:%X, Sec_CHAR:%C",timer_sec,timer_sec,timer_sec);
delay_ms(250);

////////////// HEX to DECIMAL CONVERSION ///////////////////
sec_buf = timer_sec%16; // give the remainder from division of timer_sec
timer_sec = 10*(timer_sec/16) + sec_buf;
min_buf = timer_min%16; // give the remainder from division of timer_sec
timer_min = 10*(timer_min/16) + min_buf;
hour_buf = timer_hour%16; // give the remainder from division of timer_sec
timer_hour = 10*(timer_hour/16) + hour_buf;
day_buf = timer_day%16; // give the remainder from division of timer_sec
timer_day = 10*(timer_day/16) + day_buf;
//////// END ==> HEX to DECIMAL CONVERSION /////////////
}

/////////////// SETUP MENU ///////////////////////////////
void setup_menu()
{
char cmd, cmd1, cmd2;
lcd_putc("\f1:System 2:Timer");
lcd_putc("\n3:CTRL 4:Back ");
do {
cmd=kbd_getc();
} while ((cmd!='1')&&(cmd!='2')&&(cmd!='3')&&(cmd!='4'));

// SYSTEM RESET MENU //
if(cmd=='1')
{
lcd_putc("\fRESET SYSTEM ? ");
lcd_putc("\n *:YES #:NO ");
do
{ cmd1=kbd_getc(); }
while ((cmd1!='*')&&(cmd1!='#'));
if(cmd1=='*') // RESET CLOCK & TIMER if '*'
set_clock();
} // END OF SYSTEM RESET MENU //

// TIMER MENU //
if(cmd=='2')
{
lcd_putc("\f1:Clear TIMER ");
lcd_putc("\n2:Display Record");
do
{ cmd1=kbd_getc(); }
while ((cmd1!='1')&&(cmd1!='2')&&(cmd1!='#'));
if(cmd1=='1') // CLEAR TIMER VALUE
{
sec_off = 0; // Stores the TOTAL ACCUMULATED duration or how long it has been OFF //
min_off = 0;
hour_off = 0;
day_off = 0;
}
if(cmd1=='2') // RESET CLOCK & TIMER if '*'
{
for (i=1;i<11;i++)
{
printf(lcd_putc,"\fRec%02X: %02X/%02X/%02X",i,day_last[i],mth_last[i],year_last[i]);
printf(lcd_putc,"\n %02X:%02X:%02X",hour_last[i],min_last[i],sec_last[i]);
delay_ms(2000);
}
}
} // END OF TIMER MENU //

// CONTROL MENU //
if(cmd=='3')
{
lcd_putc("\fSET AUTO OFF ? ");
lcd_putc("\n *:YES #:NO ");
do
{ cmd1=kbd_getc(); }
while ((cmd1!='*')&&(cmd1!='#'));
if(cmd1=='*') // RESET CLOCK & TIMER if '*'
set_control();
if(cmd1=='#')
{
timer_sec=0;
timer_min=0;
timer_hour=0;
timer_day=0;
}
}
} // END OF SETUP MENU FUNCTION //


void main() {
char cmd;
byte day,mth,year,dow,hour,min,sec;
Just_wokeup = 0;
day_off = 0;
hour_off = 0; // Timer initialize to 00:00:00
min_off = 0;
sec_off = 0;
sec_old = 0;
i = 0;
for (i=1;i<11;i++)
{
sec_last[i] = 0; // LAST OFF TIME initialize to 00:00:00//
min_last[i] = 0;
hour_last[i] = 0;
day_last[i] = 0;
mth_last[i] = 0;
year_last[i] = 0;
}
timer_sec=0; //
timer_min=0; //// Initial value for AUTO TIMER OFF TIME
timer_hour=0; ////
timer_day=14; //
rtc_init();
lcd_init();
kbd_init();

// To switch ON back light of LCD --> PIN PORTA,5 //
output_high(PIN_A5);
////////////////////////////////////////////////////
lcd_putc("\fFLOW MONITORING");
lcd_putc("\n *:Set #:Run ");
do {
cmd=kbd_getc();
} while ((cmd!='*')&&(cmd!='#'));
if(cmd=='*')
set_clock();
// To switch OFF backlight of LCD --> PIN PORTA,5 //
output_low(PIN_A5);
output_low(PIN_E2); // Initial settings ==> PORTE,2 == 0

//########################################## Below is for SLEEP MODE
// long counter;
// sleep_mode=FALSE; // init sleep flag
// ext_int_edge(H_TO_L); // init interrupt triggering for button press
// enable_interrupts(INT_EXT);// turn on interrupts
// enable_interrupts(GLOBAL);
// printf("\n\n");
// counter=0; // reset the counter
//################ Above is for SLEEP MODE

/////////////////////////////////// MAIN ROUTINE ////////////////////////////
while (TRUE) {
cmd=kbd_getc();
if(cmd=='*') // SET REAL TIME CLOCK
{
// To switch ON backlight of LCD --> PIN PORTA,5 //
output_high(PIN_A5);
setup_menu(); // GOTO SETUP MENU
// To switch OFF backlight of LCD --> PIN PORTA,5 //
output_low(PIN_A5);
////////////////////////////////////////////////////
}
lcd_putc('\f');
rtc_get_date( day, mth, year, dow);
rtc_get_time( hour, min, sec );
if(input(PIN_E0) == 1) //## SPECIAL DESIGN ## || if WATER FLOW ie RE0=1 ==> increase timer ### PORTE,0 == FLOW SWITCH
{
if (sec != sec_old) // ONLY increment sec if SEC != SEC_OLD, if same means no need 2 increase timer
{
sec_off = sec_off + 1; //(sec - sec_old);
if (sec_off == 60) // if sec_off == 60 ie 60sec,then clr & increment min_off
{
sec_off = 0;
min_off = min_off + 1;
}
if (min_off == 60) // if min_off == 60 ie 60 min,then clr & increment hour_off
{
min_off = 0;
hour_off = hour_off + 1;
}
if (hour_off == 24) // if hour_off == 24 ie 24 hours,then clr & increment day_off
{
hour_off = 0;
day_off = day_off + 1;
}
previously_OFF = 1; // previously_OFF flag to indicate previously & currently NO WATER
}
output_high(PIN_E2); // Specially chg to ON RELAY (240Vac) when timer RUN
}
else // if no water is detected !!! (NO FLOW)
{
if(previously_OFF == 1) // if HIGH (1), means PREVIOUSLY/BEFORE THAT 'NO WATER FLOW'
{
///////////////////// TO SAVE 10 PREVIOUS DOWN TIME ////////////////////////////////

sec_last[1] = sec; // 1st
min_last[1] = min; // 1st
hour_last[1] = hour; // 1st
day_last[1] = day; // 1st
mth_last[1] = mth; // 1st
year_last[1] = year; // 1st

////////// TO SAVE 10 PREVIOUS DOWN TIME /////////////
previously_OFF = 0; // chg to LOW, to mean that WATER IS FLOWING even for (n-1) cycle
}
// if PORTE,0 == LOW, PORTE,2 == LOW TO SWITCH OFF SOXHLET
output_low(PIN_E2); // Specially chg to OFF RELAY (240Vac) when timer OFF
}

////////////////// END OF CHECKING WATER FLOW AT PIN_RE0 ///////////////


cmd=kbd_getc();
if(cmd=='*') // SET REAL TIME CLOCK
{
// To switch ON backlight of LCD --> PIN PORTA,5 //
output_high(PIN_A5);
////////////////////////////////////////////////////

setup_menu(); // GOTO SETUP MENU

// To switch OFF backlight of LCD --> PIN PORTA,5 //
output_low(PIN_A5);
////////////////////////////////////////////////////
}

if(input(PIN_C5)==0)
{
output_float(PIN_B0);
output_float(PIN_B1);
output_float(PIN_B2);
output_float(PIN_B4);
output_float(PIN_B5);
output_float(PIN_B6);
output_float(PIN_B7);
OUTPUT_FLOAT(PIN_C6);
OUTPUT_FLOAT(PIN_C7);
// PORT_B_PULLUPS(TRUE);
Just_wokeup = 1;
}
else // if RC5==1, then ALLOW DISPLAY
{
delay_ms(150);
if(input(PIN_C5)==1)
{
if(Just_wokeup==1)
{
delay_ms(150);// delay100ms to let LCD power up properly
lcd_init();
kbd_init();
lcd_putc("\f Power Returned ");
lcd_putc("\n ");
printf("\f Main Power Returned");
delay_ms(150);
Just_wokeup = 0;
}
// FOR DISPLAY ON LCD
printf(lcd_putc,"%2X/%2X/%2X %2X%2X%2X",day,mth,year,hour,min,sec);
printf(lcd_putc,"\nt=%02d:%02d:%02d %03dDy",hour_off,min_off,sec_off, day_off);
// FOR DISPLAY USING RS-232
printf("\f Current Date & Time : %2X/%2X/%2X %2X:%2X:%2X",day,mth,year,hour,min,sec);
printf("\n\r Last Stopped Time : %2D/%2D/%2D %2D:%2D:%2D",day_last,mth_last,year_last,hour_last,min_last,sec_last);
printf("\n\r Total Down Time : %02d:%02d:%02d %03d Day",hour_off,min_off,sec_off, day_off);
printf("\n\r Selected ON/OFF time: %02D:%02D:%02D %03D Day",timer_hour,timer_min,timer_sec,timer_day);
// printf("\n\r Selected ON/OFF time: %02D:%02D:%02D %03D Day",timer_day,timer_hour,timer_min,timer_sec);
// printf("\n\r tday:%X, thour:%X, tmin:%X, tsec:%X",timer_day,timer_hour,timer_min,timer_sec);
// } // END of if(input(PIN_C5)==1)
delay_ms(250);
} // end of ELSE on top

cmd=kbd_getc();
if(cmd=='*') // SET REAL TIME CLOCK
{
// To switch ON backlight of LCD --> PIN PORTA,5 //
output_high(PIN_A5);
setup_menu(); // GOTO SETUP MENU
// To switch OFF backlight of LCD --> PIN PORTA,5 //
output_low(PIN_A5);
////////////////////////////////////////////////////
}
cmd=kbd_getc();
if(cmd=='*') // SET REAL TIME CLOCK
{
// To switch ON backlight of LCD --> PIN PORTA,5 //
output_high(PIN_A5);
////////////////////////////////////////////////////
setup_menu(); // GOTO SETUP MENU
// To switch OFF backlight of LCD --> PIN PORTA,5 //
output_low(PIN_A5);
////////////////////////////////////////////////////
}
sec_old = sec; //copy value of sec to sec_old REGISTER; if later in the loop SEC=SEC_OLD,dont increment sec_off

//////////////////////////////////
if(((int)timer_day==day_off)&&((int)timer_hour==hour_off)&&((int)timer_min==min_off)&&((int)timer_sec==sec_off))
{
output_low(PIN_E2);
lcd_putc("\f Timer Complete ");
lcd_putc("\n1:End 2:Resume");
do {
cmd=kbd_getc();
} while ((cmd!='1')&&(cmd!='2'));
if(cmd=='1')
{
day_off = 0;
hour_off = 0;
min_off = 0;
sec_off = 0;
lcd_putc("\fEnd of Operation");
lcd_putc("\n#:Restart timer ");
do {
cmd=kbd_getc();
} while (cmd!='#');
}
}
///For SLEEP MODE
// if(INPUT(PIN_B3)==0) // if sleep flag set
// {
// Just_wokeup = 1; // to make sure LCD & KBD will be INITIALIZED later
// sleep(); // make processor sleep if SLEEP_MODE is TRUE !!!
// }
// if(Just_wokeup == 1)
// {
// lcd_init();
// kbd_init();
// printf(lcd_putc,"%2X/%2X/%2X %2X%2X%2X",day,mth,year,hour,min,sec);
// printf(lcd_putc,"\nt=%02d:%02d:%02d %03dDy",hour_off,min_off,sec_off, day_off);
// Just_wokeup = 0; // Make sure INITIALIZATION is done ONCE ONLY..
// }

} // Do not delete ==> Closing Brace for WHILE Loop
} // DO NOT DELETE ==> CLOSING BRACE for MAIN


So sorry that it's quite crude...The initial sleep function for the MCU doesn't work.So as temporary solution,I need to let the PIC to run continuosly while shutting off all the other peripherals when main power is not available.
ernest



Joined: 11 Feb 2004
Posts: 51

View user's profile Send private message

PostPosted: Thu Oct 07, 2004 1:47 am     Reply with quote

Mark wrote:
Putting the device to sleep does not start the program over. Post you entire program.


#include <16F877A.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <ds1302.c>
#include <lcd.c>
#include <kbd.c>

int day_off, hour_off, min_off, sec_off, sec_old;
int inc_flag; // flag to represent FLOW ( 0 = FLOW OFF, 1 = FLOW ON)
int sec_last[11], min_last[11], hour_last[11], day_last[11],mth_last[11], year_last[11]; // create 10 elements array for storing previous
///////////////////////////////////////// BELOW FUNCTIONS FOR SLEEP !!!
// global flag to send processor into sleep mode
//short sleep_mode;
// external interrupt when button pushed and released
//#INT_EXT
//void ext_isr()
// {
// static short button_pressed=FALSE;
// if(!button_pressed) // if button action and was not pressed
// {
// button_pressed=TRUE; // the button is now down
// sleep_mode=TRUE; // activate sleep
// printf("The processor is now sleeping.\r\n");
// ext_int_edge(L_TO_H); // change so interrupts on release
// }
// else // if button action and was pressed
// {
// button_pressed=FALSE; // the button is now up
// sleep_mode=FALSE; // reset sleep flag
// ext_int_edge(H_TO_L); // change so interrupts on press
// }
// if(!input(PIN_B3)) // keep button action sychronized wth button flag
// button_pressed=TRUE;
// delay_ms(100); // debounce button
// } //end of ext_isr
//////////////////////////////////////// ABOVE FUNCTIONS FOR SLEEP !!!

byte get_bcd() {
char first,second;
do {
first=kbd_getc();
} while ((first<'0') || (first>'9'));
lcd_putc(first);
first-='0';
do {
second=kbd_getc();
} while ((second<'0') || (second>'9'));
lcd_putc(second);

return((first<<4)|(second-'0'));
}

void set_clock(){
byte day,mth,year,dow,hour,min;
lcd_putc("\fYear 20: "); // Year 2000 -- 2099 //
year=get_bcd();
lcd_putc("\fMonth: ");
mth=get_bcd();
lcd_putc("\fDay: ");
day=get_bcd();
lcd_putc("\fWeekday 1-7: ");
dow=get_bcd();
lcd_putc("\fHour: ");
hour=get_bcd();
lcd_putc("\fMin: ");
min=get_bcd();
rtc_set_datetime(day,mth,year,dow,hour,min);
}


//////////// CONTROL SETUP MENU ////////////////////////////////
void set_control(){
int sec_buf, min_buf, hour_buf, day_buf;
lcd_putc("\fToff Day: ");
timer_day=get_bcd();
printf("\n\r Day_DEC:%D, Day_HEX:%X, Day_CHAR:%C",timer_day,timer_day,timer_day);
lcd_putc("\fToff Hour: ");
timer_hour=get_bcd();
printf("\n\r Hour_DEC:%D, Hour_HEX:%X, Hour_CHAR:%C",timer_hour,timer_hour,timer_hour);
lcd_putc("\fToff Min: ");
timer_min=get_bcd();
printf("\n\r Min_DEC:%D, Min_HEX:%X, Min_CHAR:%C",timer_min,timer_min,timer_min);
lcd_putc("\fToff Sec: ");
timer_sec=get_bcd();
printf("\n\r Sec_DEC:%D, Sec_HEX:%X, Sec_CHAR:%C",timer_sec,timer_sec,timer_sec);
delay_ms(250);

////////////// HEX to DECIMAL CONVERSION ///////////////////
sec_buf = timer_sec%16; // give the remainder from division of timer_sec
timer_sec = 10*(timer_sec/16) + sec_buf;
min_buf = timer_min%16; // give the remainder from division of timer_sec
timer_min = 10*(timer_min/16) + min_buf;
hour_buf = timer_hour%16; // give the remainder from division of timer_sec
timer_hour = 10*(timer_hour/16) + hour_buf;
day_buf = timer_day%16; // give the remainder from division of timer_sec
timer_day = 10*(timer_day/16) + day_buf;
//////// END ==> HEX to DECIMAL CONVERSION /////////////
}

/////////////// SETUP MENU ///////////////////////////////
void setup_menu()
{
char cmd, cmd1, cmd2;
lcd_putc("\f1:System 2:Timer");
lcd_putc("\n3:CTRL 4:Back ");
do {
cmd=kbd_getc();
} while ((cmd!='1')&&(cmd!='2')&&(cmd!='3')&&(cmd!='4'));

// SYSTEM RESET MENU //
if(cmd=='1')
{
lcd_putc("\fRESET SYSTEM ? ");
lcd_putc("\n *:YES #:NO ");
do
{ cmd1=kbd_getc(); }
while ((cmd1!='*')&&(cmd1!='#'));
if(cmd1=='*') // RESET CLOCK & TIMER if '*'
set_clock();
} // END OF SYSTEM RESET MENU //

// TIMER MENU //
if(cmd=='2')
{
lcd_putc("\f1:Clear TIMER ");
lcd_putc("\n2:Display Record");
do
{ cmd1=kbd_getc(); }
while ((cmd1!='1')&&(cmd1!='2')&&(cmd1!='#'));
if(cmd1=='1') // CLEAR TIMER VALUE
{
sec_off = 0; // Stores the TOTAL ACCUMULATED duration or how long it has been OFF //
min_off = 0;
hour_off = 0;
day_off = 0;
}
if(cmd1=='2') // RESET CLOCK & TIMER if '*'
{
for (i=1;i<11;i++)
{
printf(lcd_putc,"\fRec%02X: %02X/%02X/%02X",i,day_last[i],mth_last[i],year_last[i]);
printf(lcd_putc,"\n %02X:%02X:%02X",hour_last[i],min_last[i],sec_last[i]);
delay_ms(2000);
}
}
} // END OF TIMER MENU //

// CONTROL MENU //
if(cmd=='3')
{
lcd_putc("\fSET AUTO OFF ? ");
lcd_putc("\n *:YES #:NO ");
do
{ cmd1=kbd_getc(); }
while ((cmd1!='*')&&(cmd1!='#'));
if(cmd1=='*') // RESET CLOCK & TIMER if '*'
set_control();
if(cmd1=='#')
{
timer_sec=0;
timer_min=0;
timer_hour=0;
timer_day=0;
}
}
} // END OF SETUP MENU FUNCTION //


void main() {
char cmd;
byte day,mth,year,dow,hour,min,sec;
Just_wokeup = 0;
day_off = 0;
hour_off = 0; // Timer initialize to 00:00:00
min_off = 0;
sec_off = 0;
sec_old = 0;
i = 0;
for (i=1;i<11;i++)
{
sec_last[i] = 0; // LAST OFF TIME initialize to 00:00:00//
min_last[i] = 0;
hour_last[i] = 0;
day_last[i] = 0;
mth_last[i] = 0;
year_last[i] = 0;
}
timer_sec=0; //
timer_min=0; //// Initial value for AUTO TIMER OFF TIME
timer_hour=0; ////
timer_day=14; //
rtc_init();
lcd_init();
kbd_init();

// To switch ON back light of LCD --> PIN PORTA,5 //
output_high(PIN_A5);
////////////////////////////////////////////////////
lcd_putc("\fFLOW MONITORING");
lcd_putc("\n *:Set #:Run ");
do {
cmd=kbd_getc();
} while ((cmd!='*')&&(cmd!='#'));
if(cmd=='*')
set_clock();
// To switch OFF backlight of LCD --> PIN PORTA,5 //
output_low(PIN_A5);
output_low(PIN_E2); // Initial settings ==> PORTE,2 == 0

//########################################## Below is for SLEEP MODE
// long counter;
// sleep_mode=FALSE; // init sleep flag
// ext_int_edge(H_TO_L); // init interrupt triggering for button press
// enable_interrupts(INT_EXT);// turn on interrupts
// enable_interrupts(GLOBAL);
// printf("\n\n");
// counter=0; // reset the counter
//################ Above is for SLEEP MODE

/////////////////////////////////// MAIN ROUTINE ////////////////////////////
while (TRUE) {
cmd=kbd_getc();
if(cmd=='*') // SET REAL TIME CLOCK
{
// To switch ON backlight of LCD --> PIN PORTA,5 //
output_high(PIN_A5);
setup_menu(); // GOTO SETUP MENU
// To switch OFF backlight of LCD --> PIN PORTA,5 //
output_low(PIN_A5);
////////////////////////////////////////////////////
}
lcd_putc('\f');
rtc_get_date( day, mth, year, dow);
rtc_get_time( hour, min, sec );
if(input(PIN_E0) == 1) //## SPECIAL DESIGN ## || if WATER FLOW ie RE0=1 ==> increase timer ### PORTE,0 == FLOW SWITCH
{
if (sec != sec_old) // ONLY increment sec if SEC != SEC_OLD, if same means no need 2 increase timer
{
sec_off = sec_off + 1; //(sec - sec_old);
if (sec_off == 60) // if sec_off == 60 ie 60sec,then clr & increment min_off
{
sec_off = 0;
min_off = min_off + 1;
}
if (min_off == 60) // if min_off == 60 ie 60 min,then clr & increment hour_off
{
min_off = 0;
hour_off = hour_off + 1;
}
if (hour_off == 24) // if hour_off == 24 ie 24 hours,then clr & increment day_off
{
hour_off = 0;
day_off = day_off + 1;
}
previously_OFF = 1; // previously_OFF flag to indicate previously & currently NO WATER
}
output_high(PIN_E2); // Specially chg to ON RELAY (240Vac) when timer RUN
}
else // if no water is detected !!! (NO FLOW)
{
if(previously_OFF == 1) // if HIGH (1), means PREVIOUSLY/BEFORE THAT 'NO WATER FLOW'
{
///////////////////// TO SAVE 10 PREVIOUS DOWN TIME ////////////////////////////////

sec_last[1] = sec; // 1st
min_last[1] = min; // 1st
hour_last[1] = hour; // 1st
day_last[1] = day; // 1st
mth_last[1] = mth; // 1st
year_last[1] = year; // 1st

////////// TO SAVE 10 PREVIOUS DOWN TIME /////////////
previously_OFF = 0; // chg to LOW, to mean that WATER IS FLOWING even for (n-1) cycle
}
// if PORTE,0 == LOW, PORTE,2 == LOW TO SWITCH OFF SOXHLET
output_low(PIN_E2); // Specially chg to OFF RELAY (240Vac) when timer OFF
}

////////////////// END OF CHECKING WATER FLOW AT PIN_RE0 ///////////////


cmd=kbd_getc();
if(cmd=='*') // SET REAL TIME CLOCK
{
// To switch ON backlight of LCD --> PIN PORTA,5 //
output_high(PIN_A5);
////////////////////////////////////////////////////

setup_menu(); // GOTO SETUP MENU

// To switch OFF backlight of LCD --> PIN PORTA,5 //
output_low(PIN_A5);
////////////////////////////////////////////////////
}

if(input(PIN_C5)==0)
{
output_float(PIN_B0);
output_float(PIN_B1);
output_float(PIN_B2);
output_float(PIN_B4);
output_float(PIN_B5);
output_float(PIN_B6);
output_float(PIN_B7);
OUTPUT_FLOAT(PIN_C6);
OUTPUT_FLOAT(PIN_C7);
// PORT_B_PULLUPS(TRUE);
Just_wokeup = 1;
}
else // if RC5==1, then ALLOW DISPLAY
{
delay_ms(150);
if(input(PIN_C5)==1)
{
if(Just_wokeup==1)
{
delay_ms(150);// delay100ms to let LCD power up properly
lcd_init();
kbd_init();
lcd_putc("\f Power Returned ");
lcd_putc("\n ");
printf("\f Main Power Returned");
delay_ms(150);
Just_wokeup = 0;
}
// FOR DISPLAY ON LCD
printf(lcd_putc,"%2X/%2X/%2X %2X%2X%2X",day,mth,year,hour,min,sec);
printf(lcd_putc,"\nt=%02d:%02d:%02d %03dDy",hour_off,min_off,sec_off, day_off);
// FOR DISPLAY USING RS-232
printf("\f Current Date & Time : %2X/%2X/%2X %2X:%2X:%2X",day,mth,year,hour,min,sec);
printf("\n\r Last Stopped Time : %2D/%2D/%2D %2D:%2D:%2D",day_last,mth_last,year_last,hour_last,min_last,sec_last);
printf("\n\r Total Down Time : %02d:%02d:%02d %03d Day",hour_off,min_off,sec_off, day_off);
printf("\n\r Selected ON/OFF time: %02D:%02D:%02D %03D Day",timer_hour,timer_min,timer_sec,timer_day);
// printf("\n\r Selected ON/OFF time: %02D:%02D:%02D %03D Day",timer_day,timer_hour,timer_min,timer_sec);
// printf("\n\r tday:%X, thour:%X, tmin:%X, tsec:%X",timer_day,timer_hour,timer_min,timer_sec);
// } // END of if(input(PIN_C5)==1)
delay_ms(250);
} // end of ELSE on top

cmd=kbd_getc();
if(cmd=='*') // SET REAL TIME CLOCK
{
// To switch ON backlight of LCD --> PIN PORTA,5 //
output_high(PIN_A5);
setup_menu(); // GOTO SETUP MENU
// To switch OFF backlight of LCD --> PIN PORTA,5 //
output_low(PIN_A5);
////////////////////////////////////////////////////
}
cmd=kbd_getc();
if(cmd=='*') // SET REAL TIME CLOCK
{
// To switch ON backlight of LCD --> PIN PORTA,5 //
output_high(PIN_A5);
////////////////////////////////////////////////////
setup_menu(); // GOTO SETUP MENU
// To switch OFF backlight of LCD --> PIN PORTA,5 //
output_low(PIN_A5);
////////////////////////////////////////////////////
}
sec_old = sec; //copy value of sec to sec_old REGISTER; if later in the loop SEC=SEC_OLD,dont increment sec_off

//////////////////////////////////
if(((int)timer_day==day_off)&&((int)timer_hour==hour_off)&&((int)timer_min==min_off)&&((int)timer_sec==sec_off))
{
output_low(PIN_E2);
lcd_putc("\f Timer Complete ");
lcd_putc("\n1:End 2:Resume");
do {
cmd=kbd_getc();
} while ((cmd!='1')&&(cmd!='2'));
if(cmd=='1')
{
day_off = 0;
hour_off = 0;
min_off = 0;
sec_off = 0;
lcd_putc("\fEnd of Operation");
lcd_putc("\n#:Restart timer ");
do {
cmd=kbd_getc();
} while (cmd!='#');
}
}
///For SLEEP MODE
// if(INPUT(PIN_B3)==0) // if sleep flag set
// {
// Just_wokeup = 1; // to make sure LCD & KBD will be INITIALIZED later
// sleep(); // make processor sleep if SLEEP_MODE is TRUE !!!
// }
// if(Just_wokeup == 1)
// {
// lcd_init();
// kbd_init();
// printf(lcd_putc,"%2X/%2X/%2X %2X%2X%2X",day,mth,year,hour,min,sec);
// printf(lcd_putc,"\nt=%02d:%02d:%02d %03dDy",hour_off,min_off,sec_off, day_off);
// Just_wokeup = 0; // Make sure INITIALIZATION is done ONCE ONLY..
// }

} // Do not delete ==> Closing Brace for WHILE Loop
} // DO NOT DELETE ==> CLOSING BRACE for MAIN


So sorry that it's quite crude...The initial sleep function for the MCU doesn't work.So as temporary solution,I need to let the PIC to run continuosly while shutting off all the other peripherals when main power is not available.
ernest



Joined: 11 Feb 2004
Posts: 51

View user's profile Send private message

PostPosted: Thu Oct 07, 2004 1:49 am     Reply with quote

Mark wrote:
Putting the device to sleep does not start the program over. Post you entire program.


#include <16F877A.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <ds1302.c>
#include <lcd.c>
#include <kbd.c>

int day_off, hour_off, min_off, sec_off, sec_old;
int inc_flag; // flag to represent FLOW ( 0 = FLOW OFF, 1 = FLOW ON)
int sec_last[11], min_last[11], hour_last[11], day_last[11],mth_last[11], year_last[11]; // create 10 elements array for storing previous
///////////////////////////////////////// BELOW FUNCTIONS FOR SLEEP !!!
// global flag to send processor into sleep mode
//short sleep_mode;
// external interrupt when button pushed and released
//#INT_EXT
//void ext_isr()
// {
// static short button_pressed=FALSE;
// if(!button_pressed) // if button action and was not pressed
// {
// button_pressed=TRUE; // the button is now down
// sleep_mode=TRUE; // activate sleep
// printf("The processor is now sleeping.\r\n");
// ext_int_edge(L_TO_H); // change so interrupts on release
// }
// else // if button action and was pressed
// {
// button_pressed=FALSE; // the button is now up
// sleep_mode=FALSE; // reset sleep flag
// ext_int_edge(H_TO_L); // change so interrupts on press
// }
// if(!input(PIN_B3)) // keep button action sychronized wth button flag
// button_pressed=TRUE;
// delay_ms(100); // debounce button
// } //end of ext_isr
//////////////////////////////////////// ABOVE FUNCTIONS FOR SLEEP !!!
byte get_bcd() {
char first,second;
do {
first=kbd_getc();
} while ((first<'0') || (first>'9'));
lcd_putc(first);
first-='0';
do {
second=kbd_getc();
} while ((second<'0') || (second>'9'));
lcd_putc(second);

return((first<<4)|(second-'0'));
}

void set_clock(){
byte day,mth,year,dow,hour,min;
lcd_putc("\fYear 20: "); // Year 2000 -- 2099 //
year=get_bcd();
lcd_putc("\fMonth: ");
mth=get_bcd();
lcd_putc("\fDay: ");
day=get_bcd();
lcd_putc("\fWeekday 1-7: ");
dow=get_bcd();
lcd_putc("\fHour: ");
hour=get_bcd();
lcd_putc("\fMin: ");
min=get_bcd();
rtc_set_datetime(day,mth,year,dow,hour,min);
}

/////////////// SETUP MENU ///////////////////////////////
void setup_menu()
{
char cmd, cmd1, cmd2;
lcd_putc("\f1:System 2:Timer");
lcd_putc("\n3:CTRL 4:Back ");
do {
cmd=kbd_getc();
} while ((cmd!='1')&&(cmd!='2')&&(cmd!='3')&&(cmd!='4'));

// SYSTEM RESET MENU //
if(cmd=='1')
{
lcd_putc("\fRESET SYSTEM ? ");
lcd_putc("\n *:YES #:NO ");
do
{ cmd1=kbd_getc(); }
while ((cmd1!='*')&&(cmd1!='#'));
if(cmd1=='*') // RESET CLOCK & TIMER if '*'
set_clock();
} // END OF SYSTEM RESET MENU //

// CONTROL MENU //
if(cmd=='3')
{
lcd_putc("\fSET AUTO OFF ? ");
lcd_putc("\n *:YES #:NO ");
do
{ cmd1=kbd_getc(); }
while ((cmd1!='*')&&(cmd1!='#'));
if(cmd1=='*') // RESET CLOCK & TIMER if '*'
set_control();
if(cmd1=='#')
{
timer_sec=0;
timer_min=0;
timer_hour=0;
timer_day=0;
}
}
} // END OF SETUP MENU FUNCTION //

void main() {
char cmd;
byte day,mth,year,dow,hour,min,sec;
Just_wokeup = 0;
day_off = 0;
hour_off = 0; // Timer initialize to 00:00:00
min_off = 0;
sec_off = 0;
sec_old = 0;
i = 0;
for (i=1;i<11;i++)
{ sec_last[i] = 0; // LAST OFF TIME initialize to 00:00:00//
min_last[i] = 0;
hour_last[i] = 0;
day_last[i] = 0;
mth_last[i] = 0;
year_last[i] = 0; }
timer_sec=0; //
timer_min=0; //// Initial value for AUTO TIMER OFF TIME
timer_hour=0; ////
timer_day=14; //
rtc_init();
lcd_init();
kbd_init();

// To switch ON back light of LCD --> PIN PORTA,5 //
output_high(PIN_A5);
////////////////////////////////////////////////////
lcd_putc("\fFLOW MONITORING");
lcd_putc("\n *:Set #:Run ");
do {
cmd=kbd_getc();
} while ((cmd!='*')&&(cmd!='#'));
if(cmd=='*')
set_clock();
// To switch OFF backlight of LCD --> PIN PORTA,5 //
output_low(PIN_A5);
output_low(PIN_E2); // Initial settings ==> PORTE,2 == 0

//########################################## Below is for SLEEP MODE
// long counter;
// sleep_mode=FALSE; // init sleep flag
// ext_int_edge(H_TO_L); // init interrupt triggering for button press
// enable_interrupts(INT_EXT);// turn on interrupts
// enable_interrupts(GLOBAL);
// printf("\n\n");
// counter=0; // reset the counter
//################ Above is for SLEEP MODE

/////////////////////////////////// MAIN ROUTINE ////////////////////////////
while (TRUE) {
cmd=kbd_getc();
if(cmd=='*') // SET REAL TIME CLOCK
{
// To switch ON backlight of LCD --> PIN PORTA,5 //
output_high(PIN_A5);
setup_menu(); // GOTO SETUP MENU
// To switch OFF backlight of LCD --> PIN PORTA,5 //
output_low(PIN_A5);
////////////////////////////////////////////////////
}
lcd_putc('\f');
rtc_get_date( day, mth, year, dow);
rtc_get_time( hour, min, sec );
if(input(PIN_E0) == 1) //## SPECIAL DESIGN ## || if WATER FLOW ie RE0=1 ==> increase timer ### PORTE,0 == FLOW SWITCH
{
if (sec != sec_old) // ONLY increment sec if SEC != SEC_OLD, if same means no need 2 increase timer
{
sec_off = sec_off + 1; //(sec - sec_old);
if (sec_off == 60) // if sec_off == 60 ie 60sec,then clr & increment min_off
{
sec_off = 0;
min_off = min_off + 1;
}
if (min_off == 60) // if min_off == 60 ie 60 min,then clr & increment hour_off
{
min_off = 0;
hour_off = hour_off + 1;
}
if (hour_off == 24) // if hour_off == 24 ie 24 hours,then clr & increment day_off
{
hour_off = 0;
day_off = day_off + 1;
}
previously_OFF = 1; // previously_OFF flag to indicate previously & currently NO WATER
}
output_high(PIN_E2); // Specially chg to ON RELAY (240Vac) when timer RUN
}
else // if no water is detected !!! (NO FLOW)
{
if(previously_OFF == 1) // if HIGH (1), means PREVIOUSLY/BEFORE THAT 'NO WATER FLOW'
{
///////////////////// TO SAVE 10 PREVIOUS DOWN TIME ////////////////////////////////

sec_last[1] = sec; // 1st
min_last[1] = min; // 1st
hour_last[1] = hour; // 1st
day_last[1] = day; // 1st
mth_last[1] = mth; // 1st
year_last[1] = year; // 1st

////////// TO SAVE 10 PREVIOUS DOWN TIME /////////////
previously_OFF = 0; // chg to LOW, to mean that WATER IS FLOWING even for (n-1) cycle
}
// if PORTE,0 == LOW, PORTE,2 == LOW TO SWITCH OFF SOXHLET
output_low(PIN_E2); // Specially chg to OFF RELAY (240Vac) when timer OFF
}

////////////////// END OF CHECKING WATER FLOW AT PIN_RE0 ///////////////


cmd=kbd_getc();
if(cmd=='*') // SET REAL TIME CLOCK
{
// To switch ON backlight of LCD --> PIN PORTA,5 //
output_high(PIN_A5);
////////////////////////////////////////////////////

setup_menu(); // GOTO SETUP MENU

// To switch OFF backlight of LCD --> PIN PORTA,5 //
output_low(PIN_A5);
////////////////////////////////////////////////////
}

if(input(PIN_C5)==0)
{
output_float(PIN_B0);
output_float(PIN_B1);
output_float(PIN_B2);
output_float(PIN_B4);
output_float(PIN_B5);
output_float(PIN_B6);
output_float(PIN_B7);
OUTPUT_FLOAT(PIN_C6);
OUTPUT_FLOAT(PIN_C7);
// PORT_B_PULLUPS(TRUE);
Just_wokeup = 1;
}
else // if RC5==1, then ALLOW DISPLAY
{
delay_ms(150);
if(input(PIN_C5)==1)
{
if(Just_wokeup==1)
{
delay_ms(150);// delay100ms to let LCD power up properly
lcd_init();
kbd_init();
lcd_putc("\f Power Returned ");
lcd_putc("\n ");
printf("\f Main Power Returned");
delay_ms(150);
Just_wokeup = 0;
}
// FOR DISPLAY ON LCD
printf(lcd_putc,"%2X/%2X/%2X %2X%2X%2X",day,mth,year,hour,min,sec);
printf(lcd_putc,"\nt=%02d:%02d:%02d %03dDy",hour_off,min_off,sec_off, day_off);
// FOR DISPLAY USING RS-232
printf("\f Current Date & Time : %2X/%2X/%2X %2X:%2X:%2X",day,mth,year,hour,min,sec);
printf("\n\r Last Stopped Time : %2D/%2D/%2D %2D:%2D:%2D",day_last,mth_last,year_last,hour_last,min_last,sec_last);
printf("\n\r Total Down Time : %02d:%02d:%02d %03d Day",hour_off,min_off,sec_off, day_off);
printf("\n\r Selected ON/OFF time: %02D:%02D:%02D %03D Day",timer_hour,timer_min,timer_sec,timer_day);
// printf("\n\r Selected ON/OFF time: %02D:%02D:%02D %03D Day",timer_day,timer_hour,timer_min,timer_sec);
// printf("\n\r tday:%X, thour:%X, tmin:%X, tsec:%X",timer_day,timer_hour,timer_min,timer_sec);
// } // END of if(input(PIN_C5)==1)
delay_ms(250);
} // end of ELSE on top

cmd=kbd_getc();
if(cmd=='*') // SET REAL TIME CLOCK
{
// To switch ON backlight of LCD --> PIN PORTA,5 //
output_high(PIN_A5);
setup_menu(); // GOTO SETUP MENU
// To switch OFF backlight of LCD --> PIN PORTA,5 //
output_low(PIN_A5);
////////////////////////////////////////////////////
}
cmd=kbd_getc();
if(cmd=='*') // SET REAL TIME CLOCK
{
// To switch ON backlight of LCD --> PIN PORTA,5 //
output_high(PIN_A5);
////////////////////////////////////////////////////
setup_menu(); // GOTO SETUP MENU
// To switch OFF backlight of LCD --> PIN PORTA,5 //
output_low(PIN_A5);
////////////////////////////////////////////////////
}
sec_old = sec; //copy value of sec to sec_old REGISTER; if later in the loop SEC=SEC_OLD,dont increment sec_off

//////////////////////////////////
if(((int)timer_day==day_off)&&((int)timer_hour==hour_off)&&((int)timer_min==min_off)&&((int)timer_sec==sec_off))
{
output_low(PIN_E2);
lcd_putc("\f Timer Complete ");
lcd_putc("\n1:End 2:Resume");
do {
cmd=kbd_getc();
} while ((cmd!='1')&&(cmd!='2'));
if(cmd=='1')
{
day_off = 0;
hour_off = 0;
min_off = 0;
sec_off = 0;
lcd_putc("\fEnd of Operation");
lcd_putc("\n#:Restart timer ");
do {
cmd=kbd_getc();
} while (cmd!='#');
}
}
///For SLEEP MODE
// if(INPUT(PIN_B3)==0) // if sleep flag set
// {
// Just_wokeup = 1; // to make sure LCD & KBD will be INITIALIZED later
// sleep(); // make processor sleep if SLEEP_MODE is TRUE !!!
// }
// if(Just_wokeup == 1)
// {
// lcd_init();
// kbd_init();
// printf(lcd_putc,"%2X/%2X/%2X %2X%2X%2X",day,mth,year,hour,min,sec);
// printf(lcd_putc,"\nt=%02d:%02d:%02d %03dDy",hour_off,min_off,sec_off, day_off);
// Just_wokeup = 0; // Make sure INITIALIZATION is done ONCE ONLY..
// }

} // Do not delete ==> Closing Brace for WHILE Loop
} // DO NOT DELETE ==> CLOSING BRACE for MAIN


So sorry that it's quite crude...The initial sleep function for the MCU doesn't work.So as temporary solution,I need to let the PIC to run continuosly while shutting off all the other peripherals when main power is not available.
ernest



Joined: 11 Feb 2004
Posts: 51

View user's profile Send private message

PostPosted: Thu Oct 07, 2004 1:50 am     Reply with quote

Mark wrote:
Putting the device to sleep does not start the program over. Post you entire program.


#include <16F877A.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <ds1302.c>
#include <lcd.c>
#include <kbd.c>

int day_off, hour_off, min_off, sec_off, sec_old;
int inc_flag; // flag to represent FLOW ( 0 = FLOW OFF, 1 = FLOW ON)
int sec_last[11], min_last[11], hour_last[11], day_last[11],mth_last[11], year_last[11]; // create 10 elements array for storing previous
///////////////////////////////////////// BELOW FUNCTIONS FOR SLEEP !!!
// global flag to send processor into sleep mode
//short sleep_mode;
// external interrupt when button pushed and released
//#INT_EXT
//void ext_isr()
// {
// static short button_pressed=FALSE;
// if(!button_pressed) // if button action and was not pressed
// {
// button_pressed=TRUE; // the button is now down
// sleep_mode=TRUE; // activate sleep
// printf("The processor is now sleeping.\r\n");
// ext_int_edge(L_TO_H); // change so interrupts on release
// }
// else // if button action and was pressed
// {
// button_pressed=FALSE; // the button is now up
// sleep_mode=FALSE; // reset sleep flag
// ext_int_edge(H_TO_L); // change so interrupts on press
// }
// if(!input(PIN_B3)) // keep button action sychronized wth button flag
// button_pressed=TRUE;
// delay_ms(100); // debounce button
// } //end of ext_isr
//////////////////////////////////////// ABOVE FUNCTIONS FOR SLEEP !!!
byte get_bcd() {
char first,second;
do {
first=kbd_getc();
} while ((first<'0') || (first>'9'));
lcd_putc(first);
first-='0';
do {
second=kbd_getc();
} while ((second<'0') || (second>'9'));
lcd_putc(second);

return((first<<4)|(second-'0'));
}

void set_clock(){
byte day,mth,year,dow,hour,min;
lcd_putc("\fYear 20: "); // Year 2000 -- 2099 //
year=get_bcd();
lcd_putc("\fMonth: ");
mth=get_bcd();
lcd_putc("\fDay: ");
day=get_bcd();
lcd_putc("\fWeekday 1-7: ");
dow=get_bcd();
lcd_putc("\fHour: ");
hour=get_bcd();
lcd_putc("\fMin: ");
min=get_bcd();
rtc_set_datetime(day,mth,year,dow,hour,min);
}

/////////////// SETUP MENU ///////////////////////////////
void setup_menu()
{
char cmd, cmd1, cmd2;
lcd_putc("\f1:System 2:Timer");
lcd_putc("\n3:CTRL 4:Back ");
do {
cmd=kbd_getc();
} while ((cmd!='1')&&(cmd!='2')&&(cmd!='3')&&(cmd!='4'));

// SYSTEM RESET MENU //
if(cmd=='1')
{
lcd_putc("\fRESET SYSTEM ? ");
lcd_putc("\n *:YES #:NO ");
do
{ cmd1=kbd_getc(); }
while ((cmd1!='*')&&(cmd1!='#'));
if(cmd1=='*') // RESET CLOCK & TIMER if '*'
set_clock();
} // END OF SYSTEM RESET MENU //

// CONTROL MENU //
if(cmd=='3')
{
lcd_putc("\fSET AUTO OFF ? ");
lcd_putc("\n *:YES #:NO ");
do
{ cmd1=kbd_getc(); }
while ((cmd1!='*')&&(cmd1!='#'));
if(cmd1=='*') // RESET CLOCK & TIMER if '*'
set_control();
if(cmd1=='#')
{
timer_sec=0;
timer_min=0;
timer_hour=0;
timer_day=0;
}
}
} // END OF SETUP MENU FUNCTION //

void main() {
char cmd;
byte day,mth,year,dow,hour,min,sec;
Just_wokeup = 0;
day_off = 0;
hour_off = 0; // Timer initialize to 00:00:00
min_off = 0;
sec_off = 0;
sec_old = 0;
i = 0;
for (i=1;i<11;i++)
{ sec_last[i] = 0; // LAST OFF TIME initialize to 00:00:00//
min_last[i] = 0;
hour_last[i] = 0;
day_last[i] = 0;
mth_last[i] = 0;
year_last[i] = 0; }
timer_sec=0; //
timer_min=0; //// Initial value for AUTO TIMER OFF TIME
timer_hour=0; ////
timer_day=14; //
rtc_init();
lcd_init();
kbd_init();

// To switch ON back light of LCD --> PIN PORTA,5 //
output_high(PIN_A5);
////////////////////////////////////////////////////
lcd_putc("\fFLOW MONITORING");
lcd_putc("\n *:Set #:Run ");
do {
cmd=kbd_getc();
} while ((cmd!='*')&&(cmd!='#'));
if(cmd=='*')
set_clock();
// To switch OFF backlight of LCD --> PIN PORTA,5 //
output_low(PIN_A5);
output_low(PIN_E2); // Initial settings ==> PORTE,2 == 0

//########################################## Below is for SLEEP MODE
// long counter;
// sleep_mode=FALSE; // init sleep flag
// ext_int_edge(H_TO_L); // init interrupt triggering for button press
// enable_interrupts(INT_EXT);// turn on interrupts
// enable_interrupts(GLOBAL);
// printf("\n\n");
// counter=0; // reset the counter
//################ Above is for SLEEP MODE

/////////////////////////////////// MAIN ROUTINE ////////////////////////////
while (TRUE) {
cmd=kbd_getc();
if(cmd=='*') // SET REAL TIME CLOCK
{
// To switch ON backlight of LCD --> PIN PORTA,5 //
output_high(PIN_A5);
setup_menu(); // GOTO SETUP MENU
// To switch OFF backlight of LCD --> PIN PORTA,5 //
output_low(PIN_A5);
////////////////////////////////////////////////////
}
lcd_putc('\f');
rtc_get_date( day, mth, year, dow);
rtc_get_time( hour, min, sec );
if(input(PIN_E0) == 1) //## SPECIAL DESIGN ## || if WATER FLOW ie RE0=1 ==> increase timer ### PORTE,0 == FLOW SWITCH
{
if (sec != sec_old) // ONLY increment sec if SEC != SEC_OLD, if same means no need 2 increase timer
{
sec_off = sec_off + 1; //(sec - sec_old);
if (sec_off == 60) // if sec_off == 60 ie 60sec,then clr & increment min_off
{
sec_off = 0;
min_off = min_off + 1;
}
if (min_off == 60) // if min_off == 60 ie 60 min,then clr & increment hour_off
{
min_off = 0;
hour_off = hour_off + 1;
}
if (hour_off == 24) // if hour_off == 24 ie 24 hours,then clr & increment day_off
{
hour_off = 0;
day_off = day_off + 1;
}
previously_OFF = 1; // previously_OFF flag to indicate previously & currently NO WATER
}
output_high(PIN_E2); // Specially chg to ON RELAY (240Vac) when timer RUN
}
else // if no water is detected !!! (NO FLOW)
{
if(previously_OFF == 1) // if HIGH (1), means PREVIOUSLY/BEFORE THAT 'NO WATER FLOW'
{
///////////////////// TO SAVE 10 PREVIOUS DOWN TIME ////////////////////////////////

sec_last[1] = sec; // 1st
min_last[1] = min; // 1st
hour_last[1] = hour; // 1st
day_last[1] = day; // 1st
mth_last[1] = mth; // 1st
year_last[1] = year; // 1st

////////// TO SAVE 10 PREVIOUS DOWN TIME /////////////
previously_OFF = 0; // chg to LOW, to mean that WATER IS FLOWING even for (n-1) cycle
}
// if PORTE,0 == LOW, PORTE,2 == LOW TO SWITCH OFF SOXHLET
output_low(PIN_E2); // Specially chg to OFF RELAY (240Vac) when timer OFF
}

////////////////// END OF CHECKING WATER FLOW AT PIN_RE0 ///////////////


cmd=kbd_getc();
if(cmd=='*') // SET REAL TIME CLOCK
{
// To switch ON backlight of LCD --> PIN PORTA,5 //
output_high(PIN_A5);
////////////////////////////////////////////////////

setup_menu(); // GOTO SETUP MENU

// To switch OFF backlight of LCD --> PIN PORTA,5 //
output_low(PIN_A5);
////////////////////////////////////////////////////
}

if(input(PIN_C5)==0)
{
output_float(PIN_B0);
output_float(PIN_B1);
output_float(PIN_B2);
output_float(PIN_B4);
output_float(PIN_B5);
output_float(PIN_B6);
output_float(PIN_B7);
OUTPUT_FLOAT(PIN_C6);
OUTPUT_FLOAT(PIN_C7);
// PORT_B_PULLUPS(TRUE);
Just_wokeup = 1;
}
else // if RC5==1, then ALLOW DISPLAY
{
delay_ms(150);
if(input(PIN_C5)==1)
{
if(Just_wokeup==1)
{
delay_ms(150);// delay100ms to let LCD power up properly
lcd_init();
kbd_init();
lcd_putc("\f Power Returned ");
lcd_putc("\n ");
printf("\f Main Power Returned");
delay_ms(150);
Just_wokeup = 0;
}
// FOR DISPLAY ON LCD
printf(lcd_putc,"%2X/%2X/%2X %2X%2X%2X",day,mth,year,hour,min,sec);
printf(lcd_putc,"\nt=%02d:%02d:%02d %03dDy",hour_off,min_off,sec_off, day_off);
// FOR DISPLAY USING RS-232
printf("\f Current Date & Time : %2X/%2X/%2X %2X:%2X:%2X",day,mth,year,hour,min,sec);
printf("\n\r Last Stopped Time : %2D/%2D/%2D %2D:%2D:%2D",day_last,mth_last,year_last,hour_last,min_last,sec_last);
printf("\n\r Total Down Time : %02d:%02d:%02d %03d Day",hour_off,min_off,sec_off, day_off);
printf("\n\r Selected ON/OFF time: %02D:%02D:%02D %03D Day",timer_hour,timer_min,timer_sec,timer_day);
// printf("\n\r Selected ON/OFF time: %02D:%02D:%02D %03D Day",timer_day,timer_hour,timer_min,timer_sec);
// printf("\n\r tday:%X, thour:%X, tmin:%X, tsec:%X",timer_day,timer_hour,timer_min,timer_sec);
// } // END of if(input(PIN_C5)==1)
delay_ms(250);
} // end of ELSE on top

cmd=kbd_getc();
if(cmd=='*') // SET REAL TIME CLOCK
{
// To switch ON backlight of LCD --> PIN PORTA,5 //
output_high(PIN_A5);
setup_menu(); // GOTO SETUP MENU
// To switch OFF backlight of LCD --> PIN PORTA,5 //
output_low(PIN_A5);
////////////////////////////////////////////////////
}
cmd=kbd_getc();
if(cmd=='*') // SET REAL TIME CLOCK
{
// To switch ON backlight of LCD --> PIN PORTA,5 //
output_high(PIN_A5);
////////////////////////////////////////////////////
setup_menu(); // GOTO SETUP MENU
// To switch OFF backlight of LCD --> PIN PORTA,5 //
output_low(PIN_A5);
////////////////////////////////////////////////////
}
sec_old = sec; //copy value of sec to sec_old REGISTER; if later in the loop SEC=SEC_OLD,dont increment sec_off

//////////////////////////////////
if(((int)timer_day==day_off)&&((int)timer_hour==hour_off)&&((int)timer_min==min_off)&&((int)timer_sec==sec_off))
{
output_low(PIN_E2);
lcd_putc("\f Timer Complete ");
lcd_putc("\n1:End 2:Resume");
do {
cmd=kbd_getc();
} while ((cmd!='1')&&(cmd!='2'));
if(cmd=='1')
{
day_off = 0;
hour_off = 0;
min_off = 0;
sec_off = 0;
lcd_putc("\fEnd of Operation");
lcd_putc("\n#:Restart timer ");
do {
cmd=kbd_getc();
} while (cmd!='#');
}
}
///For SLEEP MODE
// if(INPUT(PIN_B3)==0) // if sleep flag set
// {
// Just_wokeup = 1; // to make sure LCD & KBD will be INITIALIZED later
// sleep(); // make processor sleep if SLEEP_MODE is TRUE !!!
// }
} // Do not delete ==> Closing Brace for WHILE Loop
} // DO NOT DELETE ==> CLOSING BRACE for MAIN


So sorry that it's quite crude...The initial sleep function for the MCU doesn't work.So as temporary solution,I need to let the PIC to run continuosly while shutting off all the other peripherals when main power is not available.
ernest



Joined: 11 Feb 2004
Posts: 51

View user's profile Send private message

PostPosted: Thu Oct 07, 2004 1:52 am     Reply with quote

Mark wrote:
Putting the device to sleep does not start the program over. Post you entire program.


#include <16F877A.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <ds1302.c>
#include <lcd.c>
#include <kbd.c>

int day_off, hour_off, min_off, sec_off, sec_old;
int inc_flag; // flag to represent FLOW ( 0 = FLOW OFF, 1 = FLOW ON)
int sec_last[11], min_last[11], hour_last[11], day_last[11],mth_last[11], year_last[11]; // create 10 elements array for storing previous
///////////////////////////////////////// BELOW FUNCTIONS FOR SLEEP !!!
// global flag to send processor into sleep mode
//short sleep_mode;
// external interrupt when button pushed and released
//#INT_EXT
//void ext_isr()
// {
// static short button_pressed=FALSE;
// if(!button_pressed) // if button action and was not pressed
// {
// button_pressed=TRUE; // the button is now down
// sleep_mode=TRUE; // activate sleep
// printf("The processor is now sleeping.\r\n");
// ext_int_edge(L_TO_H); // change so interrupts on release
// }
// else // if button action and was pressed
// {
// button_pressed=FALSE; // the button is now up
// sleep_mode=FALSE; // reset sleep flag
// ext_int_edge(H_TO_L); // change so interrupts on press
// }
// if(!input(PIN_B3)) // keep button action sychronized wth button flag
// button_pressed=TRUE;
// delay_ms(100); // debounce button
// } //end of ext_isr
//////////////////////////////////////// ABOVE FUNCTIONS FOR SLEEP !!!
byte get_bcd() {
char first,second;
do {
first=kbd_getc();
} while ((first<'0') || (first>'9'));
lcd_putc(first);
first-='0';
do {
second=kbd_getc();
} while ((second<'0') || (second>'9'));
lcd_putc(second);

return((first<<4)|(second-'0'));
}

void set_clock(){
byte day,mth,year,dow,hour,min;
lcd_putc("\fYear 20: "); // Year 2000 -- 2099 //
year=get_bcd();
lcd_putc("\fMonth: ");
mth=get_bcd();
lcd_putc("\fDay: ");
day=get_bcd();
lcd_putc("\fWeekday 1-7: ");
dow=get_bcd();
lcd_putc("\fHour: ");
hour=get_bcd();
lcd_putc("\fMin: ");
min=get_bcd();
rtc_set_datetime(day,mth,year,dow,hour,min);
}

/////////////// SETUP MENU ///////////////////////////////
void setup_menu()
{
char cmd, cmd1, cmd2;
lcd_putc("\f1:System 2:Timer");
lcd_putc("\n3:CTRL 4:Back ");
do {
cmd=kbd_getc();
} while ((cmd!='1')&&(cmd!='2')&&(cmd!='3')&&(cmd!='4'));

// SYSTEM RESET MENU //
if(cmd=='1')
{
lcd_putc("\fRESET SYSTEM ? ");
lcd_putc("\n *:YES #:NO ");
do
{ cmd1=kbd_getc(); }
while ((cmd1!='*')&&(cmd1!='#'));
if(cmd1=='*') // RESET CLOCK & TIMER if '*'
set_clock();
} // END OF SYSTEM RESET MENU //

// CONTROL MENU //
if(cmd=='3')
{
lcd_putc("\fSET AUTO OFF ? ");
lcd_putc("\n *:YES #:NO ");
do
{ cmd1=kbd_getc(); }
while ((cmd1!='*')&&(cmd1!='#'));
if(cmd1=='*') // RESET CLOCK & TIMER if '*'
set_control();
if(cmd1=='#')
{
timer_sec=0;
timer_min=0;
timer_hour=0;
timer_day=0;
}
}
} // END OF SETUP MENU FUNCTION //

void main() {
char cmd;
byte day,mth,year,dow,hour,min,sec;
Just_wokeup = 0;
day_off = 0;
hour_off = 0; // Timer initialize to 00:00:00
min_off = 0;
sec_off = 0;
sec_old = 0;
i = 0;
for (i=1;i<11;i++)
{ sec_last[i] = 0; // LAST OFF TIME initialize to 00:00:00//
min_last[i] = 0;
hour_last[i] = 0;
day_last[i] = 0;
mth_last[i] = 0;
year_last[i] = 0; }
timer_sec=0; //
timer_min=0; //// Initial value for AUTO TIMER OFF TIME
timer_hour=0; ////
timer_day=14; //
rtc_init();
lcd_init();
kbd_init();

// To switch ON back light of LCD --> PIN PORTA,5 //
output_high(PIN_A5);
////////////////////////////////////////////////////
lcd_putc("\fFLOW MONITORING");
lcd_putc("\n *:Set #:Run ");
do {
cmd=kbd_getc();
} while ((cmd!='*')&&(cmd!='#'));
if(cmd=='*')
set_clock();
// To switch OFF backlight of LCD --> PIN PORTA,5 //
output_low(PIN_A5);
output_low(PIN_E2); // Initial settings ==> PORTE,2 == 0

//########################################## Below is for SLEEP MODE
// long counter;
// sleep_mode=FALSE; // init sleep flag
// ext_int_edge(H_TO_L); // init interrupt triggering for button press
// enable_interrupts(INT_EXT);// turn on interrupts
// enable_interrupts(GLOBAL);
// printf("\n\n");
// counter=0; // reset the counter
//################ Above is for SLEEP MODE

/////////////////////////////////// MAIN ROUTINE ////////////////////////////
while (TRUE) {
cmd=kbd_getc();
if(cmd=='*') // SET REAL TIME CLOCK
{
// To switch ON backlight of LCD --> PIN PORTA,5 //
output_high(PIN_A5);
setup_menu(); // GOTO SETUP MENU
// To switch OFF backlight of LCD --> PIN PORTA,5 //
output_low(PIN_A5);
////////////////////////////////////////////////////
}
lcd_putc('\f');
rtc_get_date( day, mth, year, dow);
rtc_get_time( hour, min, sec );
if(input(PIN_E0) == 1) //## SPECIAL DESIGN ## || if WATER FLOW ie RE0=1 ==> increase timer ### PORTE,0 == FLOW SWITCH
{
if (sec != sec_old) // ONLY increment sec if SEC != SEC_OLD, if same means no need 2 increase timer
{
sec_off = sec_off + 1; //(sec - sec_old);
if (sec_off == 60) // if sec_off == 60 ie 60sec,then clr & increment min_off
{
sec_off = 0;
min_off = min_off + 1;
}
if (min_off == 60) // if min_off == 60 ie 60 min,then clr & increment hour_off
{
min_off = 0;
hour_off = hour_off + 1;
}
if (hour_off == 24) // if hour_off == 24 ie 24 hours,then clr & increment day_off
{
hour_off = 0;
day_off = day_off + 1;
}
previously_OFF = 1; // previously_OFF flag to indicate previously & currently NO WATER
}
output_high(PIN_E2); // Specially chg to ON RELAY (240Vac) when timer RUN
}
else // if no water is detected !!! (NO FLOW)
{
if(previously_OFF == 1) // if HIGH (1), means PREVIOUSLY/BEFORE THAT 'NO WATER FLOW'
{
///////////////////// TO SAVE 10 PREVIOUS DOWN TIME ////////////////////////////////

sec_last[1] = sec; // 1st
min_last[1] = min; // 1st
hour_last[1] = hour; // 1st
day_last[1] = day; // 1st
mth_last[1] = mth; // 1st
year_last[1] = year; // 1st

////////// TO SAVE 10 PREVIOUS DOWN TIME /////////////
previously_OFF = 0; // chg to LOW, to mean that WATER IS FLOWING even for (n-1) cycle
}
// if PORTE,0 == LOW, PORTE,2 == LOW TO SWITCH OFF SOXHLET
output_low(PIN_E2); // Specially chg to OFF RELAY (240Vac) when timer OFF
}

////////////////// END OF CHECKING WATER FLOW AT PIN_RE0 ///////////////


cmd=kbd_getc();
if(cmd=='*') // SET REAL TIME CLOCK
{
// To switch ON backlight of LCD --> PIN PORTA,5 //
output_high(PIN_A5);
////////////////////////////////////////////////////

setup_menu(); // GOTO SETUP MENU

// To switch OFF backlight of LCD --> PIN PORTA,5 //
output_low(PIN_A5);
////////////////////////////////////////////////////
}

if(input(PIN_C5)==0)
{
output_float(PIN_B0);
output_float(PIN_B1);
output_float(PIN_B2);
output_float(PIN_B4);
output_float(PIN_B5);
output_float(PIN_B6);
output_float(PIN_B7);
OUTPUT_FLOAT(PIN_C6);
OUTPUT_FLOAT(PIN_C7);
// PORT_B_PULLUPS(TRUE);
Just_wokeup = 1;
}
else // if RC5==1, then ALLOW DISPLAY
{
delay_ms(150);
if(input(PIN_C5)==1)
{
if(Just_wokeup==1)
{
delay_ms(150);// delay100ms to let LCD power up properly
lcd_init();
kbd_init();
lcd_putc("\f Power Returned ");
lcd_putc("\n ");
printf("\f Main Power Returned");
delay_ms(150);
Just_wokeup = 0;
}
// FOR DISPLAY ON LCD
printf(lcd_putc,"%2X/%2X/%2X %2X%2X%2X",day,mth,year,hour,min,sec);
printf(lcd_putc,"\nt=%02d:%02d:%02d %03dDy",hour_off,min_off,sec_off, day_off);
// FOR DISPLAY USING RS-232
printf("\f Current Date & Time : %2X/%2X/%2X %2X:%2X:%2X",day,mth,year,hour,min,sec);
// } // END of if(input(PIN_C5)==1)
delay_ms(250);
} // end of ELSE on top

cmd=kbd_getc();
if(cmd=='*') // SET REAL TIME CLOCK
{
// To switch ON backlight of LCD --> PIN PORTA,5 //
output_high(PIN_A5);
setup_menu(); // GOTO SETUP MENU
// To switch OFF backlight of LCD --> PIN PORTA,5 //
output_low(PIN_A5);
////////////////////////////////////////////////////
}
cmd=kbd_getc();
if(cmd=='*') // SET REAL TIME CLOCK
{
// To switch ON backlight of LCD --> PIN PORTA,5 //
output_high(PIN_A5);
////////////////////////////////////////////////////
setup_menu(); // GOTO SETUP MENU
// To switch OFF backlight of LCD --> PIN PORTA,5 //
output_low(PIN_A5);
////////////////////////////////////////////////////
}
sec_old = sec; //copy value of sec to sec_old REGISTER; if later in the loop SEC=SEC_OLD,dont increment sec_off

//////////////////////////////////
if(((int)timer_day==day_off)&&((int)timer_hour==hour_off)&&((int)timer_min==min_off)&&((int)timer_sec==sec_off))
{
output_low(PIN_E2);
lcd_putc("\f Timer Complete ");
lcd_putc("\n1:End 2:Resume");
do {
cmd=kbd_getc();
} while ((cmd!='1')&&(cmd!='2'));
if(cmd=='1')
{
day_off = 0;
hour_off = 0;
min_off = 0;
sec_off = 0;
lcd_putc("\fEnd of Operation");
lcd_putc("\n#:Restart timer ");
do {
cmd=kbd_getc();
} while (cmd!='#');
}
}
///For SLEEP MODE
// if(INPUT(PIN_B3)==0) // if sleep flag set
// {
// Just_wokeup = 1; // to make sure LCD & KBD will be INITIALIZED later
// sleep(); // make processor sleep if SLEEP_MODE is TRUE !!!
// }
} // Do not delete ==> Closing Brace for WHILE Loop
} // DO NOT DELETE ==> CLOSING BRACE for MAIN


So sorry that it's quite crude...The initial sleep function for the MCU doesn't work.So as temporary solution,I need to let the PIC to run continuosly while shutting off all the other peripherals when main power is not available.
ernest



Joined: 11 Feb 2004
Posts: 51

View user's profile Send private message

PostPosted: Thu Oct 07, 2004 1:56 am     Reply with quote

Mark wrote:
Putting the device to sleep does not start the program over. Post you entire program.


#include <16F877A.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <ds1302.c>
#include <lcd.c>
#include <kbd.c>

int day_off, hour_off, min_off, sec_off, sec_old;
int inc_flag; // flag to represent FLOW ( 0 = FLOW OFF, 1 = FLOW ON)
int sec_last[11], min_last[11], hour_last[11], day_last[11],mth_last[11], year_last[11]; // create 10 elements array for storing previous
//////////////////////////////////// BELOW FUNCTIONS FOR SLEEP !!!
// global flag to send processor into sleep mode
//short sleep_mode;
// external interrupt when button pushed and released
//#INT_EXT
//void ext_isr()
// {
// static short button_pressed=FALSE;
// if(!button_pressed) // if button action and was not pressed
// {
// button_pressed=TRUE; // the button is now down
// sleep_mode=TRUE; // activate sleep
// printf("The processor is now sleeping.\r\n");
// ext_int_edge(L_TO_H); // change so interrupts on release
// }
// else // if button action and was pressed
// {
// button_pressed=FALSE; // the button is now up
// sleep_mode=FALSE; // reset sleep flag
// ext_int_edge(H_TO_L); // change so interrupts on press
// }
// if(!input(PIN_B3)) // keep button action sychronized wth button flag
// button_pressed=TRUE;
// delay_ms(100); // debounce button
// } //end of ext_isr
//////////////////////////////////////// ABOVE FUNCTIONS FOR SLEEP !!!
byte get_bcd() {
char first,second;
do {
first=kbd_getc();
} while ((first<'0') || (first>'9'));
lcd_putc(first);
first-='0';
do {
second=kbd_getc();
} while ((second<'0') || (second>'9'));
lcd_putc(second);

return((first<<4)|(second-'0'));
}

void set_clock(){
byte day,mth,year,dow,hour,min;
lcd_putc("\fYear 20: "); // Year 2000 -- 2099 //
year=get_bcd();
lcd_putc("\fMonth: ");
mth=get_bcd();
lcd_putc("\fDay: ");
day=get_bcd();
lcd_putc("\fWeekday 1-7: ");
dow=get_bcd();
lcd_putc("\fHour: ");
hour=get_bcd();
lcd_putc("\fMin: ");
min=get_bcd();
rtc_set_datetime(day,mth,year,dow,hour,min);
}

/////////////// SETUP MENU ///////////////////////////////
void setup_menu()
{
char cmd, cmd1, cmd2;
lcd_putc("\f1:System 2:Timer");
lcd_putc("\n3:CTRL 4:Back ");
do {
cmd=kbd_getc();
} while ((cmd!='1')&&(cmd!='2')&&(cmd!='3')&&(cmd!='4'));

// SYSTEM RESET MENU //
if(cmd=='1')
{
lcd_putc("\fRESET SYSTEM ? ");
lcd_putc("\n *:YES #:NO ");
do
{ cmd1=kbd_getc(); }
while ((cmd1!='*')&&(cmd1!='#'));
if(cmd1=='*') // RESET CLOCK & TIMER if '*'
set_clock();
} // END OF SYSTEM RESET MENU //

// CONTROL MENU //
if(cmd=='3')
{
lcd_putc("\fSET AUTO OFF ? ");
lcd_putc("\n *:YES #:NO ");
do
{ cmd1=kbd_getc(); }
while ((cmd1!='*')&&(cmd1!='#'));
if(cmd1=='*') // RESET CLOCK & TIMER if '*'
set_control();
if(cmd1=='#')
{
timer_sec=0;
timer_min=0;
timer_hour=0;
timer_day=0;
}
}
} // END OF SETUP MENU FUNCTION //

void main() {
char cmd;
byte day,mth,year,dow,hour,min,sec;
Just_wokeup = 0;
day_off = 0;
hour_off = 0; // Timer initialize to 00:00:00
min_off = 0;
sec_off = 0;
sec_old = 0;
i = 0;
for (i=1;i<11;i++)
{ sec_last[i] = 0; // LAST OFF TIME initialize to 00:00:00//
min_last[i] = 0;
hour_last[i] = 0;
day_last[i] = 0;
mth_last[i] = 0;
year_last[i] = 0; }
timer_sec=0; //
timer_min=0; //// Initial value for AUTO TIMER OFF TIME
timer_hour=0; ////
timer_day=14; //
rtc_init();
lcd_init();
kbd_init();

// To switch ON back light of LCD --> PIN PORTA,5 //
output_high(PIN_A5);
////////////////////////////////////////////////////
lcd_putc("\fFLOW MONITORING");
lcd_putc("\n *:Set #:Run ");
do {
cmd=kbd_getc();
} while ((cmd!='*')&&(cmd!='#'));
if(cmd=='*')
set_clock();
// To switch OFF backlight of LCD --> PIN PORTA,5 //
output_low(PIN_A5);
output_low(PIN_E2); // Initial settings ==> PORTE,2 == 0

//########################################## Below is for SLEEP MODE
// long counter;
// sleep_mode=FALSE; // init sleep flag
// ext_int_edge(H_TO_L); // init interrupt triggering for button press
// enable_interrupts(INT_EXT);// turn on interrupts
// enable_interrupts(GLOBAL);
// printf("\n\n");
// counter=0; // reset the counter
//################ Above is for SLEEP MODE

/////////////////////////////////// MAIN ROUTINE ////////////////////////////
while (TRUE) {
cmd=kbd_getc();
if(cmd=='*') // SET REAL TIME CLOCK
{
// To switch ON backlight of LCD --> PIN PORTA,5 //
output_high(PIN_A5);
setup_menu(); // GOTO SETUP MENU
// To switch OFF backlight of LCD --> PIN PORTA,5 //
output_low(PIN_A5);
////////////////////////////////////////////////////
}
lcd_putc('\f');
rtc_get_date( day, mth, year, dow);
rtc_get_time( hour, min, sec );
if(input(PIN_E0) == 1) //## SPECIAL DESIGN ## || if WATER FLOW ie RE0=1 ==> increase timer ### PORTE,0 == FLOW SWITCH
{
if (sec != sec_old) // ONLY increment sec if SEC != SEC_OLD, if same means no need 2 increase timer
{
sec_off = sec_off + 1; //(sec - sec_old);
if (sec_off == 60) // if sec_off == 60 ie 60sec,then clr & increment min_off
{
sec_off = 0;
min_off = min_off + 1;
}
if (min_off == 60) // if min_off == 60 ie 60 min,then clr & increment hour_off
{
min_off = 0;
hour_off = hour_off + 1;
}
if (hour_off == 24) // if hour_off == 24 ie 24 hours,then clr & increment day_off
{
hour_off = 0;
day_off = day_off + 1;
}
previously_OFF = 1; // previously_OFF flag to indicate previously & currently NO WATER
}
output_high(PIN_E2); // Specially chg to ON RELAY (240Vac) when timer RUN
}
else // if no water is detected !!! (NO FLOW)
{
if(previously_OFF == 1) // if HIGH (1), means PREVIOUSLY/BEFORE THAT 'NO WATER FLOW'
{
///////////////////// TO SAVE 10 PREVIOUS DOWN TIME ////////////////////////////////

sec_last[1] = sec; // 1st
min_last[1] = min; // 1st
hour_last[1] = hour; // 1st
day_last[1] = day; // 1st
mth_last[1] = mth; // 1st
year_last[1] = year; // 1st

////////// TO SAVE 10 PREVIOUS DOWN TIME /////////////
previously_OFF = 0; // chg to LOW, to mean that WATER IS FLOWING even for (n-1) cycle
}
// if PORTE,0 == LOW, PORTE,2 == LOW TO SWITCH OFF SOXHLET
output_low(PIN_E2); // Specially chg to OFF RELAY (240Vac) when timer OFF
}

////////////////// END OF CHECKING WATER FLOW AT PIN_RE0
cmd=kbd_getc();
if(cmd=='*') // SET REAL TIME CLOCK
{
output_high(PIN_A5); // ON backlight
setup_menu(); // GOTO SETUP MENU
output_low(PIN_A5); // off backlight
}

if(input(PIN_C5)==0)
{
output_float(PIN_B0);
output_float(PIN_B1);
output_float(PIN_B2);
output_float(PIN_B4);
output_float(PIN_B5);
output_float(PIN_B6);
output_float(PIN_B7);
OUTPUT_FLOAT(PIN_C6);
OUTPUT_FLOAT(PIN_C7);
// PORT_B_PULLUPS(TRUE);
Just_wokeup = 1;
}
else // if RC5==1, then ALLOW DISPLAY
{
delay_ms(150);
if(input(PIN_C5)==1)
{
if(Just_wokeup==1)
{
delay_ms(150);// delay100ms to let LCD power up properly
lcd_init();
kbd_init();
lcd_putc("\f Power Returned ");
lcd_putc("\n ");
printf("\f Main Power Returned");
delay_ms(150);
Just_wokeup = 0;
}
// FOR DISPLAY ON LCD
printf(lcd_putc,"%2X/%2X/%2X %2X%2X%2X",day,mth,year,hour,min,sec);
printf(lcd_putc,"\nt=%02d:%02d:%02d %03dDy",hour_off,min_off,sec_off, day_off);
// FOR DISPLAY USING RS-232
printf("\f Current Date & Time : %2X/%2X/%2X %2X:%2X:%2X",day,mth,year,hour,min,sec);
// } // END of if(input(PIN_C5)==1)
delay_ms(250);
} // end of ELSE on top
cmd=kbd_getc();
if(cmd=='*') // SET REAL TIME CLOCK
{
output_high(PIN_A5);// ON backlight
setup_menu(); // GOTO SETUP MENU
output_low(PIN_A5); // OFF backlight
}
cmd=kbd_getc();
if(cmd=='*') // SET REAL TIME CLOCK
{
output_high(PIN_A5); // ON backlight
setup_menu(); // GOTO SETUP MENU
output_low(PIN_A5); // Off backlight
}
sec_old = sec; //copy value of sec to sec_old REGISTER; if later in the loop SEC=SEC_OLD,dont increment sec_off

if(((int)timer_day==day_off)&&((int)timer_hour==hour_off)&&((int)timer_min==min_off)&&((int)timer_sec==sec_off))
{
output_low(PIN_E2);
lcd_putc("\f Timer Complete ");
lcd_putc("\n1:End 2:Resume");
do {
cmd=kbd_getc();
} while ((cmd!='1')&&(cmd!='2'));
if(cmd=='1')
{
day_off = 0;
hour_off = 0;
min_off = 0;
sec_off = 0;
lcd_putc("\fEnd of Operation");
lcd_putc("\n#:Restart timer ");
do {
cmd=kbd_getc();
} while (cmd!='#');
}
}
///For SLEEP MODE
// if(INPUT(PIN_B3)==0) // if sleep flag set
// {
// Just_wokeup = 1; // to make sure LCD & KBD will be INITIALIZED later
// sleep(); // make processor sleep if SLEEP_MODE is TRUE !!!
// }
} // Do not delete ==> Closing Brace for WHILE Loop
} // DO NOT DELETE ==> CLOSING BRACE for MAIN


So sorry that it's quite crude...The initial sleep function for the MCU doesn't work.So as temporary solution,I need to let the PIC to run continuosly while shutting off all the other peripherals when main power is not available.
ernest



Joined: 11 Feb 2004
Posts: 51

View user's profile Send private message

PostPosted: Thu Oct 07, 2004 2:01 am     Reply with quote

Mark wrote:
Putting the device to sleep does not start the program over. Post you entire program.


#include <16F877A.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <ds1302.c>
#include <lcd.c>
#include <kbd.c>

int day_off, hour_off, min_off, sec_off, sec_old;
int inc_flag; // flag to represent FLOW ( 0 = FLOW OFF, 1 = FLOW ON)
int sec_last[11], min_last[11], hour_last[11], day_last[11],mth_last[11], year_last[11]; // create 10 elements array for storing previous
//////////////////////////////////// BELOW FUNCTIONS FOR SLEEP !!!
// global flag to send processor into sleep mode
//short sleep_mode;
// external interrupt when button pushed and released
//#INT_EXT
//void ext_isr()
// {
// static short button_pressed=FALSE;
// if(!button_pressed) // if button action and was not pressed
// {
// button_pressed=TRUE; // the button is now down
// sleep_mode=TRUE; // activate sleep
// printf("The processor is now sleeping.\r\n");
// ext_int_edge(L_TO_H); // change so interrupts on release
// }
// else // if button action and was pressed
// {
// button_pressed=FALSE; // the button is now up
// sleep_mode=FALSE; // reset sleep flag
// ext_int_edge(H_TO_L); // change so interrupts on press
// }
// if(!input(PIN_B3)) // keep button action sychronized wth button flag
// button_pressed=TRUE;
// delay_ms(100); // debounce button
// } //end of ext_isr
//////////////////////////////////////// ABOVE FUNCTIONS FOR SLEEP !!!
byte get_bcd() {
char first,second;
do {
first=kbd_getc();
} while ((first<'0') || (first>'9'));
lcd_putc(first);
first-='0';
do {
second=kbd_getc();
} while ((second<'0') || (second>'9'));
lcd_putc(second);

return((first<<4)|(second-'0'));
}

void set_clock(){
byte day,mth,year,dow,hour,min;
lcd_putc("\fYear 20: "); // Year 2000 -- 2099 //
year=get_bcd();
lcd_putc("\fMonth: ");
mth=get_bcd();
lcd_putc("\fDay: ");
day=get_bcd();
lcd_putc("\fWeekday 1-7: ");
dow=get_bcd();
lcd_putc("\fHour: ");
hour=get_bcd();
lcd_putc("\fMin: ");
min=get_bcd();
rtc_set_datetime(day,mth,year,dow,hour,min);
}

/////////////// SETUP MENU ///////////////////////////////
void setup_menu()
{
char cmd, cmd1, cmd2;
lcd_putc("\f1:System 2:Timer");
lcd_putc("\n3:CTRL 4:Back ");
do { //truncated to save space
} // END OF SETUP MENU FUNCTION //

void main() {
char cmd;
byte day,mth,year,dow,hour,min,sec;
Just_wokeup = 0;
day_off = 0; hour_off=0; min_off=0; sec_off=0; sec_old=0; i = 0;
for (i=1;i<11;i++)
{ sec_last[i] = 0; // LAST OFF TIME initialize to 00:00:00//
min_last[i] = 0;
hour_last[i] = 0;
day_last[i] = 0;
mth_last[i] = 0;
year_last[i] = 0; }

rtc_init();
lcd_init();
kbd_init();
output_high(PIN_A5); // ON backlight
lcd_putc("\fFLOW MONITORING");
lcd_putc("\n *:Set #:Run ");
do {
cmd=kbd_getc();
} while ((cmd!='*')&&(cmd!='#'));
if(cmd=='*')
set_clock();
output_low(PIN_A5); // OFF backlight
output_low(PIN_E2); // Initial settings ==> PORTE,2 == 0

//########################################## Below is for SLEEP MODE
// long counter;
// sleep_mode=FALSE; // init sleep flag
// ext_int_edge(H_TO_L); // init interrupt triggering for button press
// enable_interrupts(INT_EXT);// turn on interrupts
// enable_interrupts(GLOBAL);
// printf("\n\n");
// counter=0; // reset the counter
//################ Above is for SLEEP MODE

/////////////////////////////////// MAIN ROUTINE ////////////////////////////
while (TRUE) {
cmd=kbd_getc();
if(cmd=='*') // SET REAL TIME CLOCK
{
output_high(PIN_A5); // ON backlight
setup_menu(); // GOTO SETUP MENU
output_low(PIN_A5); // OFF backlight
}
lcd_putc('\f');
rtc_get_date( day, mth, year, dow);
rtc_get_time( hour, min, sec );
if(input(PIN_E0) == 1) //
{
if (sec != sec_old) // ONLY increment sec if SEC != SEC_OLD, if same means no need 2 increase timer
{
sec_off = sec_off + 1; //(sec - sec_old);
if (sec_off == 60) // if sec_off == 60 ie 60sec,then clr & increment min_off
{
sec_off = 0;
min_off = min_off + 1;
}
if (min_off == 60) // if min_off == 60 ie 60 min,then clr & increment hour_off
{
min_off = 0;
hour_off = hour_off + 1;
}
if (hour_off == 24) // if hour_off == 24 ie 24 hours,then clr & increment day_off
{
hour_off = 0;
day_off = day_off + 1;
}
previously_OFF = 1; // previously_OFF flag to indicate previously & currently NO WATER
}
output_high(PIN_E2); // Specially chg to ON RELAY (240Vac) when timer RUN
}
else // if no water is detected !!! (NO FLOW)
{
if(previously_OFF == 1) // if HIGH (1), means PREVIOUSLY/BEFORE THAT 'NO WATER FLOW'
{
////////////// TO SAVE 10 PREVIOUS DOWN TIME //////////
sec_last[1] = sec; // 1st
min_last[1] = min; // 1st
hour_last[1] = hour; // 1st
day_last[1] = day; // 1st
mth_last[1] = mth; // 1st
year_last[1] = year; // 1st
previously_OFF = 0; // chg to LOW, to mean that WATER IS FLOWING even for (n-1) cycle
}
output_low(PIN_E2); // Specially chg to OFF RELAY (240Vac) when timer OFF
}
////////////////// END OF CHECKING WATER FLOW AT PIN_RE0
cmd=kbd_getc();
if(cmd=='*') // SET REAL TIME CLOCK
{
output_high(PIN_A5); // ON backlight
setup_menu(); // GOTO SETUP MENU
output_low(PIN_A5); // off backlight
}
if(input(PIN_C5)==0)
{
output_float(PIN_B0);
output_float(PIN_B1);
output_float(PIN_B2);
output_float(PIN_B4);
output_float(PIN_B5);
output_float(PIN_B6);
output_float(PIN_B7);
OUTPUT_FLOAT(PIN_C6);
OUTPUT_FLOAT(PIN_C7);
// PORT_B_PULLUPS(TRUE);
Just_wokeup = 1;
}
else // if RC5==1, then ALLOW DISPLAY
{
delay_ms(150);
if(input(PIN_C5)==1)
{
if(Just_wokeup==1)
{
delay_ms(150);// delay100ms to let LCD power up properly
lcd_init();
kbd_init();
lcd_putc("\f Power Returned ");
lcd_putc("\n ");
printf("\f Main Power Returned");
delay_ms(150);
Just_wokeup = 0;
}
// FOR DISPLAY ON LCD
printf(lcd_putc,"%2X/%2X/%2X %2X%2X%2X",day,mth,year,hour,min,sec);
printf(lcd_putc,"\nt=%02d:%02d:%02d %03dDy",hour_off,min_off,sec_off, day_off);
// FOR DISPLAY USING RS-232
printf("\f Current Date & Time : %2X/%2X/%2X %2X:%2X:%2X",day,mth,year,hour,min,sec);
// } // END of if(input(PIN_C5)==1)
delay_ms(250);
} // end of ELSE on top
cmd=kbd_getc();
if(cmd=='*') // SET REAL TIME CLOCK
{
output_high(PIN_A5);// ON backlight
setup_menu(); // GOTO SETUP MENU
output_low(PIN_A5); // OFF backlight
}
cmd=kbd_getc();
if(cmd=='*') // SET REAL TIME CLOCK
{
output_high(PIN_A5); // ON backlight
setup_menu(); // GOTO SETUP MENU
output_low(PIN_A5); // Off backlight
}
sec_old = sec; //copy value of sec to sec_old REGISTER; if later in the loop SEC=SEC_OLD,dont increment sec_off

if(((int)timer_day==day_off)&&((int)timer_hour==hour_off)&&((int)timer_min==min_off)&&((int)timer_sec==sec_off))
{
output_low(PIN_E2);
lcd_putc("\f Timer Complete ");
lcd_putc("\n1:End 2:Resume");
do {
cmd=kbd_getc();
} while ((cmd!='1')&&(cmd!='2'));
if(cmd=='1')
{
day_off = 0;
hour_off = 0;
min_off = 0;
sec_off = 0;
lcd_putc("\fEnd of Operation");
lcd_putc("\n#:Restart timer ");
do {
cmd=kbd_getc();
} while (cmd!='#');
}
}
///For SLEEP MODE
// if(INPUT(PIN_B3)==0) // if sleep flag set
// {
// Just_wokeup = 1; // to make sure LCD & KBD will be INITIALIZED later
// sleep(); // make processor sleep if SLEEP_MODE is TRUE !!!
// }
} // Do not delete ==> Closing Brace for WHILE Loop
} // DO NOT DELETE ==> CLOSING BRACE for MAIN


So sorry that it's quite crude...The initial sleep function for the MCU doesn't work.So as temporary solution,I need to let the PIC to run continuosly while shutting off all the other peripherals when main power is not available.
erhan
Guest







PostPosted: Sat Jan 01, 2005 4:05 pm     Reply with quote

maybe for this topic it's late but helps other ones!
yes, max232 draws much current (in very low power circuits) if you want to sleep()!
when designing batery supplied circuits its better to use ds275 for rs232. It gets its power from rs232 line.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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