admin 发表于 2020-1-18 13:03:46

DIY数字仪表:G0板开发:读取传感器数据

创建工程










00:00 / 02:08



流畅
1x









主程序主要代码main.c
MX_GPIO_Init();MX_USART2_UART_Init();//G0和F7通信MX_USART1_UART_Init();MX_USART3_UART_Init();MX_USART4_UART_Init();//PrintfMX_I2C1_Init();APP_Init(); while (1){      tmp = HAL_GetTick();      if(tmp - tick > 1000)      {                tick = tmp;                APP_Process();                printf("APP_Process\n\r");      }}读取传感器数据STM32CubeMX配置I2C外设
https://www.stmcu.com.cn/upload/20200103/169.pnghttps://www.stmcu.com.cn/upload/20200103/170.png主要代码sht30hi2c.c
/* Includes ------------------------------------------------------------------*/#include "i2c.h"#include "sht30.h"extern I2C_HandleTypeDef hi2c1;typedef enum{SHT30_OK       = 0x00U,SHT30_ERROR    = 0x01U,} SHT30_StatusTypeDef;//======================================I2Cx redefine start===================================static void I2Cx_Error(uint8_t Addr){/* De-initialize the I2C communication bus */HAL_I2C_DeInit(&hi2c1);    /* Re-Initialize the I2C communication bus */MX_I2C1_Init();} static void SHT3X_WriteCMD(unsigned short cmd){      uint8_t buf;      buf=cmd>>8;      buf=cmd;      HAL_StatusTypeDef status = HAL_OK;      status = HAL_I2C_Master_Transmit(&hi2c1, SHT3X_WR_ADDR, buf, 2, 1000);      if(status != HAL_OK)      {                /* I2C error occurred */               I2Cx_Error(SHT3X_WR_ADDR);      }}static int SHT3X_ReadData(unsigned short cmd, unsigned char *buf, unsigned short len){      HAL_StatusTypeDef status = HAL_OK;      status = HAL_I2C_Mem_Read(&hi2c1, SHT3X_WR_ADDR, cmd, I2C_MEMADD_SIZE_16BIT, buf, len, 1000);      if(status != HAL_OK)      {                /* I2C error occurred */               I2Cx_Error(SHT3X_WR_ADDR);      }      return SHT30_OK;}static unsigned char SHT3X_CalcCrc(unsigned char *crcdata, unsigned char nbrOfBytes){    unsigned char Bit;      // bit mask    unsigned char crc = 0xFF; // calculated checksum    unsigned char byteCtr;    // byte counter      // calculates 8-Bit checksum with given polynomial   for(byteCtr = 0; byteCtr < nbrOfBytes; byteCtr++)    {      crc ^= (crcdata);      for(Bit = 8; Bit > 0; --Bit)      {             if(crc & 0x80) crc = (crc << 1) ^ POLYNOMIAL;            else         crc = (crc << 1);      }    }    return crc;} void SHT3X_Init(void){      HAL_Delay(20);    SHT3X_WriteCMD(CMD_MEAS_PERI_10_H); } int SHT3X_GetTempAndHumi(short *temp, short *humi){    unsigned long intrawValueTemp;    // temperature raw value from sensor    unsigned long intrawValueHumi;    // humidity raw value from sensor    unsigned char Rdata={0};         if(SHT3X_ReadData(CMD_FETCH_DATA, Rdata, 6)==SHT30_ERROR)      {                return SHT30_ERROR;      }    if(Rdata == SHT3X_CalcCrc(Rdata, 2) && Rdata == SHT3X_CalcCrc(&Rdata, 2))    {      rawValueTemp = (Rdata << 8) | Rdata;      rawValueHumi = (Rdata << 8) | Rdata;      *temp =(short)(1750 * rawValueTemp / 65535 - 450);      *humi =(short)(1000 * rawValueHumi / 65535);                   HAL_GPIO_TogglePin(USR_LED1_GPIO_Port, USR_LED1_Pin);//LED1      return SHT30_OK;    }    return SHT30_ERROR;}sht30.h/* Define to prevent recursive inclusion -------------------------------------*/#ifndef __sht30_H#define __sht30_H#ifdef __cplusplus extern "C" {#endif/* Includes ------------------------------------------------------------------*/#include "main.h"#define CMD_READ_SERIALNBR   0x3780 // read serial number#define CMD_READ_STATUS      0xF32D // read status register#define CMD_CLEAR_STATUS   0x3041 // clear status register#define CMD_HEATER_ENABLE    0x306D // enabled heater#define CMD_HEATER_DISABLE   0x3066 // disable heater#define CMD_SOFT_RESET       0x30A2 // soft reset#define CMD_MEAS_CLOCKSTR_H0x2C06 // meas. clock stretching, high rep.#define CMD_MEAS_CLOCKSTR_M0x2C0D // meas. clock stretching, medium rep.#define CMD_MEAS_CLOCKSTR_L0x2C10 // meas. clock stretching, low rep.#define CMD_MEAS_POLLING_H   0x2400 // meas. no clock stretching, high rep.#define CMD_MEAS_POLLING_M   0x240B // meas. no clock stretching, medium rep.#define CMD_MEAS_POLLING_L   0x2416 // meas. no clock stretching, low rep.#define CMD_MEAS_PERI_05_H   0x2032 // meas. periodic 0.5 mps, high rep.#define CMD_MEAS_PERI_05_M   0x2024 // meas. periodic 0.5 mps, medium rep.#define CMD_MEAS_PERI_05_L   0x202F // meas. periodic 0.5 mps, low rep.#define CMD_MEAS_PERI_1_H    0x2130 // meas. periodic 1 mps, high rep.#define CMD_MEAS_PERI_1_M    0x2126 // meas. periodic 1 mps, medium rep.#define CMD_MEAS_PERI_1_L    0x212D // meas. periodic 1 mps, low rep.#define CMD_MEAS_PERI_2_H    0x2236 // meas. periodic 2 mps, high rep.#define CMD_MEAS_PERI_2_M    0x2220 // meas. periodic 2 mps, medium rep.#define CMD_MEAS_PERI_2_L    0x222B // meas. periodic 2 mps, low rep.#define CMD_MEAS_PERI_4_H    0x2334 // meas. periodic 4 mps, high rep.#define CMD_MEAS_PERI_4_M    0x2322 // meas. periodic 4 mps, medium rep.#define CMD_MEAS_PERI_4_L    0x2329 // meas. periodic 4 mps, low rep.#define CMD_MEAS_PERI_10_H   0x2737 // meas. periodic 10 mps, high rep.#define CMD_MEAS_PERI_10_M   0x2721 // meas. periodic 10 mps, medium rep.#define CMD_MEAS_PERI_10_L   0x272A // meas. periodic 10 mps, low rep.#define CMD_FETCH_DATA       0xE000 // readout measurements for periodic mode#define CMD_R_AL_LIM_LS      0xE102 // read alert limits, low set#define CMD_R_AL_LIM_LC      0xE109 // read alert limits, low clear#define CMD_R_AL_LIM_HS      0xE11F // read alert limits, high set#define CMD_R_AL_LIM_HC      0xE114 // read alert limits, high clear#define CMD_W_AL_LIM_LS      0x6100 // write alert limits, low set#define CMD_W_AL_LIM_LC      0x610B // write alert limits, low clear#define CMD_W_AL_LIM_HS      0x611D // write alert limits, high set#define CMD_W_AL_LIM_HC      0x6116 // write alert limits, high clear#define CMD_NO_SLEEP         0x303E#define POLYNOMIAL0x31 // P(x) = x^8 + x^5 + x^4 + 1 = 00110001#define SHT3XADDR   0x44               //SHT3X的I2C地址#define SHT3X_WR_ADDR   (SHT3XADDR << 1)#define SHT3X_RD_ADDR    (SHT3X_WR_ADDR | 0x01)void SHT3X_Init(void);int SHT3X_GetTempAndHumi(short *temp, short *humi);#ifdef __cplusplus}#endif#endif /*__ usart_H */

G0子板与F7子板通信STM32CubeMX配置USART外设根据原理图,STM32CubeMX配置G0通过USART2与F7子板通信外设:

https://www.stmcu.com.cn/upload/20200103/171.pnghttps://www.stmcu.com.cn/upload/20200103/172.png主要代码app.c
static short temperature = 0, humidity = 0;#define FRM_START                0x68#define FRM_CMD_UPD_SENSOR       0x01#define FRM_FIXLEN               14#define FRM_END0               0x5c#define FRM_END1               0x6e#define FRM_POS_START            0#define FRM_POS_CMD            1#define FRM_POS_LEN            2#define FRM_POS_DATA             3#define FRM_POS_CRC            11#define FRM_POS_END0             12#define FRM_POS_END1             13//eg:68 cmd len t t h h x x y y crc 16 / crc = 68 + cmd + len + t + t + h + h + x + x + y + y 16extern UART_HandleTypeDef huart2;uint8_t cal_crc(uint8_t *buf,uint8_t len){       uint8_t t_crc = 0;       uint8_t r_crc = 0;      //check crc      for(int i=0; i

页: [1]
查看完整版本: DIY数字仪表:G0板开发:读取传感器数据