当前位置:网站首页>STM32——串口学习笔记(一个字节、16位数据、字符串、数组)
STM32——串口学习笔记(一个字节、16位数据、字符串、数组)
2022-07-26 03:00:00 【最穷不过要饭、不死总会出头】
一、串口相关配置
void uart_init(u32 bound){
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //使能USART1,GPIOA时钟
//USART1_TX GPIOA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9 USART1的RXD、TXD引脚 也需要查数据手册
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 这两种模式需要查数据手册8.1.11
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.9
//USART1_RX GPIOA.10初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
USART_Init(USART1, &USART_InitStructure); //初始化串口1
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启串口接受中断
USART_Cmd(USART1, ENABLE); //使能串口1
}二、串口发送数据函数封装
1、发送一个字节
void Usart_SendByte( USART_TypeDef * pUSARTx, uint8_t ch)
{
/* 发送一个字节数据到USART */
USART_SendData(pUSARTx,ch);
/* 等待发送数据寄存器为空 */
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
}2、发送一个16位数据
void Usart_SendHalfWord( USART_TypeDef * pUSARTx, uint16_t ch)
{
uint8_t temp_h, temp_l;
/* 取出高八位 */
temp_h = (ch&0XFF00)>>8;
/* 取出低八位 */
temp_l = ch&0XFF;
/* 发送高八位 */
USART_SendData(pUSARTx,temp_h);
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
/* 发送低八位 */
USART_SendData(pUSARTx,temp_l);
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
}3、发送8位的数组
示例:
#define DEBUG_USART5 UART5
uint8_t card_num_main[16];
Usart_SendArray( DEBUG_USART5,card_num_main, 16);
void Usart_SendArray( USART_TypeDef * pUSARTx, uint8_t *array, uint16_t num)
{
uint8_t i;
for(i=0; i<num; i++)
{
/* 发送一个字节数据到USART */
Usart_SendByte(pUSARTx,array[i]);
}
/* 等待发送完成 */
while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TC)==RESET);
}4、字符串发送函数
void HMISends(char *buf1) //字符串发送函数
{
uint8_t i=0;
while(1)
{
if(buf1[i]!='\0')
{
USART_SendData(DEBUG_USART2,buf1[i]); //发送一个字节
while(USART_GetFlagStatus(DEBUG_USART2,USART_FLAG_TXE)==RESET){};//等待发送结束
i++;
}
else
return ;
}
}5、将文本转换为数字发送
串口中断接收:
void USART1_IRQHandler(void) //串口1中断服务程序
{
u8 Res,temp;
#if SYSTEM_SUPPORT_OS //如果SYSTEM_SUPPORT_OS为真,则需要支持OS.
OSIntEnter();
#endif
Res =USART_ReceiveData(USART1); //读取接收到的数据 //一个字节
temp=Res-'0';
USART_RX_BUF[USART_RX_STA&0X3FFF]=temp ;
USART_RX_STA++;
if(temp==1)
LED0=0;
if(temp==2)
LED0=1;
#if SYSTEM_SUPPORT_OS //如果SYSTEM_SUPPORT_OS为真,则需要支持OS.
OSIntExit();
#endif
}
#endif
发送:
while(1)
{
len=USART_RX_STA&0x3fff;//得到此次接收到的数据长度
if(len>0)
{
for(t=0;t<len;t++)
{
USART_SendData(USART1, USART_RX_BUF[t]);//向串口1发送数据
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
}
USART_RX_STA=0;
}
}6、串口接收到单字节数据,并发送
u8 temp;
void USART1_IRQHandler(void) //串口1中断服务程序
{
u8 Res;
#if SYSTEM_SUPPORT_OS //如果SYSTEM_SUPPORT_OS为真,则需要支持OS.
OSIntEnter();
#endif
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是0x0d 0x0a结尾)
{
Res =USART_ReceiveData(USART1); //读取接收到的数据 //一个字节
temp = Res;
USART_SendData(USART1, temp);//向串口1发送数据
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
}
#if SYSTEM_SUPPORT_OS //如果SYSTEM_SUPPORT_OS为真,则需要支持OS.
OSIntExit();
#endif
}
#endif
extern u8 temp;
while(1)
{
if(temp == 0x01)
{
temp=0;
LED0=1;
}
else if(temp == 0x02)
{
temp=0;
LED0=0;
}
}边栏推荐
- The LAAS protocol elephant of defi 2.0 is the key to revitalizing the development of defi track
- .net serialize enumeration as string
- ES6 advanced - inherit parent class attributes with constructors
- Teach you to rely on management hand in hand
- Nahamcon CTF 2022 babyrev reverse analysis
- (pc+wap) dream weaving template vegetable and fruit websites
- 循环与分支(一)
- Personally test five efficient and practical ways to get rid of orders, and quickly collect them to help you quickly find high-quality objects!
- Literature speed reading | in the face of danger, anxious people run faster?
- Binary search 33. search rotation sort array
猜你喜欢
随机推荐
文件操作(一)——文件简介与文件的打开方式和关闭
移位距离和假设的应用
图像识别(六)| 激活函数
持续交付和DevOps是一对好基友
AMD64 (x86_64) architecture ABI document: medium
Vofa+ serial port debugging assistant
Arthas' dynamic load class (retransform)
Golang 中‘...‘的用法
Wechat official account mutual aid, open white groups, and small white newspaper groups to keep warm
Be highly vigilant! Weaponization of smartphone location data on the battlefield
案例:使用keepalived+Haproxy搭建Web群集
Annotation development management third-party beans
eslint常见报错集合
Is it safe to open galaxy securities account by mobile phone?
GAMES101复习:着色(Shading)、渲染管线
Pipnet: face key point detection for natural scenes "pixel in pixel net: directions efficient facial landmark detection in the wild"
(pc+wap) dream weaving template vegetable and fruit websites
assert _Aligns
Chapter 3 business function development (delete clues)
[sql] usage of self connection








