找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3987|回复: 3
收起左侧

MSP430单片机的实时时钟及温度显示程序

[复制链接]
ID:395176 发表于 2019-4-14 09:23 | 显示全部楼层 |阅读模式
最近学习MSP430,把以前学习的DS1302和DB18B20移植过来了,把时序搞清楚还是比较简单的,温度显示做得比较粗糙,分享一下(PS:后续有时间添加按键设置及其他功能
da3ea7c4adeb0d689b75910cf2d828e.jpg

单片机源程序如下:
  1. #include<msp430x14x.h>
  2. #include"lcd1602.c"
  3. #include"ds1302.c"
  4. #include"ds18b20.c"
  5. //typedef unsigned char uchar;
  6. //typedef unsigned int uint;
  7. //uchar str1[]="hongchao12345678";
  8. //uchar str2[]="hongchao12345678";
  9. //uchar LcdMove[16+sizeof(str1)+16];
  10. uchar index=0,T500MSfg=0,flash=0;
  11. uchar psec=0xaa;
  12. uchar time[8],str[12];
  13. //uchar second=10,minute=20,hour=11;
  14. /*
  15. void Lcd_Move()
  16. {
  17.     uchar i;
  18.     for(i=0;i<16;i++)
  19.     {
  20.         LcdMove[i]=' ';
  21.     }
  22.     for(i=0;i<sizeof(str1);i++)
  23.     {
  24.         LcdMove[16+i]=str1[i];
  25.     }
  26.     for(i=16+sizeof(str1);i<sizeof(LcdMove);i++)
  27.     {
  28.         LcdMove[i]=' ';
  29.     }
  30. }
  31. */
  32. void Time0_Init()
  33. {
  34.     TACTL=TASSEL_1+ ID_3+ MC_1;
  35.     CCTL0=CCIE;
  36.     CCR0=1024;

  37. }
  38. /*
  39. void clock()
  40. {
  41.      second++;
  42.      if(second>59)
  43.      {
  44.             second=0;
  45.             minute++;
  46.             if(minute>59)
  47.             {
  48.                   minute=0;
  49.                   hour++;
  50.             }
  51.      }
  52. }
  53. void Display()
  54. {
  55.       uchar s0,s1;
  56.       uchar m0,m1;
  57.       uchar h0,h1;
  58.       s0=second/10;
  59.       s1=second%10;
  60.       m0=minute/10;
  61.       m1=minute%10;
  62.       h0=hour/10;
  63.       h1=hour%10;
  64.       s0+=0x30;
  65.       s1+=0x30;

  66.       m0+=0x30;
  67.       m1+=0x30;

  68.       h0+=0x30;
  69.       h1+=0x30;
  70.        LcdShowStr1(4,0,h0);
  71.        LcdShowStr1(5,0,h1);
  72.        LcdShowStr1(7,0,m0);
  73.        LcdShowStr1(8,0,m1);
  74.        LcdShowStr1(10,0,s0);
  75.        LcdShowStr1(11,0,s1);
  76. }
  77. */
  78. void DS1302_Time()
  79. {
  80.                         //for(index=0; index<7; index++)
  81.                         //{
  82.                                 //time[index] = DS1302SingleRead(index);
  83.                         DS1302BurstRead(time);
  84.                         //}
  85.                         if(psec != time[0])
  86.                         {
  87.                                 str[0] = '2';
  88.                                 str[1] = '0';
  89.                                 str[2] = (time[6] >> 4) + 0x30;
  90.                                 str[3] = (time[6] & 0x0F) + 0x30;
  91.                                 str[4] = '-';
  92.                                 str[5] = (time[4] >> 4) + 0x30;
  93.                                 str[6] = (time[4] & 0x0F) + 0x30;
  94.                                 str[7] = '-';
  95.                                 str[8] = (time[3] >> 4) + 0x30;
  96.                                 str[9] = (time[3] & 0x0F) + 0x30;
  97.                                 str[10] = '\0';
  98.                                 LcdShowStr(0, 0, str);

  99.                                 str[0] = (time[5] & 0x0F) + 0x30;
  100.                                 str[1] = '\0';
  101.                                 LcdShowStr(11, 0, "WEEK");
  102.                                 LcdShowStr(15, 0, str);

  103.                                 str[0] = (time[2] >> 4) + 0x30;
  104.                                 str[1] = (time[2] & 0x0F) + 0x30;
  105.                                 str[2] = ':';
  106.                                 str[3] = (time[1] >> 4) + 0x30;
  107.                                 str[4] = (time[1] & 0x0F) + 0x30;
  108.                                 str[5] = ':';
  109.                                 str[6] = (time[0] >> 4) + 0x30;
  110.                                 str[7] = (time[0] & 0x0F) + 0x30;
  111.                                 str[8] = '\0';
  112.                                 LcdShowStr(8, 1, str);

  113.                                 psec = time[0];
  114.                         }
  115. }
  116. void DS18b20_Temp()
  117. {
  118.     uchar ack;
  119.     uchar num0,num1;
  120.     int temp=0;
  121.     // ack=DS_Ready();
  122.     ack=GetTemp(&temp);
  123.     if(ack==0)
  124.     {
  125.         num1=temp>>4;
  126.        // num2=temp<<4;
  127.        // num2=num2>>4;
  128.         num0=num1/10+0x30;
  129.         num1=num1%10+0x30;
  130.        // num2=num2%10+0x30;
  131.         LcdShowStr1(0,1,'T');
  132.         LcdShowStr1(1,1,'H');
  133.         LcdShowStr1(2,1,' ');
  134.         LcdShowStr1(3,1,num0);
  135.         LcdShowStr1(4,1,num1);
  136.       //  LcdShowStr1(5,1,'.');
  137.      //   LcdShowStr1(6,1,num2);

  138.     }
  139.     else
  140.     {
  141.       // LcdShowStr1(0,1,'E');
  142.       // LcdShowStr1(1,1,'R');
  143.       // LcdShowStr1(2,1,'R');
  144.       // LcdShowStr1(3,1,'0');
  145.       // LcdShowStr1(4,1,'R');
  146.        LcdShowStr(0,1,"ERR0R");
  147.     }
  148.     DSStart();
  149. }
  150. void main()
  151. {
  152.       /*下面六行程序关闭所有的IO口*/
  153.     P1DIR = 0XFF;P1OUT = 0XFF;
  154.     P2DIR = 0XFF;P2OUT = 0XFF;
  155.     P3DIR = 0XFF;P3OUT = 0XFF;
  156.     P4DIR = 0XFF;P4OUT = 0XFF;
  157.     P5DIR = 0XFF;P5OUT = 0XFF;
  158.     P6DIR = 0XFF;P6OUT = 0XFF;
  159.    // uchar i;
  160.     WDTCTL = WDTPW + WDTHOLD;
  161.     InitLcd1602();
  162.   //  TACTL=TASSEL_2;
  163.    // Lcd_Move();
  164.     Time0_Init();    //定时器使用32768,250ms
  165.     DS1302_Init();
  166.     DSStart();
  167.     _EINT();
  168.    // LcdShowStr(0,1," CLOCK  TIME ");
  169.   //  LcdShowStr1(6,0,':');
  170.   //  LcdShowStr1(9,0,':');
  171.    // LcdShowStr(0,0,"   :   :   ")
  172.     while(1)
  173.     {
  174.        //  LcdShowStr(1,0,"w23123123!");
  175.         // LcdShowStr(0,1,"hongchao 123");
  176.             if(flash)
  177.             {
  178.                   flash=0;
  179.                   DS1302_Time();
  180.                  DS18b20_Temp();
  181.                  // LcdShowStrLen(1,0,LcdMove+index,16);
  182.                  // index++;
  183.                   //if(index>sizeof(LcdMove))
  184.                 //  {
  185.                 //        index=0;
  186.                  // }
  187.                //   Display();
  188.            }
  189.     }

  190. }
  191. #pragma vector =TIMERA0_VECTOR
  192. __interrupt void TIME_A()
  193. {
  194.      // T500MSfg++;
  195.       flash=1;
  196.       //if(T500MSfg>2)
  197.      // {
  198.        // T500MSfg=0;
  199.       //  flash=1;
  200.        // clock();
  201.      // }
  202. }
复制代码

全部资料51hei下载地址:
demo.zip (54.25 KB, 下载次数: 46)

评分

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

查看全部评分

回复

使用道具 举报

ID:243174 发表于 2019-4-14 22:34 | 显示全部楼层
非常不错,谢谢分享
回复

使用道具 举报

ID:413728 发表于 2019-4-19 14:41 | 显示全部楼层
谢谢分享
回复

使用道具 举报

ID:541533 发表于 2019-5-23 18:35 | 显示全部楼层
作者请问

在运行你这个程序后出现这个怎么解决啊    function"get Temp"declared implicitly
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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