新手入门
- 积分
- 4
- 金钱
- 4
- 注册时间
- 2021-9-23
- 在线时间
- 0 小时
|
1金钱
#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
void My_USART_Init(void)
{
GPIO_InitTypeDef GPIO_Init_Struct;
USART_InitTypeDef USART_Init_Struct;
NVIC_InitTypeDef NVIC_Init_Struct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
USART_DeInit(USART1);//复位串口1
GPIO_Init_Struct.GPIO_Mode = GPIO_Mode_AF_PP ;
GPIO_Init_Struct.GPIO_Pin = GPIO_Pin_9 ;
GPIO_Init_Struct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_Init_Struct);
GPIO_Init_Struct.GPIO_Mode = GPIO_Mode_IPU ;
GPIO_Init_Struct.GPIO_Pin = GPIO_Pin_10 ;
GPIO_Init(GPIOA,&GPIO_Init_Struct);
USART_Init_Struct.USART_BaudRate = 9600 ; //波特率
USART_Init_Struct.USART_HardwareFlowControl = USART_HardwareFlowControl_None ; //硬件流
USART_Init_Struct.USART_Mode = USART_Mode_Rx|USART_Mode_Tx; // 使能发送和接收
USART_Init_Struct.USART_Parity =USART_Parity_No ; // 奇偶校验与否
USART_Init_Struct.USART_StopBits = USART_StopBits_1 ;
USART_Init_Struct.USART_WordLength = USART_WordLength_8b ;
USART_Init(USART1,&USART_Init_Struct); //初始化串口
USART_Cmd(USART1,ENABLE); //使能串口
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); // 开启接收中断
NVIC_Init_Struct.NVIC_IRQChannel = USART1_IRQn ;
NVIC_Init_Struct.NVIC_IRQChannelCmd = ENABLE ;
NVIC_Init_Struct.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_Init_Struct.NVIC_IRQChannelSubPriority = 3;
NVIC_Init(&NVIC_Init_Struct);
}
void USART1_IRQHandler_1(void){
u8 res;
if(USART_GetITStatus(USART1,USART_IT_RXNE))
{
res = USART_ReceiveData(USART1);
USART_SendData(USART1,res);
}
}
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
My_USART_Init();
delay_init();
LED_Init();
GPIO_SetBits(GPIOD,GPIO_Pin_2);
while(1)
{
GPIO_SetBits(GPIOA,GPIO_Pin_8);
delay_ms(500);
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
delay_ms(500);
};
}
有没有大佬能帮我看看代码的问题出在哪里?发送了却没有回应
|
|