菜鸟请教 1602扩展控制时钟调整
刚刚开始玩,1602+ds1302++lm35,依葫芦画瓢写了程序,目前能显示时间、温度,但是想通过扩展板的按键更改时间,应该如何实现。#include <LiquidCrystal.h>
#include <stdio.h>
#include <string.h>
#include <DS1302.h>
LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);
uint8_t CE_PIN = 2;
uint8_t IO_PIN = 3;
uint8_t SCLK_PIN = 11;
char buf;
char day;
String comdata = "";
int numdata ={0}, j = 0, mark = 0;
DS1302 rtc(CE_PIN, IO_PIN, SCLK_PIN);
int potPin = 4;
float temperature = 0;
long val=0;
void print_time()
{
Time t = rtc.time();
memset(day, 0, sizeof(day));
switch (t.day)
{
case 1: strcpy(day, "Sun"); break;
case 2: strcpy(day, "Mon"); break;
case 3: strcpy(day, "Tue"); break;
case 4: strcpy(day, "Wed"); break;
case 5: strcpy(day, "Thu"); break;
case 6: strcpy(day, "Fri"); break;
case 7: strcpy(day, "Sat"); break;
}
snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d ", day, t.yr, t.mon, t.date);
lcd.clear();
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print(buf);
snprintf(buf, sizeof(buf), "%02d:%02d:%02d", t.hr, t.min, t.sec);
lcd.setCursor(0, 1);
lcd.print(buf);
}
void setup()
{
Serial.begin(9600);
rtc.write_protect(false);
rtc.halt(false);
}
void loop()
{
while (Serial.available() > 0)
{
comdata += char(Serial.read());
delay(2);
mark = 1;
}
if(mark == 1)
{
Serial.print("You inputed : ");
Serial.println(comdata);
for(int i = 0; i < comdata.length() ; i++)
{
if(comdata == ',' || comdata == 0x10 || comdata == 0x13)
{
j++;
}
else
{
numdata = numdata * 10 + (comdata - '0');
}
}
Time t(numdata, numdata, numdata, numdata, numdata, numdata, numdata);
rtc.time(t);
mark = 0;j=0;
comdata = String("");
for(int i = 0; i < 7 ; i++) numdata=0;
}
print_time();
val = analogRead(potPin);
temperature = (val*0.0048828125*1000);
lcd.setCursor(10, 1) ;
lcd.print((long)temperature / 10);
lcd.print(".");
lcd.print( (long)temperature % 10);
lcd.print((char)223);
lcd.print("C");
delay(1000);
} #include <LiquidCrystal.h>
LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);
char msgs = {"Right Key OK ",
"Up Key OK ",
"Down Key OK",
"Left Key OK",
"Select Key OK" };
int adc_key_val ={50, 200, 400, 600, 800 };
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;
void setup()
{
lcd.clear();
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print("ADC key testing");
}
void loop()
{
adc_key_in = analogRead(0); // read the value from the sensor
key = get_key(adc_key_in);// convert into key press
if (key != oldkey) // if keypress is detected
{
delay(50);// wait for debounce time
adc_key_in = analogRead(0); // read the value from the sensor
key = get_key(adc_key_in); // convert into key press
if (key != oldkey)
{
lcd.setCursor(0, 1);
oldkey = key;
if (key >=0){
lcd.print(msgs);
}
}
}
delay(100);
}
// Convert ADC value to key number
int get_key(unsigned int input)
{
int k;
for (k = 0; k < NUM_KEYS; k++)
{
if (input < adc_key_val)
{
return k;
}
}
if (k >= NUM_KEYS)k = -1;// No valid key pressed
return k;
}这是高手写的,我不懂……这么实现选择和输入 本帖最后由 nngh 于 2013-5-20 21:52 编辑
飞雪非血 发表于 2013-5-17 21:45 static/image/common/back.gif
这是高手写的,我不懂……这么实现选择和输入
试试看如下思路:
1.加入按键部分电路初始化到程序开头int adc_key_val ={50, 200, 400, 600, 800 };
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;2.在loop主循环中加入按键检测代码adc_key_in = analogRead(0); // read the value from the sensor
key = get_key(adc_key_in);// convert into key press
if (key != oldkey) // if keypress is detected
{
delay(50);// wait for debounce time
adc_key_in = analogRead(0); // read the value from the sensor
key = get_key(adc_key_in); // convert into key press
if (key != oldkey)
{
lcd.setCursor(0, 1);
oldkey = key;
}
}当然不能少了将按键的模拟电压转换为按键代码的函数部分
// Convert ADC value to key number
int get_key(unsigned int input)
{
int k;
for (k = 0; k < NUM_KEYS; k++)
{
if (input < adc_key_val)
{
return k;
}
}
if (k >= NUM_KEYS)k = -1;// No valid key pressed
return k;
}
3.开始处理按键跳转
设定 menu = key;
switch(menu){
//show onLCD the key pressed
case 4: //SELECT 4-key
按选择键时,你的代码......;
break;
case 3: //LEFT 3-key
按左键时,你的代码......;
break;
case 1: //UP 1-key
按上键时,你的代码......;
break;
case 2: //DOWN2-key
按下键时,你的代码......;
break;
case 5: //RIGHT 5-key
按右键时,你的代码......;
break;
}
那个时间模块我没用过,不懂怎么改时间,只能告诉你这么多了,我也是新手,共同努力吧。
另求高人指点!
同问,目前相关教程貌似很少。 谢谢3楼,不过还是没实现,继续请教高手 继续请教,还是没搞定
页:
[1]