我的板子是原子哥的STM32F407ZGT6型号 我这个串口程序,在单步调试时,在发送数据,就一直卡在 while(!(USART_GetFlagStatus(USART1, USART_FLAG_TC))) // 判断数据是否发送完成 这句话,通过使用示波器,发现真的没有数据发送,而且使用电脑的USB转串口发送数据,也无法接收。请高手帮忙看一下是什么问题
#include "main.h"
void LED_Init(void);
void uart_init(u32 bound);
#define USART_REC_LEN 200 //
#define EN_USART1_RX 1 //
u8 USART_RX_BUF[USART_REC_LEN]; //
u16 USART_RX_STA;
void uart_init(u32 bound);
u8 USART_RX_BUF[USART_REC_LEN];
u16 USART_RX_STA=0;
void uart_init(u32 bound){
//
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//
//GPIO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10|GPIO_Pin_2|GPIO_Pin_3; //GPIOA9?GPIOA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //??50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //??????
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //??
GPIO_Init(GPIOA,&GPIO_InitStructure); //???PA9,PA10
//
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); //
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); //
//USART1
USART_InitStructure.USART_BaudRate = bound;//
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//
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; //IRQ????
NVIC_Init(&NVIC_InitStructure); //??????????VIC????
}
void USART1_IRQHandler(void)
{
u8 Res;
LED_DS0(1);
LED_DS1(1);
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
Res =USART_ReceiveData(USART1);//(USART1->DR);
LED_DS0(1);
LED_DS1(1);
if((USART_RX_STA&0x8000)==0)//
{
if(USART_RX_STA&0x4000)//
{
if(Res!=0x0a)USART_RX_STA=0;//
else USART_RX_STA|=0x8000; //
}
else //
{
if(Res==0x0d)USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//
}
}
}
}
}
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init(168);
uart_init(9600);
LED_Init();
LED_DS0(0);
LED_DS1(0);
if((((USART1->CR1)&(uint32_t)USART_CR1_UE)>>13)==0 ) // 确认串口是否使能
{
LED_DS0(1);
}
LED_DS0(0);
while((((USART1->CR1)&((uint32_t)0x00000004))>>2)==0 ) // 判断接收器是否打开,如果没打开进入while
{
LED_DS0(1);
USART1->CR1|=((uint32_t)0x00000004); // 打开接收器
}
LED_DS0(0);
while((((USART1->CR1)&((uint32_t)0x00000008))>>3)==0 ) // 判断发送器是否打开,如果没打开进入while
{
LED_DS1(1);
USART1->CR1|=((uint32_t)0x00000008); // 打开发送器
}
LED_DS1(0);
///////////////////////////
while(1)
{
while((((USART1->SR)&USART_FLAG_TXE)>>7)==0) // 判断数据是否移到移位寄存器,如果没有,那么进入while 且警示灯亮
{
LED_DS0(1);
}
while(1)
{
USART_SendData(USART1, 0x01); // 发送数据0x01
LED_DS0(0);
while(!(USART_GetFlagStatus(USART1, USART_FLAG_TC))) // 判断数据是否发送完成
{
LED_DS1(1);
LED_DS0(1);
}
LED_DS0(0);
USART_ClearFlag(USART1,USART_FLAG_TC); // 清除发送完成标志位
}
}
return 0;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>