实验内容:通过串口向上位机发送单个的字符和字符串
实验目的:熟悉串口的配置,为后续试验做好准备工作
串口相关配置函数
#include "stm32f10x.h"
void Uart1_Init(void)
{
//uart 的GPIO管脚初始化 PA9 usart1_TX PA10 USART_RX
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//悬空输入
GPIO_Init(GPIOA,&GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//只有串口1 使用72M,其他串口使用36M
USART_InitTypeDef USART_InitStructure;
//串口参数配置:115200,8,1,无奇偶校验,无硬流量控制 ,使能发送和接收
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
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);
//使能串口中断,并设置优先级
// NVIC_InitTypeDef NVIC_InitStructure;
// NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;//USART1_IRQChannel_Priority;
// NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
// NVIC_Init(&NVIC_InitStructure);
USART_Cmd(USART1, ENABLE);
}
/*发送字符是通过查询字符串的状态来来不断的发送下一个数据。接受数据是通过中断来实现的,把接受的数据放入缓冲区,来实现。*/
//发送一个字符
void Usart_SendData(unsigned char x)
{
USART_SendData(USART1,x);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)!=SET);//查询发送数完成志位,等待发送完毕
}
//发送字符串
//void PrintUart1(unsigned char *Str)
//{ while(*Str)
// {
// USART_SendData(USART1, (unsigned char)*Str++);
// while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
// }
//}
LED相关配置函数
#include "stm32f10x.h"
void Delay(void)
{
unsigned long ik;
for(ik=0;ik<0xffff8;ik++);
}
//由于LED4与液晶屏复用,故此处屏蔽掉了LED4
void LedBlink(unsigned int x)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOF,&GPIO_InitStructure);
switch (x)
{
case 0:
GPIO_SetBits(GPIOF,GPIO_Pin_7 | GPIO_Pin_9 | GPIO_Pin_10);
Delay();
GPIO_ResetBits(GPIOF,GPIO_Pin_7 | GPIO_Pin_9 | GPIO_Pin_10);
Delay();
break ;
case 1:
GPIO_SetBits(GPIOF,GPIO_Pin_7 | GPIO_Pin_6 | GPIO_Pin_10);
GPIO_ResetBits(GPIOF,GPIO_Pin_9);
Delay();
GPIO_SetBits(GPIOF,GPIO_Pin_9);
Delay();
break ;
case 2:
GPIO_SetBits(GPIOF,GPIO_Pin_7 | GPIO_Pin_6 | GPIO_Pin_9);
GPIO_ResetBits(GPIOF,GPIO_Pin_10);
Delay();
GPIO_SetBits(GPIOF,GPIO_Pin_10);
Delay();
break ;
case 3:
GPIO_SetBits(GPIOF,GPIO_Pin_6 | GPIO_Pin_9 | GPIO_Pin_10);
GPIO_ResetBits(GPIOF,GPIO_Pin_7);
Delay();
GPIO_SetBits(GPIOF,GPIO_Pin_7);
Delay();
break ;
default:
break;
}
}
主函数
#include "stm32f10x.h"
#include "led.h"
#include "uart.h"
int main(void)
{
Uart1_Init();
//发送单个字符
Usart_SendData(0x33);
Delay();
Usart_SendData(0x44);
Delay();
// 发送字符串
unsigned char ss[]="STM32";
unsigned char *p;
p=ss;
PrintUart1(p);
while (1)
{
LedBlink(2);//LED2闪烁
}
}
此例程还存在一个问题,在串口初始化函数完成之后,自动发送一个字符”80“,至今没找出是哪里出了岔子,请高手指点,多谢
共5条
1/1 1 跳转至页
STM32学习笔记(usart)

关键词: STM32 学习 笔记 usart 发送 USAR
共5条
1/1 1 跳转至页
回复
打赏帖 | |
---|---|
宏定义和const关键字定义被打赏5分 | |
【功率监测与控制系统DIY活动成果贴】DIY功率计与LabVIEW数据采集被打赏100分 | |
【Freertos】任务管理被打赏10分 | |
分享博世的两种不同的喷射系统模式被打赏5分 | |
汽车+开路实验与短路实验被打赏10分 | |
多点式电子控制汽油喷射系统知识分享被打赏10分 | |
分享机械控制式汽油喷射系统被打赏5分 | |
【分享开发笔记,赚取电动螺丝刀】解决基于CH341制作无线模块时芯片发热问题被打赏31分 | |
【分享开发笔记,赚取电动螺丝刀】使用STM32F103ZE主控调试RS485通讯的避坑经验被打赏36分 | |
【分享开发笔记,赚取电动螺丝刀】移植xprintf模块被打赏27分 |