[mw_shl_code=c,true]#include "stm32f10x.h"
#include "delay.h"
/************************************************
ALIENTEK ????STM32F103??·?°????é0
?¤????°?
************************************************/
void RCC_Init(void);
void USART1_IRQHandler(void);
void USART1_Init(void);
int main(void)
{
RCC_Init();
USART1_Init();
USART1_IRQHandler();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //??????????·?×é
while(1)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_3);
delay_ms(300);
GPIO_SetBits(GPIOA,GPIO_Pin_3);
delay_ms(300);
};
}
void USART1_Init(void)
{
//GPIO?????è??
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOD,ENABLE); //GPIO???±???è??
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); //?®???±???è??
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //RX
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; //TX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //LED0-->

D.2 ????????
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //???ì????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO????????50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //?ù???è?¨??????????GPIOD.2
GPIO_SetBits(GPIOA,GPIO_Pin_3); //PD.2 ??????
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART ???????è??
USART_InitStructure.USART_BaudRate = 9600; //?¨????
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_InitStructure.USART_WordLength = USART_WordLength_8b; //×??¤
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3 ; //??????????3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×???????3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ?¨??????
NVIC_Init(&NVIC_InitStructure); //?ù?????¨????????????VIC?????÷
USART_Init(USART1,&USART_InitStructure); //?ù?????¨????????????VIC?????÷
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //?????®??????????
USART_Cmd(USART1,ENABLE); //?®??????
}
void USART1_IRQHandler(void)
{
u8 onyourmark;
if(USART_GetITStatus(USART1, USART_IT_RXNE)) // ????????
{
onyourmark = USART_ReceiveData(USART1); //?????????®????????
USART_SendData(USART1, onyourmark); //·?????????????
}
}
void RCC_Init(void)
{
ErrorStatus HSEStartUpStatus;
NVIC_InitTypeDef NVIC_InitStructure;
/* SYSCLK, HCLK, PCLK2 and PCLK1 configuration -----------------------------*/
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
/* ?????±?????????????????????±??/2?????à?·?????±?? ??????72M */
if(HSEStartUpStatus == SUCCESS)
{
/* Enable Prefetch Buffer */
// FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state */
// FLASH_SetLatency(FLASH_Latency_2);
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* APB2 PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* APB1 PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* APB1 ADCCLK = HCLK/8 */
RCC_ADCCLKConfig(RCC_PCLK2_Div8);
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div2, RCC_PLLMul_9); // 16 /2 * 9 = 72
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08);
}/* endof if(HSEStartUpStatus == SUCCESS) */
/* ?????±???????§°??????????·???? */
else /* elseof if (HSEStartUpStatus == SUCCESS) */
{
while(1);
// SYSRESET(); //????????
}
/* Enable and configure RCC global IRQ channel */
NVIC_InitStructure.NVIC_IRQChannel = RCC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
[/mw_shl_code]