这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录 前一修订版 | |||
m:ws:ws5:lcd [2015/12/05 19:53] admin |
m:ws:ws5:lcd [2015/12/06 19:47] (当前版本) admin |
||
---|---|---|---|
行 2: | 行 2: | ||
//版权声明什么的最无聊了,大家随便用,随便复制,随便改. | //版权声明什么的最无聊了,大家随便用,随便复制,随便改. | ||
//Copyright is nothing, feel free to use it in anyway. | //Copyright is nothing, feel free to use it in anyway. | ||
+ | //v151206 加入了从串口修改时间的功能 | ||
+ | |||
#include <avr/pgmspace.h> | #include <avr/pgmspace.h> | ||
#include "font.h" | #include "font.h" | ||
#include <stdio.h> | #include <stdio.h> | ||
+ | |||
#define LCD_SCK 13 //LCD SPI时钟 | #define LCD_SCK 13 //LCD SPI时钟 | ||
#define LCD_SI 11 //LCD SPI数据端口 | #define LCD_SI 11 //LCD SPI数据端口 | ||
行 12: | 行 13: | ||
#define LCD_CD 9 //LCD SPI命令/数据 | #define LCD_CD 9 //LCD SPI命令/数据 | ||
#define LCD_RST 8 //LCD SPI复位 | #define LCD_RST 8 //LCD SPI复位 | ||
+ | |||
//对arduino端口操作方式的重新定义 | //对arduino端口操作方式的重新定义 | ||
#define Set(x) digitalWrite(x,1) | #define Set(x) digitalWrite(x,1) | ||
行 18: | 行 19: | ||
#define Out(x) pinMode(x, 1) | #define Out(x) pinMode(x, 1) | ||
#define In(x) pinMode(x, 0) | #define In(x) pinMode(x, 0) | ||
+ | |||
//SPI写入 | //SPI写入 | ||
void SPI_write(unsigned char dat) | void SPI_write(unsigned char dat) | ||
行 32: | 行 33: | ||
Set(LCD_SCK); | Set(LCD_SCK); | ||
} | } | ||
+ | Clr(LCD_SCK); | ||
} | } | ||
+ | |||
//向LCD写入命令 | //向LCD写入命令 | ||
void write_cmd(unsigned char dat) | void write_cmd(unsigned char dat) | ||
行 40: | 行 42: | ||
SPI_write(dat); | SPI_write(dat); | ||
} | } | ||
+ | |||
//向LCD写入数据 | //向LCD写入数据 | ||
void write_dat(unsigned char dat) | void write_dat(unsigned char dat) | ||
行 47: | 行 49: | ||
SPI_write(dat); | SPI_write(dat); | ||
} | } | ||
+ | |||
//LCD初始化 | //LCD初始化 | ||
void LCD_init(){ | void LCD_init(){ | ||
行 67: | 行 69: | ||
Set(LCD_CS); | Set(LCD_CS); | ||
} | } | ||
+ | |||
//LCD 光标地址设定 | //LCD 光标地址设定 | ||
void LCD_add(unsigned page, unsigned column) | void LCD_add(unsigned page, unsigned column) | ||
行 75: | 行 77: | ||
write_cmd(column & 0x0f); | write_cmd(column & 0x0f); | ||
} | } | ||
+ | |||
//LCD 屏幕内容清除 | //LCD 屏幕内容清除 | ||
void Clr_LCD() | void Clr_LCD() | ||
行 93: | 行 95: | ||
Set(LCD_CS); | Set(LCD_CS); | ||
} | } | ||
+ | |||
//在某一个位置上显示一个字符 | //在某一个位置上显示一个字符 | ||
void Dis_char(unsigned char line, unsigned char column, char *chr) | void Dis_char(unsigned char line, unsigned char column, char *chr) | ||
行 110: | 行 112: | ||
Set(LCD_CS); | Set(LCD_CS); | ||
} | } | ||
+ | |||
//在某一个位置上显示一个字符串 | //在某一个位置上显示一个字符串 | ||
void Dis_str(unsigned char line, unsigned char column, char *str) | void Dis_str(unsigned char line, unsigned char column, char *str) | ||
行 121: | 行 123: | ||
} | } | ||
} | } | ||
+ | |||
+ | unsigned long os_time; | ||
+ | String inputString = ""; // a string to hold incoming data | ||
+ | boolean stringComplete = false; // whether the string is complete | ||
- | unsigned long os_time; | ||
void setup() { | void setup() { | ||
// put your setup code here, to run once: | // put your setup code here, to run once: | ||
行 152: | 行 157: | ||
Dis_str(1,4,"-----TIMER-----"); | Dis_str(1,4,"-----TIMER-----"); | ||
Dis_str(2,31, dispstr); | Dis_str(2,31, dispstr); | ||
- | while(millis() - os_time <= 1000); //这种计时方式比用delay准确 | + | while(millis() - os_time <= 1000) |
+ | { | ||
+ | if (stringComplete) { | ||
+ | Serial.println(inputString); | ||
+ | sscanf(inputString.c_str(), "h%dm%ds%d",&hour,&mi,&sec); //改时间的语句 | ||
+ | // clear the string: | ||
+ | inputString = ""; | ||
+ | stringComplete = false; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | //这种计时方式比用delay准确 | ||
os_time = millis(); | os_time = millis(); | ||
+ | } | ||
+ | |||
+ | void serialEvent() { | ||
+ | while (Serial.available()) { | ||
+ | // get the new byte: | ||
+ | char inChar = (char)Serial.read(); | ||
+ | // add it to the inputString: | ||
+ | inputString += inChar; | ||
+ | // if the incoming character is a newline, set a flag | ||
+ | // so the main loop can do something about it: | ||
+ | if (inChar == '\n') { | ||
+ | stringComplete = true; | ||
+ | } | ||
+ | } | ||
} | } | ||
</code> | </code> |