天南地北客
发表于: 2018-8-23 07:34:02 | 显示全部楼层

想学习如何制作Launchpad控制的时钟?在本篇文章中,我们不止于此,会做得更多:这不仅是一个数字时钟,它还可以定期测量和显示房间的温度和相对湿度。本篇文章基于TI的LaunchPad开发板制作,并且使用Energia IDE开发环境编写软件代码。

launchpad-diy-clock.jpg


使用的材料

●    MSP430 LaunchPad开发板

●    4位七段数码管

●    DHT22温度和湿度传感器

●    DS1307实时时钟

●    32.768 kHz晶振

●    CR2025 3V电池

●    连接导线和面包板


连接原理图

launchpad-diy-clockschematic-765.jpg

从原理图中可以看出,该电路有四个主要组件:LaunchPad、DS1307实时时钟、DHT22传感器和4位七段数码管。


DS1307是一款实时时钟IC,可通过I2C总线串行提供时间和日期信息。传输的数据包括小时、分钟、秒、年、月和日等信息。 DS1307需要一个频率为32.768 kHz的外部时钟,可由晶体振荡器提供。当IC的主电源被切断时,IC切换到后备电源模式并继续以微小电流模式工作。

launchpad-diy-clock-rtc.jpg


使用LaunchPad的内部I2C模块直接从DS1307读取时间信息。可以使用RTCLib库。 RTC和LaunchPad之间的连接通过SCL(P1.6)和SDA(P1.7)线完成,如原理图所示。这些连线通过2.2K的电阻R2和R3上拉至VCC,以保证正确的I2C操作。


DS1307需要5V电源电压。由于LaunchPad不提供5V输出,因此使用外部5V AC-DC适配器。 CR2025 3V电池连接到DS1307的VBAT引脚作为备用电源,可以使用几年。


在我们的应用程序中,我们没有外部控件来设置DS1307的日期和时间。要将当前日期和时间信息提供给DS1307,我们使用以下命令;

  1. RTC.adjust(DateTime(__DATE__, __TIME__));
复制代码

此命令将编程器计算机的当前日期和时间作为输入,并在上载Sketch后设置DS1307。它应在第一次上传后注释掉,并仅在需要新设置时使用。


电路的温度和湿度传感器是DHT22,也称为AM2302。 DHT22通过串行单数据总线进行通信。当微控制器发送起始位时,DHT22传输一个40位数据包,其中包括相对湿度和温度信息。在我们的应用程序中,我们使用适用于Energia的DHT22库从DHT22收集信息。

launchpad-diy-clock-dht22.jpg


该电路的另一个主要部件是4位七段数码管。时间、温度和相对湿度信息显示在该数码管上。由于段线对于所有四个数字都是共用的,因此必须使用多路复用方法。在这种方法中,我们通过使用PNP晶体管2N3906作为开关一次仅导通一个数字。数字在3ms周期内依次打开和关闭。由于此动作足够快,人眼无法识别排序并看到所有四个数字都打开。为限制LED段电流,串联一只100欧电阻。

launchpad-diy-clock-temperature.jpg


软件代码

该应用程序的软件内置于Energia IDE中,如下所示。该程序循环运行,包括以下操作;

●    初始化电路板

●    从DS1307读取时间

●    显示一段时间的时间

●    读取相对湿度和温度数据

●    显示温度一段时间

●    显示相对湿度一段时间。


Energia IDE中的sketch详细阐述了每一步。请在帖子后面提出您遇到的问题。

  1. #include "Wire.h"
  2. #include "RTClib.h"
  3. #include "DHT22_430.h"
  4. #define DHTPIN P2_6
  5. RTC_DS1307 RTC;
  6. DHT22 mySensor(DHTPIN);

  7. unsigned long timestamp;
  8. unsigned int clockduration = 0;
  9. unsigned int tempduration = 0;
  10. unsigned int humduration = 0;

  11. // Seven Segment Display driver function
  12. void displaydigit (int value, int digit, boolean dot) {
  13.   
  14.   // Create each digit by turning on and off LED segments
  15.   P1OUT |= 0b00011111;
  16.   P2OUT |= 0b00111000;
  17.   switch (value) {
  18.   case 0:
  19.     P1OUT &= 0b11100100; P2OUT &= 0b11001111; break;
  20.   case 1:
  21.     P1OUT &= 0b11111111; P2OUT &= 0b11001111; break;
  22.   case 2:
  23.     P1OUT &= 0b11100101; P2OUT &= 0b11010111; break;
  24.   case 3:
  25.     P1OUT &= 0b11110101; P2OUT &= 0b11000111; break;
  26.   case 4:
  27.     P1OUT &= 0b11111110; P2OUT &= 0b11000111; break;
  28.   case 5:
  29.     P1OUT &= 0b11110100; P2OUT &= 0b11100111; break;
  30.   case 6:
  31.     P1OUT &= 0b11100100; P2OUT &= 0b11100111; break;
  32.   case 7:
  33.     P1OUT &= 0b11111101; P2OUT &= 0b11001111; break;
  34.   case 8:
  35.     P1OUT &= 0b11100100; P2OUT &= 0b11000111; break;
  36.   case 9:
  37.     P1OUT &= 0b11110100; P2OUT &= 0b11000111; break;
  38.   case 10:  
  39.     P1OUT &= 0b11111111; P2OUT &= 0b11111111; break;
  40.   case 11:
  41.     P1OUT &= 0b11111100; P2OUT &= 0b11010111; break;
  42.   case 12:
  43.     P1OUT &= 0b11101100; P2OUT &= 0b11010111; break;
  44.   }

  45.   // Turn on/off the dot LED
  46.   if (dot) P1OUT &= 0b11111011;
  47.   else P1OUT |= 0b00000100;
  48.   
  49.   
  50.   // Turn on the selected digit
  51.   digitalWrite(P1_5, HIGH);
  52.   digitalWrite(P2_0, HIGH);
  53.   digitalWrite(P2_1, HIGH);
  54.   digitalWrite(P2_2, HIGH);
  55.   
  56.   switch (digit) {  
  57.   case 1:
  58.     digitalWrite(P1_5, LOW); break;
  59.   case 2:
  60.     digitalWrite(P2_0, LOW); break;
  61.   case 3:
  62.     digitalWrite(P2_1, LOW); break;
  63.   case 4:
  64.     digitalWrite(P2_2, LOW); break;
  65.   }
  66. }

  67. void setup()
  68. {
  69.   // Set the port directions
  70.   P1DIR |= 0b00111111;
  71.   P2DIR |= 0b00111111;


  72.   mySensor.begin();
  73.   Wire.begin();
  74.   RTC.begin();
  75.   
  76.   // Write the current date and time to DS1397
  77.   RTC.adjust(DateTime(__DATE__, __TIME__));
  78. }

  79. void loop()
  80. {
  81.   
  82.   // Read and display the time
  83.   while (clockduration <= 500) {
  84.    
  85.     timestamp = micros();
  86.     DateTime now = RTC.now();
  87.     while (timestamp + 3000 >= micros());
  88.    
  89.     if (now.hour()/10)
  90.       displaydigit((now.hour()/10),1,0);
  91.     else
  92.       displaydigit(10,1,0);
  93.     delay(3);
  94.     if (now.second()%2)
  95.       displaydigit((now.hour()),2,1);
  96.     else
  97.       displaydigit((now.hour()),2,0);
  98.     delay(3);
  99.     displaydigit((now.minute()/10),3,0);
  100.     delay(3);
  101.     displaydigit((now.minute()),4,0);

  102.     clockduration++;
  103.   }
  104.   
  105.   displaydigit(10,1,0);
  106.   displaydigit(10,2,0);
  107.   displaydigit(10,3,0);
  108.   displaydigit(10,4,0);

  109.   // Read and display the temperature and the relative humidity
  110.   mySensor.get();
  111.   int32_t h = mySensor.humidityX10();
  112.   int32_t t = mySensor.temperatureX10();


  113.   while (tempduration <= 300) {
  114.     delay(3);
  115.     displaydigit(t/100,1,0);
  116.     delay(3);
  117.     displaydigit((t/10),2,1);
  118.     delay(3);
  119.     displaydigit(t,3,0);
  120.     delay(3);
  121.     displaydigit(11,4,0);
  122.     tempduration++;
  123.   }

  124.   while (humduration <= 300) {
  125.     delay(3);
  126.     displaydigit(h/100,1,0);
  127.     delay(3);
  128.     displaydigit((h/10),2,1);
  129.     delay(3);
  130.     displaydigit(h,3,0);
  131.     delay(3);
  132.     displaydigit(12,4,0);
  133.     humduration++;
  134.   }
  135.   
  136.   clockduration=0;
  137.   tempduration=0;
  138.   humduration=0;

  139. }
复制代码

跳转到指定楼层
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题 26 | 回复: 45



手机版|

GMT+8, 2024-4-19 00:46 , Processed in 0.149295 second(s), 8 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

YiBoard一板网 © 2015-2022 地址:河北省石家庄市长安区高营大街 ( 冀ICP备18020117号 )

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