访问手机版页面
你的位置:老古开发网 > STM32单片机 > STM32单片机的USART > 正文  
STM32单片机的Usart2串口的调试方法
内容导读:

先是参考http://wenku.baidu.com/view/78f6b1350b4c2e3f572763e9.html调通了usart1

然后将程序进行修改,对Usart2进行配置,配置完了之后,程序还是没有正确,然后在voidGPIO_cfg();函数中添加一句

GPIO_PinRemapConfig(GPIO_Remap_USART2,ENABLE);

最后程序如下,

#include“stm32f10x_lib.h”

FlagStatusRX_status;

FlagStatusTx_status;

voidRCC_cfg(void);

voidGPIO_cfg(void);

voidUSART_cfg(void);

voidNVIC_cfg(void);

unsignedcharRxbuf[20];

inTIndex_send,index_rev;

u8flag;

intmain()

{

inTI;

unsignedcharTxBuf1[]=“WelcometomySTM32!I‘midiot!”;

index_send=0;

index_rev=0;

flag=0;

RCC_cfg();

GPIO_cfg();

NVIC_cfg();

USART_cfg();

//清除标志位,否则第1位数据会丢失

USART_ClearFlag(USART2,USART_FLAG_TC);

//发送数据

//PB5的作用是显示正在发送数据

//当有数据在发送的时候,PB5会亮

for(i=0;TxBuf1[i]!=’\0‘;i++)

{

USART_SendData(USART2,TxBuf1[i]);

GPIO_SetBits(GPIOB,GPIO_Pin_5);

//等待数据发送完毕

while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET);

GPIO_ResetBits(GPIOB,GPIO_Pin_5);

}

while(1)

{

}

}

//RCC时钟配置

voidRCC_cfg()

{

//将RCC寄存器重新设置为默认值

RCC_DeInit();

//打开GPIO时钟,复用功能,串口1的时钟

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO,ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);

}

//IO口配置

voidGPIO_cfg()

{

GPIO_InitTypeDefGPIO_InitStructure;

//PAD5作为US2的TX端,打开复用,负责发送数据

GPIO_PinRemapConfig(GPIO_Remap_USART2,ENABLE);

GPIO_StrucTInit(&GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;

GPIO_Init(GPIOD,&GPIO_InitStructure);

//PD6作为US2的RX端,负责接收数据

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOD,&GPIO_InitStructure);

//LED显示串口正在发送/接收数据

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;

GPIO_Init(GPIOB,&GPIO_InitStructure);

}

//串口初始化

voidUSART_cfg()

{

USART_InitTypeDefUSART_InitStructure;

//将结构体设置为缺省状态

USART_StructInit(&USART_InitStructure);

//波特率设置为115200

USART_InitStructure.USART_BaudRate=115200;

//一帧数据的宽度设置为8bits

USART_InitStructure.USART_WordLength=USART_WordLength_8b;

//在帧结尾传输1个停止位

USART_InitStructure.USART_StopBits=USART_StopBits_1;

//奇偶失能模式,无奇偶校验

USART_InitStructure.USART_Parity=USART_Parity_No;

//发送/接收使能

USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;

//硬件流控制失能

USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;

//设置串口2

USART_Init(USART2,&USART_InitStructure);

//打开串口2的中断响应函数,接收中断

USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);

//打开串口2

USART_Cmd(USART2,ENABLE);

}

//配置中断

voidNVIC_cfg()

{

NVIC_InitTypeDefNVIC_InitStructure;

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//选择中断分组2

NVIC_InitStructur

标签:USART串口,STM32单片机,
来源:未知 作者:工程师周亮 时间:2018/11/20 16:10:00
相关阅读
推荐阅读
阅读排行
最近更新
商品推荐