/* Includes ------------------------------------------------------------------*/ #include "stm32f10x_lib.h" #include "stdio.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ u8 USART2_Counter=0; u8 USART2_Send_Flage=0;
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void); /* 系统时钟设置 */ void GPIO_Configuration(void); /* 端口设置 */ void NVIC_Configuration(void); /* NVIC中断向量设置 */ /* Private functions ---------------------------------------------------------*/
/******************************************************************************* * Function Name : main * Description : Main program * Input : None * Output : None * Return : None *******************************************************************************/ int main(void) {
/* 系统时钟设置 */ RCC_Configuration(); /* NVIC中断向量设置 */ NVIC_Configuration();
/* 端口设置 */ GPIO_Configuration();
/*系统时钟设置 HCLK=36MKZ,SysTick=4.5MKZ,Reload=49500,每11ms 产生一个中断,开系统时钟中断*/
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); SysTick_SetReload(49500); SysTick_ITConfig(ENABLE); SysTick_CounterCmd(SysTick_Counter_Enable); while(1) { if(USART2_Send_Flage==1) { GPIO_WriteBit(GPIOA, GPIO_Pin_0,(BitAction)~GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_0)); USART2_Send_Flage=0; } } }
/******************************************************************************* * Function Name : RCC_Configuration * Description : 配置不同的系统时钟. * Input : None * Output : None * Return : None *******************************************************************************/ void RCC_Configuration(void) { ErrorStatus HSEStartUpStatus; /* RCC system reset(for debug purpose) */ RCC_DeInit();
/* Enable HSE */ RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */ HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS) { /* HCLK = SYSCLK =36MHZ*/ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK2 = HCLK=36MHZ */ RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 =18MHZ*/ RCC_PCLK1Config(RCC_HCLK_Div2);
/* Flash 2 wait state */ FLASH_SetLatency(FLASH_Latency_2); /* Enable Prefetch Buffer */ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/*系统时钟36MK PLLCLK = 4MHz * 9 = 36MHz */ RCC_PLLConfig(RCC_PLLSource_HSE_Div2, RCC_PLLMul_9);
/* 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) { } }
/* 开 GPIOA时钟 36MHZ*/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
}
/******************************************************************************* * Function Name : GPIO_Configuration * Description : 配置不同的GPIO引脚. * Input : None * Output : None * Return : None *******************************************************************************/ void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/******************************************************************************* * Function Name : NVIC_Configuration * Description : Configures Vector Table base location. * Input : None * Output : None * Return : None *******************************************************************************/ void NVIC_Configuration(void) { #ifdef VECT_TAB_RAM /* Set the Vector Table base location at 0x20000000 */ NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); #else /* VECT_TAB_FLASH */ /* Set the Vector Table base location at 0x08000000 */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); #endif
}
#ifdef DEBUG /******************************************************************************* * Function Name : assert_failed * Description : Reports the name of the source file and the source line number * where the assert error has occurred. * Input : - file: pointer to the source file name * - line: assert error line source number * Output : None * Return : None *******************************************************************************/ void assert_failed(u8* file, u32 line) { /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d
", file, line) */
/* Infinite loop */ while (1) { } } #endif
中断处理/******************************************************************************* * Function Name : SysTickHandler * Description : This function handles SysTick Handler. * Input : None * Output : None * Return : None *******************************************************************************/ void SysTickHandler(void) { extern u8 USART2_Counter; extern u8 USART2_Send_Flage; USART2_Counter++; if(USART2_Counter==300) // 系统时钟每11ms产生一个中断,则3.3s则产生300次中断。3.3s GPIOA的引脚翻转一次,所接的LCD指示灯应该闪烁一次。 { USART2_Send_Flage=1; USART2_Counter=0; } }
但是我所设置的指示灯一直都没有亮过,我把设置GPIO翻转写在中断里也是同样的情况,我怀疑没有进入系统时钟中断。我主要的目的是实现用系统时钟定时,指示灯只是一个测试的单元。 为什么会出现这种情况,请高手指点!!谢谢!! |