初级会员

- 积分
- 68
- 金钱
- 68
- 注册时间
- 2021-6-12
- 在线时间
- 19 小时
|
1金钱
探索者开发板,串口1改串口2,能改的地方都改了,烧录到开发板就没有用,下面是我改的程序
#ifndef __USART_H_
#define __USART_H_
#include "stm32f4xx.h"
#include "stdio.h"
void uart_init(u32 bound);
void Usart_SendByte(USART_TypeDef *pUSARTx, uint8_t ch);
void Usart_SendString(USART_TypeDef *pUSARTx, char *str);
#endif
void uart_init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/*第一步 GPIO时钟使能,串口时钟使能*/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //GPIOA时钟使能
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//USART1时钟使能
/*第二步 设置引脚复用器映射*/
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2);//GPIOA9映射到串口1
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2);//GPIOA10映射到串口1
USART_DeInit(USART2);
/*第三步 初始化GPIO口*/
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2; //PA9 TX
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF; //复用
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;//推挽
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//50mhz
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOA,&GPIO_InitStructure); //配置GPIOA
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;//PA10 RX
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;//复用
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;//上拉
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;//推挽
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//50mhz
GPIO_Init(GPIOA,&GPIO_InitStructure); //配置GPIOA
/*第四步 串口参数初始化*/
USART_InitStructure.USART_BaudRate=bound; //波特率
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;//收发通信
USART_InitStructure.USART_Parity=USART_Parity_No;//不校验
USART_InitStructure.USART_StopBits=USART_StopBits_1;//停止位为1
USART_InitStructure.USART_WordLength=USART_WordLength_8b;//字长8位
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;//无硬件流
USART_Init(USART2,&USART_InitStructure); //配置串口
/*第五步 开启中断 初始化NVIC 使能中断*/
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); //开启串口1接收中断
NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn; //USART的IRQN
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; //使能
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1; //抢占优先级1
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1; //响应优先级1
NVIC_Init(&NVIC_InitStructure);//配置NVIC
/*使能串口*/
USART_Cmd(USART2,ENABLE);//串口使能
}
void Usart_SendByte(USART_TypeDef *pUSARTx, uint8_t ch)
{
USART_SendData(pUSARTx,ch);
while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TXE)==RESET);
}
void Usart_SendString(USART_TypeDef *pUSARTx, char *str)
{
unsigned int k=0;
do
{
Usart_SendByte(pUSARTx,str[k]);
k++;
}while(str[k]!='\0');
while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TC));
}
void USART2_IRQHandler(void)
{
u8 Res;
if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET) //接收中断,数据必须是0x0d,0x0a结尾
{
Res=USART_ReceiveData(USART2); //USART1->DR 读取接收数据
USART_SendData(USART2,Res);
}
}
下面是主函数
#include "stm32f4xx.h"
#include "delay.h"
#include "led.h"
#include "beep.h"
#include "key.h"
#include "usart.h"
int main(void)
{
uart_init(115200);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
Usart_SendByte(USART1,'w');
// Usart_SendString(USART1,"第一次写程序:");
// printf("wsw is wsw");
while(1)
{}
}
|
|