找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1997|回复: 0
收起左侧

单片机驱动TFT1.44液晶显示万年历(含农历+温度)完整源代码

[复制链接]
ID:140489 发表于 2022-6-15 14:36 | 显示全部楼层 |阅读模式
TFT1.44液晶显示万年
可按键调时,调节时被选中的项显示蓝色底色,星期是自动计算的,不用调节;DS1302加备用电池,掉电继续走时
MCU=STC12C5A60S2
接线说明:
sbit LCD_SCL        =P1^7;//接模块CLK引脚,接裸屏Pin9_SCL
sbit LCD_SDA        =P1^5;//接模块DIN/MOSI引脚,接裸屏Pin8_SDA
sbit LCD_DC      =P3^6;//接模块D/C引脚,接裸屏Pin7_A0  RS
sbit LCD_CS      =P3^7;//接模块CE引脚,接裸屏Pin12_CS
sbit LCD_RST        =P3^4;//接模块RST引脚,接裸屏Pin6_RES
//---------------------------End of液晶屏接线---------------------------------//

sbit TSCLK = P1^0;//时钟线 接到P10上用杜邦线
sbit TIO   = P1^1;//数据线,接到P11上
sbit TRST  = P1^2;//使能端,接到P12上

sbit DS18B20_IO = P2^2;  //DS18B20通信引脚

sbit key1=P3^3;//调时选项键
sbit key2=P3^1;//加键
sbit key3=P3^2;//减键

制作出来的实物图如下:
51hei图片_20220615141732.jpg

单片机源程序如下:
  1. #include "config.h"
  2. #include "DS1302.H"
  3. #include "TFT144_ST7735.h"
  4. #include "ds18b20.h"
  5. #include "nongli.h"

  6. //bit debug_flag = 0; //debug mode = 1, uart printf enable
  7. bit timer_flag_250ms = 0; //250ms定时触发标志位
  8. bit timer_flag_1s = 0;    //1s定时触发标志位
  9. bit timer_flag_2s = 0;    //2s定时触发标志位
  10. bit timer_flag_5s = 0;    //5s定时触发标志位

  11. sbit key1=P3^3;
  12. sbit key2=P3^1;
  13. sbit key3=P3^2;
  14. bit lock1,lock2,lock3;
  15. uint8 cont1,cont2,cont3,set_num;
  16. uint8 T0RH = 0;  //T0重载值的高字节
  17. uint8 T0RL = 0;  //T0重载值的低字节

  18. void Timer0Init(void);                //10毫秒@11.0592MHz
  19. void ReadAndShowRtc();
  20. void ReadAndShowTemperature(int8 comp);


  21. /////////////////////////////////////
  22. void LCD_Show_Format()        //固定显示内容
  23. {
  24.         Display_Pure_Color(COLOR_BLACK);//背景色
  25. //        Display_Pure_Color(COLOR_LIGHT_BLUE); //背景色

  26.         // YYYY-MM-DD
  27.         Display_Digit_12x16(4+0,        4, COLOR_SILVER, COLOR_BLACK, 2);        //2
  28.         Display_Digit_12x16(4+12,        4, COLOR_SILVER, COLOR_BLACK, 0);        //0
  29. //        Display_Digit_12x16(4+24,        4, COLOR_SILVER, COLOR_BLACK, 2);        //2
  30. //        Display_Digit_12x16(4+36,        4, COLOR_SILVER, COLOR_BLACK, 2);        //2
  31.         Display_Digit_12x16(4+48,        4, COLOR_SILVER, COLOR_BLACK, 10);        //-
  32. //        Display_Digit_12x16(4+60,        4, COLOR_SILVER, COLOR_BLACK, 0);        //0
  33. //        Display_Digit_12x16(4+72,        4, COLOR_SILVER, COLOR_BLACK, 6);        //6
  34.         Display_Digit_12x16(4+84,        4, COLOR_SILVER, COLOR_BLACK, 10);        //-
  35. //        Display_Digit_12x16(4+96,        4, COLOR_SILVER, COLOR_BLACK, 0);        //0
  36. //        Display_Digit_12x16(4+108,        4, COLOR_SILVER, COLOR_BLACK, 8);        //8

  37.         // 星期三
  38.         Display_HZ_16x16(0,24, COLOR_SILVER, COLOR_BLACK, 0);        //星
  39.         Display_HZ_16x16(16,24, COLOR_SILVER, COLOR_BLACK, 1);        //期
  40. //        Display_HZ_16x16(32,24, COLOR_SILVER, COLOR_BLACK, 4);        //三


  41.         // HH:MI:SS
  42. //        Display_Digit_24x48(0, 44, COLOR_YELLOW_GREEN, COLOR_BLACK, 1);
  43. //        Display_Digit_24x48(24, 44, COLOR_YELLOW_GREEN, COLOR_BLACK, 2);
  44.         Display_Digit_24x48(48, 44, COLOR_YELLOW_GREEN, COLOR_BLACK, 10);//冒号
  45. //        Display_Digit_24x48(72, 44, COLOR_YELLOW_GREEN, COLOR_BLACK, 2);
  46. //        Display_Digit_24x48(96, 44, COLOR_YELLOW_GREEN, COLOR_BLACK, 1);



  47.         // 25.0℃

  48. //        Display_Digit_12x24(0,  100, COLOR_LIGHT_BLUE, COLOR_BLACK, 2);        //2
  49. //        Display_Digit_12x24(12, 100, COLOR_LIGHT_BLUE, COLOR_BLACK, 5);        //5
  50.         Display_Digit_12x24(24, 100, COLOR_LIGHT_BLUE, COLOR_BLACK, 12);        //.
  51. //        Display_Digit_12x24(36, 100, COLOR_LIGHT_BLUE, COLOR_BLACK, 0);        //0
  52.         Display_Digit_16x24(48, 100, COLOR_LIGHT_BLUE, COLOR_BLACK, 13);        //℃

  53. }



  54. ///////////////////////////
  55. void keyscan()         //按键扫描
  56. {
  57.         uchar minute,hour,day,month,year;

  58.         year=BCD_Chg_Dat(read_1302(read_nian));
  59.         month=BCD_Chg_Dat(read_1302(read_yue));
  60.         day=BCD_Chg_Dat(read_1302(read_ri));
  61.         hour=BCD_Chg_Dat(read_1302(read_shi));
  62.         minute=BCD_Chg_Dat(read_1302(read_fen));


  63.         if(key1)
  64.         {
  65.                 lock1=0;
  66.                 cont1=0;        
  67.         }
  68.         else if(lock1==0)
  69.         {
  70.                 cont1++;
  71.                 if(cont1>10)
  72.                 {
  73.                         cont1=0;
  74.                         lock1=1;
  75.                         set_num++;
  76.                         if(set_num==1)
  77.                         {
  78.                                 write_1302(0x8e, 0x00);  //撤销写保护以允许写入数据
  79.                                 write_1302(write_miao,0x80); //写秒80,时钟停止走时;
  80.                         }
  81.                         if(set_num==6)                                                   //调时完毕,时钟启动
  82.                         {                                
  83.                                 set_num=0;
  84.                                 write_1302(write_miao,0x00); //写秒0,时钟启动走时
  85.                                 write_1302(0x8e,0x80);                 //保护启动
  86.                         }
  87.                 }        
  88.         }

  89.         if(key2)
  90.         {
  91.                 lock2=0;
  92.                 cont2=0;        
  93.         }
  94.         else if(lock2==0) //短按 加
  95.         {
  96.                 cont2++;
  97.                 if(cont2>10)
  98.                 {
  99.                         cont2=0;
  100.                         lock2=1;
  101.                         switch(set_num)
  102.                         {
  103.                                 
  104.                                 case 1: year++;
  105.                                                 if(year==99)
  106.                                                         year=21;
  107.                                                 write_1302(write_nian,Dat_Chg_BCD(year)); //写年
  108.                                                 break;
  109.                                 case 2: month++;
  110.                                                 if(month==13)
  111.                                                         month=1;
  112.                                                 if(month==2 && day>28){day=28;write_1302(write_ri,Dat_Chg_BCD(day));}
  113.                                                 write_1302(write_yue,Dat_Chg_BCD(month)); //写月
  114.                                                 break;         
  115.                                                                   //公历闰年遵循的规律为: 四年一闰,百年不闰,四百年再闰
  116.                                 case 3: day++;          //if((year % 400 == 0)||(year % 4 == 0)&&(year % 100 != 0))闰年的计算方法
  117.                                                 if((month==2&&year%4!=0)&&day>28) day=1;//平年2月28天                                                                                       
  118.                                                 if((month==2&&year%4==0)&&day>29) day=1;//闰年2月29天
  119.                                                 if((month==1 || month==3 ||month==5 ||month==7 ||month==8 ||month==10 || month==12)&&day>31)day=1;//31天的月份
  120.                                                 if((month==4 || month==6 ||month==9 ||month==11)&&day>30 )day=1;//30天的月份
  121.                                                 write_1302(write_ri,Dat_Chg_BCD(day)); //写日
  122.                                                 break;
  123.                                 case 4: hour++;
  124.                                                 if(hour==24)
  125.                                                         hour=0;
  126.                                                 write_1302(write_shi,Dat_Chg_BCD(hour)); //写小时
  127.                                                 break;
  128.                                 case 5: minute++;
  129.                                                 if(minute==59)
  130.                                                         minute=0;
  131.                                                 write_1302(write_fen,Dat_Chg_BCD(minute)); //写分钟
  132.                                                 break;                                                                                 
  133.                                                                                                                                  
  134.                         }
  135.                 }        
  136.         }
  137.         else if(cont2<100)        //长按 连加
  138.         {
  139.                 cont2++;
  140.                 if(cont2==100)
  141.                 {
  142.                         cont2=70;
  143.                         switch(set_num)
  144.                         {
  145.                                 case 1: year++;
  146.                                                 if(year==99)
  147.                                                         year=21;
  148.                                                 write_1302(write_nian,Dat_Chg_BCD(year)); //写年
  149.                                                 break;
  150.                                 case 2: month++;
  151.                                                 if(month==13)
  152.                                                         month=1;
  153.                                                 if(month==2 && day>28){day=28;write_1302(write_ri,Dat_Chg_BCD(day));}
  154.                                                 write_1302(write_yue,Dat_Chg_BCD(month)); //写月
  155.                                                 break;
  156.                                 case 3: day++;                                                
  157.                                                 if((month==2&&year%4!=0)&&day>28) day=1;//平年2月28天                                                                                       
  158.                                                 if((month==2&&year%4==0)&&day>29) day=1;//闰年2月29天
  159.                                                 if((month==1 || month==3 ||month==5 ||month==7 ||month==8 ||month==10 || month==12)&&day>31)day=1;
  160.                                                 if((month==4 || month==6 ||month==9 ||month==11)&&day>30 )day=1;
  161.                                                 write_1302(write_ri,Dat_Chg_BCD(day)); //写日
  162.                                                 break;
  163.                                 case 4: hour++;
  164.                                                 if(hour==24)
  165.                                                         hour=0;
  166.                                                 write_1302(write_shi,Dat_Chg_BCD(hour)); //写小时
  167.                                                 break;
  168.                                 case 5: minute++;
  169.                                                 if(minute==59)
  170.                                                         minute=0;
  171.                                                 write_1302(write_fen,Dat_Chg_BCD(minute)); //写分钟
  172.                                                 break;                                                                                 
  173.                         }
  174.                                        
  175.                 }        
  176.         
  177.         }
  178.     ///////////////////////////////
  179.         if(key3)
  180.         {
  181.                 lock3=0;
  182.                 cont3=0;        
  183.         }
  184.         else if(lock3==0)//短按 减
  185.         {
  186.                 cont3++;
  187.                 if(cont3>10)
  188.                 {
  189.                         cont3=0;
  190.                         lock3=1;
  191.                         switch(set_num)
  192.                         {
  193.                                 case 1: year--;
  194.                                                 if(year==21)
  195.                                                         year=99;
  196.                                                 write_1302(write_nian,Dat_Chg_BCD(year)); //写年
  197.                                                 break;
  198.                                 case 2: month--;
  199.                                                 if(month==0)
  200.                                                         month=12;
  201.                                                 if(month==2 && day>28){day=28;write_1302(write_ri,Dat_Chg_BCD(day));}
  202.                                                 write_1302(write_yue,Dat_Chg_BCD(month)); //写月
  203.                                                 break;
  204.                                 case 3: day--;
  205.                                                 if((month==2&&year%4!=0)&&day==0) day=28;//平年2月28天                                                                                       
  206.                                                 if((month==2&&year%4==0)&&day==0) day=29;//闰年2月29天
  207.                                                 if((month==1 || month==3 ||month==5 ||month==7 ||month==8 ||month==10 || month==12)&&day==0)day=31;
  208.                                                 if((month==4 || month==6 ||month==9 ||month==11)&&day==0 )day=30;                                                
  209.                                                 write_1302(write_ri,Dat_Chg_BCD(day)); //写日
  210.                                                 break;
  211.                                 case 4: hour--;
  212.                                                 if(hour==255)
  213.                                                         hour=23;
  214.                                                 write_1302(write_shi,Dat_Chg_BCD(hour)); //写小时
  215.                                                 break;
  216.                                 case 5: minute--;
  217.                                                 if(minute==255)
  218.                                                         minute=59;
  219.                                                 write_1302(write_fen,Dat_Chg_BCD(minute)); //写分钟
  220.                                                 break;                                                                                 

  221.                         }
  222.                 }        
  223.         }
  224.         else if(cont3<100)        //长按 连减
  225.         {
  226.                 cont3++;
  227.                 if(cont3==100)
  228.                 {
  229.                         cont3=70;
  230.                         switch(set_num)
  231.                         {
  232.                                 case 1: year--;
  233.                                                 if(year==21)
  234.                                                         year=99;
  235.                                                 write_1302(write_nian,Dat_Chg_BCD(year)); //写年
  236.                                                 break;
  237.                                 case 2: month--;
  238.                                                 if(month==0)
  239.                                                         month=12;
  240.                                                 if(month==2 && day>28){day=28;write_1302(write_ri,Dat_Chg_BCD(day));}
  241.                                                 write_1302(write_yue,Dat_Chg_BCD(month)); //写月
  242.                                                 break;
  243.                                 case 3: day--;
  244.                                                 if((month==2&&year%4!=0)&&day==0) day=28;//平年2月28天                                                                                       
  245.                                                 if((month==2&&year%4==0)&&day==0) day=29;//闰年2月29天
  246.                                                 if((month==1 || month==3 ||month==5 ||month==7 ||month==8 ||month==10 || month==12)&&day==0)day=31;
  247.                                                 if((month==4 || month==6 ||month==9 ||month==11)&&day==0 )day=30;                                                
  248.                                                 write_1302(write_ri,Dat_Chg_BCD(day)); //写日
  249.                                                 break;
  250.                                 case 4: hour--;
  251.                                                 if(hour==255)
  252.                                                         hour=23;
  253.                                                 write_1302(write_shi,Dat_Chg_BCD(hour)); //写小时
  254.                                                 break;
  255.                                 case 5: minute--;
  256.                                                 if(minute==255)
  257.                                                         minute=59;
  258.                                                 write_1302(write_fen,Dat_Chg_BCD(minute)); //写分钟
  259.                                                 break;                                                                                 
  260.                         }
  261.                 }               
  262.         }
  263. }

  264. /////////////////////////
  265. /*显示农历日期---------------------------------------------------------------*/
  266. void displaynl(void)
  267. {
  268.         nian=read_1302(read_nian);
  269.         yue=read_1302(read_yue);
  270.         ri=read_1302(read_ri);

  271.         Conversion(0,nian,yue,ri);        //公历转农历
  272.         if (month_moon==1) {Display_HZ_16x16(64,24, COLOR_SILVER, COLOR_BLACK, 15);}//正
  273.         if (month_moon==2) {Display_HZ_16x16(64,24, COLOR_SILVER, COLOR_BLACK, 3);}        //二
  274.         if (month_moon==3) {Display_HZ_16x16(64,24, COLOR_SILVER, COLOR_BLACK, 4);}        //三
  275.         if (month_moon==4) {Display_HZ_16x16(64,24, COLOR_SILVER, COLOR_BLACK, 5);}        //四
  276.         if (month_moon==5) {Display_HZ_16x16(64,24, COLOR_SILVER, COLOR_BLACK, 6);}        //五
  277.         if (month_moon==6) {Display_HZ_16x16(64,24, COLOR_SILVER, COLOR_BLACK, 7);}        //六
  278.         if (month_moon==7) {Display_HZ_16x16(64,24, COLOR_SILVER, COLOR_BLACK, 9);}        //七
  279.         if (month_moon==8) {Display_HZ_16x16(64,24, COLOR_SILVER, COLOR_BLACK, 10);}//八
  280.         if (month_moon==9) {Display_HZ_16x16(64,24, COLOR_SILVER, COLOR_BLACK, 11);}//九
  281.         if (month_moon==10){Display_HZ_16x16(64,24, COLOR_SILVER, COLOR_BLACK, 12);}//十
  282.         if (month_moon==11){Display_HZ_16x16(64,24, COLOR_SILVER, COLOR_BLACK, 13);}//冬
  283.         if (month_moon==12){Display_HZ_16x16(64,24, COLOR_SILVER, COLOR_BLACK, 14);}//腊
  284.                         Display_HZ_16x16(80,24, COLOR_SILVER, COLOR_BLACK, 16);        //显示“月”字

  285.         if (day_moon==1)   
  286.         {
  287.                 Display_HZ_16x16(96,24, COLOR_SILVER, COLOR_BLACK, 17);//初
  288.             Display_HZ_16x16(112,24, COLOR_SILVER, COLOR_BLACK, 2);//一
  289.         }

  290.         if (day_moon==2)
  291.         {
  292.                 Display_HZ_16x16(96,24, COLOR_SILVER, COLOR_BLACK, 17);//初
  293.             Display_HZ_16x16(112,24, COLOR_SILVER, COLOR_BLACK, 3);//二        
  294.         }
  295.         if (day_moon==3)
  296.         {
  297.                 Display_HZ_16x16(96,24, COLOR_SILVER, COLOR_BLACK, 17);//初
  298.             Display_HZ_16x16(112,24, COLOR_SILVER, COLOR_BLACK, 4);//三        
  299.         }
  300.         if (day_moon==4)
  301.         {
  302.                 Display_HZ_16x16(96,24, COLOR_SILVER, COLOR_BLACK, 17);//初
  303.             Display_HZ_16x16(112,24, COLOR_SILVER, COLOR_BLACK, 5);//四        
  304.         }
  305.         if (day_moon==5)
  306.         {
  307.                 Display_HZ_16x16(96,24, COLOR_SILVER, COLOR_BLACK, 17);//初
  308.             Display_HZ_16x16(112,24, COLOR_SILVER, COLOR_BLACK, 6);//五        
  309.         }
  310.         if (day_moon==6)
  311.         {
  312.                 Display_HZ_16x16(96,24, COLOR_SILVER, COLOR_BLACK, 17);//初
  313.             Display_HZ_16x16(112,24, COLOR_SILVER, COLOR_BLACK, 7);//六        
  314.         }
  315.         if (day_moon==7)
  316.         {
  317.                 Display_HZ_16x16(96,24, COLOR_SILVER, COLOR_BLACK, 17);//初
  318.             Display_HZ_16x16(112,24, COLOR_SILVER, COLOR_BLACK, 9);//七        
  319.         }
  320.         if (day_moon==8)
  321.         {
  322.                 Display_HZ_16x16(96,24, COLOR_SILVER, COLOR_BLACK, 17);//初
  323.             Display_HZ_16x16(112,24, COLOR_SILVER, COLOR_BLACK, 10);//八        
  324.         }
  325.         if (day_moon==9)
  326.         {
  327.                 Display_HZ_16x16(96,24, COLOR_SILVER, COLOR_BLACK, 17);//初
  328.             Display_HZ_16x16(112,24, COLOR_SILVER, COLOR_BLACK, 11);//九        
  329.         }
  330.         if (day_moon==10)
  331.         {
  332.                 Display_HZ_16x16(96,24, COLOR_SILVER, COLOR_BLACK, 17);//初
  333.             Display_HZ_16x16(112,24, COLOR_SILVER, COLOR_BLACK, 12);//十        
  334.         }
  335.         if (day_moon==11)
  336.         {
  337.                 Display_HZ_16x16(96,24, COLOR_SILVER, COLOR_BLACK, 12);//十
  338.             Display_HZ_16x16(112,24, COLOR_SILVER, COLOR_BLACK, 2);//一        
  339.         }
  340.         if (day_moon==12)
  341.         {
  342.                 Display_HZ_16x16(96,24, COLOR_SILVER, COLOR_BLACK, 12);//十
  343.             Display_HZ_16x16(112,24, COLOR_SILVER, COLOR_BLACK, 3);//二        
  344.         }
  345.         if (day_moon==13)
  346.         {
  347.                 Display_HZ_16x16(96,24, COLOR_SILVER, COLOR_BLACK, 12);//十
  348.             Display_HZ_16x16(112,24, COLOR_SILVER, COLOR_BLACK, 4);//三        
  349.         }
  350.         if (day_moon==14)
  351.         {
  352.                 Display_HZ_16x16(96,24, COLOR_SILVER, COLOR_BLACK, 12);//十
  353.             Display_HZ_16x16(112,24, COLOR_SILVER, COLOR_BLACK, 5);//四        
  354.         }
  355.         if (day_moon==15)
  356.         {
  357.                 Display_HZ_16x16(96,24, COLOR_SILVER, COLOR_BLACK, 12);//十
  358.             Display_HZ_16x16(112,24, COLOR_SILVER, COLOR_BLACK, 6);//五        
  359.         }
  360.         if (day_moon==16)
  361.         {
  362.                 Display_HZ_16x16(96,24, COLOR_SILVER, COLOR_BLACK, 12);//十
  363.             Display_HZ_16x16(112,24, COLOR_SILVER, COLOR_BLACK, 7);//六        
  364.         }
  365.         if (day_moon==17)
  366.         {
  367.                 Display_HZ_16x16(96,24, COLOR_SILVER, COLOR_BLACK, 12);//十
  368.             Display_HZ_16x16(112,24, COLOR_SILVER, COLOR_BLACK, 9);//七        
  369.         }
  370.         if (day_moon==18)
  371.         {
  372.                 Display_HZ_16x16(96,24, COLOR_SILVER, COLOR_BLACK, 12);//十
  373.             Display_HZ_16x16(112,24, COLOR_SILVER, COLOR_BLACK, 10);//八        
  374.         }
  375.         if (day_moon==19)
  376.         {
  377.                 Display_HZ_16x16(96,24, COLOR_SILVER, COLOR_BLACK, 12);//十
  378.             Display_HZ_16x16(112,24, COLOR_SILVER, COLOR_BLACK, 11);//九        
  379.         }
  380.         if (day_moon==20)
  381.         {
  382.                 Display_HZ_16x16(96,24, COLOR_SILVER, COLOR_BLACK, 3);//二
  383.             Display_HZ_16x16(112,24, COLOR_SILVER, COLOR_BLACK, 12);//十        
  384.         }
  385.         if (day_moon==21)
  386.         {
  387.                 Display_HZ_16x16(96,24, COLOR_SILVER, COLOR_BLACK, 18);//廿
  388.             Display_HZ_16x16(112,24, COLOR_SILVER, COLOR_BLACK, 2);//一        
  389.         }
  390.         if (day_moon==22)
  391.         {
  392.                 Display_HZ_16x16(96,24, COLOR_SILVER, COLOR_BLACK, 18);//廿
  393.             Display_HZ_16x16(112,24, COLOR_SILVER, COLOR_BLACK, 3);//二        
  394.         }
  395.         if (day_moon==23)
  396.         {
  397.                 Display_HZ_16x16(96,24, COLOR_SILVER, COLOR_BLACK, 18);//廿
  398.             Display_HZ_16x16(112,24, COLOR_SILVER, COLOR_BLACK, 4);//三        
  399.         }
  400.         if (day_moon==24)
  401.         {
  402.                 Display_HZ_16x16(96,24, COLOR_SILVER, COLOR_BLACK, 18);//廿
  403.             Display_HZ_16x16(112,24, COLOR_SILVER, COLOR_BLACK, 5);//四        
  404.         }
  405.         if (day_moon==25)
  406.         {
  407.                 Display_HZ_16x16(96,24, COLOR_SILVER, COLOR_BLACK, 18);//廿
  408.             Display_HZ_16x16(112,24, COLOR_SILVER, COLOR_BLACK, 6);//五        
  409.         }
  410.         if (day_moon==26)
  411.         {
  412.                 Display_HZ_16x16(96,24, COLOR_SILVER, COLOR_BLACK, 18);//廿
  413.             Display_HZ_16x16(112,24, COLOR_SILVER, COLOR_BLACK, 7);//六        
  414.         }
  415.         if (day_moon==27)
  416.         {
  417.                 Display_HZ_16x16(96,24, COLOR_SILVER, COLOR_BLACK, 18);//廿
  418.             Display_HZ_16x16(112,24, COLOR_SILVER, COLOR_BLACK, 9);//七        
  419.         }
  420.         if (day_moon==28)
  421.         {
  422.                 Display_HZ_16x16(96,24, COLOR_SILVER, COLOR_BLACK, 18);//廿
  423.             Display_HZ_16x16(112,24, COLOR_SILVER, COLOR_BLACK, 10);//八        
  424.         }
  425.         if (day_moon==29)
  426.         {
  427.                 Display_HZ_16x16(96,24, COLOR_SILVER, COLOR_BLACK, 18);//廿
  428.             Display_HZ_16x16(112,24, COLOR_SILVER, COLOR_BLACK, 11);//九        
  429.         }
  430.         if (day_moon==30)
  431.         {
  432.                 Display_HZ_16x16(96,24, COLOR_SILVER, COLOR_BLACK, 4);//三
  433.             Display_HZ_16x16(112,24, COLOR_SILVER, COLOR_BLACK, 12);//十        
  434.         }

  435. }

  436. ////////////////////////////

  437. void displayxq(void) //显示星期
  438. {

  439.         unsigned char sel,dd,mo,yy;

  440.         dd = read_1302(read_ri);//日
  441.         mo = read_1302(read_yue);//月
  442.         yy = read_1302(read_nian);//年
  443.         Conver_week(0,yy,mo,dd);//调用公历换算星期子函数
  444.     sel=week;//week是公历转换星期子函数的运行结果,结果为0-6,0是星期日
  445.         if(sel==0)        {Display_HZ_16x16(32,24, COLOR_SILVER, COLOR_BLACK, 8);}        //0=星期日  
  446.           if(sel==6)  {Display_HZ_16x16(32,24, COLOR_SILVER, COLOR_BLACK, 7);}    //
  447.           if(sel==5)  {Display_HZ_16x16(32,24, COLOR_SILVER, COLOR_BLACK, 6);}   //
  448.           if(sel==4)  {Display_HZ_16x16(32,24, COLOR_SILVER, COLOR_BLACK, 5);}    //
  449.         if(sel==3)  {Display_HZ_16x16(32,24, COLOR_SILVER, COLOR_BLACK, 4);}    //
  450.           if(sel==2)  {Display_HZ_16x16(32,24, COLOR_SILVER, COLOR_BLACK, 3);}    //
  451.           if(sel==1)  {Display_HZ_16x16(32,24, COLOR_SILVER, COLOR_BLACK, 2);}    //星期一
  452. }  


  453. /////////////////////////
  454. void main()
  455. {
  456.     int8 slen = 0;
  457.         uint8 pdata uart_cmd_buf[64] = {0};

  458.         DS18B20_Start();
  459.         Timer0Init();        //T0定时10ms
  460.     DS1302_Init();        //初始化RTC时钟
  461. /*
  462. #ifdef MCU_STC12
  463.         //P3.2 set as push-pull output mode, for LCD backlight
  464.         P3M1 &= ~(1<<2);
  465.         P3M0 |=  (1<<2);
  466. #endif */


  467.         SPI_Init();
  468.         LCD_Init();
  469.         delay_ms(500);
  470.         LCD_Show_Format();

  471.     while(1)
  472.     {
  473.         if (timer_flag_250ms)  //每250ms读取一次时间
  474.         {
  475.                         ReadAndShowRtc();
  476.                         displaynl();
  477.                         displayxq();
  478.             timer_flag_250ms = 0;
  479.         }
  480.         if (timer_flag_5s)  //每隔5s执行以下分支
  481.         {
  482.                         ReadAndShowTemperature(-5);
  483.                         
  484.             timer_flag_5s = 0;
  485.                 }
  486.     }
  487. }



  488. void ReadAndShowRtc() //读rtc并显示
  489. {

  490.         read_rtc();
  491.         if(set_num==1)
  492.         {
  493.                 Display_Digit_12x16(4+24, 4, COLOR_SILVER, COLOR_BLUE, nian/10);
  494.                 Display_Digit_12x16(4+36, 4, COLOR_SILVER, COLOR_BLUE, nian%10);
  495.         }
  496.         else
  497.         {
  498.                 Display_Digit_12x16(4+24, 4, COLOR_SILVER, COLOR_BLACK, nian/10);
  499.                 Display_Digit_12x16(4+36, 4, COLOR_SILVER, COLOR_BLACK, nian%10);        
  500.         }

  501.         if(set_num==2)
  502.         {
  503.                 Display_Digit_12x16(4+60, 4, COLOR_SILVER, COLOR_BLUE, yue/10);
  504.                 Display_Digit_12x16(4+72, 4, COLOR_SILVER, COLOR_BLUE, yue%10);
  505.         }
  506.         else
  507.         {
  508.                 Display_Digit_12x16(4+60, 4, COLOR_SILVER, COLOR_BLACK, yue/10);
  509.                 Display_Digit_12x16(4+72, 4, COLOR_SILVER, COLOR_BLACK, yue%10);        
  510.         }

  511.         if(set_num==3)
  512.         {

  513.                 Display_Digit_12x16(4+96, 4, COLOR_SILVER, COLOR_BLUE, ri/10);
  514.                 Display_Digit_12x16(4+108, 4, COLOR_SILVER, COLOR_BLUE, ri%10);
  515.         }
  516.         else
  517.         {
  518.                 Display_Digit_12x16(4+96, 4, COLOR_SILVER, COLOR_BLACK, ri/10);
  519.                 Display_Digit_12x16(4+108, 4, COLOR_SILVER, COLOR_BLACK, ri%10);
  520.         
  521.         }


  522.         if(set_num==4)
  523.         {

  524.                 Display_Digit_24x48(0, 44, COLOR_YELLOW_GREEN, COLOR_BLUE, shi /10);
  525.                 Display_Digit_24x48(24, 44, COLOR_YELLOW_GREEN, COLOR_BLUE, shi %10);
  526.         }
  527.         else
  528.         {
  529.                 Display_Digit_24x48(0, 44, COLOR_YELLOW_GREEN, COLOR_BLACK, shi /10);
  530.                 Display_Digit_24x48(24, 44, COLOR_YELLOW_GREEN, COLOR_BLACK, shi %10);
  531.         
  532.         }
  533.         
  534.         if(set_num==5)
  535.         {

  536.                 Display_Digit_24x48(72, 44, COLOR_YELLOW_GREEN, COLOR_BLUE, fen /10);
  537.                 Display_Digit_24x48(96, 44, COLOR_YELLOW_GREEN, COLOR_BLUE, fen %10);
  538.         }
  539.         else
  540.         {
  541.                 Display_Digit_24x48(72, 44, COLOR_YELLOW_GREEN, COLOR_BLACK, fen /10);
  542.                 Display_Digit_24x48(96, 44, COLOR_YELLOW_GREEN, COLOR_BLACK, fen %10);        
  543.         }


  544.         Display_Digit_12x24(100,100, COLOR_RED, COLOR_BLACK, miao /10);        //        秒
  545.         Display_Digit_12x24(100+12,100, COLOR_RED, COLOR_BLACK, miao %10);        //秒
  546.                                                                         // 字颜色         底色                                               
  547. }
  548.    

  549. void ReadAndShowTemperature(int8 comp)
  550. {
  551.         int8 slen = 0, n = 0;
  552.         static uint8 pdata sbuf[5+1] = {' ', ' ', ' ', '.', ' ', 0};
  553.         uint16 tmp_n = 0;

  554.         // DS18B20的小数位四舍五入显示结果速查表,4bit=索引取值范围 0 - 15
  555.         uint8 code dect_lookup_tab[] = {0, 1, 1, 2, 3, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 9};

  556.         if(DS18B20_Get_Temperature(&ds18b20_temp_r, &ds18b20_temp_sign_r))
  557.         {
  558.                 //ds18b20_temp_r = ds18b20_temp_r * 10 * 0.0625 + 0.5;
  559.                
  560.                 if(ds18b20_temp_r != ds18b20_temp_s || ds18b20_temp_sign_r != ds18b20_temp_sign_s)
  561.                 {
  562.                         int8 intT = ds18b20_temp_r >> 4;        //分离出温度值整数部分
  563.                         int8 decT = ds18b20_temp_r & 0x0F;        //分离出温度值小数部分,DS18B20的小数位一共4bit,乘以0.625(也就是10/16)后对0.5作四舍五入可以得到比较精确的数值
  564.                         
  565. //                        if(1 == debug_flag) printf(">raw:intT.decT = (%c)%bd.%bd\r\n", ds18b20_temp_sign_r > 0 ? '+' : '-', intT, dect_lookup_tab[decT]);

  566.                         //针对个体DS18B20模块,进行温度补偿处理,减少显示温度与实际温度误差
  567.                         tmp_n = 10 * intT + dect_lookup_tab[decT];
  568.                         tmp_n += (ds18b20_temp_sign_r > 0 ? comp : -comp);
  569.                         intT  = tmp_n / 10;
  570.                         decT  = tmp_n % 10;
  571.                         
  572.                         // 在液晶屏上显示温度
  573.                         if(ds18b20_temp_sign_r >= 0 && ' ' != sbuf[0])
  574.                         {
  575.                                 sbuf[0] = ' ';
  576.                                 Display_Digit_12x24(22+0,  100, COLOR_BLACK, COLOR_BLACK, 10);
  577.                         }
  578.                         else if(ds18b20_temp_sign_r < 0 && '-' != sbuf[0])
  579.                         {
  580.                                 sbuf[0] = '-';
  581.                                 Display_Digit_12x24(22+0,  100, COLOR_LIGHT_BLUE, COLOR_BLACK, 10);
  582.                         }
  583.                                 
  584.                         n = intT / 10;
  585.                         if('0' + n != sbuf[1])
  586.                         {
  587.                                 sbuf[1] = '0' + n;
  588.                                 Display_Digit_12x24(0, 100, COLOR_LIGHT_BLUE, COLOR_BLACK, n);
  589.                         }

  590.                         n = intT % 10;
  591.                         if('0' + n != sbuf[2])
  592.                         {
  593.                                 sbuf[2] = '0' + n;
  594.                                 Display_Digit_12x24(12, 100, COLOR_LIGHT_BLUE, COLOR_BLACK, n);
  595.                         }

  596.                         n = decT;
  597.                         if('0' + n != sbuf[4])
  598.                         {
  599.                                 sbuf[4] = '0' + n;
  600.                                 Display_Digit_12x24(36, 100, COLOR_LIGHT_BLUE, COLOR_BLACK, n);
  601.                         }
  602.         
  603.                         ds18b20_temp_s = ds18b20_temp_r;
  604.                         ds18b20_temp_sign_s = ds18b20_temp_sign_r;
  605.                 }
  606.         }

  607.         DS18B20_Start(); // 重新启动下一次转换
  608. }


  609. void Timer0Init(void)                //10毫秒@11.0592MHz
  610. {
  611.         AUXR &= 0x7F;        //定时器时钟12T模式
  612.         TMOD &= 0xF0;        //设置定时器模式
  613.         TMOD |= 0x01;        //配置T0为模式1
  614.         T0RH = 0xDC;        //T0重载值的高字节
  615.         T0RL = 0x00;        //T0重载值的低字节
  616.         TH0 = T0RH;                //设置定时初值
  617.         TL0 = T0RL;                //设置定时初值
  618.     ET0 = 1;        //使能T0中断
  619.         TF0 = 0;        //清除TF0标志
  620.         TR0 = 1;        //定时器0开始计时
  621.         EA = 1;
  622. }

  623. /* T0中断服务函数,实现系统定时处理 */
  624. void Timer0_Interrupt_Service() interrupt 1
  625. {
  626.     static uint8 pdata tmr1s = 0;
  627.     static uint8 pdata tmr2s = 0;
  628.     static uint8 pdata tmr5s = 0;
  629.     static uint8 pdata tmr250ms = 0;
  630.    
  631.     TH0 = T0RH;  //重新加载重载值
  632.     TL0 = T0RL;
  633.     tmr250ms++;  //定时250ms
  634.     if (tmr250ms >= 250/TIMER0_SLICE_MS)
  635.     {
  636.         tmr250ms = 0;
  637.         timer_flag_250ms = 1;
  638.         tmr1s++;  //定时1s
  639.         if (tmr1s >= 4)
  640.         {
  641.             tmr1s = 0;
  642.             timer_flag_1s = 1;
  643.                         tmr2s++;  //定时2s
  644.                         tmr5s++;  //定时5s
  645.         }
  646.         if (tmr2s >= 2)
  647.         {
  648.             tmr2s = 0;
  649.             timer_flag_2s = 1;
  650.         }
  651.         if (tmr5s >= 5)
  652.         {
  653.             tmr5s = 0;
  654.             timer_flag_5s = 1;
  655.         }
  656.     }
  657.         keyscan();
  658. }
复制代码
51hei.png

Keil代码下载: DS1302时钟tft液晶显示.zip (290.25 KB, 下载次数: 73)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表