参考制作的秒表时钟
单片机源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "key.h"
- #include "exti.h"
- #include "sys.h"
- #include "usart.h"
- #include "lcd.h"
- #include "usmart.h"
- #include "rtc.h"
- #include "timer.h"
- #include "touch.h"
- #include "myiic.h"
- #include "24cxx.h"
- #include "tsensor.h"
- #include "adc.h"
- u16 jia;
- u16 jian;
- u32 tiao=0;
- u16 min;
- u16 sec;
- u16 msec;
- u32 jishu;
-
- u8 flag=1;
- u32 dao;
- u32 dao1;
- u32 dao2;
- u8 daoshi;
- u16 daofen;
- u16 daomiao;
- u8 daoshi1;
- u16 daofen1;
- u16 daomiao1;
-
-
- int main(void)
- {
- u8 t;
- u16 adcx;
- float temp;//温度
- float temperate;
- NVIC_Configuration();
- delay_init(); //延时函数初始化
- uart_init(9600); //串口初始化为9600
- LED_Init(); //初始化与LED连接的硬件接口
- LCD_Init(); //初始化LCD
- KEY_Init();
- EXTIX_Init();
- Adc_Init();
- T_Adc_Init();
- tp_dev.init();
-
- TIM3_Int_Init(100,7199);//10Khz的计数频率,计数到5000为500ms
- usmart_dev.init(72); //初始化USMART
- POINT_COLOR=RED;//设置字体为红色
- while(RTC_Init()) //RTC初始化 ,一定要初始化成功
- {
- LCD_ShowString(60,130,200,16,16,"RTC ERROR! ");
- delay_ms(800);
- LCD_ShowString(60,130,200,16,16,"RTC Trying...");
- }
- //显示时间
- POINT_COLOR=BLUE;//设置字体为蓝色
- LCD_ShowString(60,130,200,16,16," - - ");
- LCD_ShowString(60,162,200,16,16," : : ");
- LCD_ShowNum(84,162,calendar.min,2,16);
- LCD_ShowString(60,216,200,16,16,"change");
- LCD_ShowString(60,260,200,16,16,"start");
- LCD_ShowString(160,260,200,16,16,"end");
- LCD_DrawRectangle(55,216,115,242);
- LCD_DrawRectangle(55,260,105,276);
- LCD_DrawRectangle(150,260,195,276);
- LCD_ShowString(60,30,200,16,16,"TEMP_VAL:");
- LCD_ShowString(60,50,200,16,16,"TEMP_VOL:0.000V");
- LCD_ShowString(60,70,200,16,16,"TEMPERATE:00.00C");
- dao1=dao;
-
- while(1)
- {
- u32 now;
- static u8 k=0;
- static u8 c;
-
- extern u32 temp2;//时间
-
-
- //温度显示
- adcx=T_Get_Adc_Average(ADC_CH_TEMP,10);
- LCD_ShowxNum(132,30,adcx,4,16,0);//显示ADC的值
- temp=(float)adcx*(3.3/4096);
- temperate=temp;//保存温度传感器的电压值
- adcx=temp;
- LCD_ShowxNum(132,50,adcx,1,16,0); //显示电压值整数部分
- temp-=(u8)temp; //减掉整数部分
- LCD_ShowxNum(148,50,temp*1000,3,16,0X80); //显示电压小数部分
- temperate=(1.43-temperate)/0.0043+25; //计算出当前温度值
- LCD_ShowxNum(140,70,(u8)temperate,2,16,0); //显示温度整数部分
- temperate-=(u8)temperate;
- LCD_ShowxNum(164,70,temperate*100,2,16,0X80);//显示温度小数部分
- //日期显示
- if(t!=calendar.sec)
- {
- t=calendar.sec;
- LCD_ShowNum(60,130,calendar.w_year,4,16);
- LCD_ShowNum(100,130,calendar.w_month,2,16);
- LCD_ShowNum(124,130,calendar.w_date,2,16);
- switch(calendar.week)
- {
- case 0:
- LCD_ShowString(60,148,200,16,16,"Sunday ");
- break;
- case 1:
- LCD_ShowString(60,148,200,16,16,"Monday ");
- break;
- case 2:
- LCD_ShowString(60,148,200,16,16,"Tuesday ");
- break;
- case 3:
- LCD_ShowString(60,148,200,16,16,"Wednesday");
- break;
- case 4:
- LCD_ShowString(60,148,200,16,16,"Thursday ");
- break;
- case 5:
- LCD_ShowString(60,148,200,16,16,"Friday ");
- break;
- case 6:
- LCD_ShowString(60,148,200,16,16,"Saturday ");
- break;
- }
- }
-
- //秒表显示
- if (c==3)
- {
- LCD_ShowNum(60,162,min,2,16);
- LCD_ShowNum(84,162,sec,2,16);
- LCD_ShowNum(108,162,msec,2,16);
- }
-
-
- //倒计时显示
- if (c==4)
- {
- LCD_ShowNum(60,162,daoshi,2,16);
- LCD_ShowNum(84,162,daofen,2,16);
- LCD_ShowNum(108,162,daomiao,2,16);
- daoshi=dao/3600;
- daofen=dao/60%60;
- daomiao=dao%60;
- if (now>0)
- {
- dao2=(temp-now);
- dao=dao1-dao2;
- if (dao==0)
- {
- c=0;
- LED0=!LED0;
- LED1=!LED1;
- }
- }
- }
- if (c!=3&&c!=4)
- {
- LCD_ShowNum(60,162,calendar.hour,2,16);
- LCD_ShowNum(84,162,calendar.min,2,16);
- LCD_ShowNum(108,162,calendar.sec,2,16);
- if (calendar.min==0&&calendar.sec==0)
- {
- LED0=!LED0;
- LED1=!LED1;
- }
- }
- delay_ms(10);
-
-
-
- tp_dev.scan(0);
- if(tp_dev.sta&TP_PRES_DOWN) //触摸屏被按下
- {
- if((115>tp_dev.x[0]&&tp_dev.x[0]>55)&&(242>tp_dev.y[0]&&tp_dev.y[0]>216))
- {
- k=1+k;
- LED0=!LED0;
-
- if (k==5)
- {
- k=0;
- }
- switch(k )
- {
- case 0:
- LCD_ShowString(60,188,260,16,16,"time ");
- jia = 0;
- jian = 0;
- c=0;
- break;
- case 1:
- LCD_ShowString(60,188,260,16,16,"min ");
- jia = 60;
- jian = 60;
- break;
- case 2:
- LCD_ShowString(60,188,260,16,16,"hour ");
- jia = 3600;
- jian = 3600;
- break;
- case 3:
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, DISABLE); //时钟失能
- LCD_ShowString(60,188,260,16,16,"count ");
- jishu=0;
- min=0;
- sec=0;
- msec=0;
- jia = 0;
- jian = 0;
- c=3;
- break;
- case 4:
- LCD_ShowString(60,188,260,16,16,"count down");
- dao=0;
- daoshi=0;
- daofen=0;
- daomiao=0;
- c=4;
- break;
- }
- }
-
-
- if(c==3&&(105>tp_dev.x[0]&&tp_dev.x[0]>55)&&(286>tp_dev.y[0]&&tp_dev.y[0]>260))
- {
- flag+=1;
- LED0=!LED0;
-
- }
- if (flag>1)
- {
- flag=0;
- }
- if (flag==1)
- {
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, DISABLE); //时钟失能
- }
- if (flag==0)
- {
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟使能
- }
-
- if (c==4&&(105>tp_dev.x[0]&&tp_dev.x[0]>55)&&(286>tp_dev.y[0]&&tp_dev.y[0]>260)&&(dao!=0))
- {
- LED0=!LED0;
- now=temp2;
- }
- if(c!=3&&c!=4&&(105>tp_dev.x[0]&&tp_dev.x[0]>55)&&(286>tp_dev.y[0]&&tp_dev.y[0]>260))
- {
- LED0=!LED0;
- tiao+=jia;
- }
- if(c==3&&(195>tp_dev.x[0]&&tp_dev.x[0]>150)&&(286>tp_dev.y[0]&&tp_dev.y[0]>260))
- {
- LED0=!LED0;
- jishu=0;
- min=0;
- sec=0;
- msec=0;
- flag=1;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, DISABLE); //时钟失能
- }
- if (c==4&&(195>tp_dev.x[0]&&tp_dev.x[0]>150)&&(286>tp_dev.y[0]&&tp_dev.y[0]>260))
- {
- LED0=!LED0;
- dao=60+dao1;
- dao1=dao;
- }
- if (c!=3&&c!=4&&(195>tp_dev.x[0]&&tp_dev.x[0]>150)&&(286>tp_dev.y[0]&&tp_dev.y[0]>260))
- {
- LED0=!LED0;
- tiao-=jian;
- }
- delay_ms(100);
- }
- }
- }
复制代码
所有资料51hei提供下载:
RTC时钟秒表.7z
(280.75 KB, 下载次数: 37)
|