数据类型
一个字节8个比特位,串口发送函数的第三个参数,写入的是数据的数量,以字节为单位。
/* exact-width signed integer types */
typedef signed char int8_t;
typedef signed short int int16_t;
typedef signed int int32_t;
typedef signed __INT64 int64_t;
/* exact-width unsigned integer types */
typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
typedef unsigned int uint32_t;
typedef unsigned __INT64 uint64_t;
HAL库 串口发送函数 HAL_UART_Transmit();
参数huart: 填写串口句柄的指针
参数pdate: 填写要发送的数据指针
参数size: 填写要发送数据的数量,以字节为单位
参数timeout:超时时间,单位ms 该函数存在一个返回值,返回发送成功还是失败
#include "main.h"
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
uint8_t bytenumber = 0x5a;
uint8_t byteArray[] = {1,2,3,4,5};
char ch='a';
char *str="hello world"; 字符串指针存放字符串
HAL_UART_Transmit (&huart1,&bytenumber ,1,HAL_MAX_DELAY );1句柄,2数据地址,3以字节为单位的数量,4超时时间。
HAL_UART_Transmit (&huart1,byteArray ,5,HAL_MAX_DELAY );
HAL_UART_Transmit (&huart1,(uint8_t *)&ch,1,HAL_MAX_DELAY );
HAL_UART_Transmit (&huart1,(uint8_t *) str ,strlen(str),HAL_MAX_DELAY );得加个头文件
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
HAL库 串口接收函数 HAL_UART_Receive();
接收数据,控制板载LED亮,和灭。
while (1)
{
uint8_t Rxdate; //用来接收数据
HAL_UART_Receive (&huart1 ,&Rxdate ,1,HAL_MAX_DELAY );//1串口句柄,2指向接收
//缓冲区的指针,3接收数据的数量,以字节为单位
if(Rxdate == '0')
{
HAL_GPIO_WritePin (GPIOC,GPIO_PIN_13,GPIO_PIN_SET );//开漏模式写1高阻态,灭。
}
else if(Rxdate == '1')
{
HAL_GPIO_WritePin (GPIOC,GPIO_PIN_13,GPIO_PIN_RESET );//开漏模式写0导通,亮。
}