aboutsummaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rwxr-xr-xmain.c64
1 files changed, 43 insertions, 21 deletions
diff --git a/main.c b/main.c
index 87a53d0..72f3761 100755
--- a/main.c
+++ b/main.c
@@ -12,7 +12,21 @@
#include "term.h" //Declarations for Escape codes
#endif
+unsigned int Adc_Read(unsigned int channel) {
+ ADFM = 1; // results right justified
+ ADCS0 = 1; //conversion speed = 64*Tosc
+ ADCS1 = 1;
+ ADCS2 = 1;
+ ADCON0bits.CHS = channel;
+ ADON = 1; // Turn on ADC
+ __delay_ms(1);
+ GO_DONE = 1;
+ while(ADCON0bits.GO_DONE == 1);
+ return((ADRESH << 8) + ADRESL); // ex 1024
+}
+
int main() {
+ TRISA0 = 0x01; // RA is input (ADC)
TRISC = 0x00; // Set PORTC as outputs
PORTC = 0x00; // Set all PORTC pins LOW
@@ -30,7 +44,7 @@ int main() {
I2C_Master_Init(100000); // Init I2C Master with 100KHz Clock
#ifdef DEBUG
- UART_send_string(CURSOR(13, 24)); // Set cursor at line 12, column 23
+ UART_send_string(CURSOR(13, 23)); // Set cursor at line 13, column 23
UART_send_string(GREEN); // Set Green
UART_send_string("I2C Module Initialized and active\r\n");
UART_send_string(CLRATTR); // No color
@@ -38,7 +52,7 @@ int main() {
Lcd_Init();
#ifdef DEBUG
- UART_send_string(CURSOR(14, 24)); // Set cursor at line 12, column 23
+ UART_send_string(CURSOR(14, 23)); // Set cursor at line 14, column 23
UART_send_string(GREEN); // Set Green
UART_send_string("LCD Module Initialized and active\r\n");
UART_send_string(CLRATTR); // No color
@@ -52,7 +66,6 @@ int main() {
display_Lcd_Layout();
// Set an initial date time
- //Set_Time_Date();
//Set_Time();
//Set_Date();
//Set_DayOfWeek();
@@ -70,40 +83,40 @@ int main() {
#ifdef VFD
Vfd_Set_Brightness(3); //Max brightness 3 - minimum
#endif
-
+
while (1) {
// Set time and date variables from DS3231
-#ifdef DEBUG
- UART_send_string("Updating current date time from DS3231\r\n");
-#endif
+ //#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
+ //#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
+ //#ifdef DEBUG
+ // UART_send_string("Formatting chars");
+ //#endif
format_DateTimeChars();
// Format temperature
-#ifdef DEBUG
- UART_send_string("Formating temperature");
-#endif
+ //#ifdef DEBUG
+ // UART_send_string("Formating temperature");
+ //#endif
format_Temperature();
// Determine day of the week
-#ifdef DEBUG
- UART_send_string("Determining day of week");
-#endif
+ //#ifdef DEBUG
+ // UART_send_string("Determining day of week");
+ //#endif
getWeekDay(dayOfWeek);
// Enter loop and update display if sec changes from DS3231
@@ -114,6 +127,14 @@ int main() {
// Set temp_sec to current sec from DS3231
sec_chg = sec;
+ // Get current LDR reading
+ ldr = Adc_Read(0);
+
+ if (ldr_chg != ldr) {
+ Vfd_Set_Brightness(1024 / ldr);
+ ldr_chg = ldr;
+ }
+
#ifdef DEBUG
UART_send_string(CURSOR(19, 1));
sprintf(buf, "%sTIME:%s\t%02d:%02d:%02d\r\n", YELLOW, CLRATTR,
@@ -130,8 +151,9 @@ int main() {
sprintf(buf, "%sWKDAY:%s\t%d\t\t%sDAY:%s\t%s\r\n", YELLOW, CLRATTR,
dayOfWeek, YELLOW, CLRATTR, weekday);
UART_send_string(buf);
- sprintf(buf, "%sTEMP:\t%s%c%c%c.%c%cC%s", YELLOW, CLRATTR,
- temp_sign, temp_2, temp_1, temp_0, 0xB0, CLRATTR);
+ sprintf(buf, "%sTEMP:\t%s%c%c%c.%c%cC%s\t\t%sLDR:%s\t\%d\r\n",
+ YELLOW, CLRATTR, temp_sign, temp_2, temp_1, temp_0, 0xB0,
+ CLRATTR, YELLOW, CLRATTR, ldr);
UART_send_string(buf);
#endif
}