From 8edef0db2eea068da5ab00715039074fe16324b9 Mon Sep 17 00:00:00 2001 From: William Harrington Date: Wed, 7 Aug 2019 18:48:15 -0500 Subject: Refactor variables and functions. Add user input functionality and improve effeciency to reduce application size in MCU. --- main.c | 418 +++++++++++++++++++++++++++++++++++++++++++++++------------------ main.h | 90 +++++++------- 2 files changed, 353 insertions(+), 155 deletions(-) diff --git a/main.c b/main.c index eb2eada..e749e43 100755 --- a/main.c +++ b/main.c @@ -1,22 +1,46 @@ #include "conf.h" //Configuration settings #include "main.h" //Declarations for main -#include "display.h" -#include "i2c.h" -#include "ds3231.h" //Declarations for RTC +//Initialize variables #ifdef ADC -#include "adc.h" //Declarations for ADC +// Initialize variable for LDR +unsigned int ldr = 0; #endif -#include "beep.h" //Declarations for BEEP +// Initialize variable for VFD brightness level +unsigned int brtlvl_chg = 0; -#ifdef DEBUG -#include "uart.h" //Declarations for UART -#include "term.h" //Declarations for Escape codes -#endif +// Boolean if external interrupt occurs +unsigned int update = 0; + +// Boolean for setting time manually with user input +unsigned int edit_datetime = 0; + +// Initialize variables for date time and alarms +unsigned int sec = 0; +unsigned int min = 0; +unsigned int hour = 0; +unsigned int day = 1; +unsigned int date = 0; +unsigned int month = 0; +unsigned int year = 00; +unsigned int century = 20; + +// Initialize variables for temperature +unsigned int temperature_lsb = 0; +int temperature_msb = 0; +unsigned char temp_sign = ' '; void main(void) { TRISA0 = 0x01; // RA is input (ADC) + TRISBbits.TRISB2 = 1; + TRISBbits.TRISB4 = 1; + TRISBbits.TRISB5 = 1; + + GIE = 0; //Disable interrupts until set up + OPTION_REGbits.nRBPU = 0; // PORTB pull-ups are enabled by individual port latch values + INTE = 1; // Enable RB0 as external interrupt + GIE = 1; // Enable all interrupts from now on TRISC = 0x00; // Set PORTC as outputs PORTC = 0x00; // Set all PORTC pins LOW @@ -24,10 +48,6 @@ void main(void) { 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 @@ -74,7 +94,7 @@ void main(void) { //Set_Time(); //Set_Date(); //Set_DayOfWeek(Get_DayOfWeek(year, month, date)); - //Set_Sqwe(0x40); //1Hz + Set_Sqwe(0x40); //1Hz // Write default alarm values @@ -91,16 +111,41 @@ void main(void) { #endif while (1) { - Update_Current_Date_Time(); - Read_Alarms_Temp(); - Get_Alarm_Status(); - format_DateTimeChars(); - format_Temperature(); - getWeekDay(Get_DayOfWeek(year, month, date)); if (update) { + Lcd_Set_Cursor(1, 3); + Lcd_Write_Char(':'); + Lcd_Set_Cursor(1, 6); + Lcd_Write_Char(':'); + Update_Current_Date_Time(); + Read_Alarms_Temp(); + Get_Alarm_Status(); + format_Temperature(); update_Display(); + update = 0; + __delay_ms(500); + } else { + Lcd_Set_Cursor(1, 3); + Lcd_Write_Char(' '); + Lcd_Set_Cursor(1, 6); + Lcd_Write_Char(' '); } + // Edit time when Set button is pressed + if (!SETB) { + __delay_ms(250); + edit_Date_Time(); + } + +#ifdef ADC + // 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; + } +#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, @@ -115,44 +160,18 @@ void main(void) { 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); + day, YELLOW, CLRATTR, Get_WeekDay(day)); 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); + sprintf(buf, "%sISR:\t%s%d", YELLOW, CLRATTR, update); + UART_send_string(buf); #endif - } } -// Format unsigned int to unsigned chars - -void format_DateTimeChars() { - sec_0 = sec % 10 + '0'; - sec_1 = sec / 10 + '0'; - min_0 = min % 10 + '0'; - min_1 = min / 10 + '0'; - hour_0 = hour % 10 + '0'; - hour_1 = hour / 10 + '0'; - day_0 = date % 10 + '0'; - day_1 = date / 10 + '0'; - month_0 = month % 10 + '0'; - month_1 = month / 10 + '0'; - year_0 = year % 10 + '0'; - year_1 = year / 10 + '0'; - alarm1_sec_0 = alarm1_sec % 10 + '0'; - alarm1_sec_1 = alarm1_sec / 10 + '0'; - alarm1_min_0 = alarm1_min % 10 + '0'; - alarm1_min_1 = alarm1_min / 10 + '0'; - alarm1_hour_0 = alarm1_hour % 10 + '0'; - alarm1_hour_1 = alarm1_hour / 10 + '0'; - alarm2_min_0 = alarm2_min % 10 + '0'; - alarm2_min_1 = alarm2_min / 10 + '0'; - alarm2_hour_0 = alarm2_hour % 10 + '0'; - alarm2_hour_1 = alarm2_hour / 10 + '0'; -} - // Format msb and lsb for temperature display void format_Temperature() { @@ -185,40 +204,9 @@ void format_Temperature() { if (temperature_lsb == 75) { temp_0 = '7'; } - - temp_1 = temperature_msb % 10 + '0'; - temp_2 = temperature_msb / 10 + '0'; } -// Determine day of week from DS3231 - -void getWeekDay(unsigned int dayOfWeek) { - switch (dayOfWeek) { - case 1: - weekday = "Sun"; - break; - case 2: - weekday = "Mon"; - break; - case 3: - weekday = "Tue"; - break; - case 4: - weekday = "Wed"; - break; - case 5: - weekday = "Thu"; - break; - case 6: - weekday = "Fri"; - break; - case 7: - weekday = "Sat"; - break; - default: - break; - } -} +// Determine month name from month value // Determine Alarm status from control registers @@ -227,49 +215,71 @@ void Get_Alarm_Status() { alarm2_status = (control_reg >> 1) & 0x01; // Read alarm2 INT enable bit A2IE } -void display_Intro(); -void display_Lcd_Layout(); +void display_Digit(unsigned int data) { + Lcd_Write_Char(MSB(data)); + Lcd_Write_Char(LSB(data)); +} -void update_Display(void) { +void display_Intro() { + // Give an intro message on the LCD + Lcd_Clear(); + Lcd_Set_Cursor(1, 4); + Lcd_Write_String("Desk Clock"); + Lcd_Set_Cursor(2, 15); + Lcd_Write_String("V1"); + __delay_ms(1000); //display for 1sec +} + +void display_Lcd_Layout() { + // Setup time date display format + Lcd_Clear(); + Lcd_Set_Cursor(1, 1); + Lcd_Write_String("HH:mm:ss -PP.P"); + Lcd_Write_Char(0xDF); + Lcd_Write_Char('C'); + Lcd_Set_Cursor(2, 1); + Lcd_Write_String(" ddd DD/MM/CCYY "); + + __delay_ms(1000); +} + +void update_Display() { // Display Hours Lcd_Set_Cursor(1, 1); - Lcd_Write_Char(hour_1); - Lcd_Write_Char(hour_0); + display_Digit(hour); // Display minutes Lcd_Set_Cursor(1, 4); - Lcd_Write_Char(min_1); - Lcd_Write_Char(min_0); + display_Digit(min); // Display seconds Lcd_Set_Cursor(1, 7); - Lcd_Write_Char(sec_1); - Lcd_Write_Char(sec_0); + display_Digit(sec); // Display day Lcd_Set_Cursor(2, 6); - Lcd_Write_Char(day_1); - Lcd_Write_Char(day_0); + display_Digit(date); // Display month Lcd_Set_Cursor(2, 9); - Lcd_Write_Char(month_1); - Lcd_Write_Char(month_0); + display_Digit(month); - // Display year + // Display century Lcd_Set_Cursor(2, 12); - Lcd_Write_Char(year_1); - Lcd_Write_Char(year_0); + display_Digit(century); + + // Display year + Lcd_Set_Cursor(2, 14); + display_Digit(year); // Display day of week - Lcd_Set_Cursor(2, 1); - Lcd_Write_String(weekday); + Lcd_Set_Cursor(2, 2); + Lcd_Write_String(Get_WeekDay(day)); // Display temperature Lcd_Set_Cursor(1, 10); Lcd_Write_Char(temp_sign); - Lcd_Write_Char(temp_2); - Lcd_Write_Char(temp_1); + display_Digit(temperature_msb); Lcd_Set_Cursor(1, 14); Lcd_Write_Char(temp_0); @@ -280,23 +290,207 @@ void update_Display(void) { if (min == 30 && sec == 00) { alarm(1); } +} -#ifdef ADC - // Get current LDR reading and calculate for brightness levels 0 - 3 - ldr = Adc_Read(0); +void edit_Date_Time(void) { + Lcd_Set_Cursor(2, 16); + Lcd_Write_Char('S'); + edit_datetime++; + while (edit_datetime > 0 && edit_datetime < 7) { + + // Set colons if not enabled when Set button is pressed + Lcd_Set_Cursor(1, 3); + Lcd_Write_Char(':'); + Lcd_Set_Cursor(1, 6); + Lcd_Write_Char(':'); + + switch (edit_datetime) { + case 1: //Hours + Lcd_Set_Cursor(2, 16); + Lcd_Write_Char('h'); + if (!DECR) { + __delay_ms(250); + if (hour == 0) { + hour = 23; + } else { + hour -= 1; + } + Lcd_Set_Cursor(1, 1); + display_Digit(hour); + + } else if (!INCR) { + __delay_ms(250); + if (hour + 1 > 23) { + hour = 0; + } else { + hour += 1; + } + + Lcd_Set_Cursor(1, 1); + display_Digit(hour); + } + break; + case 2: //Minutes + Lcd_Set_Cursor(2, 16); + Lcd_Write_Char('m'); + if (!DECR) { + __delay_ms(250); + if (min == 0) { + min = 59; + } else { + min -= 1; + } + Lcd_Set_Cursor(1, 4); + display_Digit(min); + + } else if (!INCR) { + __delay_ms(250); + if (min + 1 > 59) { + min = 0; + } else { + min += 1; + } + + Lcd_Set_Cursor(1, 4); + display_Digit(min); + } + break; + + case 3: //Seconds + Lcd_Set_Cursor(2, 16); + Lcd_Write_Char('s'); + if (!DECR) { + __delay_ms(250); + if (sec == 0) { + sec = 59; + } else { + sec -= 1; + } + Lcd_Set_Cursor(1, 7); + display_Digit(sec); + + } else if (!INCR) { + __delay_ms(250); + if (sec + 1 > 59) { + sec = 0; + } else { + sec += 1; + } + + Lcd_Set_Cursor(1, 7); + display_Digit(sec); + } + break; + + case 6: //Date + Lcd_Set_Cursor(2, 16); + Lcd_Write_Char('D'); + if (!DECR) { + __delay_ms(250); + if (date - 1 == 0) { + date = Get_Days_In_Month(year, month); + } else { + date -= 1; + } + + Lcd_Set_Cursor(2, 2); + Lcd_Write_String(Get_WeekDay(Get_DayOfWeek(year, month, date))); + Lcd_Set_Cursor(2, 6); + display_Digit(date); + + } else if (!INCR) { + __delay_ms(250); + //TODO USE Formula to get days based on month year (and leap) + if (date + 1 > Get_Days_In_Month(year, month)) { + date = 1; + } else { + date += 1; + } + + Lcd_Set_Cursor(2, 2); + Lcd_Write_String(Get_WeekDay(Get_DayOfWeek(year, month, date))); + Lcd_Set_Cursor(2, 6); + display_Digit(date); + } + break; + + case 5: //Month + Lcd_Set_Cursor(2, 16); + Lcd_Write_Char('M'); + if (!DECR) { + __delay_ms(250); + if (month - 1 == 0) { + month = 12; + } else { + month -= 1; + } + Lcd_Set_Cursor(2, 9); + display_Digit(month); + + } else if (!INCR) { + __delay_ms(250); + if (month + 1 > 12) { + month = 1; + } else { + month += 1; + } + + Lcd_Set_Cursor(2, 9); + display_Digit(month); + } + break; + + case 4: //Year + Lcd_Set_Cursor(2, 16); + Lcd_Write_Char('Y'); + if (!DECR) { + __delay_ms(250); + if (year == 0) { + year = 99; + } else { + year -= 1; + } + Lcd_Set_Cursor(2, 14); + display_Digit(year); + + } else if (!INCR) { + __delay_ms(250); + if (year + 1 > 99) { + year = 0; + } else { + year += 1; + } + + Lcd_Set_Cursor(2, 14); + display_Digit(year); + } + + //TODO Maybe add century + break; + + default: + break; + } - if (brtlvl_chg != brtlvl) { - Vfd_Set_Brightness(brtlvl); - brtlvl_chg = brtlvl; + if (!SETB) { + __delay_ms(250); + edit_datetime++; + } + } + + if (edit_datetime > 6) { + Set_Time(); + Set_DayOfWeek(Get_DayOfWeek(year, month, date)); + Set_Date(); + edit_datetime = 0; + Lcd_Set_Cursor(2, 16); + Lcd_Write_String(" "); } -#endif } void __interrupt() isr(void) { - if (INTF == 1) { - INTF = 0; + if (INTF == 1) { // External interrupt detected update = 1; - } else { - update = 0; + INTF = 0; } } \ No newline at end of file diff --git a/main.h b/main.h index 3429b15..92f369d 100755 --- a/main.h +++ b/main.h @@ -1,70 +1,74 @@ +#include "lcd.h" //Declaratons for LCD +#include "i2c.h" +#include "ds3231.h" //Declarations for RTC + #ifdef ADC -// Initialize variable for LDR -unsigned int ldr = 0; +#include "adc.h" //Declarations for ADC #endif -// Initialize variables for VFD brightness level +#include "beep.h" //Declarations for BEEP + +#ifdef DEBUG +#include "uart.h" //Declarations for UART +#include "term.h" //Declarations for Escape codes +#endif + +#ifdef ADC +// Declare variable for LDR +unsigned int ldr; +#endif + +// Declare variables for VFD brightness level #define brtlvl ldr/256 -unsigned int brtlvl_chg = 0; +unsigned int brtlvl_chg; // Update to 1 if RB0 interrupt occurs, otherwise 0 -unsigned int update = 0; - -// Initialize variables for date time and alarms -unsigned int sec = 0; -unsigned int min = 37; -unsigned int hour = 21; -unsigned int day = 4; -unsigned int date = 31; -unsigned int month = 7; -unsigned int year = 19; -unsigned int century = 20; +unsigned int update; + +// Boolean if setting time manually with user input +unsigned int edit_datetime; + +// Declare variables for date time and alarms +unsigned int sec; +unsigned int min; +unsigned int hour; +unsigned int day; +unsigned int date; +unsigned int month; +unsigned int year; +unsigned int century; unsigned int alarm1_sec, alarm1_min, alarm1_hour; unsigned int alarm2_min, alarm2_hour; unsigned int status_reg, alarm1_status, alarm2_status; unsigned int control_reg; -// Initialize variables for temperature -unsigned int temperature_lsb = 0; -int temperature_msb = 0; -unsigned char temp_sign = ' '; - -// Initialize variable for week day -char* weekday; - -// Initialize variable for detecting seconds change -unsigned int sec_chg = 0; - -// Initialize variables for splitting decimal digits to characters -unsigned char sec_0, sec_1; -unsigned char min_0, min_1; -unsigned char hour_0, hour_1; -unsigned char day_0, day_1; -unsigned char month_0, month_1; -unsigned char year_0, year_1; -unsigned char temp_0, temp_1, temp_2; -unsigned char alarm1_sec_0, alarm1_sec_1, alarm1_min_0, alarm1_min_1, -alarm1_hour_0, alarm1_hour_1; -unsigned char alarm2_min_0, alarm2_min_1, alarm2_hour_0, alarm2_hour_1; +// Declare variables for temperature +unsigned int temperature_lsb; +int temperature_msb; +unsigned char temp_sign; + +// Declare variables for splitting decimal digit to character +unsigned char temp_0; + +// Define MSB / LSB +#define LSB(x) ((x % 10) + '0') // x >> 4 +#define MSB(x) ((x / 10) + '0') // x & 0x0F #ifdef DEBUG #include // included for sprintf -// Initialize a variable to use as a buffer for sprintf +// Declare a variable to use as a buffer for sprintf char buf[40]; #endif +void display_Digit(unsigned int); void display_Intro(void); void display_Lcd_Layout(void); -// Format unsigned int to unsigned chars -void format_DateTimeChars(void); +void edit_Date_Time(void); // Format msb and lsb for temperature display void format_Temperature(void); -// Determine day of week from DS3231 -void getWeekDay(unsigned int); - // Determine Alarm status from control registers void Get_Alarm_Status(void); -- cgit v1.2.3-54-g00ecf