新手入门
- 积分
- 17
- 金钱
- 17
- 注册时间
- 2022-8-8
- 在线时间
- 4 小时
|
1金钱
帮忙解决一下,xcom接收不到的问题
下面是代码:
#include "stm32f4xx.h"
#include "usart.h"
#include "delay.h"
void my_usart1_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//串口时钟使能
RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOA,ENABLE);//GPIO时钟使能
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);//引脚复用映射
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);//引脚复用映射
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;//模式设为复用
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);//GPIO初始化
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;//模式设为复用
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);//GPIO初始化
USART_InitStructure.USART_BaudRate=115200;//设置传输速率
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;//不使用硬件流控制
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;//串口使能设置
USART_InitStructure.USART_Parity=USART_Parity_No;//不使用奇偶校验
USART_InitStructure.USART_StopBits=USART_StopBits_1;//设置一个停止位
USART_InitStructure.USART_WordLength=USART_WordLength_8b;//使用8位的字长
USART_Init(USART1,&USART_InitStructure);//串口初始化
USART_Cmd(USART1,ENABLE);//串口使能
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//中断使能函数
NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;//串口1的通道
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;//使能通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;//抢占优先级设置
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;//响应优先级设置
NVIC_Init(&NVIC_InitStructure);//初始化NVIC
}
void USART1_IRQHandler(void)//中断服务函数
{
u8 res;
if(USART_GetITStatus(USART1,USART_IT_RXNE))//判断中断1是否发生
{
res=USART_ReceiveData(USART1);
USART_SendData(USART1,res);
}
}
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//分组2
my_usart1_init();
while(1);
}
|
最佳答案
查看完整内容[请看2#楼]
RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOA,ENABLE);//GPIO时钟使能
应该是 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
|