#include "Headfiles.h"
void RCC_HSE_Configuration(void);
void delay(u32 nCount);
void delay_us(u32 nus);
void delay_ms(u16 nms);
void delay_us( u32 nus )
{
u32 temp;
SysTick->LOAD=9*nus; //ʱ¼ä¼ÓÔØ
SysTick->VAL=0x00; //Çå¿Õ¼ÆÊýÆ÷
SysTick->CTRL=0x01; //¿ªÊ¼µ¹Êý
do
{
temp=SysTick->CTRL;
}while((temp&0x01)&&(!(temp&(1<<16))));
SysTick->CTRL=0x00;//Çå¿Õ¼ÆÊýÆ÷
SysTick->VAL=0x00; //¹Ø±Õ¼ÆÊýÆ÷
}
void delay_ms( u16 nms )
{
u32 temp;
SysTick->LOAD=9000*nms; //ʱ¼ä¼ÓÔØ
SysTick->VAL=0x00; //Çå¿Õ¼ÆÊýÆ÷
SysTick->CTRL=0x01; //¿ªÊ¼µ¹Êý
do
{
temp=SysTick->CTRL;
}while((temp&0x01)&&(!(temp&(1<<16))));
SysTick->CTRL=0x00;//Çå¿Õ¼ÆÊýÆ÷
SysTick->VAL=0x00; //¹Ø±Õ¼ÆÊýÆ÷
}
//ϵͳʱÖÓ³õʼ»¯
void RCC_HSE_Configuration(void)
{
RCC_DeInit();
RCC_HSEConfig( RCC_HSE_ON );
if( RCC_WaitForHSEStartUp()==SUCCESS )
{
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource()!=0X08);
}
}
void RCC_Configuration(void)
{
SystemInit();//72M
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
//LED
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 ; //TX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//FUYONGÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 ;//RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;// ¸¡¿ÕÊäÈë
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig( NVIC_PriorityGroup_1 );//·Ö×é
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0; //ÇÀÕ¼ÓÅÏȼ¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1; //ÏìÓ¦ÓÅÏȼ¶
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
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_Tx|USART_Mode_Rx;//Êä³öÊäÈëµÄʹÄÜ
USART_Init(USART2,&USART_InitStructure);//³õʼ»¯
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);//¿ªÆô½ÓÊÕÖжϣ¬½ÓÊÕÊý¾Ý²úÉúÖжÏ
USART_Cmd(USART2,ENABLE);//¿ªÆôÍâÉè
USART_ClearFlag(USART2,USART_FLAG_TC);//Çå¿Õ·¢ËÍÍê³É±ê־λ
}
int main (void) {
//target_init();
//OSInit();
RCC_Configuration(); //ʱÖÓ³õʼ»¯
GPIO_Configuration(); //¶Ë¿Ú³õʼ»¯
NVIC_Configuration(); //ÓÅÏȼ¶
USART_Configuration();
while(1);
}
中断程序
void USART2_IRQHandler(void)
{
u8 k=0;
if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET) //²úÉúÖжÏ
{
k=USART_ReceiveData(USART2);
USART_SendData(USART2,k+1);
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET); //·¢ËÍÊý¾Ý¼Ä´æÆ÷¿Õ±ê־λ ÊÇ·ñ·¢ËÍÍê³É
}
}
|