/********************************************** * Binary Watch * by Mastro Gippo * * (c) 2008 **********************************************/ #include "main.h" #define RowH PIN_A1 #define RowM PIN_A2 #define PwmDel delay_us(0) int1 OnLed; int8 Seconds = 0; int8 Hours = 0, Minutes = 0; void PrintRows(int8 R1, int8 R2); #int_RTCC void RTCC_isr(void) { Seconds++; // Increment number of seconds if(Seconds == 60) {Minutes++; Seconds = 0; if(Minutes == 60) {Hours++; Minutes = 0; if(Hours == 24) {Days++; Hours = 0; }}} } void main() { int8 Pse; int8 Cnt; //Setup... setup_timer_0(RTCC_INTERNAL|RTCC_DIV_32); setup_timer_1(T1_DISABLED); setup_comparator(NC_NC_NC_NC); setup_vref(FALSE); enable_interrupts(INT_RTCC); enable_interrupts(GLOBAL); port_a_pullups(FALSE); output_low(RowH); output_low(RowM); output_low(PIN_C0); output_low(PIN_C1); output_low(PIN_C2); output_low(PIN_C3); output_low(PIN_C4); output_low(PIN_C5); #use fast_io(A) set_tris_a(0xF9); #use fast_io(C) set_tris_c(0x00); Hours = 13; Minutes = 1; while(1) { if(input(PIN_A0)) //Button pressed; Show time { Pse = Seconds; Cnt = 5; //Show time for 5 seconds while(Cnt > 0) { if(Seconds != Pse) { Pse = Seconds; Cnt--; } PrintRows(Minutes, Hours); } //If button is still pressed after 5 seconds, set up if(input(PIN_A0)) { output_high(RowM); output_c(0x20); delay_ms(2000); output_c(0x00); output_low(RowM); Pse = Seconds; Cnt = 4; while(Cnt > 0) { if(input(PIN_A0)) //Increase minutes every button press { Cnt = 4; delay_ms(10); //Simple debounce Minutes++; if(Minutes == 60) Minutes = 0; //Shows current setting, while waiting //for the user to release the button while(input(PIN_A0)){PrintRows(Minutes, 0);}; } //If the user doesn't press the button for 4sec, //switch to Hour setting if(Seconds != Pse) { Pse = Seconds; Cnt--; } PrintRows(Minutes, 0); } Pse = Seconds; Cnt = 4; while(Cnt > 0) { if(input(PIN_A0)) { Cnt = 4; delay_ms(10); Hours++; if(Hours == 24) Hours = 0; while(input(PIN_A0)){PrintRows(0, Hours);}; } if(Seconds != Pse) { Pse = Seconds; Cnt--; } PrintRows(0, Hours); } Pse = Seconds; Cnt = 5; while(Cnt > 0) { if(Seconds != Pse) { Pse = Seconds; Cnt--; } PrintRows(Minutes, Hours); } } } } } void PrintRows(int8 R1, int8 R2) { static int8 i; output_low(RowH); output_high(RowM); //I have only one resistor, so I must //turn on only one led at a time #use fast_io(C) output_c(R1 & 0x01); output_c(R1 & 0x02); output_c(R1 & 0x04); output_c(R1 & 0x08); output_c(R1 & 0x10); output_c(R1 & 0x20); output_low(RowM); output_high(RowH); output_c(R2 & 0x01); output_c(R2 & 0x02); output_c(R2 & 0x04); output_c(R2 & 0x08); output_c(R2 & 0x10); output_c(0); output_low(RowM); output_low(RowH); }