[其他ST产品] 【STM32G4评测+03+MDK+Stm32cube连接环境传感器】

[复制链接]
901|1
 楼主| dami 发表于 2021-3-28 20:54 | 显示全部楼层 |阅读模式
本帖最后由 dami 于 2021-3-29 09:45 编辑

【STM32G4评测+03+MDK+Stm32cube连接环境传感器】1,打开Stm32cube,升级到最新的6.20。
2,配置板子为nucleo-STM32G491RE。
3,配置串口3,TIM3,(因为是选的板子所以串口一默认打开的,ld2也是配置好了的)。
    设置为接收中断,发送不中断。波特率115200.
4,生成代码。
5,用mdk打开。
6,工程中startup_stm32g491xx.s没加上所以编译有错。加上。
7,实验目标:串口3读传感器数据送到串口一打印。
8,项目中增加SVM40.C SVM40.h文件。  SVM40.h
  1. void svm40_start_measurement(void);
  2. void svm40_get_signals(void);
  3. void svm40_get_raw_signals(void);
  4. void svm40_stop_measurement(void);
  5. void svm40_get_temperature_offset(void);
  6. void svm40_set_temperature_offset(void);
  7. void svm40_get_voc_parameters(void);
  8. void svm40_set_voc_parameters(void);
  9. void svm40_store_input_parameters(void);
  10. void svm40_get_voc_states(void);
  11. void svm40_set_voc_states(void);
  12. void svm40_get_version(void);
  13. void svm40_device_reset(void);
SVM40.C


  1. #include "stm32g4xx_hal.h"
  2. #include "stm32g4xx_hal_uart.h"
  3. extern UART_HandleTypeDef huart3;
  4. void svm40_start_measurement(void)
  5. {        
  6.         uint8_t buf2[]={0x7E, 0x00, 0x00, 0x01, 0x00, 0xFE, 0x7E};
  7.         HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
  8. }        
  9. void svm40_get_signals(void)
  10. {        
  11.         uint8_t buf2[]={0x7E, 0x00, 0x03, 0x01, 0x0A, 0xF1, 0x7E};
  12.         HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);

  13. }        
  14. void svm40_get_raw_signals(void)
  15. {        
  16.         uint8_t buf2[]={0x7E, 0x00, 0x03, 0x01, 0x0B, 0xF0, 0x7E};
  17.         HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);

  18. }        
  19. void svm40_stop_measurement(void)
  20. {        
  21.         uint8_t buf2[]={0x7E, 0x00, 0x01, 0x00, 0xFE, 0x7E};
  22.         HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
  23. }        
  24. void svm40_get_temperature_offset(void)
  25. {        
  26.         uint8_t buf2[]={0x7E, 0x00, 0x60, 0x01, 0x01, 0x9D, 0x7E};
  27.         HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
  28. }        
  29. void svm40_set_temperature_offset(void)
  30. {        
  31.         uint8_t buf2[]={0x7E, 0x00, 0x60, 0x05, 0x81, 0x00, 0x00, 0x00, 0x00, 0x19, 0x7E};
  32.         HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
  33. }        

  34. //MOSI  0x7E 0x00 0x60 0x01 0x08 0x96 0x7E
  35. //MISO  0x7E 0x00 0x60 0x00 0x08 0x00 0x64 0x00 0x0C 0x00 0xB4 0x00 0x32 0x41 0x7E
  36. void svm40_get_voc_parameters(void)
  37. {        
  38.         uint8_t buf2[]={0x7E, 0x00, 0x60, 0x01, 0x08, 0x96, 0x7E};
  39.         HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
  40. }        
  41. void svm40_set_voc_parameters(void)
  42. {        
  43.         uint8_t buf2[]={0x7E, 0x00, 0x60, 0x09, 0x88, 0x00, 0x64, 0x00, 0x0C, 0x00, 0xB4, 0x00, 0x32, 0xB8, 0x7E};
  44.         HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
  45. }        
  46. void svm40_store_input_parameters(void)
  47. {        
  48.         uint8_t buf2[]={0x7E, 0x00, 0x60, 0x01, 0x80, 0x1E, 0x7E};
  49.         HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
  50. }        
  51. void svm40_get_voc_states(void)
  52. {        
  53.         uint8_t buf2[]={0x7E, 0x00, 0x61, 0x01, 0x08, 0x95, 0x7E};
  54.         HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
  55. }        
  56. void svm40_set_voc_states(void)
  57. {        
  58.         uint8_t buf2[]={0x7E, 0x00, 0x61, 0x09, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x32, 0x00, 0x00, 0xDB, 0x7E};
  59.         HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
  60. }        
  61. void svm40_get_version(void)
  62. {        
  63.         uint8_t buf2[]={0x7E, 0x00, 0xD1, 0x00, 0x2E, 0x7E};
  64.         HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
  65. }        
  66. void svm40_device_reset(void)
  67. {        
  68.         uint8_t buf2[]={0x7E, 0x00, 0xD3, 0x00, 0x2C, 0x7E};
  69.         HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
  70. }        
9,main.c修改
  1. #include "main.h"
  2. #include "SVM40.h"
  3. I2C_HandleTypeDef hi2c1;
  4. UART_HandleTypeDef hlpuart1;
  5. UART_HandleTypeDef huart4;
  6. UART_HandleTypeDef huart3;
  7. TIM_HandleTypeDef htim3;
  8. void SystemClock_Config(void);
  9. static void MX_GPIO_Init(void);
  10. static void MX_I2C1_Init(void);
  11. static void MX_LPUART1_UART_Init(void);
  12. static void MX_UART4_Init(void);
  13. static void MX_USART3_UART_Init(void);
  14. static void MX_TIM3_Init(void);
  15. uint8_t uart3sv[4],uart4sv[4];
  16. uint8_t uart3buf[100],uart4buf[100];
  17. static uint8_t p_uart3=0;//,p_uart4=0;
  18. uint32_t volatile soft_tim1=0,soft_tim2=0;
  19. void delayms(uint32_t x)
  20. {
  21.         for(uint32_t i=0;i<x;i++)
  22.         {
  23.                 for(uint32_t j=0;j<50000;j++){};
  24.         }
  25. }        
  26. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  27. {
  28.         if(htim == &htim3)
  29.         {
  30.                 if((soft_tim1)>1000000)        soft_tim1 =0;
  31.           else                    soft_tim1++;
  32.     if((soft_tim2)>1000000)        soft_tim2 =0;
  33.           else                    soft_tim2++;
  34.         }
  35. }
  36. void read_svm40(uint16_t *pvoc,uint16_t *phumi,uint16_t *ptemp)
  37. {
  38.         uint8_t buf2[200];
  39.         svm40_get_signals();
  40.         delayms(200);
  41.         if(p_uart3 >=13)
  42.         {        
  43.                 for(uint8_t i=0;i<13;i++)
  44.                         buf2[i]=uart3buf[i];
  45.         }
  46.         p_uart3=0;
  47.         *pvoc = buf2[5];
  48.         *pvoc <<=8;
  49.         *pvoc += buf2[6];
  50.         *pvoc =*pvoc / 10;
  51.         *phumi = buf2[7];
  52.         *phumi <<=8;
  53.         *phumi += buf2[8];
  54.         *ptemp = buf2[9];
  55.         *ptemp <<=8;
  56.         *ptemp += buf2[10];
  57.         *ptemp = *ptemp/2;
  58. }        

  59. int main(void)
  60. {
  61.   /* USER CODE BEGIN 1 */
  62.   /* USER CODE END 1 */
  63.   /* MCU Configuration--------------------------------------------------------*/
  64.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  65.   HAL_Init();
  66.   /* USER CODE BEGIN Init */
  67.   /* USER CODE END Init */
  68.   /* Configure the system clock */
  69.   SystemClock_Config();
  70.   /* USER CODE BEGIN SysInit */
  71.   /* USER CODE END SysInit */
  72.   /* Initialize all configured peripherals */
  73.   MX_GPIO_Init();
  74.   MX_I2C1_Init();
  75.   MX_LPUART1_UART_Init();
  76.   MX_UART4_Init();
  77.   MX_USART3_UART_Init();
  78.   MX_TIM3_Init();
  79.   /* USER CODE BEGIN 2 */
  80.   HAL_TIM_Base_Start_IT(&htim3);
  81.         HAL_UART_Receive_IT(&huart3,&uart3sv[0],1);
  82.         HAL_UART_Receive_IT(&huart4,&uart4sv[0],1);
  83.         delayms(500);
  84.         svm40_start_measurement();
  85.         delayms(500);
  86.         //static uint8_t cont=0;
  87.         static uint16_t tempH,tempL,temp, humiH,humiL,humi,voc;
  88.         uint8_t buf_T[]={"Temperature is :\n\r"};
  89.         uint8_t buf_H[]={"Humidity is :\n\r"};
  90.         uint8_t buf_V[]={"Noxious gas is :\n\r"};
  91.         //uint8_t buf_K[]={"\n\r"};
  92.         uint8_t buf1[200];
  93.   /* USER CODE END 2 */
  94.   /* Infinite loop */
  95.   /* USER CODE BEGIN WHILE */
  96.   while (1)
  97.   {
  98.                 HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
  99.                 delayms(500);
  100.                 HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET);
  101.     delayms(500);
  102.           p_uart3=0;//clear uart3 receive buf;
  103.           read_svm40(&voc,&humi,&temp);
  104.           humiH = humi / 100;
  105.           humiL = humi %100;
  106.           tempH =temp / 100;
  107.           tempL =temp % 100;
  108.                 HAL_UART_Transmit(&hlpuart1,buf_T,sizeof(buf_T),0xffff);
  109.           delayms(30);
  110.           sprintf(buf1,"%2d.%2d\n\r",tempH,tempL);
  111.           HAL_UART_Transmit(&hlpuart1,buf1,10,0xffff);
  112.           delayms(10);
  113.           HAL_UART_Transmit(&hlpuart1,buf_H,sizeof(buf_H),0xffff);
  114.           delayms(10);
  115.           sprintf(buf1,"%2d.%2d\n\r",humiH,humiL);
  116.           HAL_UART_Transmit(&hlpuart1,buf1,7,0xffff);
  117.           delayms(10);
  118.           HAL_UART_Transmit(&hlpuart1,buf_V,sizeof(buf_V),0xffff);
  119.           delayms(10);
  120.           sprintf(buf1,"%d \n\r",voc);
  121.           HAL_UART_Transmit(&hlpuart1,buf1,6,0xffff);
  122.           delayms(10);
  123.     /* USER CODE END WHILE */
  124.     /* USER CODE BEGIN 3 */
  125.   }
  126.   /* USER CODE END 3 */
  127. }
  128. //后面是系统自动生成,省略。

  129. #include "stm32g4xx_hal.h"
  130. #include "stm32g4xx_hal_uart.h"
  131. extern UART_HandleTypeDef huart3;
  132. void svm40_start_measurement(void)
  133. {        
  134.         uint8_t buf2[]={0x7E, 0x00, 0x00, 0x01, 0x00, 0xFE, 0x7E};
  135.         HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
  136. }        
  137. void svm40_get_signals(void)
  138. {        
  139.         uint8_t buf2[]={0x7E, 0x00, 0x03, 0x01, 0x0A, 0xF1, 0x7E};
  140.         HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);

  141. }        
  142. void svm40_get_raw_signals(void)
  143. {        
  144.         uint8_t buf2[]={0x7E, 0x00, 0x03, 0x01, 0x0B, 0xF0, 0x7E};
  145.         HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);

  146. }        
  147. void svm40_stop_measurement(void)
  148. {        
  149.         uint8_t buf2[]={0x7E, 0x00, 0x01, 0x00, 0xFE, 0x7E};
  150.         HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
  151. }        
  152. void svm40_get_temperature_offset(void)
  153. {        
  154.         uint8_t buf2[]={0x7E, 0x00, 0x60, 0x01, 0x01, 0x9D, 0x7E};
  155.         HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
  156. }        
  157. void svm40_set_temperature_offset(void)
  158. {        
  159.         uint8_t buf2[]={0x7E, 0x00, 0x60, 0x05, 0x81, 0x00, 0x00, 0x00, 0x00, 0x19, 0x7E};
  160.         HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
  161. }        

  162. //MOSI  0x7E 0x00 0x60 0x01 0x08 0x96 0x7E
  163. //MISO  0x7E 0x00 0x60 0x00 0x08 0x00 0x64 0x00 0x0C 0x00 0xB4 0x00 0x32 0x41 0x7E
  164. void svm40_get_voc_parameters(void)
  165. {        
  166.         uint8_t buf2[]={0x7E, 0x00, 0x60, 0x01, 0x08, 0x96, 0x7E};
  167.         HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
  168. }        
  169. void svm40_set_voc_parameters(void)
  170. {        
  171.         uint8_t buf2[]={0x7E, 0x00, 0x60, 0x09, 0x88, 0x00, 0x64, 0x00, 0x0C, 0x00, 0xB4, 0x00, 0x32, 0xB8, 0x7E};
  172.         HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
  173. }        
  174. void svm40_store_input_parameters(void)
  175. {        
  176.         uint8_t buf2[]={0x7E, 0x00, 0x60, 0x01, 0x80, 0x1E, 0x7E};
  177.         HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
  178. }        
  179. void svm40_get_voc_states(void)
  180. {        
  181.         uint8_t buf2[]={0x7E, 0x00, 0x61, 0x01, 0x08, 0x95, 0x7E};
  182.         HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
  183. }        
  184. void svm40_set_voc_states(void)
  185. {        
  186.         uint8_t buf2[]={0x7E, 0x00, 0x61, 0x09, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x32, 0x00, 0x00, 0xDB, 0x7E};
  187.         HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
  188. }        
  189. void svm40_get_version(void)
  190. {        
  191.         uint8_t buf2[]={0x7E, 0x00, 0xD1, 0x00, 0x2E, 0x7E};
  192.         HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
  193. }        
  194. void svm40_device_reset(void)
  195. {        
  196.         uint8_t buf2[]={0x7E, 0x00, 0xD3, 0x00, 0x2C, 0x7E};
  197.         HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
  198. }        
10,编译,调试。连接传感器和板子。运行。观察串口1数据。


                     ----------------------------------完------------------------------------




mmuuss586 发表于 2021-4-12 15:06 | 显示全部楼层

感谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则

66

主题

1080

帖子

6

粉丝
快速回复 在线客服 返回列表 返回顶部