Example #1
0
// -----------------------------------------------------------------
int main() {
    // Initialize the lcd
    lcd_init();
    fdev_setup_stream(&lcd_stream, lcd_putchar, 0, _FDEV_SETUP_WRITE); 
    lcd_clear_and_home();
 
    // initialize eeprom and TWI/I2C
    eeprom_init();
 
    // specify 7 bit device address of eeprom chip
    #define EEPROM_DEVICE_ID 0b1010000
    fprintf_P(&lcd_stream, PSTR("24LC256 ADDR: %02X"), EEPROM_DEVICE_ID);
 
    // specify eeprom memory address for data storage
    #define EEPROM_DATA_ADDRESS 0x0000
 
    // create local copy of eeprom data
    EEPROM_DATA e_data;
 
    // read eeprom contents at location 0000
    e_data = read_eeprom(0);
 
    // show what we read from the eeprom -
    // note: the very first read on a new eeprom
    // will show uninitalized data
    show_eeprom_data(&e_data);
 
    // process data
    if(e_data.need_initalization){
        // set all data item values if first time
        e_data.need_initalization = false;
        strcpy_P(e_data.author, PSTR("Noter"));
        e_data.read_count = 1;
        e_data.brightness = 0x33;
        e_data.version = 1.01;
    } else {
        // check contents against the values written when initializing
        if((e_data.version != 1.01)||
            (strcmp_P(e_data.author, PSTR("Noter")) !=0)||
            (e_data.brightness != 0x33)){
                    lcd_line_four();
                    fprintf_P(&lcd_stream, PSTR("DATA ERROR - ")); 
                    while(true);    // and freeze
                } else {
                    // increment read_count 
                    e_data.read_count += 1;
                }
    }
 
    // write data to eeprom memory at location 0000
    write_eeprom(0, &e_data);
 
    // and show the read count
    lcd_line_two();
    fprintf_P(&lcd_stream, PSTR("READ COUNT = %d"), e_data.read_count);
 
    // done
    while(true);
}
Example #2
0
void getTemp() {
	uint16_t last_sample = 0;
	double this_temp;
	double temp_avg;
	uint16_t i;
	temp_avg = 0.0;

	for (i=0; i<500; i++) {
		last_sample = adc_read();
		this_temp = sampleToFahrenheit(last_sample);
		temp_avg = temp_avg + this_temp/500.0;
	}

	double c = fahrenheitToCelsius(temp_avg);
	double k = celsiusToKelvin(c);

	// write message to LCD
	lcd_init();
	FILE lcd_stream = FDEV_SETUP_STREAM(lcd_putchar, 0, _FDEV_SETUP_WRITE);
	lcd_home();
	lcd_write_string(PSTR("ADC: "));
	lcd_write_int16(last_sample);
	lcd_write_string(PSTR(" of 1024 "));
	lcd_line_two();

	fprintf_P(&lcd_stream, PSTR("%.2f"), temp_avg);
	lcd_write_data(0xdf);
	lcd_write_string(PSTR("F"));

	lcd_line_three();
	fprintf_P(&lcd_stream, PSTR("%.2f"), c);
	lcd_write_data(0xdf);
	lcd_write_string(PSTR("C"));

	lcd_line_four();
	fprintf_P(&lcd_stream, PSTR("%.2f"), k);
	lcd_write_data(0xdf);
	lcd_write_string(PSTR("K"));
}
Example #3
0
// optional callback function for TWI/I2C driver
void handle_TWI_result(uint8_t return_code){
    if(return_code!=TWI_success){
        lcd_line_four();
        fprintf_P(&lcd_stream, PSTR("I2C ERROR - %02X"), return_code); 
    }
}