oneWire test

Dependencies:   BSP_DISCO_F746NG DS1820

Committer:
JohnnyK
Date:
Wed Apr 15 17:35:43 2020 +0000
Revision:
1:980bb838fa61
Parent:
0:0f48b5222d3d
First

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JohnnyK 0:0f48b5222d3d 1 #include "mbed.h"
JohnnyK 0:0f48b5222d3d 2 #include "stm32746g_discovery_lcd.h"
JohnnyK 0:0f48b5222d3d 3 #include "DS1820.h"
JohnnyK 0:0f48b5222d3d 4
JohnnyK 0:0f48b5222d3d 5 #define MAX_SENSOSRS 1
JohnnyK 0:0f48b5222d3d 6
JohnnyK 0:0f48b5222d3d 7 Thread thread;
JohnnyK 0:0f48b5222d3d 8 volatile float temp = 0;
JohnnyK 0:0f48b5222d3d 9
JohnnyK 0:0f48b5222d3d 10 void oneWireThread() {
JohnnyK 0:0f48b5222d3d 11 DS1820* ds1820[MAX_SENSOSRS];
JohnnyK 0:0f48b5222d3d 12 DigitalOut led(LED1);
JohnnyK 0:0f48b5222d3d 13 OneWire oneWire(D8);
JohnnyK 0:0f48b5222d3d 14 int sensorsFound = 0;
JohnnyK 0:0f48b5222d3d 15
JohnnyK 0:0f48b5222d3d 16 printf("OneWireThread Starting...\n");
JohnnyK 0:0f48b5222d3d 17
JohnnyK 0:0f48b5222d3d 18 for (sensorsFound = 0; sensorsFound < MAX_SENSOSRS; sensorsFound++) {
JohnnyK 0:0f48b5222d3d 19 ds1820[sensorsFound] = new DS1820(&oneWire);
JohnnyK 0:0f48b5222d3d 20 if (!ds1820[sensorsFound]->begin()) {
JohnnyK 0:0f48b5222d3d 21 delete ds1820[sensorsFound];
JohnnyK 0:0f48b5222d3d 22 break;
JohnnyK 0:0f48b5222d3d 23 }
JohnnyK 0:0f48b5222d3d 24 }
JohnnyK 0:0f48b5222d3d 25
JohnnyK 0:0f48b5222d3d 26 if(sensorsFound != 0) {
JohnnyK 0:0f48b5222d3d 27 printf("Found %d DS1820 sensors.\n", sensorsFound);
JohnnyK 0:0f48b5222d3d 28 while (1) {
JohnnyK 0:0f48b5222d3d 29 printf("----------------\r\n");
JohnnyK 0:0f48b5222d3d 30 for (int i = 0; i < sensorsFound; i++)
JohnnyK 0:0f48b5222d3d 31 ds1820[i]->startConversion(); // start temperature conversion from analog to digital
JohnnyK 0:0f48b5222d3d 32 ThisThread::sleep_for(1000); // let DS1820 sensors complete the temperature conversion
JohnnyK 0:0f48b5222d3d 33 for (int i = 0; i < sensorsFound; i++) {
JohnnyK 0:0f48b5222d3d 34 if (ds1820[i]->isPresent())
JohnnyK 0:0f48b5222d3d 35 temp = ds1820[i]->read();
JohnnyK 0:0f48b5222d3d 36 printf("temp[%d] = %3.1f%cC\r\n", i, temp, 176); // read temperature
JohnnyK 0:0f48b5222d3d 37 }
JohnnyK 0:0f48b5222d3d 38 ThisThread::sleep_for(5000);
JohnnyK 0:0f48b5222d3d 39 }
JohnnyK 0:0f48b5222d3d 40 }else{
JohnnyK 0:0f48b5222d3d 41 printf("No DS1820 sensor found!\r\n");
JohnnyK 0:0f48b5222d3d 42 }
JohnnyK 0:0f48b5222d3d 43 }
JohnnyK 0:0f48b5222d3d 44
JohnnyK 0:0f48b5222d3d 45 int main()
JohnnyK 0:0f48b5222d3d 46 {
JohnnyK 0:0f48b5222d3d 47 printf("Starting\n");
JohnnyK 0:0f48b5222d3d 48 thread.start(oneWireThread);
JohnnyK 0:0f48b5222d3d 49 char buffer[20]= {0};
JohnnyK 0:0f48b5222d3d 50 HAL_Delay(500);
JohnnyK 0:0f48b5222d3d 51 BSP_LCD_Init();
JohnnyK 0:0f48b5222d3d 52 BSP_LCD_LayerDefaultInit(LTDC_ACTIVE_LAYER, LCD_FB_START_ADDRESS);
JohnnyK 0:0f48b5222d3d 53 BSP_LCD_SelectLayer(LTDC_ACTIVE_LAYER);
JohnnyK 0:0f48b5222d3d 54 BSP_LCD_SetFont(&LCD_DEFAULT_FONT);
JohnnyK 0:0f48b5222d3d 55 HAL_Delay(500);
JohnnyK 0:0f48b5222d3d 56
JohnnyK 0:0f48b5222d3d 57 while(1){
JohnnyK 0:0f48b5222d3d 58 BSP_LCD_Clear(LCD_COLOR_BLACK);
JohnnyK 0:0f48b5222d3d 59 BSP_LCD_SetBackColor(LCD_COLOR_BLACK);
JohnnyK 0:0f48b5222d3d 60 BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
JohnnyK 0:0f48b5222d3d 61 BSP_LCD_DisplayStringAt(0, 1, (uint8_t *)"MBED", CENTER_MODE);
JohnnyK 0:0f48b5222d3d 62 BSP_LCD_SetTextColor(LCD_COLOR_YELLOW);
JohnnyK 0:0f48b5222d3d 63 BSP_LCD_DisplayStringAt(0, 100, (uint8_t *)"DISCOVERY STM32F746NG", CENTER_MODE);
JohnnyK 0:0f48b5222d3d 64 sprintf (buffer, "temp = %3.1fC", temp);
JohnnyK 0:0f48b5222d3d 65 BSP_LCD_DisplayStringAt(20, 175, (uint8_t *)buffer, LEFT_MODE);
JohnnyK 0:0f48b5222d3d 66 HAL_Delay(2000);//ThisThread::sleep_for(1000); //not working
JohnnyK 0:0f48b5222d3d 67
JohnnyK 0:0f48b5222d3d 68 }
JohnnyK 1:980bb838fa61 69 }