#ifndef F_CPU #define F_CPU 16000000UL // telling controller crystal frequency (16 MHz AVR ATMega328P) #endif // SPI INTERFACE DEFINES #define MOSI 3 // MOSI it's PORT B, PIN 3 #define MISO 4 // MISO it's PORT B, PIN 4 #define SCK 5 // SCK it's PORT B, PIN 5 #define SS 2 // SS it's PORT B, PIN 2 // RESET THE DISPLAY #define RST 0 // RESET it's PORT B, PIN 0 //DISPLAY MODE SELECT - Input to select either command/address or data input. #define DC 1 // DC it's PORT B, PIN 1 // codes array of negative sign static const unsigned char neg[4] = {0x30,0x30,0x30,0x30}; // codes array of digits [0..9] static const unsigned char font6x8[10][16] = { { 0xFC, 0xFE, 0xFE, 0x06, 0x06, 0xFE, 0xFE, 0xFC, 0x01, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x01}, // 0 { 0x00, 0x00, 0x18, 0x1C, 0xFE, 0xFE, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x01, 0x00}, // 1 { 0x0C, 0x8E, 0xCE, 0xE6, 0xE6, 0xBE, 0x9E, 0x0C, 0x01, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x01}, // 2 { 0x00, 0x04, 0x06, 0x26, 0x76, 0xFE, 0xDE, 0x8C, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x03, 0x01}, // 3 { 0x3C, 0x3E, 0x7C, 0x60, 0x60, 0xFC, 0xFE, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x01}, // 4 { 0x1C, 0x3E, 0x3E, 0x36, 0x36, 0xF6, 0xF6, 0xE4, 0x01, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x01}, // 5 { 0xFC, 0xFE, 0xFE, 0x36, 0x36, 0xF6, 0xF6, 0xE4, 0x01, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x01}, // 6 { 0x04, 0x06, 0x06, 0x86, 0xE6, 0xFE, 0x7E, 0x1C, 0x00, 0x00, 0x00, 0x01, 0x03, 0x01, 0x00, 0x00}, // 7 { 0xCC, 0xFE, 0xFE, 0x36, 0x36, 0xFE, 0xFE, 0xCC, 0x01, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x01}, // 8 { 0x3C, 0x7E, 0x7E, 0x66, 0x66, 0xFE, 0xFE, 0xFC, 0x01, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x01} // 9 }; // codes array of word "TEMP:" static const unsigned char TEMP_1[165] = { 0x02,0x06,0x06,0xFE,0xFE,0xFE,0x06,0x06,0x02,0x00,0xFC,0xFE,0xFE,0x26,0x26,0x24,0x00,0xFC,0xFE,0xFE,0x1C,0x38,0x70,0x38,0x1C,0xFE,0xFE,0xFC, 0x00,0xFC,0xFE,0xFE,0x66,0x66,0x7E,0x7E,0x3C,0x00,0x8C,0x8C,0x00,0x00,0x00,0x01,0x03,0x01,0x00,0x00,0x00,0x00,0x01,0x03,0x03,0x03,0x03,0x01,0x00,0x01,0x03,0x01,0x00,0x00,0x00,0x00,0x00,0x01,0x03, 0x01,0x00,0x01,0x03,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x0C,0x1E,0x33,0x33,0x1E,0x0C,0x00,0xF8,0xFC,0x0C,0x9C,0x98,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x03,0x03,0x01, }; // codes array of word "LUX:" const unsigned char TEMP_2[60] = { 0xFC,0xFE,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0xFC,0xFE,0xFC,0x00,0x00,0xFC,0xFE,0xFC,0x00,0x04,0x8E,0xDE,0xFC,0xF8,0xFC,0xDE,0x8E,0x04,0x00,0x8C,0x8C, 0x01,0x03,0x03,0x03,0x03,0x03,0x03,0x01,0x00,0x01,0x03,0x03,0x03,0x03,0x03,0x03,0x01,0x00,0x01,0x03,0x03,0x01,0x00,0x01,0x03,0x03,0x01,0x00,0x01,0x01 }; #include #include #include // Port Initialization void Port_Init() { DDRB = (1<0) {SPDR = 0x30;} //Load data into the buffer of display (display negative sign) else {SPDR = 0x00;} //Load data into the buffer of display (clear negative sign) while(!(SPSR & (1<> 1; // Next bit if(PIND & (1 << 2)) data |= 0x80; // put bit into byte _delay_us(62); } return data; } // Function to write byte to DS18B20 void write_18b20(unsigned char data) { unsigned char i; for(i = 0; i < 8; i++) { DDRD |= (1 << 2); // Set PD2 pin of the PORTD as output _delay_us(2); // Timing if(data & 0x01) DDRD &= ~(1 << 2); //if we want to write 1, release the line else DDRD |= (1 << 2); // if not will keep low data = data >> 1; // Next bit _delay_us(62); // Timing DDRD &= ~(1 << 2); // Set PD2 pin of the PORTD as input _delay_us(2); } } // Function to display light level void Read_Lux() { uint16_t buffer; unsigned int temp_int_1,temp_int_2,temp_int_3,temp_int_0; // single digits, double digits, triple digits, quarter digits buffer = get_LightLevel(); // read the result of analog to digital convert light level temp_int_0 = buffer % 10000 / 1000; // quarter-digit temp_int_1 = buffer % 1000 / 100; // triple-digit temp_int_2 = buffer % 100 / 10; // double-digit temp_int_3 = buffer % 10; // single-digit if(temp_int_0 > 0) // if result is quarter-digit number { Display_Dig(temp_int_0,32,2); // display 1 digit of light level Display_Dig(temp_int_1,41,2); // display 2 digit of light level Display_Dig(temp_int_2,50,2); // display 3 digit of light level Display_Dig(temp_int_3,59,2); // display 4 digit of light level } else { if(temp_int_1 > 0) // if result is triple-digit number { Off_Dig(32,2); // clear 1 sign of number Display_Dig(temp_int_1,41,2); // display 1 digit of light level Display_Dig(temp_int_2,50,2); // display 2 digit of light level Display_Dig(temp_int_3,59,2); // display 3 digit of light level } else { if(temp_int_2 > 0) // if result is double-digit number { Off_Dig(32,2); // clear 1 sign of number Off_Dig(41,2); // clear 2 sign of number Display_Dig(temp_int_2,50,2); // display 1 digit of light level Display_Dig(temp_int_3,59,2); // display 2 digit of light level } else // if result is single-digit number { Off_Dig(32,2); // clear 1 sign of number Off_Dig(41,2); // clear 2 sign of number Off_Dig(50,2); // clear 3 sign of number Display_Dig(temp_int_3,59,2); // display 1 digit of light level } } } } // Function to display temperature void Read_Temp() { unsigned int buffer; unsigned int temp_int_1,temp_int_2,temp_int_3; // single digits, double digits, triple digits, quarter digits unsigned char Temp_H, Temp_L, OK_Flag, temp_flag; DS18B20_init(); // Initialization of DS18B20 write_18b20(0xCC); // Sensor code check write_18b20(0x44); // Start temperature conversion _delay_ms(1000); // Sensor polling delay DS18B20_init(); // Initialization of DS18B20 write_18b20(0xCC); // Sensor code check write_18b20(0xBE); // Command to read the contents of the Sensor RAM Temp_L = read_18b20(); // Read first two byte Temp_H = read_18b20(); temp_flag = 1; // 1-positive temperature, 0-negative temperature // Get negative temperature if(Temp_H &(1 << 3)) // Sign Bit Check (if bit is set - negative temperature) { signed int temp; temp_flag = 0; // flag is set 0 - negative temperature temp = (Temp_H << 8)|Temp_L; temp = -temp; // Convert the additional code in direct Temp_L = temp; Temp_H = temp >> 8; } buffer = ((Temp_H << 4) & 0x70)|(Temp_L >> 4); temp_int_1 = buffer % 1000 / 100; // triple-digit temp_int_2 = buffer % 100 / 10; // double-digit temp_int_3 = buffer % 10; // single-digit // If temperature is negative display sign of temperature, else clear if(temp_flag == 0) {Display_Neg(1);} else {Display_Neg(0);} if(temp_int_1 > 0) // if result is triple-digit number { Display_Dig(temp_int_1,45,0); // display 1 digit of temperature Display_Dig(temp_int_2,54,0); // display 2 digit of temperature Display_Dig(temp_int_3,63,0); // display 3 digit of temperature } else { if(temp_int_2 > 0) // if result is double-digit number { Off_Dig(45,0); // clear 1 sign of number Display_Dig(temp_int_2,54,0); // display 1 digit of temperature Display_Dig(temp_int_3,63,0); // display 2 digit of temperature } else // if result is single-digit number { Off_Dig(45,0); // clear 1 sign of number Off_Dig(54,0); // clear 2 sign of number Display_Dig(temp_int_3,63,0); // display 1 digit of temperature } } } // This ISR is fired whenever a match occurs of timer count with compare value (every 1 second) ISR (TIMER1_COMPA_vect) { // Reading, displaying of temperature and light level Read_Temp(); Read_Lux(); } // Function to display words "TEMP" and "LUX" void Display_label() { // Word "TEMP" Display_SetXY(0,0); // Set the address of position on display (up row) for (int index=0; index<105; index++) { if (index==40){Display_SetXY(0,1);} // Set the address of position on display (bottom row) if (index==80){Display_SetXY(72,0);} // Set the address of position on display (up row) if (index==92){Display_SetXY(72,1);} // Set the address of position on display (bottom row) SPDR = TEMP_1[index]; // Load codes array data into the buffer of display while(!(SPSR & (1<