word格式文档:
基于stm32定时中断的电子闹钟.doc
(2.9 MB, 售价: 1 E币)
一、系统主要功能
可以通过LCD的输出显示公历和农历时间,通过按键设置时间和闹钟;通过蜂鸣器响应闹钟。 三、电路原理图、接口、硬件构成 1.原理图
2.接口 本次实验使用了串口、定时器、中断接口。 3.硬件组成 (1)实验设计程序流程图如图左所示,中断流程图如图右所示。
(2)该设计分为软件设计和硬件设计两大模块,硬件电路由 ARM 最小系统电路、时钟显示电路和闹钟提醒电路组成,采用stm32f103RCT6芯片,芯片管脚图示如下。 (3)时钟电路 此电路主要是复位电路和时钟电路两部分,其中复位电路采用按键手动复位和上电自动复位组合,电路如图所示:晶振采用的是 8MHz 和 32.786KHz , 8MKz 分别接 STM32 的 5 脚和 6 脚, 32.786KHz 分别接 STM32 的 3 脚和 4 脚。 (4)闹钟提醒电路 本次实验设计的闹钟提醒电路为蜂鸣器电路,接入芯片的PC7引脚,当时间为设置闹钟时间时,蜂鸣器工作,发出响声,提醒电路如图所示。
四、核心代码(带注释)
- #include "delay.h"
- #include "sys.h"
- #include "lcd.h"
- #include "dht11.h"
- #include "ds1302.h"
- #include "KEY.h"
- #include "beep.h"
- u8 temp;
- u8 humi;
- u8 t=0;
- u8 flag=0,flag1=0,flag2=0,flag3=1;
- u8 a,b,c;
- int min1=10,hour1=10;
- DHT11_Data_TypeDef DHT11_Data;
- void TIM3_Int_Init(u16 arr,u16 psc)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟使能
- //定时器TIM3初始化
- 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(TIM3, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
- TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //使能指定的TIM3中断,允许更新中断
- //中断优先级NVIC设置
- NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM中断
- 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, DISABLE); //使能TIMx
- }
- void gui0(u8 mode)
- {
- LCD_ShowPicture(0,0,480,320);
- if(flag3==1)
- {
- LCD_ShowPicture3(445,0,479,34);
- }
- LCD_ShowChinese(0+40,0,0,BLUE,32,mode);//字
- LCD_ShowChinese(32+40,0,8,BLUE,32,mode);
- LCD_ShowChinese(64+40,0,9,BLUE,32,mode);
- LCD_ShowChinese(96+40,0,10,BLUE,32,mode);
- LCD_ShowNum(52,40,temp,2,BLUE,32,mode);//温度
- LCD_ShowChinese(132,40,12,BLUE,32,mode);
- LCD_ShowChinese(128+80+30,0,0,BLUE,32,mode);
- LCD_ShowChinese(160+80+30,0,8,BLUE,32,mode);
- LCD_ShowChinese(192+80+30,0,11,BLUE,32,mode);
- LCD_ShowChinese(224+80+30,0,10,BLUE,32,mode);
- LCD_ShowNum(248+30,40,humi,2,BLUE,32,mode);
- LCD_ShowChar(280+30,40,'%',BLUE,32,mode);
- LCD_ShowNum(20,80,hour/10,1,BLUE,160,mode);//时间
- LCD_ShowNum(110,80,hour%10,1,BLUE,160,mode);
- LCD_ShowChar(200,65,':',BLUE,160,mode);
- LCD_ShowNum(290,80,min/10,1,BLUE,160,mode);
- LCD_ShowNum(380,80,min%10,1,BLUE,160,mode);
- LCD_ShowNum(0,250,year+2000,4,BLUE,32,mode);
- LCD_ShowChinese(64,250,13,BLUE,32,mode);
- LCD_ShowNum(96,250,month,2,BLUE,32,mode);
- LCD_ShowChinese(128,250,14,BLUE,32,mode);
- LCD_ShowNum(160,250,day,2,BLUE,32,mode);
- LCD_ShowChinese(192,250,15,BLUE,32,mode);
- LCD_ShowChinese(224,250,16,BLUE,32,mode);
- LCD_ShowChinese(256,250,week,BLUE,32,mode);
- LCD_ShowNum(0,283,hour1,2,BLUE,32,mode);
- LCD_ShowChar(33,283,':',BLUE,32,mode);
- LCD_ShowNum(50,283,min1,2,BLUE,32,mode);
- }
- void keyscan(u8 mode)
- {
- switch(t)
- {
- case KEY0_PRES:
- if(min1==min&&hour1==hour)
- {
- flag2=1;
- BEEP(OFF);
- }
- switch(flag)
- {
- case 1: hour++; if(hour>23)hour=0;LCD_ShowPicture2(20,80,190,240); LCD_ShowNum(20,80,hour/10,1,BLUE,160,mode);//时间
- LCD_ShowNum(110,80,hour%10,1,BLUE,160,mode);break;
- case 2: min++; if(min>59) min=0;LCD_ShowPicture2(290,80,460,240);LCD_ShowNum(290,80,min/10,1,BLUE,160,mode);
- LCD_ShowNum(380,80,min%10,1,BLUE,160,mode); break;
- case 3: year++; LCD_ShowPicture2(0,250,64,282); LCD_ShowNum(0,250,year+2000,4,BLUE,32,mode);break;
- case 4: month++; if(month>12) month=1;LCD_ShowPicture2(96,250,128,282); LCD_ShowNum(96,250,month,2,BLUE,32,mode);break;
- case 5: day++; if(day>31) day=1; LCD_ShowPicture2(160,250,192,282);LCD_ShowNum(160,250,day,2,BLUE,32,mode); break;
- case 6: week++; if(week>7) week=1; LCD_ShowPicture2(256,250,288,282);LCD_ShowChinese(256,250,week,BLUE,32,mode);break;
- case 7: hour1++; if(hour1>23)hour1=0;LCD_ShowPicture2(0,283,32,315); LCD_ShowNum(0,283,hour1,2,BLUE,32,mode);break;
- case 8: min1++; if(min1>59)min1=0;LCD_ShowPicture2(50,283,82,315);LCD_ShowNum(50,283,min1,2,BLUE,32,mode);break;
- case 9: flag3=1; LCD_ShowPicture3(445,0,479,34); break;
- default: break;
- }
- break;
- case KEY1_PRES:
- if(min1==min&&hour1==hour)
- {
- flag2=1;
- BEEP( OFF );
- }
- switch(flag)
- {
- case 1: hour--; if(hour<0)hour=23; LCD_ShowPicture2(20,80,190,240);LCD_ShowNum(20,80,hour/10,1,BLUE,160,mode);//时间
- LCD_ShowNum(110,80,hour%10,1,BLUE,160,mode); break;
- case 2: min--; if(min<0) min=59; LCD_ShowPicture2(290,80,460,240);LCD_ShowNum(290,80,min/10,1,BLUE,160,mode);
- LCD_ShowNum(380,80,min%10,1,BLUE,160,mode); break;
- case 3: year--; LCD_ShowPicture2(0,250,64,282); LCD_ShowNum(0,250,year+2000,4,BLUE,32,mode);break;
- case 4: month--; if(month<1) month=12; LCD_ShowPicture2(96,250,128,282); LCD_ShowNum(96,250,month,2,BLUE,32,mode);break;
- case 5: day--; if(day<1) day=31; LCD_ShowPicture2(160,250,192,282);LCD_ShowNum(160,250,day,2,BLUE,32,mode); break;
- case 6: week--; if(week<1) week=7; LCD_ShowPicture2(256,250,288,282);LCD_ShowChinese(256,250,week,BLUE,32,mode); break;
- case 7: hour1--; if(hour1<0)hour1=23;LCD_ShowPicture2(0,283,32,315); LCD_ShowNum(0,283,hour1,2,BLUE,32,mode);break;
- case 8: min1--; if(min1<0)min1=59;LCD_ShowPicture2(50,283,82,315);LCD_ShowNum(50,283,min1,2,BLUE,32,mode);break;
- case 9: flag3=0; LCD_ShowPicture2(445,0,480,36);break;
- default: break;
- }
- break;
- case WKUP_PRES:
- cc1();flag++;
- switch(flag)
- {
- case 1: TIM_Cmd(TIM3, DISABLE); LCD_DrawLine(20,242,190,243,BLUE);break;
- case 2: LCD_ShowPicture1(242,243);LCD_DrawLine(290,242,460,243,BLUE); break;
- case 3: LCD_ShowPicture1(242,243); LCD_DrawLine(0,287,64,288,BLUE);break;
- case 4: LCD_ShowPicture1(287,288);LCD_DrawLine(96,287,128,288,BLUE);break;
- case 5: LCD_ShowPicture1(287,288); LCD_DrawLine(160,287,192,288,BLUE); break;
- case 6: LCD_ShowPicture1(287,288);LCD_DrawLine(256,287,288,288,BLUE); break;
- case 7: LCD_ShowPicture1(287,288); LCD_DrawLine(0,316,32,317,BLUE);break;
- case 8: LCD_ShowPicture1(316,317); LCD_DrawLine(50,316,82,317,BLUE);break;
- case 9: LCD_ShowPicture1(316,317); LCD_DrawLine(445,37,479,38,BLUE);break;
- case 10: LCD_ShowPicture2(445,37,479,38); ds_wtime();a=sec;b=min;c=hour;TIM_Cmd(TIM3, ENABLE);flag=0;break;
- default: break;
- }
- break;
- default: delay_ms(5); break;
- }
- }
- int main(void)
- {
- delay_init();
- NVIC_Configuration();
- DHT11_Init ();
- KEY_Init();
- BEEP_GPIO_Config();
- BEEP( OFF );
- TIM3_Int_Init(9999,7199);
- ds1302_init();
- ds_read_time();
- cc();
- a=sec;
- b=min;
- c=hour;
- Lcd_Init();
- LCD_Clear(WHITE);
- gui0(1);
- TIM_Cmd(TIM3, ENABLE);
- while(1)
- {
- t=KEY_Scan(0);
- keyscan(1);
- if( DHT11_Read_TempAndHumidity (&DHT11_Data ) == SUCCESS&&flag==0)
- {
- temp=DHT11_Data.temp_int;
- humi=DHT11_Data.humi_int;
- LCD_ShowPicture2(52,40,84,72);
- LCD_ShowPicture2(278,40,310,72);
- LCD_ShowNum(52,40,temp,2,BLUE,32,1);
- LCD_ShowNum(278,40,humi,2,BLUE,32,1);
- }
- if(flag==0&&min1==min&&hour1==hour&&flag2==0&&flag3==1)
- {
- BEEP( ON );
- }
- }
- }
复制代码
【必读】版权免责声明
1、本主题所有言论和内容纯属会员个人意见,与本论坛立场无关。2、本站对所发内容真实性、客观性、可用性不做任何保证也不负任何责任,网友之间仅出于学习目的进行交流。3、对提供的数字内容不拥有任何权利,其版权归原著者拥有。请勿将该数字内容进行商业交易、转载等行为,该内容只为学习所提供,使用后发生的一切问题与本站无关。 4、本网站不保证本站提供的下载资源的准确性、安全性和完整性;同时本网站也不承担用户因使用这些下载资源对自己和他人造成任何形式的损失或伤害。 5、本网站所有软件和资料均为网友推荐收集整理而来,仅供学习用途使用,请务必下载后两小时内删除,禁止商用。6、如有侵犯你版权的,请及时联系我们(电子邮箱1370723259@qq.com)指出,本站将立即改正。
|