本文的任务是当取到的数据通过串口以ASCII格式向外发送,并通过串口调试助手接受来着上位机的指令做出对应的操作。
具体任务如下:
单片机间隔一秒向上位机发送光照数据,并记录发送次数,当收到来自上位机的复位指令FA 00 FB时,清空发送次数。
通过分析usart1-board.c库,我们常会使用下列方法:
void USART1_Init(uint32_t bound);
//初始化串口 bound为波特率
void USART1_SendStr(uint8_t *Data, uint16_t length);
//发送uint8_t类型数据数组 length为发送的长度
uint16_t USART1_ReadRxBuffer( uint8_t *payload);
//接受来自串口的内容并存入传入的数据数组内 返回接受到的数据长度
代码示例:
#include <string.h>
#include "board.h"
#include "hal_key.h"
#include "tim-board.h"
#include "timer_handles.h"
#include "led_light.h"
#include "adc_reader.h"
#include <math.h> //library
#include <stdio.h>
#include "stm32l1xx_hal.h" // controller register definitions
#include "sht1x.h" // controller register definitions
#include "sht3x.h"
#include "usart1-board.h"
void Init() {
BoardInitMcu();
BoardInitPeriph();
keys_init();//按键初始化
setTimer2Callback(Time2Handler);