DS18B20 single temperature sensor (l152)

Name of the project

l152_ds18b20_single

Description

The project query a single DS18B20 Dallas temperature sensor over a 1-wire bus. If the sensor is present, the temperature and it’s ID will be shown on a 2×16 LCD. If not, error messages will be displayed on the second row of the LCD.

Code snippet (initial code, project structure and VSCode files generated by VPC):

const uint8_t ds_err1[] = "Err! No sensor! "; 
const uint8_t ds_err2[] = "Err! Bad data!  "; 
const uint8_t ds_temp[] = "Temp="; 

void ds18b20_error(uint8_t err){
  lcd_cursor_position(1,0);
  if (err == DS18B20_BUS_ERROR){
    lcd_write_strF(ds_err2);
  }else if (err == DS18B20_NO_SENSOR) {
    lcd_write_strF(ds_err1);
  }
  while(1) {
    GPIO_ToggleBits(LD2_GPIO_Port, LD2_Pin);
    my_delay_ms(50);
  }
}
 
  
int main(void){
  /* local variables */
  uint8_t hx[3];
  uint8_t st;
  int16_t ts01_temperature;
  uint8_t i;
  
  /* mandatory system initializations */
  vpc_system_init();
  //vpc_rtc_init(); /* enable if required */  
  vpc_gpio_init();
  //vpc_usart2_uart_init(); /* enable if required */ 
  /* third-party initializations */
  lcd_init(LCD_HD44780);
  
  /* do your own initializations below */
  my_delay_ms(1000);
  lcd_cursor_position(0, 0);
  lcd_write_strF(ds_temp);
  /* init the sensor... */
  st = ow_reset();
  if (st) {
    ds18b20_error(st);
  }
  
  while(1){
    /* your forever repeating code */
    ds18b20_convert();
    my_delay_ms(1000);
    st = ds18b20_s_temp(&ts01_temperature, CELSIUS);
    if (st == DS18B20_OK) {
      lcd_cursor_position(0, 5);
      ds18b20_temp_format(ts01_temperature);
      lcd_write_str(ds18b20_formated_temp);
    }else ds18b20_error(st);
    st = ds18b20_s_read_rom();
    if (st == DS18B20_OK) {
      lcd_cursor_position(1, 0);
      for (i = 0; i < 8; i++) {
        byte2hex(ds18b20_scratchpad[i], &hx[0]);
        lcd_write_str(hx);
      }
    }else ds18b20_error(st);
    GPIO_ToggleBits(LD2_GPIO_Port, LD2_Pin);
    
  }
  return 0;
}

Project published in repository.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.