/* ANY-LICENSE V1.0 ---------------- You can use these files under any license approved by the Open Source Initiative, preferrably one of the popular licenses, as long as the license you choose is compatible to the dependencies of these files. See http://www.opensource.org/licenses/ for a list of approved licenses. Author: Martin Furter Project: Tins AVR Lib Repository: http://repos.borg.ch/projects/tins_avr_lib/trunk Copyright: 2016 */ /* ATMEGA48: DIL 2 PD0 RXD -> P2-15 3 PD1 TXD -> P2-16 27 PC4 SDA 28 PC5 SCL avrdude -c usbasp -p m168 -U flash:w:temp2ser.srec:s */ #include #include #include #include #include #include #include #define LM75_ADDR 0 #define BAUD_RATE 9600 void flash_print( const char* PROGMEM str ) { // static uint8_t i = 0; usart_add_progstr( str ); usart_send_busywait(); /* usart_add_byte( ' ' ); usart_add_byte( '=' ); usart_add_hexbyte( ++i ); usart_add_byte( '\r' ); usart_add_byte( '\n' ); usart_send_busywait(); */ } void i2c_error( uint8_t x ) { static const char str_i2c_error[] PROGMEM = "I2C ERROR\r\n"; flash_print( str_i2c_error ); usart_add_byte( 'e' ); usart_add_byte( 'x' ); usart_add_byte( 'p' ); usart_add_byte( ' ' ); usart_add_hexbyte( i2c_exp_status ); usart_add_byte( '\r' ); usart_add_byte( '\n' ); usart_send_busywait(); usart_add_byte( 'g' ); usart_add_byte( 'o' ); usart_add_byte( 't' ); usart_add_byte( ' ' ); usart_add_hexbyte( i2c_last_status ); usart_add_byte( '\r' ); usart_add_byte( '\n' ); usart_send_busywait(); /* usart_add_byte( '*' ); usart_add_byte( '\r' ); usart_add_byte( '\n' ); */ while( x ) { usart_send(); } } int main() { int16_t temp; usart_init( USART_BAUD_RATE( BAUD_RATE ) ); static const char str_hello[] PROGMEM = "temp2ser V0.1\r\n"; flash_print( str_hello ); static const char str_I2C_init[] PROGMEM = "I2C init\r\n"; flash_print( str_I2C_init ); i2c_init(); static const char str_set_config[] PROGMEM = "set config\r\n"; flash_print( str_set_config ); if( !lm75_set_config( LM75_ADDR, LM75_CFG_NUM_FAULT_6 ) ) { i2c_error( 1 ); } static const char str_set_os[] PROGMEM = "set os\r\n"; flash_print( str_set_os ); if( !lm75_set_t_os( LM75_ADDR, ((int16_t)30)<<8 ) ) { i2c_error( 1 ); } static const char str_set_hyst[] PROGMEM = "set hyst\r\n"; flash_print( str_set_hyst ); if( !lm75_set_t_hyst( LM75_ADDR, ((int16_t)27)<<8 ) ) { i2c_error( 1 ); } static const char str_read_temp[] PROGMEM = "read temp\r\n"; flash_print( str_read_temp ); if( !lm75_read_temp( LM75_ADDR, &temp ) ) { i2c_error( 0 ); } static const char str_mainloop[] PROGMEM = "mainloop\r\n"; flash_print( str_mainloop ); while( true ) { usart_send_busywait(); _delay_ms( 1000 ); // if( lm75_read_temp_quick( LM75_ADDR, &temp ) ) if( !lm75_read_temp( LM75_ADDR, &temp ) ) { i2c_error( 0 ); } if( 1 ) { int8_t th; uint8_t tl; char str[5]; th = temp >> 8; tl = temp; if( th < 0 ) { usart_add_byte( '-' ); th = -th; } else { usart_add_byte( '+' ); } uint8_to_string( th, str ); usart_add_byte( str[0] ); usart_add_byte( str[1] ); usart_add_byte( str[2] ); usart_add_byte( '.' ); usart_add_byte( tl & 0x80 ? '5' : '0' ); } else { usart_add_byte( 'E' ); usart_add_byte( 'R' ); usart_add_byte( 'R' ); } usart_add_byte( '\r' ); usart_add_byte( '\n' ); } }