oneWire test
Dependencies: BSP_DISCO_F746NG DS1820
main.cpp
00001 #include "mbed.h" 00002 #include "stm32746g_discovery_lcd.h" 00003 #include "DS1820.h" 00004 00005 #define MAX_SENSOSRS 1 00006 00007 Thread thread; 00008 volatile float temp = 0; 00009 00010 void oneWireThread() { 00011 DS1820* ds1820[MAX_SENSOSRS]; 00012 DigitalOut led(LED1); 00013 OneWire oneWire(D8); 00014 int sensorsFound = 0; 00015 00016 printf("OneWireThread Starting...\n"); 00017 00018 for (sensorsFound = 0; sensorsFound < MAX_SENSOSRS; sensorsFound++) { 00019 ds1820[sensorsFound] = new DS1820(&oneWire); 00020 if (!ds1820[sensorsFound]->begin()) { 00021 delete ds1820[sensorsFound]; 00022 break; 00023 } 00024 } 00025 00026 if(sensorsFound != 0) { 00027 printf("Found %d DS1820 sensors.\n", sensorsFound); 00028 while (1) { 00029 printf("----------------\r\n"); 00030 for (int i = 0; i < sensorsFound; i++) 00031 ds1820[i]->startConversion(); // start temperature conversion from analog to digital 00032 ThisThread::sleep_for(1000); // let DS1820 sensors complete the temperature conversion 00033 for (int i = 0; i < sensorsFound; i++) { 00034 if (ds1820[i]->isPresent()) 00035 temp = ds1820[i]->read(); 00036 printf("temp[%d] = %3.1f%cC\r\n", i, temp, 176); // read temperature 00037 } 00038 ThisThread::sleep_for(5000); 00039 } 00040 }else{ 00041 printf("No DS1820 sensor found!\r\n"); 00042 } 00043 } 00044 00045 int main() 00046 { 00047 printf("Starting\n"); 00048 thread.start(oneWireThread); 00049 char buffer[20]= {0}; 00050 HAL_Delay(500); 00051 BSP_LCD_Init(); 00052 BSP_LCD_LayerDefaultInit(LTDC_ACTIVE_LAYER, LCD_FB_START_ADDRESS); 00053 BSP_LCD_SelectLayer(LTDC_ACTIVE_LAYER); 00054 BSP_LCD_SetFont(&LCD_DEFAULT_FONT); 00055 HAL_Delay(500); 00056 00057 while(1){ 00058 BSP_LCD_Clear(LCD_COLOR_BLACK); 00059 BSP_LCD_SetBackColor(LCD_COLOR_BLACK); 00060 BSP_LCD_SetTextColor(LCD_COLOR_WHITE); 00061 BSP_LCD_DisplayStringAt(0, 1, (uint8_t *)"MBED", CENTER_MODE); 00062 BSP_LCD_SetTextColor(LCD_COLOR_YELLOW); 00063 BSP_LCD_DisplayStringAt(0, 100, (uint8_t *)"DISCOVERY STM32F746NG", CENTER_MODE); 00064 sprintf (buffer, "temp = %3.1fC", temp); 00065 BSP_LCD_DisplayStringAt(20, 175, (uint8_t *)buffer, LEFT_MODE); 00066 HAL_Delay(2000);//ThisThread::sleep_for(1000); //not working 00067 00068 } 00069 }
Generated on Tue Jul 12 2022 17:41:11 by 1.7.2