
Code permettant d'interfacer le capteur MCP9808 et afficher le résultat ainsi qu'une petite application sur l'écran de la carte DISCO
Dependencies: mbed BSP_DISCO_F746NG
Diff: main.cpp
- Revision:
- 0:96f939ccb440
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Jun 29 20:45:04 2020 +0000 @@ -0,0 +1,200 @@ +#include "mbed.h" +#include "stm32746g_discovery_lcd.h" +#include "stm32746g_discovery_ts.h" +#include "logo.h" +#include <stdlib.h> + + + + +#define MCP9808_REG_TEMP (0x05) // Registre de température +#define MCP9808_REG_CONF (0x01) // Registre de configuration +#define MCP9808_ADDR (0x30) // adresse de base 0x18<<1 + + + +//uint16_t TS_StateTypeDef variable ; +//uint16_t TS_x = variable.touchX[0]; +//uint16_t TS_y = variable.touchY[0]; + +I2C i2c(D14,D15); +// sur la carte DISCO (SDA --> D14 ; SCL --> D15) + +DigitalOut myled(LED1); + +Serial pc(USBTX, USBRX,115200); // parametres de la liaison série pour affciher les résultats sur l'hype-terminal + + +char TempCelsiusDisplay[] = "+abc.dd C"; // chaine de caracteres de la valeur de température +int var = atoi(TempCelsiusDisplay); + + +// fonction pour afficher le logo de l'iut ajouté sur l'écran LCD +void drawImage(int offsetX, int offsetY) +{ + int x = 0; + int y = 0; + uint32_t* dataPtr = (uint32_t*)logo.data; + while(y < logo.height) { + while(x < logo.width) { + BSP_LCD_DrawPixel(x + offsetX, y + offsetY, *dataPtr); + dataPtr++; + x++; + } + x = 0; + y++; + } +} + + +int main() +{ + // initialisation de l'écran LCD + + uint8_t statu; + + BSP_LCD_Init(); + BSP_LCD_LayerDefaultInit(LTDC_ACTIVE_LAYER, LCD_FB_START_ADDRESS); + BSP_LCD_SelectLayer(LTDC_ACTIVE_LAYER); + + + statu = BSP_TS_Init(BSP_LCD_GetXSize(), BSP_LCD_GetYSize()); + if (statu != TS_OK) { + BSP_LCD_Clear(LCD_COLOR_BLUE); + BSP_LCD_SetBackColor(LCD_COLOR_RED); + BSP_LCD_SetTextColor(LCD_COLOR_GREEN); + + } else { + BSP_LCD_Clear(LCD_COLOR_GREEN); + BSP_LCD_SetBackColor(LCD_COLOR_GREEN); + BSP_LCD_SetTextColor(LCD_COLOR_WHITE); + + } + + /*BSP_LCD_SetTextColor(LCD_COLOR_RED); + BSP_LCD_FillRect(50, 200, 100,50); + BSP_LCD_SetTextColor(LCD_COLOR_WHITE); + //BSP_LCD_SetBackColor(LCD_COLOR_RED); + BSP_LCD_DisplayStringAt(-50, 140, (uint8_t *)"MIN", CENTER_MODE);*/ + + // Création du bouton "Kelvin" + + BSP_LCD_SetTextColor(LCD_COLOR_RED); + BSP_LCD_FillRect(150, 200, 120,50); + BSP_LCD_SetTextColor(LCD_COLOR_WHITE); + BSP_LCD_SetBackColor(LCD_COLOR_RED); + BSP_LCD_DisplayStringAt(-30, 215, (uint8_t *)"Kelvin", CENTER_MODE); + + + + + char data_write[3]; + char data_read[2]; + int tempval; + //int tempK ; + + //tempK = var + 273; + //char nbr = (char)tempK; + //wait(3); + + + data_write[0] = MCP9808_REG_CONF; + data_write[1] = 0x00; // config msb + data_write[2] = 0x00; // config lsb + + int status = i2c.write(MCP9808_ADDR, data_write, 3, 0); + if (status != 0) { // Error + + while (1) { + //drawImage(0,0); + + + myled = !myled; + wait(0.2); + } + } + + while (1) { + + + + drawImage(0,1); // emplacement du logo de l'iut sur l'écran LCD + + // Read temperature register + data_write[0] = MCP9808_REG_TEMP; + i2c.write(MCP9808_ADDR, data_write, 1, 1); // no stop + i2c.read(MCP9808_ADDR, data_read, 2, 0); + + + +// check Ta vs Tcrit + if((data_read[0] & 0x80) == 0x80) { + //pc.printf(" temp >= critique "); + } + if((data_read[0] & 0x40) == 0x40) { + //pc.printf(" temp > limite superieur "); + } + if((data_read[0] & 0x20) == 0x20) { + //pc.printf(" temp < limite inférieure "); + } + if(data_read[0] & 0xE0) { + //pc.printf("\r\n"); + data_read[0] = data_read[0] & 0x1F; // effacer les bits de drapeau + } + if((data_read[0] & 0x10) == 0x10) { + data_read[0] = data_read[0] & 0x0F; + TempCelsiusDisplay[0] = '-'; + tempval = 256 - (data_read[0] << 4) + (data_read[1] >> 4); + } else { + TempCelsiusDisplay[0] = '+'; + tempval = (data_read[0] << 4) + (data_read[1] >> 4); + } + + // partie fractionnaire (précision 0.25°C) + if (data_read[1] & 0x08) { + if(data_read[1] & 0x04) { + TempCelsiusDisplay[5] = '7'; + TempCelsiusDisplay[6] = '5'; + } else { + TempCelsiusDisplay[5] = '5'; + TempCelsiusDisplay[6] = '0'; + } + } else { + if(data_read[1] & 0x04) { + TempCelsiusDisplay[5] = '2'; + TempCelsiusDisplay[6] = '5'; + } else { + TempCelsiusDisplay[5] = '0'; + TempCelsiusDisplay[6] = '0'; + } + } + + // Partie entière + TempCelsiusDisplay[1] = (tempval / 100) + 0x30; + TempCelsiusDisplay[2] = ((tempval % 100) / 10) + 0x30; + TempCelsiusDisplay[3] = ((tempval % 100) % 10) + 0x30; + + // Affichage du résultat + pc.printf("temperature = %s\r\n", TempCelsiusDisplay); // affichage de la température sur l'hyper-terminal + //pc.printf("temperature = %s\r\n", nbr); + + HAL_Delay(1000); + BSP_LCD_SetFont(&Font12); + BSP_LCD_SetFont(&Font20); + //sprintf((char*)text, "temperature : %5.3f cm", TempCelsiusDisplay); + BSP_LCD_DisplayStringAt(0, 70, (uint8_t *) "MCP9808 : CAPTEUR DE TEMPERATURE", CENTER_MODE); //message de bienvenue + BSP_LCD_DisplayStringAt(135, 100, (uint8_t *) TempCelsiusDisplay, CENTER_MODE); //affichage de la valeur de température sur l'écran LCD + //BSP_LCD_DisplayStringAt(135, 120, (uint8_t *) nbr, CENTER_MODE); + + + + //pc.printf("temperature= %i", tempval); + myled = !myled; + wait(1.0); + } + +} + + + +