痞酷網_PIGOO

 找回密碼
 立即註冊
!!! [系統偵測到廣告阻擋軟體] !!!

如果您覺得痞酷網對您有些許幫助,或者您認同痞酷網的理想,

那麼希望您將痞酷網設定為白名單.

並請在上論壇的時候,動動您的手指,用行動支持我們.

謝謝!
查看: 2313|回復: 6

事件紀錄電路 ARDUINO NANO & PROMINI

[複製鏈接]
發表於 2020-2-6 21:36:53 | 顯示全部樓層 |閱讀模式
本帖最後由 antlu 於 2020-2-6 09:41 PM 編輯

最近做了兩個事件紀錄電路,一個是紀錄10組觸發時間隨時可以讀取發生時間,動作是 使用 DS1302 RTC 隨時監控時間,一但 中斷觸發就紀錄當時的時間,這樣可以紀錄小貓侵入的時間,搭配錄影機就方便調影片來追蹤!! 另一個則搭配 SD卡片 把發生的時間紀錄在SD卡上,先前做了個電壓紀錄器,於是把他整合進來,也就是可以紀錄每隔 5秒 10秒 30秒 300秒 1800秒的三組外部輸入電壓數值,也可以紀錄外部觸發 中斷0 中斷1 時候的時間與電壓,這方便紀錄何時繼電器動作時候的三組電壓... 第二個因為搭配SD卡存資料,程式太大所以沒辦法做到讀取 SD卡記憶體的內容,SD卡的資料必須搭配電腦和讀卡機以 EXCEL 檔案讀取...提供大家參考..

第一台的外觀和線路 使用 ARDUINO PRO-MINI
按鍵一事件時間讀取增加,第二按鍵 事件時間讀取減少..
線路圖
EV18.jpg

PCB
EV16.jpg

EV17.jpg

外觀
EV8.jpg

EV10.jpg

EV11.jpg



第二台的外觀和線路 使用 ARDUINO NANO
外觀
EV2.jpg

EV6.jpg

EV7.jpg

EV4.jpg

EV5.jpg

EV1.jpg

EV3.jpg

線路
EV14.jpg

EV15.jpg

輸入部分
EV13.jpg

EV12.jpg

第一台程式 (使用 DS1302 RTC  LCM使用74595+LCM2402 ARDUINO PRO-MINI)
  1. // USE 74595 LCM ARDUINO PROMINI Interrupt
  2. // key in low active interrupt BECKHOFF MODULE HIGH ACTIVE
  3. //used BECKOFF PLC INPUT MODULE LOW OUT 2020.01.23
  4. #include <DS1302.h>

  5. volatile boolean int0Flag = 0;
  6. volatile boolean int1Flag = 0;
  7. volatile byte evn0_count = 0;
  8. volatile byte evn1_count = 0;
  9. //Define 74HC595 Connections with arduio
  10. const int Dsply_Latch=13; //LCM----74595 pin 12 black
  11. const int Dsply_Data=12;  //LCM----74595 pin 14 green
  12. const int Dsply_Clock=11; //LCM----74595 pin 11 blue
  13. const int RTC_CLK=6;//
  14. const int RTC_DAT=7;//
  15. const int RTC_RST=8;//
  16. const int EVN_1=2;//interrupt 0
  17. const int EVN_2=3;//interrupt 1
  18. const int ReadKey1 =4;//^ key
  19. const int ReadKey2 =5;//v key
  20. volatile byte EVN_1flag =0;
  21. volatile byte EVN_2flag =0;
  22. String date_bf0[]={"1","2","3","4","5","6","7","8","9","10"};//it is String not char ..
  23. String time_bf0[]={"a","b","c","d","e","f","g","h","i","j"};
  24. String date_bf1[]={"1","2","3","4","5","6","7","8","9","10"};//it is String not char ..
  25. String time_bf1[]={"a","b","c","d","e","f","g","h","i","j"};
  26. int bf0;
  27. int bf1;
  28. byte i =0;
  29. byte j =0;
  30. byte dspDatacount;
  31. boolean dspupdate_flag = true;
  32. // const int Led=13;//Led on off
  33. void LCDinit(void);
  34. void LCDSendByte(char d,char RS);
  35. void LCDPrint(char Line,char string[40]);//--
  36. DS1302 rtc(RTC_RST,RTC_DAT,RTC_CLK);
  37. char EVN_Array[3];
  38. char D[24];
  39. char A[20];
  40. void setup() {
  41.   Serial.begin(9600);
  42.   delay(500);
  43.   pinMode(EVN_1,INPUT ); // INPUT_PULLUP
  44.   pinMode(EVN_2, INPUT); //   
  45.   pinMode(ReadKey1, INPUT_PULLUP); //
  46.   pinMode(ReadKey2, INPUT_PULLUP); //
  47.   attachInterrupt(0,EVN_1_ISR,RISING); // set an interrupt on PinA
  48.   attachInterrupt(1,EVN_2_ISR,RISING); // set an interrupt on PinB
  49.   pinMode(Dsply_Data, OUTPUT);
  50.   pinMode(Dsply_Clock, OUTPUT);
  51.   pinMode(Dsply_Latch, OUTPUT);  
  52.   DS1302 rtc(RTC_RST,RTC_DAT,RTC_CLK);
  53.   rtc.halt(false);
  54.   
  55.   //取消寫入保護,設定日期時要這行
  56. rtc.writeProtect(false);  

  57. // 以下是設定時間的方法,在電池用完之前,只要設定一次就行了
  58. // rtc.setTime(23, 7, 0);     // 設定時間 時,分,秒 (24hr format)

  59. //  rtc.setDate(22, 1, 2020);   // 設定日期 日,月,年
  60. LCDinit();
  61. LCDclean();
  62. LCDPrint(0,"     Event Record V1");
  63. LCDPrint(1,"          2020.01.17");
  64. }

  65. void loop() {
  66.    while(1)
  67.    {


  68. //  Serial.println("end of program");
  69.    //---------start program ---------------
  70.    if(int0Flag == true)
  71.    {
  72.    
  73.     evn0_count ++;   
  74.     if (evn0_count >10){evn0_count =0;}
  75.     Serial.print("evn0_count =");
  76.     Serial.println(evn0_count);
  77.     date_bf0[evn0_count] = rtc.getDateStr();
  78.     time_bf0[evn0_count] = rtc.getTimeStr();   
  79.      dspupdate_flag = true;
  80.       LCDPrintString(0,1,"                      ");  
  81.      readData2Dsply(evn0_count);
  82.     String evn_count_str = String(evn0_count);
  83.      evn_count_str.toCharArray(EVN_Array,3);     
  84. //     LCDPrintString(0,1,"        int0 count      ");
  85.      
  86.     LCDPrintString(0,1,EVN_Array);
  87.       LCDWriteChar(0,23,'@');
  88.      delay(100);
  89.      int0Flag = false;   
  90.    }

  91.    if(int1Flag == true)
  92.    {   
  93.     evn1_count ++;
  94.     int1Flag = false;
  95.         if (evn1_count >10){evn1_count =0;}
  96.     Serial.print("evn1_count =");
  97.     Serial.println(evn1_count);
  98.     date_bf1[evn1_count] = rtc.getDateStr();
  99.     time_bf1[evn1_count] = rtc.getTimeStr();
  100.     dspupdate_flag = true;  
  101.      LCDPrintString(1,1,"                      ");
  102.     readData2Dsply(evn1_count+10);
  103.      String evn_count_str = String(evn1_count);
  104.      evn_count_str.toCharArray(EVN_Array,3);     
  105.   

  106.      LCDPrintString(1,1,EVN_Array);
  107.      LCDWriteChar(1,23,'@');
  108.      delay(100);
  109.     int1Flag = false;   
  110.    }

  111.    //read key in sw
  112.    readSW();// const int ReadKey1 =4;//^ key const int ReadKey2 =5;//v key
  113.    if( dspupdate_flag == true)
  114.    {  
  115.    dspData();//display readKey
  116.      // 日期
  117.   Serial.print(rtc.getDateStr());
  118.   Serial.print(" -- ");

  119.   // 時間
  120.   Serial.println(rtc.getTimeStr());
  121.   Serial.println();
  122.    }
  123.   }
  124. }

  125. //=============================================================
  126. //             Send Data or Command to LCD
  127. //=============================================================
  128. void LCDSendByte(char d,char RS)
  129. {
  130.   char dH,dL,temp;
  131.   //Keep Data on upper nybble
  132.   dH = d & 0xF0;         //Get MSB
  133.   dL = d & 0x0F;
  134.   dL = d << 4;           //Get LSB
  135. //Send MSB with E=clock  
  136.   temp=0;
  137.   temp=dH | RS | 0x02;  //MSB With RS+E bit
  138.   ShiftData(temp);
  139. //Send MSB with E=0
  140.   temp=0;
  141.   temp=dH | RS;  //MSB With RS bit
  142.   ShiftData(temp);
  143. //Send LSB with E=clock
  144.   temp=0;
  145.   temp=dL | RS | 0x02;  //MSB With RS+E bit
  146.   ShiftData(temp);
  147. //Send LSB with E=0
  148.   temp=0;
  149.   temp=dL | RS;  //MSB With RS bit
  150.   ShiftData(temp);  
  151. }
  152. //=============================================
  153. void ShiftData(char temp)
  154. {
  155.   int i;
  156.   for(i=0;i<8;i++)      
  157.   {
  158.     if((temp & 0x80)==0x80)    //Send 1-bit to shift register
  159.     {digitalWrite(Dsply_Data,HIGH);}
  160.     else
  161.     {digitalWrite(Dsply_Data,LOW);}         
  162.     digitalWrite(Dsply_Clock,LOW);
  163.     digitalWrite(Dsply_Clock,HIGH);
  164.     temp=temp<<1;
  165.   }
  166.   //Latch the data
  167.   digitalWrite(Dsply_Latch,LOW);
  168.   delay(1);
  169.   digitalWrite(Dsply_Latch,HIGH);        
  170. }
  171. //=================================================================
  172. //           LCD Display Initialization Function
  173. //=================================================================
  174. void LCDinit()
  175. {
  176.   int count;
  177.   char t[]={0x43,0x03,0x03,0x02,0x28,0x01,0x0C,0x06,0x02,0x02};   
  178.       for (count = 0; count <= 9; count++)
  179.       {
  180.         LCDSendByte(t[count],0);    //Command RS=0
  181.       }
  182. }
  183. //=================================================================
  184. //        Display Line on LCD at desired location Function
  185. //=================================================================
  186. void LCDPrint(char Line,char string[40])//--------------------------------------
  187. {
  188. int len,count;
  189. if(Line==0) {
  190.    LCDSendByte(0x80,0);    //Command RS=0 Move Cursor to Home   
  191. }
  192. else {
  193.    LCDSendByte(0xC0,0);    //Command RS=0 Move Cursor to Second Line LCDSendByte(0xC0,0);
  194. }
  195.         len = strlen(string);
  196.     for (count=0;count<len;count++)
  197.   {
  198.      LCDSendByte(string[count],1);  //Data RS=1
  199.   }
  200. }
  201. //=================================================================
  202. void LCDclean(void)
  203. {
  204.   LCDSendByte(0x01,0);
  205. }
  206. void LCDCurserMove(int y,int x)
  207. {
  208. // LCDSendByte((a*4+16),0);//a=1 shift right
  209. if (y == 0)
  210.        {   
  211.        LCDSendByte(0x80 + x,0);
  212.        }   
  213. else
  214.        {   
  215.        LCDSendByte(0xC0 + x,0);   
  216.        }      
  217. LCDSendByte(0x0b,0);   
  218. }

  219. void LCDPrintString(int x,int y, char string[])
  220. {  
  221.   int count, len;  
  222. // summ = x;
  223. if (x == 0)
  224.        {  
  225.          LCDSendByte(0x80+y ,0);
  226.        }
  227. else
  228.        {
  229.        LCDSendByte(0xC0+y,0);  
  230.        }      
  231.      len = strlen(string);
  232.     for (count=0;count<len;count++)
  233.   {
  234.      LCDSendByte(string[count],1);  //Data RS=1
  235.   }
  236. }


  237. void LCDWriteChar(int x,int y,char Data)
  238. {

  239. if (x == 0)
  240.        {   
  241.        LCDSendByte(0x80 + y,0);
  242.        }   
  243. else
  244.        {   
  245.        LCDSendByte(0xC0 + y,0);   
  246.        }      
  247. LCDSendByte(Data,1);   
  248. }

  249. void LCDhome(void)
  250. {
  251.   LCDSendByte(0x02,0);
  252. }

  253. void LCDDisp(void)
  254. {
  255.    LCDSendByte(0x0f,0);
  256. }

  257. void EVN_1_ISR()//interruput 0
  258. {
  259. int0Flag = true;  
  260. }

  261. void EVN_2_ISR()//interrupt 1
  262. {
  263. int1Flag = true;  
  264. }

  265. void readSW(void)
  266. {
  267. //const int ReadKey1 =4;//^ key
  268. //const int ReadKey2 =5;//v key
  269.   if((!digitalRead(ReadKey1))&&(digitalRead(ReadKey2))) //key 1 pressed++
  270.   {
  271.     delay(200);
  272.   if((!digitalRead(ReadKey1))&&(digitalRead(ReadKey2)))   
  273. //    if(!digitalRead(ReadKey1))
  274.     {
  275.       if(dspDatacount >19){dspDatacount=0;}
  276.       dspDatacount++;//display record
  277.       
  278.       Serial.print("dspDatacount = ");
  279.       Serial.println(dspDatacount);
  280.        dspupdate_flag = true;  
  281.        readData2Dsply(dspDatacount);
  282.        LCDWriteChar(0,23,'R');
  283.        LCDWriteChar(1,23,'R');
  284.     }
  285.   }
  286.   if((digitalRead(ReadKey1))&&(!digitalRead(ReadKey2))) //key 2 pressed--
  287.   {
  288.     delay(100);
  289.    if((digitalRead(ReadKey1))&&(!digitalRead(ReadKey2)))   
  290.    // if(!digitalRead(ReadKey2))
  291.     {     
  292.       if(dspDatacount !=0){dspDatacount--;}
  293.    //   Serial.print("dspDatacount = ");
  294.    //   Serial.println(dspDatacount);  
  295.        dspupdate_flag = true;
  296.        readData2Dsply(dspDatacount);  
  297.         
  298.     }
  299.   }
  300.    if((!digitalRead(ReadKey1))&&(!digitalRead(ReadKey2))) //key 1 & 2 pressed
  301.   {
  302.     delay(500);
  303.     if((!digitalRead(ReadKey1))&&(!digitalRead(ReadKey2)))
  304.     {     

  305.         for(byte i=0;i<10;i++)
  306.         {  
  307.          time_bf0[i]="";   
  308.          time_bf1[i]="";
  309.         }      
  310.         evn0_count = 0;
  311.         evn1_count = 0;
  312.         dspDatacount = 0;
  313. //      Serial.print("dspDatacount = ");
  314. //      Serial.println(dspDatacount);  
  315.       dspupdate_flag = true;
  316.       
  317.     }
  318.   }
  319.   
  320. }

  321. void dspData(void)//display readKey
  322. {
  323. //     Serial.println("LCD display *** ");
  324. //      Serial.print("evn0 display = ");
  325. //     Serial.println(evn0_count);  
  326. //     Serial.print("evn1 display = ");
  327. //     Serial.println(evn1_count);  
  328.        dspupdate_flag = false;  
  329. }

  330. void readData2Dsply(byte rcount)
  331. {
  332.   LCDWriteChar(0,0,'A');
  333.   LCDWriteChar(1,0,'B');

  334.   if(rcount<10)
  335.   {
  336. //   Serial.print("Read event count = ");
  337. //   Serial.println(rcount);
  338. //  Serial.println("Read event date&time = ");
  339. //  Serial.print(date_bf0[rcount]);
  340. //  Serial.print(" ");
  341. //  Serial.println(time_bf0[rcount]);
  342.   String rcount_str = String(rcount);
  343.   rcount_str.toCharArray(EVN_Array,3);     
  344.   LCDPrintString(0,1,EVN_Array);
  345.    date_bf0[rcount].toCharArray(D,11);
  346.    LCDPrintString(0,3,D);
  347.    time_bf0[rcount].toCharArray(D,10);
  348.    LCDPrintString(0,14,D);
  349.   }
  350.   if(rcount ==10)
  351.   {
  352.     LCDWriteChar(0,2,' ');
  353.     LCDWriteChar(0,22,' ');
  354.   }
  355.   if((rcount >10) && (rcount<=19))
  356.   {
  357. //  Serial.print("Read event count = ");
  358. //  Serial.println(rcount);
  359.    rcount=rcount-10;
  360. //  Serial.println("Read event date&time = ");
  361. //  Serial.print(date_bf1[rcount]);
  362. //  Serial.print(" ** ");
  363. //  Serial.println(time_bf1[rcount]);
  364.   String rcount_str = String(rcount);
  365.   rcount_str.toCharArray(EVN_Array,3);     
  366.   LCDPrintString(1,1,EVN_Array);  
  367.    date_bf1[rcount].toCharArray(D,11);
  368.    LCDPrintString(1,3,D);
  369.    time_bf1[rcount].toCharArray(D,10);
  370.    LCDPrintString(1,14,D);  
  371.   }
  372. }
複製代碼



第二台的程式
說明: 使用 I2C 介面的 DS1307 RTC 和 LCM2004  和 ARDUINO NANO  
按鍵一開始/停止紀錄SD ,第二按鍵 選擇時間間隔 5秒...10秒..

  1. /* FILE NAME datalog20200127eventV2
  2. *  20200201 增加了選擇內部計時的功能 兩個按鍵 按鍵一 開始與停止 按鍵二 內部時間間隔選擇
  3. 5秒 10秒 30秒 60秒 300秒 1800秒 選擇之後 按下開始 就開始計時與紀錄.

  4. 起始動作 顯示時間而以,螢幕顯示 STOP ,此時並沒有啟動開檔動作(SD CARD) 內部自定 60秒,
  5. 一但按下START/STOP 按鍵時開始開檔啟動紀錄,這時候就不在建立新檔案,若再按一次 START/STOP 鍵
  6. 只會做停止計時動作與檔案 SD.FLASH動作,在  任何狀況下可以選擇時間間隔.
  7. */

  8. #include <SPI.h>
  9. #include <SD.h>
  10. #include <Wire.h>
  11. #include "RTClib.h"
  12. #include <LiquidCrystal_I2C.h> //20200127
  13. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

  14. #define ECHO_TO_SERIAL   1 // echo data to serial port
  15. #define WAIT_TO_START    0 // Wait for serial input in setup()


  16. // The analog pins that connect to the sensors
  17. #define whitePortPin 0           // analog 0 40V white port adjustable 5 10 40V range
  18. #define redPortPin 1                // analog 1 5V red port fix 5V range
  19. #define yellowPortPin 2          // analog 2 20V yellow port fix 20V range 2017.01.01
  20. #define BANDGAPREF 14            // special indicator that we want to measure the bandgap

  21. #define aref_voltage 5.0         // we tie 3.3V to ARef and measure it with a multimeter!2017.01.01 change from 3.3 to 5.0V
  22. #define bandgap_voltage 1.1      // this is not super guaranteed but its not -too- off

  23. RTC_DS1307 RTC; // define the Real Time Clock object
  24. const int chipSelect = 10;
  25. // the logging file
  26. File logfile;

  27. void error(char *str)
  28. {
  29.   Serial.print(F("error: "));
  30.   Serial.println(str);
  31.   lcd.setCursor(1,0);
  32.   lcd.print("err ");
  33.   lcd.print(str);
  34.   while(1);
  35. }
  36. //------------20200129-----------------
  37. byte count = 0;
  38. volatile boolean int0Flag = 0;
  39. volatile boolean int1Flag = 0;
  40. volatile byte evn0_count = 0;
  41. volatile byte evn1_count = 0;
  42. const byte EVN_1=2;//interrupt 0
  43. const byte EVN_2=3;//interrupt 1
  44. const byte ReadKey1 =4;// START/STOP key
  45. const byte ReadKey2 =5;// time interval select defalt 60s ,5,10,30,60,300,1800.
  46. boolean Start_Stop_flag = false;// true is Start,false is Stop
  47. const int Time_period[6] ={60,5,10,30,300,1800};
  48. byte Time_periord_count = 0;
  49. long startTime = 0;//time delay counter 1000ms
  50. int down_count_sec =0;
  51. int time_record_count=0;
  52. boolean onesec = false;
  53. boolean delayFlag =false;
  54. boolean dsp_flag = false;
  55. //------------end--------------
  56. void setup(void)
  57. {
  58.   Serial.begin(9600);
  59.   Serial.println();
  60.   pinMode(EVN_1,INPUT ); // INPUT_PULLUP
  61.   pinMode(EVN_2, INPUT); //   
  62.   pinMode(ReadKey1, INPUT_PULLUP); // 開始/停止(存檔SD)
  63.   pinMode(ReadKey2, INPUT_PULLUP); // 選擇間隔秒數
  64.   attachInterrupt(0,EVN_1_ISR,RISING); // set an interrupt on PinA
  65.   attachInterrupt(1,EVN_2_ISR,RISING); // set an interrupt on PinB
  66.   lcd.begin (20,4);  // initialize the lcd
  67.   lcd.backlight();//To Power ON the back light

  68.   //--------20200201--------------------
  69.   //initial  一開始先進入停止模式一但啟動再做SD卡存取
  70.   while( !Start_Stop_flag)
  71.   {
  72.      if(Start_Stop_flag == false)
  73.       {
  74.       lcd.setCursor(1,2);
  75.       lcd.print("STOP ");
  76.       }
  77.    if((!digitalRead(ReadKey1))&&(digitalRead(ReadKey2))) //key 1 pressed++
  78.   {
  79.     delay(200);
  80.   if((!digitalRead(ReadKey1))&&(digitalRead(ReadKey2)))   

  81.     {
  82.       Start_Stop_flag = (!Start_Stop_flag);
  83.       if(Start_Stop_flag == true)
  84.       {
  85.         lcd.setCursor(1,2);
  86.         lcd.print("START");
  87.         down_count_sec = Time_period[Time_periord_count];//start count
  88.       }
  89.       if(Start_Stop_flag == false)
  90.       {
  91.         //logfile.flush();
  92.       }
  93.     }
  94.   }
  95.      if((digitalRead(ReadKey1))&&(!digitalRead(ReadKey2))) //key 2 pressed--
  96.   {
  97.     delay(100);
  98.    if((digitalRead(ReadKey1))&&(!digitalRead(ReadKey2)))   
  99.     {  
  100.        Time_periord_count++;         
  101.       if(Time_periord_count >5){Time_periord_count =0;}
  102.      lcd.setCursor(2,3);
  103.      lcd.print("Time period:      ");
  104.      lcd.setCursor(14,3);
  105.      lcd.print(Time_period[Time_periord_count]);
  106.      Serial.print("Time period: ");
  107.      Serial.println(Time_period[Time_periord_count]);
  108.     }
  109.   }
  110.   }
  111.   //-----------20200201  END------------
  112. #if WAIT_TO_START
  113.   Serial.println("Type any character to start");
  114.   while (!Serial.available());
  115. #endif //WAIT_TO_START

  116.   // initialize the SD card
  117.   Serial.print(F("Initializing SD card..."));
  118.   lcd.setCursor(1,0);
  119.   lcd.print("init SD ..");
  120.   // make sure that the default chip select pin is set to
  121.   // output, even if you don't use it:
  122.   pinMode(chipSelect, OUTPUT);
  123.   
  124.   // see if the card is present and can be initialized:
  125.   if (!SD.begin(chipSelect)) {
  126.     error("NO Card or NG");
  127.   }
  128.   Serial.println(F("card initialized."));
  129.   
  130.   // create a new file
  131.   char filename[] = "Antlog00.CSV";
  132.   for (uint8_t i = 0; i < 100; i++) {
  133.     filename[6] = i/10 + '0';
  134.     filename[7] = i%10 + '0';
  135.     if (! SD.exists(filename)) {
  136.       // only open a new file if it doesn't exist
  137.       logfile = SD.open(filename, FILE_WRITE);
  138.       break;  // leave the loop!
  139.     }
  140.   }
  141.   
  142.   if (! logfile) {
  143.       error("couldnt create file");
  144.      }
  145.   
  146.   Serial.print(F("Logging to: "));
  147.   Serial.println(filename);

  148.   // connect to RTC
  149.   Wire.begin();  
  150.   if (!RTC.begin()) {
  151.     logfile.println("RTC failed");
  152. #if ECHO_TO_SERIAL
  153.     Serial.println("RTC failed");
  154. #endif  //ECHO_TO_SERIAL
  155.   }
  156.   

  157.   logfile.println("millis,stamp,datetime,whitePort,redPort,yellowPort,countNo.");   
  158. #if ECHO_TO_SERIAL
  159.   Serial.println("millis,stamp,datetime,whitePort,redPort,yellowPort,countNo.");
  160. #endif //ECHO_TO_SERIAL

  161.   // If you want to set the aref to something other than 5v
  162. //  analogReference(EXTERNAL);
  163. analogReference(DEFAULT);
  164.   if (! RTC.isrunning()) {
  165.   RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
  166. }
  167. }


  168. void loop(void)
  169. {
  170.   while(1){
  171.   DateTime now;

  172.   readSW();//read START/STOP key and TIME_period select   

  173.    if(int0Flag == true)
  174.      {
  175.       lcd.clear();
  176.         if (int0Flag ==true){ evn0_count ++; }   
  177.         Serial.print(F("evn0_count ="));
  178.         Serial.println(evn0_count);
  179.         now = RTC.now();
  180.         lcd.setCursor(0, 2);
  181.         lcd.print("A");
  182.         lcd.print(now.day());
  183.         lcd.print('/');
  184.         lcd.print(now.month());
  185.         lcd.print('/');
  186.         lcd.print(now.year());
  187.         lcd.setCursor(10, 2);//position,line
  188.         lcd.print(" ");
  189.         lcd.print(now.hour());
  190.         lcd.print(':');
  191.         lcd.print(now.minute());
  192.         lcd.print(':');
  193.         lcd.print(now.second());
  194.         lcd.print(" ");            
  195.         lcd.setCursor(1,3);
  196.         lcd.print("A ");
  197.         lcd.print(evn0_count);
  198.         lcd.setCursor(10,3);
  199.         lcd.print("B ");
  200.         lcd.print(evn1_count);  

  201.         uint32_t m = millis();
  202.         logfile.print(m);           // milliseconds since start
  203.         logfile.print(", ");   
  204. #if ECHO_TO_SERIAL
  205.   Serial.print(m);         // milliseconds since start
  206.   Serial.print(", ");  
  207. #endif

  208.         now = RTC.now();
  209.         logfile.print(now.unixtime()); // seconds since 1/1/1970
  210.         logfile.print(", ");
  211.         logfile.print('"');
  212.         logfile.print(now.year(), DEC);
  213.         logfile.print("/");
  214.         logfile.print(now.month(), DEC);
  215.         logfile.print("/");
  216.         logfile.print(now.day(), DEC);
  217.         logfile.print(" ");
  218.         logfile.print(now.hour(), DEC);
  219.         logfile.print(":");
  220.         logfile.print(now.minute(), DEC);
  221.         logfile.print(":");
  222.         logfile.print(now.second(), DEC);
  223.         logfile.print('"');
  224. #if ECHO_TO_SERIAL
  225.   Serial.print(now.unixtime()); // seconds since 1/1/1970
  226.   Serial.print(", ");
  227.   Serial.print('"');
  228.   Serial.print(now.year(), DEC);
  229.   Serial.print("/");
  230.   Serial.print(now.month(), DEC);
  231.   Serial.print("/");
  232.   Serial.print(now.day(), DEC);
  233.   Serial.print(" ");
  234.   Serial.print(now.hour(), DEC);
  235.   Serial.print(":");
  236.   Serial.print(now.minute(), DEC);
  237.   Serial.print(":");
  238.   Serial.print(now.second(), DEC);
  239.   Serial.print('"');
  240. #endif //ECHO_TO_SERIAL

  241.         analogRead(whitePortPin);
  242.         delay(10);
  243.         int whitePortReading = analogRead(whitePortPin);
  244.         analogRead(redPortPin);
  245.         delay(10);
  246.         int redPortReading = analogRead(redPortPin);   
  247.         analogRead(yellowPortPin); //2017
  248.         delay(10);                 //2017
  249.         int yellowPortReading = analogRead(yellowPortPin);  //2017

  250.         float whiteVoltage = whitePortReading * aref_voltage * 8 / 1024;//2017
  251.         float redVoltage = redPortReading * aref_voltage / 1024;//2017
  252.         float yellowVoltage = yellowPortReading * aref_voltage * 4 / 1024;//2017
  253.         logfile.print(", ");   
  254.         logfile.print(whiteVoltage);//2017
  255.         logfile.print(", ");   
  256.         logfile.print(redVoltage);//2017
  257.         logfile.print(", ");   
  258.         logfile.print(yellowVoltage);//2017
  259.   
  260. #if ECHO_TO_SERIAL
  261.   Serial.print(", ");   
  262.   Serial.print(whiteVoltage);//2017
  263.   Serial.print(", ");   
  264.   Serial.print(redVoltage);//2017
  265.   Serial.print(", ");   
  266.   Serial.print(yellowVoltage);//2017
  267. #endif //ECHO_TO_SERIAL

  268.   // Log the estimated 'VCC' voltage by measuring the internal 1.1v ref
  269.         analogRead(BANDGAPREF);
  270.         delay(10);
  271.         int refReading = analogRead(BANDGAPREF);
  272.         float supplyvoltage = (bandgap_voltage * 1024) / refReading;
  273.   
  274.         logfile.print(", ");
  275.   if(int0Flag ==true)
  276.   {
  277.     logfile.print(evn0_count);
  278.     logfile.print(",A ");  
  279.     int0Flag = false;
  280.   }

  281. #if ECHO_TO_SERIAL
  282.   Serial.print(", ");   
  283.   Serial.print(evn0_count);
  284. #endif // ECHO_TO_SERIAL
  285.     logfile.println();
  286. #if ECHO_TO_SERIAL
  287.   Serial.println();
  288. #endif // ECHO_TO_SERIAL

  289.   count++;
  290.   }
  291. //--------------interrupt 1----------------------
  292.   
  293. // int0 or int1 when interrupt triggered then evn0_count & evn1_count ++ 5count save file
  294.   if(int1Flag == true)
  295.      {
  296.       lcd.clear();
  297.         if (int1Flag ==true){ evn1_count ++; }   

  298.         Serial.print(F("evn1_count ="));
  299.         Serial.println(evn1_count);
  300.         now = RTC.now();
  301.         lcd.setCursor(0, 2);
  302.         lcd.print("B");
  303.         lcd.print(now.day());
  304.         lcd.print('/');
  305.         lcd.print(now.month());
  306.         lcd.print('/');
  307.         lcd.print(now.year());
  308.         lcd.setCursor(10, 2);//position,line
  309.         lcd.print(" ");
  310.         lcd.print(now.hour());
  311.         lcd.print(':');
  312.         lcd.print(now.minute());
  313.         lcd.print(':');
  314.         lcd.print(now.second());
  315.         lcd.print(" ");
  316.       
  317.         lcd.setCursor(1,3);
  318.         lcd.print("A ");
  319.         lcd.print(evn0_count);
  320.         lcd.setCursor(10,3);
  321.         lcd.print("B ");
  322.         lcd.print(evn1_count);  
  323.    // log milliseconds since starting
  324.         uint32_t m = millis();
  325.         logfile.print(m);           // milliseconds since start
  326.         logfile.print(", ");   
  327. #if ECHO_TO_SERIAL
  328.   Serial.print(m);         // milliseconds since start
  329.   Serial.print(", ");  
  330. #endif

  331.   // fetch the time
  332.         now = RTC.now();
  333.         logfile.print(now.unixtime()); // seconds since 1/1/1970
  334.         logfile.print(", ");
  335.         logfile.print('"');
  336.         logfile.print(now.year(), DEC);
  337.         logfile.print("/");
  338.         logfile.print(now.month(), DEC);
  339.         logfile.print("/");
  340.         logfile.print(now.day(), DEC);
  341.         logfile.print(" ");
  342.         logfile.print(now.hour(), DEC);
  343.         logfile.print(":");
  344.         logfile.print(now.minute(), DEC);
  345.         logfile.print(":");
  346.         logfile.print(now.second(), DEC);
  347.         logfile.print('"');
  348. #if ECHO_TO_SERIAL
  349.   Serial.print(now.unixtime()); // seconds since 1/1/1970
  350.   Serial.print(", ");
  351.   Serial.print('"');
  352.   Serial.print(now.year(), DEC);
  353.   Serial.print("/");
  354.   Serial.print(now.month(), DEC);
  355.   Serial.print("/");
  356.   Serial.print(now.day(), DEC);
  357.   Serial.print(" ");
  358.   Serial.print(now.hour(), DEC);
  359.   Serial.print(":");
  360.   Serial.print(now.minute(), DEC);
  361.   Serial.print(":");
  362.   Serial.print(now.second(), DEC);
  363.   Serial.print('"');
  364. #endif //ECHO_TO_SERIAL

  365.         analogRead(whitePortPin);
  366.         delay(10);
  367.         int whitePortReading = analogRead(whitePortPin);
  368.         analogRead(redPortPin);
  369.         delay(10);
  370.         int redPortReading = analogRead(redPortPin);   
  371.         analogRead(yellowPortPin); //2017
  372.         delay(10);                 //2017
  373.         int yellowPortReading = analogRead(yellowPortPin);  //2017
  374.   
  375. // converting that reading to voltage, for 3.3v arduino use 3.3, for 5.0, use 5.0
  376. // float voltage = redPortReading * aref_voltage / 1024;  
  377. // float temperatureC = (voltage - 0.5) * 100 ;
  378. // float temperatureF = (temperatureC * 9 / 5) + 32;
  379.         float whiteVoltage = whitePortReading * aref_voltage * 8 / 1024;//2017
  380.         float redVoltage = redPortReading * aref_voltage / 1024;//2017
  381.         float yellowVoltage = yellowPortReading * aref_voltage * 4 / 1024;//2017
  382.         logfile.print(", ");   
  383.         logfile.print(whiteVoltage);//2017
  384.         logfile.print(", ");   
  385.         logfile.print(redVoltage);//2017
  386.         logfile.print(", ");   
  387.         logfile.print(yellowVoltage);//2017
  388.   
  389. #if ECHO_TO_SERIAL
  390.   Serial.print(", ");   
  391.   Serial.print(whiteVoltage);//2017
  392.   Serial.print(", ");   
  393.   Serial.print(redVoltage);//2017
  394.   Serial.print(", ");   
  395.   Serial.print(yellowVoltage);//2017
  396. #endif //ECHO_TO_SERIAL

  397.   // Log the estimated 'VCC' voltage by measuring the internal 1.1v ref
  398.         analogRead(BANDGAPREF);
  399.         delay(10);
  400.         int refReading = analogRead(BANDGAPREF);
  401.         float supplyvoltage = (bandgap_voltage * 1024) / refReading;   
  402.         logfile.print(", ");

  403.    if(int1Flag ==true)
  404.     {
  405.      logfile.print(evn1_count);
  406.      logfile.print(",B ");  
  407.      int1Flag = false;
  408.     }
  409. #if ECHO_TO_SERIAL
  410.   Serial.print(", ");   
  411.   Serial.print(evn0_count);
  412.   
  413. #endif // ECHO_TO_SERIAL
  414.     logfile.println();
  415. #if ECHO_TO_SERIAL
  416.   Serial.println();
  417. #endif // ECHO_TO_SERIAL

  418.   count++;
  419.   }
  420. //--------------end interrupt1----------------------------
  421. //----20200201--time delay -1s -----------
  422. if(Start_Stop_flag == true)
  423. {
  424.   if(millis()-startTime>1000)//1s 1 time
  425.    {
  426.     down_count_sec--;
  427.     onesec = true;
  428.     startTime = millis();
  429.    }
  430. // Serial.println("START status");
  431. if(down_count_sec == 0)
  432. {
  433.           uint32_t m = millis();
  434.         logfile.print(m);           // milliseconds since start
  435.         logfile.print(", ");  
  436.            now = RTC.now();
  437.         logfile.print(now.unixtime()); // seconds since 1/1/1970
  438.         logfile.print(", ");
  439.         logfile.print('"');
  440.         logfile.print(now.year(), DEC);
  441.         logfile.print("/");
  442.         logfile.print(now.month(), DEC);
  443.         logfile.print("/");
  444.         logfile.print(now.day(), DEC);
  445.         logfile.print(" ");
  446.         logfile.print(now.hour(), DEC);
  447.         logfile.print(":");
  448.         logfile.print(now.minute(), DEC);
  449.         logfile.print(":");
  450.         logfile.print(now.second(), DEC);
  451.         logfile.print('"');
  452. #if ECHO_TO_SERIAL
  453.   Serial.print(now.unixtime()); // seconds since 1/1/1970
  454.   Serial.print(", ");
  455.   Serial.print('"');
  456.   Serial.print(now.year(), DEC);
  457.   Serial.print("/");
  458.   Serial.print(now.month(), DEC);
  459.   Serial.print("/");
  460.   Serial.print(now.day(), DEC);
  461.   Serial.print(" ");
  462.   Serial.print(now.hour(), DEC);
  463.   Serial.print(":");
  464.   Serial.print(now.minute(), DEC);
  465.   Serial.print(":");
  466.   Serial.print(now.second(), DEC);
  467.   Serial.print('"');
  468. #endif //ECHO_TO_SERIAL

  469.         analogRead(whitePortPin);
  470.         delay(10);
  471.         int whitePortReading = analogRead(whitePortPin);
  472. //          Serial.print (F("whitePortPin "));
  473. //            Serial.println(whitePortPin);
  474. //         Serial.print (F("whitePortReading "));
  475.         
  476. //        Serial.println(whitePortReading);
  477.         analogRead(redPortPin);
  478.         delay(10);
  479.         int redPortReading = analogRead(redPortPin);   
  480.         analogRead(yellowPortPin); //2017
  481.         delay(10);                 //2017
  482.         int yellowPortReading = analogRead(yellowPortPin);  //2017

  483.         float whiteVoltage = whitePortReading * aref_voltage * 8 / 1024;//2017
  484.         float redVoltage = redPortReading * aref_voltage / 1024;//2017
  485.         float yellowVoltage = yellowPortReading * aref_voltage * 4 / 1024;//2017
  486.         logfile.print(", ");   
  487.         logfile.print(whiteVoltage);//2017
  488.         logfile.print(", ");   
  489.         logfile.print(redVoltage);//2017
  490.         logfile.print(", ");   
  491.         logfile.print(yellowVoltage);//2017
  492.         logfile.print(", ");
  493.         logfile.print(time_record_count);
  494.         logfile.print(",T ");  
  495.         logfile.println();

  496.   
  497.     logfile.flush(); //????? 是否需要?
  498. #if ECHO_TO_SERIAL
  499.   Serial.print(", ");   
  500.   Serial.print(whiteVoltage);//2017
  501.   Serial.print(", ");   
  502.   Serial.print(redVoltage);//2017
  503.   Serial.print(", ");   
  504.   Serial.print(yellowVoltage);//2017
  505. #endif //ECHO_TO_SERIAL
  506.     down_count_sec= Time_period[Time_periord_count];//reset timer counter
  507.     Serial.println("down count complete status");
  508.     time_record_count++;
  509.     lcd.setCursor(16,1);
  510.     lcd.print(time_record_count);
  511. }
  512. }

  513. //-----20200201   end ------------
  514.   
  515.   if(count ==5)//per 5 time save 1 time to SD card
  516.     {
  517.        logfile.flush();
  518.        count = 0;
  519.     }

  520. if(onesec ==true)//1S update once
  521. {
  522.   onesec = false;
  523.    now = RTC.now();
  524.     lcd.setCursor(0, 1);//position,line
  525.     lcd.print("TIME");
  526.     lcd.print(" ");
  527.     lcd.print(now.hour());
  528.     lcd.print(':');
  529.     lcd.print(now.minute());
  530.     lcd.print(':');
  531.     lcd.print(now.second());
  532.     lcd.print(" ");
  533.     lcd.setCursor(0, 0);
  534.     lcd.print("DATE");
  535.     lcd.print(" ");
  536.     lcd.print(now.day());
  537.     lcd.print('/');
  538.     lcd.print(now.month());
  539.     lcd.print('/');
  540.     lcd.print(now.year());
  541. }
  542. }
  543. }


  544. void EVN_1_ISR()//interruput 0
  545. {
  546. int0Flag = true;  
  547. }

  548. void EVN_2_ISR()//interrupt 1
  549. {
  550. int1Flag = true;  
  551. }

  552.   //--------20200201--------------------
  553. void readSW(void)
  554. {
  555.     if((!digitalRead(ReadKey1))&&(digitalRead(ReadKey2))) //start/stop key
  556.   {
  557.     delay(200);
  558.   if((!digitalRead(ReadKey1))&&(digitalRead(ReadKey2)))   

  559.     {
  560.      
  561.       Start_Stop_flag = (!Start_Stop_flag);
  562.       dsp_flag= true;
  563.     }
  564.   }
  565.      if((digitalRead(ReadKey1))&&(!digitalRead(ReadKey2))) //key 2 pressed--
  566.   {
  567.     delay(100);
  568.    if((digitalRead(ReadKey1))&&(!digitalRead(ReadKey2)))   
  569.     {  
  570.        Time_periord_count++;
  571.          
  572.       if(Time_periord_count >5){Time_periord_count =0;}

  573.      dsp_flag= true;
  574.      down_count_sec= Time_period[Time_periord_count];//reset timer counter
  575.      Serial.print(F("Time period: "));
  576.      Serial.println(Time_period[Time_periord_count]);   
  577.     }
  578.   }

  579. if(dsp_flag== true)
  580. {
  581.     lcd.clear();
  582.         lcd.setCursor(0,2);

  583.       if(Start_Stop_flag == false)
  584.         {
  585.          lcd.setCursor(1,2);         
  586.          lcd.print("STOP");
  587.         }

  588.       else
  589.        {      
  590.          lcd.print("START");
  591.         }   
  592.      lcd.setCursor(1,3);
  593.      lcd.print(" Time period:      ");
  594.      lcd.setCursor(14,3);
  595.      lcd.print(Time_period[Time_periord_count]);
  596.   dsp_flag = false;  
  597. }  
  598.   }

  599.   //-----------20200201  END------------
複製代碼
EV9.jpg

評分

12

查看全部評分

發表於 2020-2-7 04:32:21 | 顯示全部樓層
大師作品先收藏,再慢慢拜讀
 樓主| 發表於 2020-2-7 13:10:18 | 顯示全部樓層
spirit.twn 發表於 2020-2-7 04:32 AM
大師作品先收藏,再慢慢拜讀

您謬讚了!! 這只是新年期間的玩具而已!!
發表於 2020-2-7 16:39:39 | 顯示全部樓層
請教一下....

從硬體電路看起來
LCD 是4bits模式
程式用1bits
是否有什麼原因...
 樓主| 發表於 2020-2-7 19:02:02 | 顯示全部樓層
jason680 發表於 2020-2-7 04:39 PM
請教一下....

從硬體電路看起來

為了節省用腳 LCD 有兩個接法,第一個是使用 74HC595 把信號分成三個 DATA CLOCK LATCH 這是我以前寫的,手頭上有一堆 2402 總共腳位 DS1302 CLK DAT RST ,第二個是使用 I2C 的接法 SCLK 和 SDA 兩條線,因為 DS1307 RTC 也使用 I2C 介面,程式是別人寫的,使用 SD卡需要 SCK MISO MOSI CS 接腳,中斷 使用 INT0 INT1 按鍵兩隻腳. 雖然 ARDUINO 很方便,使用相同的MCU 但是不同的板子 UNO PROMINI NANO 接腳不同,PRO-MINI 沒有A4 A5 接線出來,所以無法做 I2C 連接, 程式下載需要外接 USB-RS232/TTL ...
發表於 2020-2-8 13:52:24 | 顯示全部樓層
antlu 發表於 2020-2-7 07:02 PM
為了節省用腳 LCD 有兩個接法,第一個是使用 74HC595 把信號分成三個 DATA CLOCK LATCH 這是我以前寫的,手 ...

看明白了,謝謝...
1. 是好幾塊板子
2. ARDUINO 只有畫出connector連接器
3. 控制LCM 中間經過74595
  serial input, parallel output 串入並出 可以節省腳位,要花時間移位...
4. RTC DS1302 走I2C界面
5. 兩個INT 有光耦合隔離
6. 其他
  按鍵(INC,DEC)跟LED 指示燈...
  SD卡再研究...
 樓主| 發表於 2020-2-8 18:32:33 | 顯示全部樓層
jason680 發表於 2020-2-8 01:52 PM
看明白了,謝謝...
1. 是好幾塊板子
2. ARDUINO 只有畫出connector連接器

RTC DS1302 不是走I2C  DS1307才是走I2C , 藍色的LCM 2004 用 I2C  .黃色的LCM 2402 用 74HC595 做的!!
您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

關閉

站長小叮嚀上一條 /1 下一條

禁閉室|手機版|連繫我們|痞酷網電子技術論壇

GMT+8, 2024-3-29 01:23 AM , Processed in 0.068636 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.