From c6fc933f95a0ce1acc0730e25a031f2d14ebfcdc Mon Sep 17 00:00:00 2001 From: William Harrington Date: Thu, 1 Aug 2019 02:53:50 -0500 Subject: Remove old Timer2/Timer1 remnants and add support to use 1Hz square wave from DS3231 and RB0 as external interrup from DS3231 SQWE @ 1Hz to update time. --- main.c | 158 +++++++++++++++++++++++++++++++++++------------------------------ main.h | 15 +++++-- 2 files changed, 96 insertions(+), 77 deletions(-) diff --git a/main.c b/main.c index c5235d1..78d0f77 100755 --- a/main.c +++ b/main.c @@ -17,16 +17,17 @@ void main(void) { TRISA0 = 0x01; // RA is input (ADC) - - TRISBbits.TRISB0 = 0; // Set RB0 as output - PORTBbits.RB0 = 0; // Set RB0 low - + TRISC = 0x00; // Set PORTC as outputs PORTC = 0x00; // Set all PORTC pins LOW TRISD = 0x00; // Set PORTD as outputs PORTD = 0x00; // Set all PORTD pins LOW + GIE = 1; // Enable global interrupt + PEIE = 1; // Enable the peripheral interrupt + INTE = 1; // Enable RB0 as external interrupt + #ifdef DEBUG Initialize_UART(); //Initialize UART module UART_send_string(CLS); // Clear screen @@ -73,6 +74,7 @@ void main(void) { //Set_Time(); //Set_Date(); //Set_DayOfWeek(Get_DayOfWeek(year, month, date)); + Set_Sqwe(0x40); //1Hz // Write default alarm values @@ -87,83 +89,86 @@ void main(void) { #ifdef VFD Vfd_Set_Brightness(3); //Max brightness 3 - minimum #endif + + while(1){ + + } +} - while (1) { - - // Set time and date variables from DS3231 - //#ifdef DEBUG - // UART_send_string("Updating current date time from DS3231\r\n"); - //#endif - Update_Current_Date_Time(); - - // Read temp - //#ifdef DEBUG - // UART_send_string("Reading Alarms and Temperature"); - //#endif - Read_Alarms_Temp(); - - // Get Alarm Status - Get_Alarm_Status(); - - // Separate the int timedate variables into chars - //#ifdef DEBUG - // UART_send_string("Formatting chars"); - //#endif - format_DateTimeChars(); - - // Format temperature - //#ifdef DEBUG - // UART_send_string("Formating temperature"); - //#endif - format_Temperature(); - - // Determine day of the week from Calendar Date - //#ifdef DEBUG - // UART_send_string("Determining day of week"); - //#endif - getWeekDay(Get_DayOfWeek(year, month, date)); - - // Enter loop and update display if sec changes from DS3231 - if (sec_chg != sec) { - - update_Display(); - - // Set temp_sec to current sec from DS3231 - sec_chg = sec; - } +void New_Second() { + // Set time and date variables from DS3231 + //#ifdef DEBUG + // UART_send_string("Updating current date time from DS3231\r\n"); + //#endif + Update_Current_Date_Time(); + + // Read temp + //#ifdef DEBUG + // UART_send_string("Reading Alarms and Temperature"); + //#endif + Read_Alarms_Temp(); + + // Get Alarm Status + Get_Alarm_Status(); + + // Separate the int timedate variables into chars + //#ifdef DEBUG + // UART_send_string("Formatting chars"); + //#endif + format_DateTimeChars(); + + // Format temperature + //#ifdef DEBUG + // UART_send_string("Formating temperature"); + //#endif + format_Temperature(); + + // Determine day of the week from Calendar Date + //#ifdef DEBUG + // UART_send_string("Determining day of week"); + //#endif + getWeekDay(Get_DayOfWeek(year, month, date)); + + // Enter loop and update display if sec changes from DS3231 + if (sec_chg != sec) { + + update_Display(); + + // Set temp_sec to current sec from DS3231 + sec_chg = sec; + } #ifdef ADC - // Get current LDR reading and calculate for brightness levels 0 - 3 - ldr = Adc_Read(0); + // Get current LDR reading and calculate for brightness levels 0 - 3 + ldr = Adc_Read(0); - if (brtlvl_chg != brtlvl) { - Vfd_Set_Brightness(brtlvl); - brtlvl_chg = brtlvl; - } + if (brtlvl_chg != brtlvl) { + Vfd_Set_Brightness(brtlvl); + brtlvl_chg = brtlvl; + } #endif #ifdef DEBUG - UART_send_string(CURSOR(19, 1)); - sprintf(buf, "%sTIME:%s\t%02d:%02d:%02d\t%sDATE:%s\t%02d/%02d/%02d\r\n", YELLOW, CLRATTR, - hour, min, sec, YELLOW, CLRATTR, date, month, year); - UART_send_string(buf); - sprintf(buf, "%sAL1:%s\t%02d:%02d:%02d\t%sSTATUS:%s %d\r\n", YELLOW, - CLRATTR, alarm1_hour, alarm1_min, alarm1_sec, YELLOW, - CLRATTR, alarm1_status); - UART_send_string(buf); - sprintf(buf, "%sAL2:%s\t%02d:%02d\t\t%sSTATUS:%s %d\r\n", YELLOW, - CLRATTR, alarm2_hour, alarm2_min, YELLOW, CLRATTR, - alarm2_status); - UART_send_string(buf); - sprintf(buf, "%sWKDAY:%s\t%d\t\t%sDAY:%s\t%s\r\n", YELLOW, CLRATTR, - day, YELLOW, CLRATTR, weekday); - UART_send_string(buf); - sprintf(buf, "%sTEMP:\t%s%c%c%c.%c%cC%s\t\t%sLDR:%s\t\%04d %d\r\n", - YELLOW, CLRATTR, temp_sign, temp_2, temp_1, temp_0, 0xB0, - CLRATTR, YELLOW, CLRATTR, ldr, brtlvl); - UART_send_string(buf); + UART_send_string(CURSOR(19, 1)); + sprintf(buf, "%sTIME:%s\t%02d:%02d:%02d\t%sDATE:%s\t%02d/%02d/%02d\r\n", YELLOW, CLRATTR, + hour, min, sec, YELLOW, CLRATTR, date, month, year); + UART_send_string(buf); + sprintf(buf, "%sAL1:%s\t%02d:%02d:%02d\t%sSTATUS:%s %d\r\n", YELLOW, + CLRATTR, alarm1_hour, alarm1_min, alarm1_sec, YELLOW, + CLRATTR, alarm1_status); + UART_send_string(buf); + sprintf(buf, "%sAL2:%s\t%02d:%02d\t\t%sSTATUS:%s %d\r\n", YELLOW, + CLRATTR, alarm2_hour, alarm2_min, YELLOW, CLRATTR, + alarm2_status); + UART_send_string(buf); + sprintf(buf, "%sWKDAY:%s\t%d\t\t%sDAY:%s\t%s\r\n", YELLOW, CLRATTR, + day, YELLOW, CLRATTR, weekday); + UART_send_string(buf); + sprintf(buf, "%sTEMP:\t%s%c%c%c.%c%cC%s\t\t%sLDR:%s\t\%04d %d\r\n", + YELLOW, CLRATTR, temp_sign, temp_2, temp_1, temp_0, 0xB0, + CLRATTR, YELLOW, CLRATTR, ldr, brtlvl); + UART_send_string(buf); #endif - } } // Format unsigned int to unsigned chars @@ -339,4 +344,11 @@ void update_Display() { if (min == 30 && sec == 00) { alarm(1); } +} + +void __interrupt() isr(void) { + if (INTF == 1) { + INTF = 0; + New_Second(); + } } \ No newline at end of file diff --git a/main.h b/main.h index f7c707d..ed84202 100755 --- a/main.h +++ b/main.h @@ -3,8 +3,13 @@ unsigned int ldr = 0; #endif +// Initialize variables for VFD brightness level #define brtlvl ldr/256 -int brtlvl_chg = 0; +unsigned int brtlvl_chg = 0; + +// Initialize for timerr interrupt value +unsigned int counter = 0; +unsigned int interrupts = 0; // Initialize variables for date time and alarms unsigned int sec = 0; @@ -49,6 +54,9 @@ unsigned char alarm2_min_0, alarm2_min_1, alarm2_hour_0, alarm2_hour_1; char buf[40]; #endif +void display_Intro(void); +void display_Lcd_Layout(void); + // Format unsigned int to unsigned chars void format_DateTimeChars(void); @@ -61,7 +69,6 @@ void getWeekDay(unsigned int); // Determine Alarm status from control registers void Get_Alarm_Status(void); -void display_Intro(void); -void display_Lcd_Layout(void); +void update_Display(void); -void update_Display(void); \ No newline at end of file +void New_Second(void); \ No newline at end of file -- cgit v1.2.3-54-g00ecf