#include "stm32f10x.h"
#include <stdio.h>
#include <string.h>
void RCC_cfg(void);
void GPIO_cfg(void);
void NVIC_cfg(void);
void TIMER_cfg(void);
int main(void)
{
RCC_cfg();
GPIO_cfg();
NVIC_cfg();
TIMER_cfg();
TIM_Cmd(TIM2,ENABLE);
while(1);
}
void RCC_cfg(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF,ENABLE);
}
void GPIO_cfg(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOF,&GPIO_InitStructure);
}
void NVIC_cfg(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIMER_cfg(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_DeInit(TIM2);
TIM_InternalClockConfig(TIM2);
TIM_TimeBaseStructure.TIM_Prescaler=36000-1;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period=3000-1;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
TIM_ClearFlag(TIM2,TIM_FLAG_Update);
TIM_ARRPreloadConfig(TIM2,DISABLE);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
}
void TIM2_IRQHandler(void)
{
u8 ReadValue;
if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET)
{
TIM_ClearITPendingBit(TIM2,TIM_FLAG_Update);
ReadValue=GPIO_ReadOutputDataBit(GPIOF,GPIO_Pin_9);//
if(ReadValue==0)
{
GPIO_SetBits(GPIOF,GPIO_Pin_9);
//GPIO_ResetBits(GPIOF,GPIO_Pin_7);
}
else
{
GPIO_ResetBits(GPIOF,GPIO_Pin_9);
//GPIO_SetBits(GPIOF,GPIO_Pin_7);
}
}
}