极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12222|回复: 3

TFT 屏 每一整秒更新顯示

[复制链接]
发表于 2013-10-1 01:34:39 | 显示全部楼层 |阅读模式
arduino mini pro + ds1307

想做的是屏上顯示 "時:分",時分中間兩點每一整秒閃一次,應該不難,但搞了兩整天仍沒搞好....

  1. DateTime now=RTC.now();
  2. if (temp_sec!=now.second()){
  3.     temp_sec=now.second();
  4.     //dot_color=!dot_color;
  5.     if (temp_sec%2==0) {
  6.       tft.fillRect(80,70,9,9,GREEN);
  7.       tft.fillRect(80,90,9,9,GREEN);
  8.       Serial.print(temp_sec);
  9.     }
  10.     else{

  11.       tft.fillRect(80,70,9,9,WHITE);
  12.       tft.fillRect(80,90,9,9,WHITE);
  13.       Serial.print(temp_sec);
  14.     }
复制代码



if temp_sec 不等 now.second
temp_sec=now.second
if temp_sec除2餘數=0 兩點綠色 否 則兩點白色

就只這樣,那兩點就是不閃,所以加了句 serial.print(temp_sec)測試,發現在serial運行正常,

再試... if (temp_sec%3==0) { ...那兩點會閃了.但是每兩秒閃一次!

最後發現不加每秒執行,順loop而行是沒問題,加了if (temp_sec!=now.second()){就出事

求大大高人打救

回复

使用道具 举报

 楼主| 发表于 2013-10-1 01:35:17 | 显示全部楼层
  1. #define LCD_CS A3   
  2. #define LCD_CD A2   
  3. #define LCD_WR A1   
  4. #define LCD_RD A0   
  5. // you can also just connect RESET to the arduino RESET pin
  6. #define LCD_RESET -1
  7. //Duemilanove/Diecimila/UNO/etc ('168 and '328 chips) microcontoller:
  8. // Color definitions
  9. #define        BLACK           0x0000
  10. #define        BLUE            0x001F
  11. #define        RED             0xF800
  12. #define        GREEN           0x07E0
  13. #define CYAN            0x07FF
  14. #define MAGENTA         0xF81F
  15. #define YELLOW          0xFFE0
  16. #define WHITE           0xFFFF
  17. #define        DS1307COLOR     0x07E0
  18. #define        BACKCOLOR       0x0000
  19. #define BMP085COLOR     0x07FF
  20. #define FRAMECOLOR      0x07FF

  21. #include "TFTLCD.h"
  22. TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
  23. ///////////////////////////////////////////////////////////////
  24. #include <Wire.h>
  25. #include <RTClib.h>
  26. RTC_DS1307 RTC; // Tiny RTC Modul
  27. const char *DOW[8] = {
  28.   "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT","SUN"};
  29. /////////////////////////////////////////////////////////////
  30. #include <Adafruit_BMP085.h>
  31. Adafruit_BMP085 bmp;
  32. ////////////////////////////////////////////
  33. unsigned char frequencyH = 0;
  34. unsigned char frequencyL = 0;

  35. unsigned int frequencyB;
  36. double frequency = 0;
  37. //////////////////////////////////////////
  38. #include "TouchScreen.h"
  39. #define YP A1  // must be an analog pin, use "An" notation!
  40. #define XM A2  // must be an analog pin, use "An" notation!
  41. #define YM  7 // can be a digital pin
  42. #define XP  6  // can be a digital pin

  43. #define TS_MINX 150
  44. #define TS_MINY 120
  45. #define TS_MAXX 920
  46. #define TS_MAXY 940

  47. // For better pressure precision, we need to know the resistance
  48. // between X+ and X- Use any multimeter to read it
  49. // For the one we're using, its 300 ohms across the X plate
  50. TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
  51. #define MINPRESSURE 10
  52. #define MAXPRESSURE 1000

  53. //////////////////////////////////
  54. char temp_str[12];
  55. int temp_sec=0;



  56. void setup(void) {
  57.   Wire.begin();
  58.   RTC.begin();
  59.   bmp.begin();
  60.   Serial.begin(9600);
  61.   //Serial.println("8 Bit LCD test!");
  62.   tft.reset();
  63.   tft.initDisplay();
  64.   tft.fillScreen(BLUE);
  65.   frequency = 92.3; //starting frequency
  66.   setFrequency();
  67.   tft.setTextColor(DS1307COLOR);
  68.   tft.setTextSize(2);
  69.   tft.setCursor(5, 15);
  70.   DateTime now=RTC.now();
  71.   temp_sec=now.second();
  72.   show_date(now.dayOfWeek(),now.day(),now.month(),now.year());
  73.   show_hour(now.hour());
  74.   show_minute(now.minute());
  75.   show_bmp085();
  76. }

  77. void loop(void) {
  78.   digitalWrite(13, HIGH);
  79.   Point p = ts.getPoint();
  80.   digitalWrite(13, LOW);
  81.   pinMode(XM, OUTPUT);
  82.   pinMode(YP, OUTPUT);
  83.   p.x = map(p.x, TS_MAXX, TS_MINX, tft.width(), 0);
  84.   p.y = map(p.y, TS_MAXY, TS_MINY, tft.height(), 0);
  85.   //tft.setTextWrap(false);
  86.   DateTime now=RTC.now();

  87.   if (temp_sec!=now.second()){
  88.     temp_sec=now.second();
  89.     //dot_color=!dot_color;
  90.     if (temp_sec%2==0) {
  91.       tft.fillRect(80,70,9,9,GREEN);
  92.       tft.fillRect(80,90,9,9,GREEN);
  93.       Serial.print(temp_sec);
  94.     }
  95.     else{

  96.       tft.fillRect(80,70,9,9,WHITE);
  97.       tft.fillRect(80,90,9,9,WHITE);
  98.       Serial.print(temp_sec);
  99.     }

  100.     tft.setCursor(5,110);
  101.     tft.setTextColor(WHITE);
  102.     tft.fillRect(5,110,50,50,BLACK);
  103.     tft.print(temp_sec);
  104.     Serial.println(temp_sec);


  105.     if (now.second()==0) {
  106.       show_minute(now.minute());
  107.       if (now.minute()==0){
  108.         show_hour(now.hour());
  109.         if (now.hour()==0) show_date(now.dayOfWeek(),now.day(),now.month(),now.year());
  110.       }     
  111.     }

  112.     if (now.second()==0 || now.second()==30) show_bmp085();  

  113.   }
  114.   if (p.z > 5){
  115.     Serial.print(p.x);
  116.     Serial.print("-");
  117.     Serial.print(p.y);
  118.     Serial.print("-");
  119.     Serial.println(p.z);
  120.   }
  121. }


  122. void setFrequency()
  123. {
  124.   frequencyB = 4 * (frequency * 1000000 + 225000) / 32768;
  125.   frequencyH = frequencyB >> 8;
  126.   frequencyL = frequencyB & 0XFF;
  127.   delay(100);
  128.   Wire.beginTransmission(0x60);
  129.   Wire.write(frequencyH);
  130.   Wire.write(frequencyL);
  131.   Wire.write(0xB0);
  132.   Wire.write(0x10);
  133.   Wire.write((byte)0x00);
  134.   Wire.endTransmission();
  135.   delay(100);  
  136. }


  137. // Draw a red frame while a button is touched
  138. void waitForIt(int x1, int y1, int x2, int y2)
  139. {
  140.   //.setColor(255, 0, 0);
  141.   //myGLCD.drawRoundRect (x1, y1, x2, y2);
  142.   //while (myTouch.dataAvailable())
  143.   //  myTouch.read();
  144.   //myGLCD.setColor(255, 255, 255);
  145.   //myGLCD.drawRoundRect (x1, y1, x2, y2);

  146.   //touch_read();
  147.   //tft.drawRect(x1,y1,x2,y2,FRAMECOLOR);
  148.   //while (p.z>10) touch_read();
  149.   //tft.drawRect(x1,y1,x2,y2,BACKCOLOR);
  150. }


  151. void show_date(int w,int d,int m, int y)
  152. {
  153.   tft.setTextColor(DS1307COLOR);
  154.   tft.fillRect(5,5,240,30,BACKCOLOR);
  155.   tft.setTextSize(2);
  156.   tft.setCursor(5, 15);
  157.   tft.print(DOW[w]);
  158.   sprintf(temp_str, "%02d-%02d-%04d", d, m, y);
  159.   tft.setCursor(60, 10);
  160.   tft.setTextSize(3);
  161.   tft.print(temp_str);
  162. }

  163. void show_hour(int h)
  164. {
  165.   tft.setTextColor(DS1307COLOR);
  166.   tft.fillRect(5,60,70,45,BACKCOLOR);
  167.   tft.setTextSize(6);
  168.   tft.setCursor(5, 60);
  169.   sprintf(temp_str,"%02d",h);
  170.   tft.print(temp_str);

  171. }

  172. void show_minute(int m)
  173. {
  174.   tft.setTextColor(DS1307COLOR);
  175.   tft.fillRect(95,60,70,45,BACKCOLOR);
  176.   tft.setTextSize(6);
  177.   tft.setCursor(95, 60);
  178.   sprintf(temp_str,"%02d",m);
  179.   tft.print(temp_str);

  180. }

  181. void show_bmp085()
  182. {
  183.   tft.setTextColor(BMP085COLOR);
  184.   tft.fillRect(170,50,70,55,BACKCOLOR);
  185.   tft.setTextSize(2);
  186.   tft.setCursor(170, 50);
  187.   tft.print(bmp.readTemperature());
  188.   tft.setCursor(170, 70);
  189.   tft.print(bmp.readAltitude());
  190.   tft.setCursor(170, 90);
  191.   tft.print(bmp.readPressure());
  192. }



复制代码
回复 支持 反对

使用道具 举报

发表于 2013-10-1 11:12:55 | 显示全部楼层
楼主测试了??
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-10-2 22:02:35 | 显示全部楼层
学慧放弃 发表于 2013-10-1 11:12
楼主测试了??

測試什麼 ?     
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-8 10:37 , Processed in 0.072547 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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