`帮忙看看我这段代码是有啥问题么,为什么可以给stm32板信号但是板子却没有实现相应得反应,提前谢谢了!!
在这里32 的程序没必要太复杂,就写一个串口程序方便测试就行。
以下的32 串口程序的功能是:将接受到的数据原封不动的发送回去。
led的功能只是一个指示功能,表示32系统正常运行。
#include <stm32f10x.h>
#include <SysTick.h>
void USART1_Init(u32 bound)
{
//端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
/* 配置IO口*/
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//TX PA9
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX PA10
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
//USART1 初始化设置
USART_InitStructure.USART_BaudRate = bound;//波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
USART_ClearFlag(USART1, USART_FLAG_TC);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启相关中断
//Usart1 NVIC 中断配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//串口1中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*********中断服务子程序************/
void USART1_IRQHandler(void)
{
unsigned char r;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
r =USART_ReceiveData(USART1);//(USART1->DR);
USART_SendData(USART1,r);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC) != SET);
}
USART_ClearFlag(USART1,USART_FLAG_TC);
}
/********LED IO口配置***********/
void LED_INIT()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13;//PC13
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_SetBits(GPIOC,GPIO_Pin_13);
}
/**************LED闪烁***************/
void display1(void)
{
GPIO_SetBits(GPIOC,GPIO_Pin_13);//点亮
delay_ms(1000);
GPIO_ResetBits(GPIOC,GPIO_Pin_13);//熄灭
delay_ms(1000);
}
/***********主函数**************/
int main()
{
SysTick_Init(72);
LED_INIT(); //led初始化
USART1_Init(9600);//串口初始化,自动开启串口中断,进行数据自动接收和发送
while(1)
{
display1();// led闪烁表示系统正常运行
}
}
以上代码我是亲测可用的。
参考:https://blog.csdn.net/qq_25939803/article/details/104962112 。
抱歉,以上参考资料没有提及具体的串口通讯问题描述和代码,无法给出解决方案。请提供更具体的问题和代码,让我能够更好地为您解决问题。谢谢。