基于STM32的多功能秒表

您所在的位置:网站首页 倒计时秒表在线制作 基于STM32的多功能秒表

基于STM32的多功能秒表

2024-06-10 09:41| 来源: 网络整理| 查看: 265

基于STM32的多功能秒表

当上电运行后,按下KEY1,秒表开始正计时,再次按下KEY1秒表停止计时,按下KEY2秒表清零,按下KEY3,手动调节秒表时间递增,按下KEY4手动调节秒表时间递减,按下KEY5秒表倒计时,倒计时到0时蜂鸣器响起来。

#include "stm32f10x.h" #include "sys.h" #define FM PBout(10) #define uint unsigned int #define KEY1 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11) #define KEY2 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_12) #define KEY3 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13) #define KEY4 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14) #define KEY5 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15) void Smg_Init(void); void Key_Init(void); void FM_Init(void); //蜂鸣器初始化 void Delay(void); void TIM3_Init(u16 arr,u16 psc); void TIM2_Init(u16 arr,u16 psc); uint arr1[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//没有小数点数码管 uint arr2[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};//小数点数码管 uint disp1[2],disp2[2]; uint ID=0; uint b=0; uint a,temp1,temp2,flag=0; int main(void) { NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置2位抢占优先级2位响应优先级 Smg_Init(); Key_Init(); FM_Init(); TIM3_Init(220,7199); //时钟参数配置 TIM2_Init(220,7199); while(1) { if(KEY1==0) { Delay(); if(KEY1==0) { ID++; if(ID==3) ID=1; } switch(ID) { case 1: TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); //使能时钟,允许更新中断 TIM_Cmd(TIM3,ENABLE); //使能时钟 break; case 2: TIM_ITConfig(TIM3,TIM_IT_Update,DISABLE); //关闭更新中断 TIM_Cmd(TIM3,DISABLE); //关闭时钟 break; } while(KEY1==0); } if(KEY2==0) //清空秒表 { a=0; b=0; GPIO_Write(GPIOA,0x3fbf); GPIO_Write(GPIOC,0x3f3f); while(KEY2==0); } if(KEY3==0) //手动加数 { a++; disp1[0]=arr1[a%10]; disp1[1]=arr1[a/10]; disp2[0]=arr2[b%10]; disp2[1]=arr1[b/10]; temp1=(disp1[1] TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE); //使能时钟,允许更新中断 TIM_Cmd(TIM2,ENABLE); //使能时钟 flag=1; while(KEY5==0); } } } void Smg_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC,ENABLE); GPIO_InitStructure.GPIO_Pin=0xffff; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStructure); GPIO_Init(GPIOC,&GPIO_InitStructure); GPIO_Write(GPIOA,0x3fbf); GPIO_Write(GPIOC,0x3f3f); } void Key_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU; //上拉 GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOB,&GPIO_InitStructure); } void FM_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOB,&GPIO_InitStructure); GPIO_SetBits(GPIOB,GPIO_Pin_10); } void TIM3_Init(u16 arr,u16 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=TIM_CKD_DIV1;//分频因子 TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//计数模式 TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure); NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn;//TIM3中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;// 抢占优先级 NVIC_InitStructure.NVIC_IRQChannelSubPriority=3; //响应优先级 NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_Init(&NVIC_InitStructure); } void TIM2_Init(u16 arr,u16 psc) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure1; NVIC_InitTypeDef NVIC_InitStructure1; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); TIM_TimeBaseStructure1.TIM_Period=arr; //周期 TIM_TimeBaseStructure1.TIM_Prescaler=psc; //分频系数 TIM_TimeBaseStructure1.TIM_ClockDivision=TIM_CKD_DIV1;//分频因子 TIM_TimeBaseStructure1.TIM_CounterMode=TIM_CounterMode_Up;//计数模式 TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure1); NVIC_InitStructure1.NVIC_IRQChannel=TIM2_IRQn;//TIM2中断 NVIC_InitStructure1.NVIC_IRQChannelPreemptionPriority=0;// 抢占优先级 NVIC_InitStructure1.NVIC_IRQChannelSubPriority=3; //响应优先级 NVIC_InitStructure1.NVIC_IRQChannelCmd=ENABLE; NVIC_Init(&NVIC_InitStructure1); } void TIM3_IRQHandler(void) //TIM3中断服务程序 { a++; disp1[0]=arr1[a%10]; disp1[1]=arr1[a/10]; disp2[0]=arr2[b%10]; disp2[1]=arr1[b/10]; temp1=(disp1[1] b=0; a=0; } TIM_ClearITPendingBit(TIM3,TIM_IT_Update); } void TIM2_IRQHandler(void) //TIM2中断服务程序 { a--; disp1[0]=arr1[a%10]; disp1[1]=arr1[a/10]; disp2[0]=arr2[b%10]; disp2[1]=arr1[b/10]; temp1=(disp1[1] b--; a=60; } } if((a==0)&&(b==0)&&(flag==1)) { flag=0;a=0;b=0; GPIO_Write(GPIOA,0x3fbf); GPIO_Write(GPIOC,0x3f3f); while(1) { FM=~FM; Delay(); } } TIM_ClearITPendingBit(TIM2,TIM_IT_Update); } void Delay(void) { uint i,j; for(i=0;i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3