diff options
author | William Harrington <kb0iic@berzerkula.org> | 2019-07-28 16:46:34 -0500 |
---|---|---|
committer | William Harrington <kb0iic@berzerkula.org> | 2019-07-28 16:46:34 -0500 |
commit | f8a22ef73eb409d460cf4c4c5948b97ca41026a9 (patch) | |
tree | 1cdaec04e4e3a6c3d34db942344ec60f78a646f6 | |
parent | d9c1b81f7f28f0b066cae716dcff0a5d511b7fc6 (diff) |
Add ADC for LDR to change brightness on ambient light.
-rwxr-xr-x | conf.h | 7 | ||||
-rwxr-xr-x | main.c | 64 | ||||
-rwxr-xr-x | main.h | 15 |
3 files changed, 59 insertions, 27 deletions
@@ -14,7 +14,7 @@ // Use internal crystal at 8MHz
#define _XTAL_FREQ 20000000
-//#define DEBUG 1
+#define DEBUG 1
// Define baud rate of 9600
#define Baud_rate 9600
@@ -36,4 +36,7 @@ #define INCR RE2
//Define Buzz
-#define BEEP RD1
\ No newline at end of file +#define BEEP RD1
+
+//Define LDR
+#define LDR RA0
\ No newline at end of file @@ -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
}
@@ -1,9 +1,13 @@ +// Initialize variable for LDR
+unsigned int ldr = 0;
+unsigned int ldr_chg = 0;
+
// Initialize variables for date time and alarms
-unsigned int sec = 30;
-unsigned int min = 42;
-unsigned int hour = 21;
+unsigned int sec = 0;
+unsigned int min = 45;
+unsigned int hour = 16;
unsigned int dayOfWeek = 1;
-unsigned int date = 27;
+unsigned int date = 28;
unsigned int month = 7;
unsigned int year = 19;
unsigned int century = 20;
@@ -40,6 +44,9 @@ unsigned char alarm2_min_0, alarm2_min_1, alarm2_hour_0, alarm2_hour_1; char buf[40];
#endif
+// Get ADC value
+unsigned int Adc_Read(unsigned int);
+
// Format unsigned int to unsigned chars
void format_DateTimeChars(void);
|