stm32定时器2输出10KHZ波形
紧接上一篇文章
/*! @function
********************************************************************************
<PRE>
º¯ÊýÃû :RCC_Configuration
¹¦ÄÜ :ʱÖÓÅäÖÃ
----------------------------------------------------------------------------------
²ÎÊý :ÎÞ
·µ»ØÖµ :ÎÞ
---------------------------------------------------------------------------------
ʹÓõÄÈ«¾Ö±äÁ¿£ºÎÞ
----------------------------------------------------------------------------------
×÷Õß :
ʱ¼ä : 2012/10/17
</PRE>
*******************************************************************************/
static void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;
RCC_ClocksTypeDef RccClocks;
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* Ñ¡ÔñHCLK£¨AHB£©Ê±ÖÓԴΪSYSCLK 1·ÖƵ */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* Ñ¡ÔñPCLK2ʱÖÓԴΪ HCLK£¨AHB£© 1·ÖƵ */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* Ñ¡ÔñPCLK1ʱÖÓԴΪ HCLK£¨AHB£© 2·ÖƵ */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* ÉèÖÃFLASHÑÓʱÖÜÆÚÊýΪ2 */
FLASH_SetLatency(FLASH_Latency_2);
/* ʹÄÜFLASHԤȡ»º´æ */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Ñ¡ÔñËøÏà»·£¨PLL£©Ê±ÖÓԴΪHSE 1·ÖƵ£¬±¶ÆµÊýΪ9£¬ÔòPLLÊä³öƵÂÊΪ 8MHz * 9 = 72MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* ʹÄÜPLL */
RCC_PLLCmd(ENABLE);
/* µÈ´ýPLLÊä³öÎȶ¨ */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
/* Ñ¡ÔñSYSCLKʱÖÓԴΪPLL */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* µÈ´ýPLL³ÉΪSYSCLKʱÖÓÔ´ */
while(RCC_GetSYSCLKSource() != 0x08);
}
/* ÅäÖÃSysTick */
RCC_GetClocksFreq(&RccClocks);
//SysTick_Config(RccClocks.HCLK_Frequency / OS_TICKS_PER_SEC);
/* ´ò¿ªGPIOʱÖÓ */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | \
RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD| RCC_APB2Periph_GPIOE,\
ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
}
/*! @function
********************************************************************************
<PRE>
º¯ÊýÃû :NVIC_Configuration
¹¦ÄÜ :ÖжϿØÖÆÆ÷ÅäÖÃ
----------------------------------------------------------------------------------
²ÎÊý :ÎÞ
·µ»ØÖµ :ÎÞ
---------------------------------------------------------------------------------
ʹÓõÄÈ«¾Ö±äÁ¿£ºÎÞ
----------------------------------------------------------------------------------
×÷Õß :
ʱ¼ä : 2012/10/17
</PRE>
*******************************************************************************/
static void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#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 , 0);
#endif
/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/* Enable the USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable the USART2 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable the USART3 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable the TIMER3 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable the TIMER3 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//72M timer2
//ÖжÏʱ¼ä£º£¨720*10£©/72000000=0.1Ms=10K
void Timer2_config() //ucosiiʱÖÓ
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Prescaler = 360-1; //36M/360=100K HZ
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //ÏòÉϼÆÊý
TIM_TimeBaseStructure.TIM_Period = 10; // ; //×°ÔØֵѡÔñ 10K
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
//TIM_TimeBaseStructure.TIM_RepetitionCounter = 4;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM2,TIM_IT_Update|TIM_IT_Trigger,ENABLE);
TIM_Cmd(TIM2, ENABLE);
}
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //¼ì²éÖ¸¶¨µÄTIMÖжϷ¢ÉúÓë·ñ:TIM ÖжÏÔ´
{
if(Ledflg==0)
{
GPIO_SetBits(GPIOC, GPIO_Pin_6);
GPIO_SetBits(GPIOC, GPIO_Pin_7);
Ledflg=1;
}
else
{
GPIO_ResetBits(GPIOC, GPIO_Pin_6);
GPIO_ResetBits(GPIOC, GPIO_Pin_7);
Ledflg=0;
}
Ledcnt=0;
}
TIM_ClearITPendingBit(TIM2, TIM_IT_Update ); //Çå³ýTIMxµÄÖжϴý´¦Àíλ:TIM ÖжÏÔ´
}