STM32F103系列的单片机一共有11个定时器,其中: 2个高级定时器、4个普通定时器、2个基本定时器、2个看门狗定时器、1个系统嘀嗒定时器
在除去看门狗定时器和系统滴答定时器的八个定时器中: TIM1和TIM8是高级定时器,支持向上,向下,向上/向下这3种计数模式。 TIM2-TIM5是通用定时器,支持向上,向下,向上/向下这3种计数模式。 TIM6和TIM7是基本的定时器,支持向上这种计数模式。
timer.c
#include "timer.h"
struct Tim3_Delay tim3_delay =
{
0,0,
0,0
};
void TIM3_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟使能
//定时器TIM8初始化
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值 Tout=((arr+1)* (psc+1) )/72000
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //使能指定的TIM7中断,允许更新中断
//中断优先级NVIC设置
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM7中断
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(TIM3, ENABLE); //使能TIMx
}
void TIM3_IRQHandler(void)
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检查TIM7更新中断发生与否
{
tim3_delay.count_8ms ++;
if(tim3_delay.count_8ms % 5 == 0)
tim3_delay.count_40ms ++;
if(tim3_delay.count_8ms % 100 == 0)
tim3_delay.count_800ms ++;
TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //清除TIMx更新中断标志 z
}
}
timer.h
#ifndef __TIMER_H
#define __TIMER_H
#include "stm32f10x.h"
#include <sys.h>
struct Tim3_Delay
{
u32 count_8ms;
u32 last_count_8ms;
u32 count_40ms;
u32 last_count_40ms;
u32 count_800ms;
u32 last_count_800ms;
};
extern struct Tim3_Delay tim3_delay;
void TIM3_Int_Init(u16 arr,u16 psc);
void TIM3_IRQHandler(void);
#endif
key.c
#include "key.h"
u16 KeyPressDown = 0x0000;//按下的键值
u16 KeyRelease = 0x0000;//弹起的键值
u16 LastKey = 0x0000;//上一次的键值
void Key_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE,&GPIO_InitStructure);
GPIO_WriteBit(GPIOE, GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12, Bit_SET);
KeyPressDown = 0;
KeyRelease = 0;
LastKey = 0;
// Key_Port |= Key_Mask;//将按键对应的IO设置为输入状态
}
//按键扫描,定时10ms执行一次
void Key_Scan(void)
{
//当前读取的键值
u16 CurrKey;
//当按键1即A4按下11110xxxx
//1110xxxx取反后00011111与上11110000得到00010000此时转换成16进制数字为0x10
CurrKey = (~Key_Port) & Key_Mask;
//11111111与上00010000得到0x10
KeyPressDown = (~LastKey) & CurrKey;
KeyRelease = LastKey & (~CurrKey);
LastKey = CurrKey;
}
key.h
#ifndef __KEY_H
#define __KEY_H
#include "sys.h"
#define Key1_Value 0x0200
#define Key2_Value 0x0400
#define Key3_Value 0x0800
#define Key4_Value 0x1000
#define Key_Mask 0x1E00
#define Key_Port (GPIO_ReadInputData(GPIOE) & Key_Mask)//先将A4567四个位全部置高,如果按键按下,则那一位置0,与上Key_Mask则按下的那一按键置高,其余置0
//如按键1,A4至低返回1110xxxx与上11110000得到按下的脚位,其余位保持和置低
void Key_Init(void);
void Key_Scan(void);
extern u16 KeyPressDown;//按下的键值
extern u16 KeyRelease;//弹起的键值
extern u16 LastKey;//上一次的键值
#endif
main.c
#include "SysTick.h"
#include "key.h"
#include "sys.h"
#include "timer.h"
int main(void)
{
SysTick_Init(72); //延时函数初始化
Key_Init();
TIM3_Int_Init(79,7199); //===== (8ms)中断 计算公式如下:溢出时间(单位为 us)= ((arr+1)*(psc+1 ))/输入时钟频率(单位为 Mhz);
while(1)
{
if(tim3_delay.last_count_8ms != tim3_delay.count_8ms)
{
Key_Scan();
tim3_delay.last_count_8ms = tim3_delay.count_8ms;
}
if(KeyPressDown == Key1_Value)//按键1
{
KeyPressDown = 0;
}
if(KeyPressDown == Key2_Value)//按键2
{
KeyPressDown = 0;
}
}
}
STM32F103C8T6基本功能代码_墨云景阳的博客-CSDN博客_stm32f103c8t6代码
|