收藏官网首页
查看: 7388|回复: 2

【官方】G0子板读取传感器与F7通信

140

主题

418

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
12682
跳转到指定楼层
楼主
发表于 2020-2-7 12:23:06 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
汉枫LPB120模块
创建工程
视频


主程序主要代码

main.c

  1. MX_GPIO_Init();
  2. MX_USART2_UART_Init();//G0和F7通信
  3. MX_USART1_UART_Init();
  4. MX_USART3_UART_Init();
  5. MX_USART4_UART_Init();//Printf
  6. MX_I2C1_Init();
  7. APP_Init();
  8. while (1)
  9.   {
  10.         tmp = HAL_GetTick();
  11.         if(tmp - tick > 1000)
  12.         {
  13.                 tick = tmp;
  14.                 APP_Process();
  15.                 printf("APP_Process\n\r");
  16.         }
  17.   }
复制代码
读取传感器数据STM32CubeMX配置I2C外设
主要代码

sht30hi2c.c

  1. /* Includes ------------------------------------------------------------------*/
  2. #include "i2c.h"
  3. #include "sht30.h"
  4. extern I2C_HandleTypeDef hi2c1;
  5. typedef enum
  6. {
  7.   SHT30_OK       = 0x00U,
  8.   SHT30_ERROR    = 0x01U,
  9. } SHT30_StatusTypeDef;
  10. //======================================I2Cx redefine start===================================
  11. static void I2Cx_Error(uint8_t Addr)
  12. {
  13.   /* De-initialize the I2C communication bus */
  14.   HAL_I2C_DeInit(&hi2c1);
  15.   
  16.   /* Re-Initialize the I2C communication bus */
  17.   MX_I2C1_Init();
  18. }

  19. static void SHT3X_WriteCMD(unsigned short cmd)
  20. {
  21.         uint8_t buf[2];
  22.         buf[0]=cmd>>8;
  23.         buf[1]=cmd;
  24.         HAL_StatusTypeDef status = HAL_OK;
  25.         status = HAL_I2C_Master_Transmit(&hi2c1, SHT3X_WR_ADDR, buf, 2, 1000);
  26.         if(status != HAL_OK)
  27.         {
  28.                 /* I2C error occurred */
  29.                I2Cx_Error(SHT3X_WR_ADDR);
  30.         }
  31. }
  32. static int SHT3X_ReadData(unsigned short cmd, unsigned char *buf, unsigned short len)
  33. {
  34.         HAL_StatusTypeDef status = HAL_OK;
  35.         status = HAL_I2C_Mem_Read(&hi2c1, SHT3X_WR_ADDR, cmd, I2C_MEMADD_SIZE_16BIT, buf, len, 1000);
  36.         if(status != HAL_OK)
  37.         {
  38.                 /* I2C error occurred */
  39.                I2Cx_Error(SHT3X_WR_ADDR);
  40.         }
  41.         return SHT30_OK;
  42. }

  43. static unsigned char SHT3X_CalcCrc(unsigned char *crcdata, unsigned char nbrOfBytes)
  44. {
  45.     unsigned char Bit;        // bit mask
  46.     unsigned char crc = 0xFF; // calculated checksum
  47.     unsigned char byteCtr;    // byte counter
  48.   
  49.     // calculates 8-Bit checksum with given polynomial
  50.     for(byteCtr = 0; byteCtr < nbrOfBytes; byteCtr++)
  51.     {
  52.         crc ^= (crcdata[byteCtr]);
  53.         for(Bit = 8; Bit > 0; --Bit)
  54.         {
  55.             if(crc & 0x80) crc = (crc << 1) ^ POLYNOMIAL;
  56.             else           crc = (crc << 1);
  57.         }
  58.     }
  59.     return crc;
  60. }

  61. void SHT3X_Init(void)
  62. {
  63.         HAL_Delay(20);
  64.     SHT3X_WriteCMD(CMD_MEAS_PERI_10_H);
  65. }

  66. int SHT3X_GetTempAndHumi(short *temp, short *humi)
  67. {
  68.     unsigned long int  rawValueTemp;    // temperature raw value from sensor
  69.     unsigned long int  rawValueHumi;    // humidity raw value from sensor
  70.     unsigned char Rdata[6]={0};      

  71.     if(SHT3X_ReadData(CMD_FETCH_DATA, Rdata, 6)==SHT30_ERROR)
  72.         {
  73.                 return SHT30_ERROR;
  74.         }
  75.     if(Rdata[2] == SHT3X_CalcCrc(Rdata, 2) && Rdata[5] == SHT3X_CalcCrc(&Rdata[3], 2))
  76.     {
  77.         rawValueTemp = (Rdata[0] << 8) | Rdata[1];
  78.         rawValueHumi = (Rdata[3] << 8) | Rdata[4];
  79.         *temp =(short)(1750 * rawValueTemp / 65535 - 450);
  80.         *humi =(short)(1000 * rawValueHumi / 65535);   
  81.                 HAL_GPIO_TogglePin(USR_LED1_GPIO_Port, USR_LED1_Pin);//LED1
  82.         return SHT30_OK;
  83.     }
  84.     return SHT30_ERROR;
  85. }
复制代码

sht30.h

  1. /* Define to prevent recursive inclusion -------------------------------------*/
  2. #ifndef __sht30_H
  3. #define __sht30_H
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif

  7. /* Includes ------------------------------------------------------------------*/
  8. #include "main.h"

  9. #define CMD_READ_SERIALNBR   0x3780 // read serial number
  10. #define CMD_READ_STATUS      0xF32D // read status register
  11. #define CMD_CLEAR_STATUS     0x3041 // clear status register
  12. #define CMD_HEATER_ENABLE    0x306D // enabled heater
  13. #define CMD_HEATER_DISABLE   0x3066 // disable heater
  14. #define CMD_SOFT_RESET       0x30A2 // soft reset
  15. #define CMD_MEAS_CLOCKSTR_H  0x2C06 // meas. clock stretching, high rep.
  16. #define CMD_MEAS_CLOCKSTR_M  0x2C0D // meas. clock stretching, medium rep.
  17. #define CMD_MEAS_CLOCKSTR_L  0x2C10 // meas. clock stretching, low rep.
  18. #define CMD_MEAS_POLLING_H   0x2400 // meas. no clock stretching, high rep.
  19. #define CMD_MEAS_POLLING_M   0x240B // meas. no clock stretching, medium rep.
  20. #define CMD_MEAS_POLLING_L   0x2416 // meas. no clock stretching, low rep.
  21. #define CMD_MEAS_PERI_05_H   0x2032 // meas. periodic 0.5 mps, high rep.
  22. #define CMD_MEAS_PERI_05_M   0x2024 // meas. periodic 0.5 mps, medium rep.
  23. #define CMD_MEAS_PERI_05_L   0x202F // meas. periodic 0.5 mps, low rep.
  24. #define CMD_MEAS_PERI_1_H    0x2130 // meas. periodic 1 mps, high rep.
  25. #define CMD_MEAS_PERI_1_M    0x2126 // meas. periodic 1 mps, medium rep.
  26. #define CMD_MEAS_PERI_1_L    0x212D // meas. periodic 1 mps, low rep.
  27. #define CMD_MEAS_PERI_2_H    0x2236 // meas. periodic 2 mps, high rep.
  28. #define CMD_MEAS_PERI_2_M    0x2220 // meas. periodic 2 mps, medium rep.
  29. #define CMD_MEAS_PERI_2_L    0x222B // meas. periodic 2 mps, low rep.
  30. #define CMD_MEAS_PERI_4_H    0x2334 // meas. periodic 4 mps, high rep.
  31. #define CMD_MEAS_PERI_4_M    0x2322 // meas. periodic 4 mps, medium rep.
  32. #define CMD_MEAS_PERI_4_L    0x2329 // meas. periodic 4 mps, low rep.
  33. #define CMD_MEAS_PERI_10_H   0x2737 // meas. periodic 10 mps, high rep.
  34. #define CMD_MEAS_PERI_10_M   0x2721 // meas. periodic 10 mps, medium rep.
  35. #define CMD_MEAS_PERI_10_L   0x272A // meas. periodic 10 mps, low rep.
  36. #define CMD_FETCH_DATA       0xE000 // readout measurements for periodic mode
  37. #define CMD_R_AL_LIM_LS      0xE102 // read alert limits, low set
  38. #define CMD_R_AL_LIM_LC      0xE109 // read alert limits, low clear
  39. #define CMD_R_AL_LIM_HS      0xE11F // read alert limits, high set
  40. #define CMD_R_AL_LIM_HC      0xE114 // read alert limits, high clear
  41. #define CMD_W_AL_LIM_LS      0x6100 // write alert limits, low set
  42. #define CMD_W_AL_LIM_LC      0x610B // write alert limits, low clear
  43. #define CMD_W_AL_LIM_HS      0x611D // write alert limits, high set
  44. #define CMD_W_AL_LIM_HC      0x6116 // write alert limits, high clear
  45. #define CMD_NO_SLEEP         0x303E


  46. #define POLYNOMIAL  0x31 // P(x) = x^8 + x^5 + x^4 + 1 = 00110001


  47. #define SHT3XADDR     0x44                 //SHT3X的I2C地址
  48. #define SHT3X_WR_ADDR     (SHT3XADDR << 1)
  49. #define SHT3X_RD_ADDR    (SHT3X_WR_ADDR | 0x01)


  50. void SHT3X_Init(void);
  51. int SHT3X_GetTempAndHumi(short *temp, short *humi);




  52. #ifdef __cplusplus
  53. }
  54. #endif
  55. #endif /*__ usart_H */
复制代码
G0子板与F7子板通信STM32CubeMX配置USART外设

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



主要代码

app.c

  1. static short temperature = 0, humidity = 0;

  2. #define FRM_START                0x68
  3. #define FRM_CMD_UPD_SENSOR       0x01
  4. #define FRM_FIXLEN               14
  5. #define FRM_END0                 0x5c
  6. #define FRM_END1                 0x6e

  7. #define FRM_POS_START            0
  8. #define FRM_POS_CMD              1
  9. #define FRM_POS_LEN              2
  10. #define FRM_POS_DATA             3
  11. #define FRM_POS_CRC              11
  12. #define FRM_POS_END0             12
  13. #define FRM_POS_END1             13

  14. //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 16
  15. extern UART_HandleTypeDef huart2;
  16. uint8_t cal_crc(uint8_t *buf,uint8_t len)
  17. {
  18.        uint8_t t_crc = 0;
  19.        uint8_t r_crc = 0;
  20.         //check crc
  21.         for(int i=0; i
复制代码




2

主题

9

帖子

423

积分

中级会员

Rank: 3Rank: 3

积分
423
沙发
发表于 2020-10-9 16:14:03 | 只看该作者
这个程序没写完啊

点评

这个项目的内容全在 https://club.gizwits.com/thread-165606-1-1.html 【汇总】STM32与机智云DIY数字仪表项目开发资源  详情 回复 发表于 2020-12-24 10:36

140

主题

418

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
12682
板凳
 楼主| 发表于 2020-12-24 10:36:21 | 只看该作者
Thief 发表于 2020-10-9 16:14
这个程序没写完啊

这个项目的内容全在  https://club.gizwits.com/thread-165606-1-1.html   
【汇总】STM32与机智云DIY数字仪表项目开发资源
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

加入Q群 返回顶部

版权与免责声明 © 2006-2024 Gizwits IoT Technology Co., Ltd. ( 粤ICP备11090211号 )

快速回复 返回顶部 返回列表