程序是按照STM32自学笔记来写的,程序不长,请路过的大神帮帮忙
#include"stm32f10x_lib.h"
void RCC_CF(void);
void GPIO_CF(void);
void USART_CF(void);
int main(void)
{
vu16 i=0;
RCC_CF();//ÉèÖÃϵͳʱÖÓ
GPIO_CF();//ÉèÖÃGPIO¿Ú
USART_CF();//ÉèÖÃUSART
//GPIO_ResetBits(GPIOA,GPIO_Pin_6);
while(1)
{ /*下面这个if语句为什么在不发送数据的时候也会执行里面的语句,使状态灯亮起来呢?
if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)==SET)
{ GPIO_SetBits(GPIOA,GPIO_Pin_6);
USART_SendData(USART1,USART_ReceiveData(USART1));
for(i=0;i<500;i++);
}
}
}
/*ʱÖÓº¯Êý*/
void RCC_CF(void)
{
ErrorStatus HSEStartUpStatus; /*¶¨Òåö¾ÙÀàÐͱäÁ¿*/
RCC_DeInit(); /*¸´Î»ÏµÍ³Ê±ÖÓÉèÖÃ*/
RCC_HSEConfig(RCC_HSE_ON); /*¿ªÆôHSE*/
HSEStartUpStatus=RCC_WaitForHSEStartUp(); /*µÈ´ýHSEÆðÕñ²¢Îȶ¨*/
if(HSEStartUpStatus==SUCCESS)
{
RCC_HCLKConfig(RCC_SYSCLK_Div1); /*Ñ¡ÔñHCLKʱÖÓԴΪSYSCLK1·ÖƵ*/
RCC_PCLK2Config(RCC_HCLK_Div1); /*Ñ¡ÔñPCLK2ʱÖÓԴΪHCLK1·ÖƵ*/
RCC_PCLK1Config(RCC_HCLK_Div2);
FLASH_SetLatency(FLASH_Latency_2); /*ÉèÖÃFlashÑÓʱÖÜÆÚÊýΪ2*/
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); /*ʹÄÜFlashÓûÈ¡»º´æ*/
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);
RCC_PLLCmd(ENABLE); /*ʹÄÜPLL*/
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);/*µÈ´ýPLLÊä³öÎȶ¨*/
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource()!=0x08);
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA,ENABLE);
}
void GPIO_CF(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
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;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
void USART_CF(void)
{
USART_InitTypeDef USART_InitStructure;
//USART_ClockInitTypeDef USART_ClockInitStructure;
USART_InitStructure.USART_BaudRate=9600;
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);
} |