这里会显示出您选择的修订版和当前版本之间的差别。
后一修订版 | 前一修订版 | ||
m:ws:ws5:lcd [2015/12/05 14:04] admin 创建 |
m:ws:ws5:lcd [2015/12/06 19:47] (当前版本) admin |
||
---|---|---|---|
行 1: | 行 1: | ||
<code C++> | <code C++> | ||
+ | //版权声明什么的最无聊了,大家随便用,随便复制,随便改. | ||
+ | //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 | + | #define LCD_SCK 13 //LCD SPI时钟 |
- | #define LCD_SI 11 | + | #define LCD_SI 11 //LCD SPI数据端口 |
- | #define LCD_CS 10 | + | #define LCD_CS 10 //LCD SPI片选 |
- | #define LCD_CD 9 | + | #define LCD_CD 9 //LCD SPI命令/数据 |
- | #define LCD_RST 8 | + | #define LCD_RST 8 //LCD SPI复位 |
+ | |||
+ | //对arduino端口操作方式的重新定义 | ||
#define Set(x) digitalWrite(x,1) | #define Set(x) digitalWrite(x,1) | ||
#define Clr(x) digitalWrite(x,0) | #define Clr(x) digitalWrite(x,0) | ||
#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写入 | ||
void SPI_write(unsigned char dat) | void SPI_write(unsigned char dat) | ||
{ | { | ||
行 28: | 行 33: | ||
Set(LCD_SCK); | Set(LCD_SCK); | ||
} | } | ||
+ | Clr(LCD_SCK); | ||
} | } | ||
+ | |||
+ | //向LCD写入命令 | ||
void write_cmd(unsigned char dat) | void write_cmd(unsigned char dat) | ||
{ | { | ||
行 35: | 行 42: | ||
SPI_write(dat); | SPI_write(dat); | ||
} | } | ||
+ | |||
+ | //向LCD写入数据 | ||
void write_dat(unsigned char dat) | void write_dat(unsigned char dat) | ||
{ | { | ||
行 41: | 行 49: | ||
SPI_write(dat); | SPI_write(dat); | ||
} | } | ||
+ | |||
+ | //LCD初始化 | ||
void LCD_init(){ | void LCD_init(){ | ||
Clr(LCD_CS); | Clr(LCD_CS); | ||
行 60: | 行 69: | ||
Set(LCD_CS); | Set(LCD_CS); | ||
} | } | ||
+ | |||
+ | //LCD 光标地址设定 | ||
void LCD_add(unsigned page, unsigned column) | void LCD_add(unsigned page, unsigned column) | ||
{ | { | ||
行 67: | 行 77: | ||
write_cmd(column & 0x0f); | write_cmd(column & 0x0f); | ||
} | } | ||
+ | |||
+ | //LCD 屏幕内容清除 | ||
void Clr_LCD() | void Clr_LCD() | ||
{ | { | ||
行 84: | 行 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) | ||
{ | { | ||
行 100: | 行 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) | ||
{ | { | ||
行 110: | 行 123: | ||
} | } | ||
} | } | ||
+ | |||
+ | unsigned long os_time; | ||
+ | String inputString = ""; // a string to hold incoming data | ||
+ | boolean stringComplete = false; // whether the string is complete | ||
void setup() { | void setup() { | ||
行 120: | 行 137: | ||
Clr_LCD(); | Clr_LCD(); | ||
Serial.begin(9600); | Serial.begin(9600); | ||
+ | os_time = millis(); | ||
} | } | ||
unsigned hour=0, mi=0, sec=0; | unsigned hour=0, mi=0, sec=0; | ||
行 125: | 行 143: | ||
void loop() { | void loop() { | ||
// put your main code here, to run repeatedly: | // put your main code here, to run repeatedly: | ||
- | sprintf(dispstr, "%d:%d:%d",hour,mi,sec); | + | sprintf(dispstr, "%02d:%02d:%02d",hour,mi,sec); //这里的%02d是可以让时分秒补零,满足两位,显示出来更加好看 |
sec++; | sec++; | ||
if (sec >= 60) | if (sec >= 60) | ||
行 137: | 行 155: | ||
hour++; | hour++; | ||
} | } | ||
- | Dis_str(0,0,"------TIMER-----"); | + | Dis_str(1,4,"-----TIMER-----"); |
- | Dis_str(1,0, dispstr); | + | Dis_str(2,31, dispstr); |
- | delay(1000); | + | 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(); | ||
} | } | ||
+ | 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> |