赞
踩
- #ifndef __TIMER1_H
- #define __TIMER1_H
- #include "stm32f10x.h"
-
- void TIM1_Init(unsigned int arr,unsigned int psc);
-
- #endif
- #include "sys.h"
- #include "timer1.h"
-
- //arr:自动重装值
- //psc:时钟预分频数
- void TIM1_Init(unsigned int arr,unsigned int psc)
- {
-
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //使能定时器1时钟
-
- //初始化TIM1
- TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
- TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
- TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
-
- TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //参数初始化
- TIM_ClearFlag(TIM1, TIM_FLAG_Update);//清中断标志位
- TIM_ITConfig(TIM1, TIM_IT_Update|TIM_IT_Trigger,ENABLE ); //使能指定的TIM1中断,允许更新中断 允许触发中断源
-
- //中断优先级NVIC设置
- NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn; //TIM1中断
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
- NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
-
- TIM_Cmd(TIM1, ENABLE); //使能TIM1
-
- }
-
- //定时器1中断服务程序
- void TIM1_UP_IRQHandler(void) //TIM8中断
- {
- if(TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)//检查指定的TIM中断发生与否:TIM 中断源
- {
- //应用代码编写区
-
- TIM_ClearITPendingBit(TIM1, TIM_IT_Update);//清除TIMx的中断待处理位:TIM 中断源
- }
- }
- #ifndef __TIMER8_H
- #define __TIMER8_H
- #include "stm32f10x.h"
-
- void TIM8_Init(unsigned int arr,unsigned int psc);
-
- #endif
- #include "sys.h"
- #include "timer8.h"
-
- //arr:自动重装值
- //psc:时钟预分频数
- void TIM8_Init(unsigned int arr,unsigned int psc)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE); //使能定时器1时钟
-
- //初始化TIM8
- TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
- TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
- TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
-
- TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); //参数初始化
- TIM_ClearFlag(TIM8, TIM_FLAG_Update);//清中断标志位
- TIM_ITConfig(TIM8, TIM_IT_Update|TIM_IT_Trigger,ENABLE ); //使能指定的TIM8中断,允许更新中断 允许触发中断源
-
- //中断优先级NVIC设置
- NVIC_InitStructure.NVIC_IRQChannel = TIM8_UP_IRQn; //TIM8中断
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
- NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
-
- TIM_Cmd(TIM8, ENABLE); //使能TIM8
-
- }
-
- //定时器8中断服务程序
- void TIM8_UP_IRQHandler(void) //TIM8中断
- {
- if(TIM_GetITStatus(TIM8, TIM_IT_Update) != RESET)//检查指定的TIM中断发生与否:TIM 中断源
- {
- //代码应用区
-
- TIM_ClearITPendingBit(TIM8, TIM_IT_Update);//清除TIMx的中断待处理位:TIM 中断源
- }
- }
- #ifndef __TIMER2_H
- #define __TIMER2_H
- #include "stm32f10x.h"
-
- void TIM2_Init(unsigned int arr,unsigned int psc);
-
- #endif
- #include "sys.h"
- #include "timer2.h"
-
- //arr:自动重装值
- //psc:时钟预分频数
- void TIM2_Init(unsigned int arr, unsigned int psc)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //时钟使能
- TIM_ClearITPendingBit(TIM2,TIM_IT_Update);//清除中断标志位
-
- TIM_TimeBaseStructure.TIM_Period = arr; //自动重装载周期值
- TIM_TimeBaseStructure.TIM_Prescaler =psc; //预分频值
- TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
- TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx
-
- TIM_ITConfig( TIM2,TIM_IT_Update | TIM_IT_Trigger, ENABLE );//使能更新触发TIM中断
-
- //中断分组设置
- NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //TIM2中断
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占优先级0级
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
- NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
-
- TIM_Cmd(TIM2, ENABLE); //使能TIMx外设
-
- }
-
- //定时器2中断服务程序
- void TIM2_IRQHandler(void)
- {
- //判断TIM2更新中断是否发生
- if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET)
- {
- //代码应用区
-
- TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
- }
- }
- #ifndef __TIMER3_H
- #define __TIMER3_H
- #include "stm32f10x.h"
-
- void TIM3_Init(unsigned int arr,unsigned int psc);
-
- #endif
- #include "sys.h"
- #include "timer3.h"
-
- //arr:自动重装值
- //psc:时钟预分频数
- void TIM3_Init(unsigned int arr, unsigned int psc)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟使能
-
- TIM_TimeBaseStructure.TIM_Period = arr; //自动重装载周期值
- TIM_TimeBaseStructure.TIM_Prescaler =psc; //预分频值
- TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
- TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx
-
- TIM_ITConfig( TIM3,TIM_IT_Update | TIM_IT_Trigger, ENABLE );//使能更新触发TIM中断
-
- //中断分组设置
- NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中断
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占优先级0级
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
- NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
-
- TIM_Cmd(TIM3, ENABLE); //使能TIMx外设
- }
-
- //中断服务程序
- void TIM3_IRQHandler(void)
- {
- if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
- {
- //代码应用区
-
- TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //清除TIMx的中断待处理位:TIM 中断源
- }
- }
- #ifndef __TIMER4_H
- #define __TIMER4_H
- #include "stm32f10x.h"
-
- void TIM4_Init(unsigned int arr,unsigned int psc);
-
- #endif
- #include "sys.h"
- #include "timer4.h"
-
- //arr:自动重装值
- //psc:时钟预分频数
- void TIM4_Init(unsigned int arr, unsigned int psc)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //时钟使能
-
- TIM_TimeBaseStructure.TIM_Period = arr; //自动重装载周期值
- TIM_TimeBaseStructure.TIM_Prescaler =psc; //预分频值
- TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
- TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx
-
- TIM_ITConfig( TIM4,TIM_IT_Update | TIM_IT_Trigger, ENABLE );//使能更新触发TIM中断
-
- //中断分组设置
- NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //TIM4中断
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占优先级0级
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //从优先级3级
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
- NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
-
- TIM_Cmd(TIM4, ENABLE); //使能TIMx外设
- }
-
- //定时器4中断服务程序
- void TIM4_IRQHandler(void)
- {
- //判断TIM4更新中断是否发生
- if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
- {
- //代码应用区
-
- TIM_ClearITPendingBit(TIM4, TIM_IT_Update ); //清除TIMx的中断待处理位:TIM 中断源
- }
- }
- #ifndef __TIMER5_H
- #define __TIMER5_H
- #include "stm32f10x.h"
-
- void TIM5_Init(unsigned int arr,unsigned int psc);
-
- #endif
- #include "sys.h"
- #include "timer5.h"
-
- //arr:自动重装值
- //psc:时钟预分频数
- void TIM5_Init(unsigned int arr, unsigned int psc)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); //时钟使能
- TIM_ClearITPendingBit(TIM5,TIM_IT_Update);//清除中断标志位
-
- TIM_TimeBaseStructure.TIM_Period = arr; //自动重装载周期值
- TIM_TimeBaseStructure.TIM_Prescaler =psc; //预分频值
- TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
- TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx
-
- TIM_ITConfig( TIM5,TIM_IT_Update | TIM_IT_Trigger, ENABLE );//使能更新触发TIM中断
-
- //中断分组设置
- NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn; //TIM5中断
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占优先级0级
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
- NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
-
- TIM_Cmd(TIM5, ENABLE); //使能TIMx外设
-
- }
-
- //定时器5中断服务程序
- void TIM5_IRQHandler(void)
- {
- //判断TIM5更新中断是否发生
- if(TIM_GetITStatus(TIM5,TIM_IT_Update)!=RESET)
- {
- //代码应用区
-
- TIM_ClearITPendingBit(TIM5,TIM_IT_Update);
- }
- }
- #ifndef __TIMER6_H
- #define __TIMER6_H
- #include "stm32f10x.h"
-
- void TIM6_Init(unsigned int arr,unsigned int psc);
-
- #endif
- #include "sys.h"
- #include "timer6.h"
-
- //arr:自动重装值
- //psc:时钟预分频数
- void TIM6_Init(unsigned int arr, unsigned int psc)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE); //时钟使能
-
- TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 计数到5000为500ms
- TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值 10Khz的计数频率
- TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
- TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
-
- TIM_ITConfig( TIM6,TIM_IT_Update|TIM_IT_Trigger,ENABLE);//使能定时器6更新触发中断
-
- NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn; //TIM3中断
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //从优先级3级
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
- NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
-
- TIM_Cmd(TIM6, ENABLE); //使能TIMx外设
-
- }
-
- //定时器6中断服务程序
- void TIM6_IRQHandler(void)
- {
- if (TIM_GetITStatus(TIM6, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
- {
- TIM_ClearITPendingBit(TIM6, TIM_IT_Update ); //清除TIMx的中断待处理位:TIM 中断源
- }
- }
- #ifndef __TIMER7_H
- #define __TIMER7_H
- #include "stm32f10x.h"
-
- void TIM7_Init(unsigned int arr,unsigned int psc);
-
- #endif
- #include "sys.h"
- #include "timer7.h"
-
- //arr:自动重装值
- //psc:时钟预分频数
- void TIM7_Init(unsigned int arr, unsigned int psc)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE); //时钟使能
-
- //定时器TIM7初始化
- TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
- TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
- TIM_TimeBaseInit(TIM7, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
-
- TIM_ITConfig(TIM7,TIM_IT_Update,ENABLE ); //使能指定的TIMx中断,允许更新中断
-
- //中断优先级NVIC设置
- NVIC_InitStructure.NVIC_IRQChannel = TIM7_IRQn; //TIMx中断
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占优先级0级
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //从优先级3级
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
- NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
-
-
- TIM_Cmd(TIM7, ENABLE); //使能TIMx
-
- }
-
- //定时器7中断服务程序
- void TIM7_IRQHandler(void)
- {
- if (TIM_GetITStatus(TIM7, TIM_IT_Update) != RESET) //检查TIM7更新中断发生与否
- {
- TIM_ClearITPendingBit(TIM7, TIM_IT_Update ); //清除TIMx更新中断标志
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。