#include"stm32f10x.h"
/*************************/
u8 tim_flag_ok = 0; //进入中断标志位
/***************************/
void Delay(u32 x) //延时函数
{
while(x--);
}
/***step1***时钟设置,启动TIM2***/
void RCC_Configuration(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //TIM2~TIM7挂在 RCC_APB1上
}
/***step2*** 中断设置***/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
//NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel; //这是旧版本的
//NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;//TIM1没有TIM2_IRQn这个结构
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/****step3***TIM2模块设置**/
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_TimeBaseInitStructure.TIM_Period = 1000;
TIM_TimeBaseInitStructure.TIM_Prescaler = 35999;//装载值要累加的基数3600为0.001s
TIM_TimeBaseInitStructure.TIM_ClockDivision = 0x0;//设置时间分割
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;//选择计数器模式
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
TIM_ClearFlag(TIM2, TIM_FLAG_Update);//清标志
//TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);//方法1标志位更新
TIM_ITConfig(TIM2,TIM_IT_CC1 ,ENABLE);//方法2中断溢出允许
TIM_Cmd(TIM2,ENABLE);
}
/***step4***中断服务子函数**/
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2,TIM_IT_CC1)!=RESET) //方法一
{
TIM_ClearITPendingBit(TIM2,TIM_IT_CC1); //清空标志位
tim_flag_ok = 1;
}
/* if(TIM_GetITStatus(TIM2,TIM_IT_Update)==SET) //方法二
{
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
//TIM_ClearITPendingBit(TIM2 , TIM_FLAG_Update);
tim_flag_ok = 1;
} */
}
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOF,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_SetBits(GPIOF,GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9);
RCC_Configuration();
NVIC_Configuration();
TIM_Configuration();
if(tim_flag_ok==1)
{
tim_flag_ok=0;
GPIO_ResetBits(GPIOF,GPIO_Pin_6);
Delay(0x3fffff);
GPIO_SetBits(GPIOF,GPIO_Pin_6);
}
}
以上是我开始写的程序 但灯就是不亮 我怀疑是没进中断 把主程序改成如下
while(1)
{
if(tim_flag_ok==1)
{
tim_flag_ok=0;
GPIO_ResetBits(GPIOF,GPIO_Pin_6);
Delay(0x3fffff);
GPIO_SetBits(GPIOF,GPIO_Pin_6);
}
}
即可 原因还是不明 |
| |
| |
|