#include #include #include #define F_CPU 20000000UL //20 MHz #include NO LONGER USED /* 7-Segment display: */ #define NUM_DIGITS 3U //3 Digits #define DIGIT_TENS 2U //left #define DIGIT_ONES 1U //middle #define DIGIT_UNIT 0U //right #define SELECT_DIGIT(digit) (1U << digit) /* DS18B20 peripherals: */ #define DS18B20_PORT PORTC #define DS18B20_DDR DDRC #define DS18B20_PIN PINC #define DS18B20_DQ PINC3 /* DS18B20 commands: */ #define DS18B20_CMD_CONVERTTEMP 0x44U #define DS18B20_CMD_RSCRATCHPAD 0xBEU #define DS18B20_CMD_WSCRATCHPAD 0x4EU #define DS18B20_CMD_CPYSCRATCHPAD 0x48U #define DS18B20_CMD_RECEEPROM 0xB8U #define DS18B20_CMD_RPWRSUPPLY 0xB4U #define DS18B20_CMD_SEARCHROM 0xF0U #define DS18B20_CMD_READROM 0x33U #define DS18B20_CMD_MATCHROM 0x55U #define DS18B20_CMD_SKIPROM 0xCCU #define DS18B20_CMD_ALARMSEARCH 0xECU /* Symbol indexes: */ #define SYM_n 10U #define SYM_o 11U #define SYM_E 12U #define SYM_R 13U #define SYM_DEG_CELCIUS 14U #define SYM_P_P 15U #define SYM_OFF 16U #define SYM_DASH 17U /* 7-Segment Layout: A F B G E C D P Pin 7 6 5 4 3 2 1 0 Segment A F B E D C G P */ static uint8_t g_au8Symbols[18U] = { /* 00 */ 0b11111100U, //0 (ABCDEF) /* 01 */ 0b00100100U, //1 (BC) /* 02 */ 0b10111010U, //2 (ABDEG) /* 03 */ 0b10101110U, //3 (ABCDG) /* 04 */ 0b01100110U, //4 (BCFG) /* 05 */ 0b11001110U, //5 (ACDFG) /* 06 */ 0b11011110U, //6 (ACDEFG) /* 07 */ 0b10100100U, //7 (ABC) /* 08 */ 0b11111110U, //8 (ABCDEFG) /* 09 */ 0b11101110U, //9 (ABCDFG) /* 10 */ 0b00010110U, //n (CEG) SYM_n /* 11 */ 0b00011110U, //o (CDEG) SYM_o /* 12 */ 0b11011010U, //E (ADEFG) SYM_E /* 13 */ 0b00010010U, //r (EG) SYM_r /* 14 */ 0b10101101U, //degC (ABCDP) SYM_DEG_CELCIUS /* 15 */ 0b11110011U, //P. (ABEFGP) SYM_P_P /* 16 */ 0b00000000U, //OFF SYM_OFF /* 17 */ 0b00000010U //- (G) SYM_DASH }; uint8_t g_u8ISRDigitSelect = 0U; uint8_t g_au8Digits[3U] = { SYM_OFF, //unit SYM_OFF, //ones SYM_OFF //tens }; void vInit( void ); void vSetValue( uint8_t u8Value, uint8_t u8Unit ); void vSetDisplayDigits( uint8_t u8Tens, uint8_t u8Ones, uint8_t u8Unit ); uint8_t u8DS18B20_Reset( void ); void vDS18B20_WriteByte( uint8_t u8Byte ); void vDS18B20_WriteBit( uint8_t u8Bit ); uint8_t u8DS18B20_ReadByte( void ); uint8_t u8DS18B20_ReadBit( void ); /************************************************************************/ /* Timer1 ISR - show values on display */ /************************************************************************/ ISR ( TIMER1_COMPA_vect ) { PORTB = 0; PORTD = 0; _delay_us(2); PORTB = SELECT_DIGIT(g_u8ISRDigitSelect); PORTD = g_au8Symbols[g_au8Digits[g_u8ISRDigitSelect]]; g_u8ISRDigitSelect++; if (g_u8ISRDigitSelect > 2U) { g_u8ISRDigitSelect = 0U; } } /************************************************************************/ /* main() */ /************************************************************************/ int main(void) { uint8_t u8TempLo, u8TempHi; float f32Temperature; /* Init I/O: */ vInit(); /* Set default value: */ vSetDisplayDigits( SYM_DASH, SYM_DASH, SYM_DASH ); /* wait 3 seconds for DS18B20 sensor */ _delay_ms(2400); while (1) { (void) u8DS18B20_Reset(); vDS18B20_WriteByte( DS18B20_CMD_SKIPROM ); vDS18B20_WriteByte( DS18B20_CMD_CONVERTTEMP ); while ( !u8DS18B20_ReadBit() ); (void) u8DS18B20_Reset(); vDS18B20_WriteByte( DS18B20_CMD_SKIPROM ); vDS18B20_WriteByte( DS18B20_CMD_RSCRATCHPAD ); u8TempLo = u8DS18B20_ReadByte(); u8TempHi = u8DS18B20_ReadByte(); f32Temperature = ((u8TempHi << 8) + u8TempLo) * 0.0625; vSetValue( (uint8_t) f32Temperature, SYM_DEG_CELCIUS ); _delay_ms(500); } //end while(1) } /************************************************************************/ /* Initialize inputs and outputs */ /************************************************************************/ void vInit( void ) { /* Port D - Pins 0-7: Outputs */ DDRD = 0b11111111U; /* Port B - Pins 0-2: Outputs */ DDRB = 0b00000111U; /* 16-bit TIMER1 in CTC mode * prescaler = 8 * 20 MHz / 8 = 2.5 MHz * Compare value = 2500 => 1 ms IRQ */ TCCR1A = 0U; TCCR1B = (1<>= 1U; } return; //void } /************************************************************************/ /* Write single bit to DS18B20 (61 us) */ /************************************************************************/ void vDS18B20_WriteBit( uint8_t u8Bit ) { //low for 1 us DS18B20_PORT &= ~ (1U<>= 1U; n |= (u8DS18B20_ReadBit()<<7U); } return n; } /************************************************************************/ /* Read single bit from DS18B20 (60 us) */ /************************************************************************/ uint8_t u8DS18B20_ReadBit( void ) { uint8_t u8Bit = 0U; //low for 1 us DS18B20_PORT &= ~ (1U<