stm32串口通讯一个很奇怪的问题 如果能够被大神解决 定不吝惜分数,谢谢了
#include "stm32f10x.h"
u16 sheng[]={1,2,3,4,5,6,7,8,9};
int i=0;
void GPIO_Configuration()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;//设置管脚模式为模拟输入
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void RCC_Configuration()
{
RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC|RCC_APB2Periph_USART1,ENABLE);//使能时钟
}
void USART_Configuration()
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate=115200;//设置波特率
USART_InitStructure.USART_WordLength=USART_WordLength_8b;//设置数据位长度
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_Init( USART1, &USART_InitStructure);
}
int main()
{
RCC_Configuration();
GPIO_Configuration();
USART_Configuration();
USART_Cmd( USART1, ENABLE);
USART_ClearFlag(USART1,USART_FLAG_TC);
delay(10);
while(i<9)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
USART_ClearFlag(USART1,USART_IT_TC);
USART_SendData(USART1,sheng[i]);
i++;
}
}
这个程序运行后PC机上串口小助手不显示接收数据 但奇怪的是只要把while(i<9)改成while(1)就接收数据,希望大神们能帮忙看看问题出在哪里
[解决办法]
while(1)
{
for (i = 0; i < 9; i++)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
USART_ClearFlag(USART1,USART_IT_TC);
USART_SendData(USART1,sheng[i]);
}
}//这样靠谱么?
[解决办法]
本帖最后由 wangfan027 于 2013-05-30 11:59:54 编辑
int main()
{
RCC_Configuration();
GPIO_Configuration();
USART_Configuration();
USART_Cmd( USART1, ENABLE);
USART_ClearFlag(USART1,USART_FLAG_TC);
delay(10);
while(i<9)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
USART_ClearFlag(USART1,USART_IT_TC);
USART_SendData(USART1,sheng[i]);
i++;
}
while(1);
}