先是參考http://wenku.baidu.com/view/78f6b1350b4c2e3f572763e9.html調(diào)通了usart1
然后將程序進(jìn)行修改,對(duì)Usart2進(jìn)行配置,配置完了之后,程序還是沒有正確,然后在voidGPIO_cfg();函數(shù)中添加一句
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();
//清除標(biāo)志位,否則第1位數(shù)據(jù)會(huì)丟失
USART_ClearFlag(USART2,USART_FLAG_TC);
//發(fā)送數(shù)據(jù)
//PB5的作用是顯示正在發(fā)送數(shù)據(jù)
//當(dāng)有數(shù)據(jù)在發(fā)送的時(shí)候,PB5會(huì)亮
for(i=0;TxBuf1[i]!=’\0‘;i++)
{
USART_SendData(USART2,TxBuf1[i]);
GPIO_SetBits(GPIOB,GPIO_Pin_5);
//等待數(shù)據(jù)發(fā)送完畢
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET);
GPIO_ResetBits(GPIOB,GPIO_Pin_5);
}
while(1)
{
}
}
//RCC時(shí)鐘配置
voidRCC_cfg()
{
//將RCC寄存器重新設(shè)置為默認(rèn)值
RCC_DeInit();
//打開GPIO時(shí)鐘,復(fù)用功能,串口1的時(shí)鐘
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端,打開復(fù)用,負(fù)責(zé)發(fā)送數(shù)據(jù)
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端,負(fù)責(zé)接收數(shù)據(jù)
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD,&GPIO_InitStructure);
//LED顯示串口正在發(fā)送/接收數(shù)據(jù)
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;
//將結(jié)構(gòu)體設(shè)置為缺省狀態(tài)
USART_StructInit(&USART_InitStructure);
//波特率設(shè)置為115200
USART_InitStructure.USART_BaudRate=115200;
//一幀數(shù)據(jù)的寬度設(shè)置為8bits
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
//在幀結(jié)尾傳輸1個(gè)停止位
USART_InitStructure.USART_StopBits=USART_StopBits_1;
//奇偶失能模式,無(wú)奇偶校驗(yàn)
USART_InitStructure.USART_Parity=USART_Parity_No;
//發(fā)送/接收使能
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
//硬件流控制失能
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
//設(shè)置串口2
USART_Init(USART2,&USART_InitStructure);
//打開串口2的中斷響應(yīng)函數(shù),接收中斷
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
評(píng)論
查看更多