//Seconds Routine using a Bresenham Algorithm from an idea by Bob Ammerman and //Roman Black //This algorithm works for any crystal frequency; and jitter and accuracy can be //altered at will. //****************************************************************************** //To use a different crystal just take the frequency divide by 4 (for pics) to //get the internal frequency. EG. 8MHz crystal has an internal cycle of 2MHz, //therefore load the counter with 2,000,000 (0x1E 84 80) the 24 bit maths does // the rest. // // For periods just do a further division //EG: 1200baud with 8MHz crystal = 8,000,000/4 /1200 = 500 (0x07 A1 20) //4:32 PM 8/5/2002 //CDB 12/6/2002 //defines for lcd_shiftout //#define shiftport PORTE //#define clockport PORTE //#define latchport PORTE #define LCDPORT PORTB #define RSPORT PORTB #define ENPORT PORTB #define TOP //const char bit_no=0; //unsigned char rs_state; //const char clockbit=1; //const char latchbit=2; const char rsbit=2; const char ebit=3; #include //Includes extra headers to access register bits in asm style #include #include #include #include "sprinkler.h" #include #include "ButtonPress.h" #__config 16242 unsigned char bt; void main() { unsigned char button; setup(); pTotals=(unsigned char *)&Totals; FirstBoot(0x10,sizeof(Totals)); //test EEPROM for previous price and total information lcd_init(); while(1) { if(SysFlag&(1<Mins && hour==pEvent->Hours && Dy==pEvent->Day) { if(++z> (MAX_EV-1)) {z=0;} //z=index for next event to be displayed OUTPUT|=(SOLENOID|LED); //Switch Solenoid and ON LED M_Diff=pEvent->Mins + pEvent->PeriodM; //add duration time to start time H_Diff=pEvent->Hours + pEvent->PeriodH; if(M_Diff>59) { M_Diff-=60; //check for minutes going past the hour H_Diff+=1; //add one to hour if so } else { H_Diff+=0; } if(H_Diff>23) //check for hour going past midnight==0:00 { H_Diff-=24; } } if(hour==H_Diff && min==M_Diff) { OUTPUT&=~(SOLENOID|LED); //Switch Solenoid and LED off H_Diff=M_Diff=0; WriteData(0x10,pTotals,sizeof(Totals)); } pEvent++; // move to next event in structure. } } void Display() { unsigned char ind; if(disptype==0)//not using Ds { lcd_pos(line,1); lcd_st(Day[Dy]); lcd_num(hour,2,1); lcd_packet(sep,1); lcd_num(min,2,0); lcd_pos(line,11); lcd_st(mode[item]); lcd_st(Padd[0]); lcd_pos(2,0); lcd_st("On @ "); lcd_num(Event[z].Hours,2,1); lcd_packet(sep,1); lcd_num(Event[z].Mins,2,0); lcd_packet('-',1); lcd_st(Day[Event[z].Day]); } if(disptype==1) { lcd_pos(line,1); lcd_st(indx[index]); lcd_num(Event[event_no].Hours,2,1); lcd_packet(sep,1); lcd_num(Event[event_no].Mins,2,0); lcd_st(Padd[0]); lcd_st(mode[item]); lcd_pos(line+1,0); lcd_st("Ev "); lcd_num(event_no+1,1,1); lcd_st(Padd[0]); lcd_num(Event[event_no].PeriodH,2,1); lcd_packet(sep,1); lcd_num(Event[event_no].PeriodM,2,0); //lcd_st(Padd[0]); lcd_packet('-',1); lcd_st(Day[Event[event_no].Day]); } //display for cost and usage if(disptype==2) { lcd_pos(line,1); lcd_num(Totals.TotalMSD,4,1); lcd_packet('.',1); lcd_num(Totals.TotalLSD,2,0); lcd_pos(line,8); lcd_st(Padd[0]); lcd_pos(line,11); lcd_num(Totals.CostMSD,1,1); lcd_packet('.',1); lcd_num(Totals.CostLSD,2,0); //lcd_st(Padd[0]); lcd_pos(line+1,0); lcd_st(Padd[0]); lcd_num(Totals.Klt,3,0); lcd_packet('.',1); lcd_num(Totals.HunLt,1,0); lcd_num(Totals.TenLt,2,0); lcd_packet('-',1); if(item==(-1)) lcd_st("Klt "); else lcd_st(mode[item]); lcd_packet(' ',1); } } void Usage() // Function to { Totals.TenLt++; if(Totals.TenLt>99) { Totals.TenLt=0; Totals.HunLt++; if(Totals.HunLt>9) { Totals.HunLt=0; Totals.Klt++; AddCost(); if(Totals.Klt>999) { Totals.Klt=0; Totals.TenLt=1; } } } } void AddCost() { //Only addition is required as we only get here after each 1Kltr has passed Totals.TotalLSD+=Totals.CostLSD; if(Totals.TotalLSD<100) { // TotalMSD+=CostMSD; #asm bsf STATUS,RP0 ;get into right bank - found by trial and error movlw Totals ;Get address of Totals Structure movwf FSR ;put it in indirect addressing register movf Totals+3,W ;Put contents of Totals.TotalLSD into W addwf INDF,W ;add W to contents of FSR= Totals.CostMSD, result imto W movwf Totals+3 ;update Totals.TotalLSD with W(W held sum of costmsd+total) btfsc STATUS,C ;goto end of function unless a 'C'arryover to next byte incf Totals+4 ; yes so increment high byte bcf STATUS,RP0 #asmend } else { Totals.TotalLSD-=100; //adjust c/p amount to amount - 100 Totals.TotalMSD++; //add one to $/£ amount #asm bsf STATUS,RP0 movlw Totals movwf FSR movf Totals+3,W addwf INDF,W movwf Totals+3 btfsc STATUS,C incf Totals+4 bcf STATUS,RP0 #asmend } } //Left justifies zero's void LZero(char digit,char *st) { if(digit<10) { *st='0'; st++; } cPrtString(st,digit); } void FirstBoot(unsigned char addr,unsigned char structsize) { unsigned char loop; if(ReadEEData(0)=='#') //Check if '#' sign is in EEPROM at addr 0x00 { for(loop=structsize;loop;loop--) { *pTotals=ReadEEData(addr); //copy data into Totals via pointer pTotals addr++; //move to next address pTotals++; //move pointer to next element in structure } pTotals-=structsize; // move pointer back to beginning of totals if(!(PCON&(1<