极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9323|回复: 5

菜鸟请教 1602扩展控制时钟调整

[复制链接]
发表于 2013-5-17 21:43:13 | 显示全部楼层 |阅读模式
刚刚开始玩,1602+ds1302++lm35,依葫芦画瓢写了程序,目前能显示时间、温度,但是想通过扩展板的按键更改时间,应该如何实现。




  1. #include <LiquidCrystal.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <DS1302.h>

  5. LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);

  6. uint8_t CE_PIN   = 2;
  7. uint8_t IO_PIN   = 3;
  8. uint8_t SCLK_PIN = 11;


  9. char buf[50];
  10. char day[10];
  11. String comdata = "";
  12. int numdata[7] ={0}, j = 0, mark = 0;

  13. DS1302 rtc(CE_PIN, IO_PIN, SCLK_PIN);


  14. int potPin = 4;                    
  15. float temperature = 0;            
  16. long val=0;                    

  17. void print_time()
  18. {
  19.    
  20.     Time t = rtc.time();
  21.    
  22.     memset(day, 0, sizeof(day));
  23.     switch (t.day)
  24.     {
  25.     case 1: strcpy(day, "Sun"); break;
  26.     case 2: strcpy(day, "Mon"); break;
  27.     case 3: strcpy(day, "Tue"); break;
  28.     case 4: strcpy(day, "Wed"); break;
  29.     case 5: strcpy(day, "Thu"); break;
  30.     case 6: strcpy(day, "Fri"); break;
  31.     case 7: strcpy(day, "Sat"); break;
  32.     }
  33.    
  34.     snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d ", day, t.yr, t.mon, t.date);

  35.       lcd.clear();
  36.   lcd.begin(16, 2);
  37.   lcd.setCursor(0,0);
  38.   lcd.print(buf);
  39.   snprintf(buf, sizeof(buf), "%02d:%02d:%02d", t.hr, t.min, t.sec);
  40.    lcd.setCursor(0, 1);
  41.    lcd.print(buf);
  42. }

  43. void setup()
  44. {
  45.     Serial.begin(9600);
  46.     rtc.write_protect(false);
  47.     rtc.halt(false);
  48. }

  49. void loop()
  50. {

  51.    
  52.     while (Serial.available() > 0)
  53.     {
  54.         comdata += char(Serial.read());
  55.         delay(2);
  56.         mark = 1;
  57.     }
  58.    
  59.     if(mark == 1)
  60.     {
  61.         Serial.print("You inputed : ");
  62.         Serial.println(comdata);
  63.         for(int i = 0; i < comdata.length() ; i++)
  64.         {
  65.             if(comdata[i] == ',' || comdata[i] == 0x10 || comdata[i] == 0x13)
  66.             {
  67.                 j++;
  68.             }
  69.             else
  70.             {
  71.                 numdata[j] = numdata[j] * 10 + (comdata[i] - '0');
  72.             }
  73.         }
  74.       
  75.         Time t(numdata[0], numdata[1], numdata[2], numdata[3], numdata[4], numdata[5], numdata[6]);
  76.         rtc.time(t);
  77.         mark = 0;j=0;
  78.       
  79.         comdata = String("");
  80.       
  81.         for(int i = 0; i < 7 ; i++) numdata[i]=0;
  82.     }
  83.    
  84.    
  85.     print_time();
  86.         
  87. val = analogRead(potPin);            
  88. temperature = (val*0.0048828125*1000);         
  89. lcd.setCursor(10, 1) ;
  90. lcd.print((long)temperature / 10);   
  91. lcd.print(".");   
  92. lcd.print( (long)temperature % 10);  
  93. lcd.print((char)223);  
  94. lcd.print("C");  

  95. delay(1000);                    

  96. }
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

 楼主| 发表于 2013-5-17 21:45:03 | 显示全部楼层
  1. #include <LiquidCrystal.h>

  2. LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);

  3. char msgs[5][16] = {"Right Key OK ",
  4.                     "Up Key OK    ",               
  5.                     "Down Key OK  ",
  6.                     "Left Key OK  ",
  7.                     "Select Key OK" };

  8. int adc_key_val[5] ={50, 200, 400, 600, 800 };
  9. int NUM_KEYS = 5;
  10. int adc_key_in;
  11. int key=-1;
  12. int oldkey=-1;

  13. void setup()
  14. {
  15.   lcd.clear();
  16.   lcd.begin(16, 2);
  17.   lcd.setCursor(0,0);
  18.   lcd.print("ADC key testing");
  19. }

  20. void loop()
  21. {
  22.   adc_key_in = analogRead(0);    // read the value from the sensor
  23.   key = get_key(adc_key_in);  // convert into key press

  24.   if (key != oldkey)   // if keypress is detected
  25.    {
  26.     delay(50);  // wait for debounce time
  27.     adc_key_in = analogRead(0);    // read the value from the sensor
  28.     key = get_key(adc_key_in);    // convert into key press
  29.     if (key != oldkey)   
  30.     {   
  31.       lcd.setCursor(0, 1);
  32.       oldkey = key;
  33.       if (key >=0){
  34.            lcd.print(msgs[key]);              
  35.       }
  36.     }
  37.   }
  38. delay(100);
  39. }

  40. // Convert ADC value to key number
  41. int get_key(unsigned int input)
  42. {
  43.     int k;
  44.    
  45.     for (k = 0; k < NUM_KEYS; k++)
  46.     {
  47.       if (input < adc_key_val[k])
  48.       {
  49.             return k;
  50.         }
  51.    }
  52.    
  53.     if (k >= NUM_KEYS)k = -1;  // No valid key pressed
  54.     return k;
  55. }
复制代码
这是高手写的,我不懂……这么实现选择和输入
回复 支持 反对

使用道具 举报

发表于 2013-5-20 21:35:01 | 显示全部楼层
本帖最后由 nngh 于 2013-5-20 21:52 编辑
飞雪非血 发表于 2013-5-17 21:45
这是高手写的,我不懂……这么实现选择和输入


试试看如下思路:
1.加入按键部分电路初始化到程序开头
  1. int adc_key_val[5] ={50, 200, 400, 600, 800 };
  2. int NUM_KEYS = 5;
  3. int adc_key_in;
  4. int key=-1;
  5. int oldkey=-1;
复制代码
2.在loop主循环中加入按键检测代码
  1. adc_key_in = analogRead(0);    // read the value from the sensor
  2.   key = get_key(adc_key_in);  // convert into key press

  3.   if (key != oldkey)   // if keypress is detected
  4.    {
  5.     delay(50);  // wait for debounce time
  6.     adc_key_in = analogRead(0);    // read the value from the sensor
  7.     key = get_key(adc_key_in);    // convert into key press
  8.     if (key != oldkey)   
  9.     {   
  10.       lcd.setCursor(0, 1);
  11.       oldkey = key;
  12.             
  13.       }
  14.     }
复制代码
当然不能少了将按键的模拟电压转换为按键代码的函数部分
  1. // Convert ADC value to key number
  2. int get_key(unsigned int input)
  3. {
  4.     int k;
  5.    
  6.     for (k = 0; k < NUM_KEYS; k++)
  7.     {
  8.       if (input < adc_key_val[k])
  9.       {
  10.             return k;
  11.         }
  12.    }
  13.    
  14.     if (k >= NUM_KEYS)k = -1;  // No valid key pressed
  15.     return k;
  16. }
复制代码


3.开始处理按键跳转
设定 menu = key;
  1. switch(menu){
  2.              //show on  LCD the key pressed
  3.             case 4: //SELECT 4-key
  4.                 按选择键时,你的代码......;
  5.               break;            
  6.             case 3: //LEFT 3-key
  7.                 按左键时,你的代码......;
  8.                 break;
  9.             case 1: //UP   1-key
  10.               按上键时,你的代码......;
  11.               break;
  12.             case 2: //DOWN  2-key
  13.               按下键时,你的代码......;
  14.               break;
  15.              case 5: //RIGHT 5-key
  16.                按右键时,你的代码......;
  17.               break;
  18.            }
复制代码

那个时间模块我没用过,不懂怎么改时间,只能告诉你这么多了,我也是新手,共同努力吧。
另求高人指点!
回复 支持 反对

使用道具 举报

发表于 2013-5-21 11:54:03 | 显示全部楼层
同问,目前相关教程貌似很少。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-5-21 23:07:36 | 显示全部楼层
谢谢3楼,不过还是没实现,继续请教高手
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-5-26 18:36:06 | 显示全部楼层
继续请教,还是没搞定
回复 支持 反对

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-5-29 07:31 , Processed in 0.043598 second(s), 21 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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