找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2986|回复: 0
打印 上一主题 下一主题
收起左侧

一个单片机实现1602屏显示万年历同时数码管显示温度

[复制链接]
跳转到指定楼层
楼主
只用一个51单片机实现2个屏同时显示,下面是仿真原理图:

下面是源程序:

  1.         #include <reg52.h>
  2. #include <stdio.h>

  3. sbit beep=P1^6;
  4. sbit led=P1^5;
  5. sbit rs=P3^6;
  6. sbit rw=P3^5;
  7. sbit e=P3^7;

  8. unsigned char code screen[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x83,0xf8,0x80,0x98,0xbf,0x86,0xaf,0xa3};         //0~9,'-','E','r'
  9. unsigned char wei[5]={0xbf,0xbf,0xbf,0xbf};

  10. unsigned char yy,mm,dd,h,m,s,*chp;


  11. void Flash_Scr(unsigned int times);

  12. void DS_Init();
  13. void DS_StartTrans();
  14. unsigned int DS_GetTemp();

  15. /****LCDPin assignment*****/

  16. #define LCD_DATAPORT  P0

  17. #define LCD_Delay     200
  18. void LCD_Cmd(unsigned char);
  19. void LCD_Data(unsigned char);
  20. void LCD_SetCursor(unsigned char,unsigned char);
  21. void LCD_Init(void);
  22. void LCD_Printf(const char string[]);
  23. void UpdateSCR(void);
  24. void IncTime(void);
  25. unsigned short MaxDay(void);

  26. enum
  27. {
  28.         Cursor_on    = 0x0e,
  29.         Cursor_off   = 0x0c,
  30.         Cursor_Shake = 0x0f
  31. }LCDCMD;

  32. enum{
  33.         Firstline,
  34.         Secondline
  35. }LCD_Currentline=Firstline;

  36. void Timer0Init(void)                //50ms@12.000MHz
  37. {        
  38.         TMOD = 0x11;        
  39.         TL0 = 0xB0;               
  40.         TH0 = 0x3C;        
  41.         TF0 = 0;               
  42.         TR0 = 1;
  43.         TR1 = 1;
  44.         ET0=1;
  45.         ET1=1;
  46.         EA=1;
  47. }
  48. unsigned short MaxV,MinV;
  49. unsigned char Mod=0;
  50. unsigned char code CsP[2][6]={{0,0,0,1,1,1},{6,9,12,5,8,11}};
  51.         
  52. void AdjustNum(bit inc)
  53. {
  54.         if(Mod!=0)
  55.         {
  56.                 if(inc==1)
  57.                 {
  58.                         if(*chp<MaxV)
  59.                         {
  60.                                 (*chp)++;
  61.                         }
  62.                         else
  63.                         {
  64.                                 *chp=MinV;
  65.                         }
  66.                 }
  67.                 else
  68.                 {
  69.                         if(*chp>MinV)
  70.                         {
  71.                                 (*chp)--;
  72.                         }
  73.                         else
  74.                         {
  75.                                 *chp=MaxV;
  76.                         }
  77.                 }
  78.         }
  79.         if(dd>MaxDay())
  80.         {
  81.                 dd=MaxDay();
  82.         }
  83.         UpdateSCR();
  84.         LCD_SetCursor(CsP[0][Mod-1],CsP[1][Mod-1]);
  85. }

  86. void ChangeMode(void)
  87. {
  88.         MinV=0;
  89.         switch(Mod)
  90.         {
  91.                 case 0:
  92.                 {
  93.                         TR0=0;
  94.                         LCD_Cmd(Cursor_Shake);
  95.                         MaxV=255;
  96.                         MinV=0;
  97.                         chp=&yy;
  98.                         break;
  99.                 }
  100.                 case 1:MaxV=12;MinV=1;chp=&mm;break;
  101.                 case 2:MaxV=MaxDay();MinV=1;chp=ⅆbreak;
  102.                 case 3:MaxV=23;chp=&h;break;
  103.                 case 4:MaxV=59;chp=&m;break;
  104.                 case 5:MaxV=59;chp=&s;break;
  105.                 default:Mod=0;TR0=1;LCD_Cmd(Cursor_off);
  106.         }
  107.         if(dd>MaxDay())
  108.         {
  109.                 dd=MaxDay();
  110.                 UpdateSCR();
  111.         }
  112.         LCD_SetCursor(CsP[0][Mod],CsP[1][Mod]);
  113.         Mod++;
  114. }

  115. sbit KM=P1^1;
  116. sbit KP=P1^0;
  117. sbit KN=P1^2;

  118. void main(void)
  119. {
  120.         
  121.         LCD_Init();
  122.         LCD_SetCursor(0,0);
  123.         LCD_Cmd(Cursor_off);
  124.         yy=46;
  125.         mm=11;
  126.         dd=30;
  127.         h=12;
  128.         m=23;
  129.         s=0;
  130.         DS_Init();
  131.         DS_StartTrans();
  132.         UpdateSCR();
  133.         Timer0Init();
  134.         while(1)
  135.         {
  136.                 while((P1&0x07)==0x07)
  137.                 {
  138.                         Flash_Scr(1);
  139.                 }
  140.                 if(KM==0)
  141.                 {
  142.                         ChangeMode();
  143.                 }
  144.                 if(KP==0)
  145.                 {
  146.                         AdjustNum(1);
  147.                 }
  148.                 if(KN==0)
  149.                 {
  150.                         AdjustNum(0);
  151.                 }
  152.                 while((P1&0x07)!=0x07)
  153.                 {
  154.                         Flash_Scr(1);
  155.                 }
  156.         }
  157. }

  158. void Timer(void) interrupt 1
  159. {
  160.         static unsigned char div=0;
  161.         TL0 = 0xB0;               
  162.         TH0 = 0x3C;        
  163.         if(div<19)
  164.         {
  165.                 div++;
  166.         }
  167.         else
  168.         {
  169.                 div=0;
  170.                 IncTime();
  171.                 UpdateSCR();
  172.         }
  173. }

  174. void DSTimer(void) interrupt 3
  175. {
  176.         static unsigned char div=0;
  177.         unsigned short temp;
  178.         if(div<19)
  179.         {
  180.                 div++;
  181.         }
  182.         else
  183.         {
  184.                 temp=DS_GetTemp();
  185.                 if(((temp>>4)>35)||((temp>>4)<20))
  186.                 {
  187.                         beep=0;
  188.                 }
  189.                 else
  190.                 {
  191.                         beep=1;
  192.                 }
  193.                 if((temp&0x1000)!=0)
  194.                 {
  195.                         wei[3]=screen[10];
  196.                         temp=(~temp)+1;
  197.                         div=0;
  198.                 }
  199.                 else
  200.                 {
  201.                         div=1;
  202.                         wei[3]=0xff;
  203.                         wei[0]=screen[((int)((temp%0x10)*6.25)%10)];
  204.                 }
  205.                 wei[div++]=screen[((int)((temp%0x10)*0.625))];
  206.                 temp>>=4;
  207.                 wei[div++]=screen[temp%10]-0x80;
  208.                 if((temp/=10)!=0)
  209.                 {
  210.                         wei[div++]=screen[temp%10];
  211.                         if((temp/=10)!=0)
  212.                                 wei[div]=screen[temp];
  213.                 }
  214.                 else
  215.                         wei[div]=0xff;
  216.                 div=0;
  217.                 DS_StartTrans();
  218.         }
  219.         if(div<10)
  220.         {
  221.                 led=1;
  222.         }
  223.         else
  224.         {
  225.                 led=beep;
  226.         }
  227. }

  228. unsigned short MaxDay(void)
  229. {
  230.         unsigned short ym;
  231.         switch(mm)
  232.         {
  233.                 case 4:
  234.                 case 6:
  235.                 case 9:
  236.                 case 11:
  237.                         return 30;
  238.                 case 2:
  239.                 {
  240.                         ym=yy+1970;
  241.                         if(ym%100==0)
  242.                         {
  243.                                 if(ym%400==0)
  244.                                 {
  245.                                         return 29;
  246.                                 }
  247.                                 return 28;
  248.                         }
  249.                         else
  250.                         {
  251.                                 if(ym%4==0)
  252.                                 {
  253.                                         return 29;
  254.                                 }
  255.                                 return 28;
  256.                         }
  257.                 }
  258.                 default: return 31;
  259.         }
  260. }

  261. void IncTime(void)
  262. {
  263.         if(s<59)
  264.         {
  265.                 s++;
  266.         }
  267.         else
  268.         {
  269.                 s=0;
  270.                 if(m<59)
  271.                 {
  272.                         m++;
  273.                 }
  274.                 else
  275.                 {
  276.                         m=0;
  277.                         if(h<23)
  278.                         {
  279.                                 h++;
  280.                         }
  281.                         else
  282.                         {
  283.                                 h=0;
  284.                                 if(dd<MaxDay())
  285.                                 {
  286.                                         dd++;
  287.                                 }
  288.                                 else
  289.                                 {
  290.                                         dd=0;
  291.                                         if(mm<12)
  292.                                         {
  293.                                                 mm++;
  294.                                         }
  295.                                         else
  296.                                         {
  297.                                                 mm=0;
  298.                                                 yy++;
  299.                                         }
  300.                                 }
  301.                         }
  302.                 }
  303.         }
  304. }

  305. void UpdateSCR(void)
  306. {
  307.         char GRAM[11];
  308.         char *buf=GRAM;
  309.         unsigned short ym=yy+1970;
  310.         buf+=sprintf(buf,"%04d-",ym);
  311.         buf+=sprintf(buf,"%02d-",(unsigned short)mm);
  312.         buf+=sprintf(buf,"%02d",(unsigned short)dd);
  313.         LCD_SetCursor(0,3);
  314.         LCD_Printf(GRAM);
  315.         buf=GRAM;
  316.         buf+=sprintf(buf,"%02d:",(unsigned short)h);
  317.         buf+=sprintf(buf,"%02d:",(unsigned short)m);
  318.         buf+=sprintf(buf,"%02d",(unsigned short)s);
  319.         LCD_SetCursor(1,4);
  320.         LCD_Printf(GRAM);
  321. }


  322. void LCD_Data(unsigned char dat)
  323. {
  324. unsigned char etime;
  325. e=0;
  326. rw=0;
  327. rs=1;
  328. LCD_DATAPORT=dat;
  329. e=1;
  330. etime=LCD_Delay;
  331. while(etime--);
  332. e=0;
  333. }
  334. void LCD_Cmd(unsigned char cmd)
  335. {
  336. unsigned etime;
  337. e=0;
  338. rw=0;
  339. rs=0;
  340. LCD_DATAPORT=cmd;
  341. e=1;
  342. etime=LCD_Delay;
  343. while(etime--);
  344. e=0;
  345. etime=170;
  346. while(etime--);
  347. }
  348. void LCD_Init()
  349. {
  350. LCD_Cmd(0x38);
  351. LCD_Cmd(0x38);
  352. LCD_Cmd(0x38);
  353. LCD_Cmd(0x38);
  354. LCD_Cmd(0x08);
  355. LCD_Cmd(0x01);
  356. LCD_Cmd(0x06);
  357. LCD_Cmd(0x0f);
  358. }

  359. void LCD_SetCursor(unsigned char Line,unsigned char Pos)
  360. {
  361.         if(Line==0)
  362.                 LCD_Cmd(0x80+Pos),LCD_Currentline=Firstline;
  363.         else if(Line==1)
  364.                 LCD_Cmd(0xc0+Pos),LCD_Currentline=Secondline;
  365.                
  366. }

  367. void LCD_Printf(const char *string)
  368. {
  369.         int i=0;
  370.         while(string[i]!='\0')
  371.         {
  372.                 if(string[i]=='\n')
  373.                 {
  374.                         if(LCD_Currentline==Firstline)
  375.                                 LCD_Cmd(0xc0);
  376.                         else
  377.                                 LCD_Cmd(0x80);
  378.                 }
  379.                 else
  380.                         LCD_Data(string[i]);
  381.                 i++;
  382.         }
  383. }

  384. /**************************DS18B20********************/


  385. void Flash_Scr(unsigned int times)
  386. {
  387.         unsigned short i,a;
  388.         while(times--!=0)
  389.         for(i=0;i<4;i++)           //???????
  390.   {
  391.    P2=wei[3-i];
  392.    P3|=(0x01<<i);
  393.          a=100;
  394.    while(a--);
  395.    P3&=0xf0;
  396.   }
  397. }

  398. void wait_us(unsigned char us)
  399. {
  400.         us/=7;
  401.         while(us--);
  402. }
  403. sbit ds=P1^7;
  404. void DS_Init()
  405. {
  406.         unsigned char i;
  407.                 ds=0;
  408.                 wait_us(250);
  409.         wait_us(250);
  410.         wait_us(250);
  411.                 ds=1;
  412.                 i=100;
  413.                 while(!ds);
  414.                 wait_us(480);
  415. }
  416. unsigned char DS_ReadByte()
  417. {
  418.         unsigned char i;
  419.         unsigned char dat=0;
  420.         ds=1;
  421.         wait_us(2);
  422.         for(i=0;i<8;i++)
  423.         {
  424.                 ds=0;
  425.                 wait_us(1);
  426.                 ds=1;
  427.                 wait_us(3);
  428.                 if(ds==1)
  429.                         dat=0x80|(dat>>1);
  430.                 else
  431.                         dat=0x7f&(dat>>1);
  432.                 wait_us(30);
  433.         }
  434.         return dat;
  435. }
  436. void DS_WriteByte(unsigned char dat)
  437. {
  438.         unsigned char i;
  439.         ds=1;
  440.         for(i=0;i<8;i++)
  441.         {
  442.                 ds=0;
  443.                 wait_us(5);
  444.                 ds=dat&0x01;
  445.                 wait_us(10);
  446.                 ds=1;
  447.                 dat>>=1;
  448.                 wait_us(5);
  449.         }
  450. }
  451. void DS_StartTrans()
  452. {
  453.         DS_Init();
  454.         DS_WriteByte(0xcc);
  455.         DS_WriteByte(0x44);
  456. }
  457. unsigned int DS_GetTemp()
  458. {
  459.         unsigned int val=0;
  460.         DS_Init();
  461.         DS_WriteByte(0xcc);
  462.         DS_WriteByte(0xbe);
  463.         val=DS_ReadByte();
  464.         val|=DS_ReadByte()*0x100;
  465.         return val;
  466. }
复制代码



万年历 温度.rar

72.87 KB, 下载次数: 29, 下载积分: 黑币 -5

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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