这是本文档旧的修订版!
#include <avr/pgmspace.h> #include "font.h" #include <stdio.h> #define LCD_SCK 13 #define LCD_SI 11 #define LCD_CS 10 #define LCD_CD 9 #define LCD_RST 8 #define Set(x) digitalWrite(x,1) #define Clr(x) digitalWrite(x,0) #define Out(x) pinMode(x, 1) #define In(x) pinMode(x, 0) void SPI_write(unsigned char dat) { for(unsigned char i = 0; i < 8; i++) { Clr(LCD_SCK); if (dat & 0x80) Set(LCD_SI); else Clr(LCD_SI); dat <<= 1; Set(LCD_SCK); } } void write_cmd(unsigned char dat) { Clr(LCD_CD); //Wrtie CMD SPI_write(dat); } void write_dat(unsigned char dat) { Set(LCD_CD); //Wrtie Data SPI_write(dat); } void LCD_init(){ Clr(LCD_CS); Clr(LCD_RST); delay(20); Set(LCD_RST); //Reset the LCD write_cmd(0xe2); /*软复位*/ delay(5); write_cmd(0x2c); /*升压步聚1*/ delay(5); write_cmd(0x2e); /*升压步聚2*/ delay(5); write_cmd(0x2f); /*升压步聚3*/ delay(5); write_cmd(0x23); /*粗调对比度,可设置范围0x20~0x27*/ write_cmd(0x81); /*微调对比度*/ write_cmd(0x2f); /*0x28,微调对比度的值,可设置范围0x00~0x3f*/ write_cmd(0xa2); /*1/9偏压比(bias)*/ write_cmd(0xc8); /*行扫描顺序:从上到下*/ write_cmd(0xa0); /*列扫描顺序:从左到右*/ write_cmd(0x40); /*起始行:第一行开始*/ write_cmd(0xaf); /*开显示*/ Set(LCD_CS); } void LCD_add(unsigned page, unsigned column) { write_cmd(0xb0 + page); write_cmd(0x10 + ((column>>4) & 0x0f)); write_cmd(column & 0x0f); } void Clr_LCD() { unsigned char i,j; Clr(LCD_CS); for(i=0;i<9;i++) { write_cmd(0xb0 + i); write_cmd(0x10); write_cmd(0x00); for(j=0;j<132;j++) { write_dat(0x00); } } Set(LCD_CS); } void Dis_char(unsigned char line, unsigned char column, char *chr) { unsigned char page = line * 2; Clr(LCD_CS); for (unsigned char i = 0; i < 2; i++) { LCD_add(page + i, column); for (unsigned j = 0; j < 8; j++) { int loc = (*chr - 32) * 16 + j + i * 8; write_dat(pgm_read_byte_near(ascii+loc)); } } Set(LCD_CS); } void Dis_str(unsigned char line, unsigned char column, char *str) { while(*str) { Dis_char(line, column, str); column += 8; str++; } } void setup() { // put your setup code here, to run once: Out(LCD_SCK); Out(LCD_SI); Out(LCD_CS); Out(LCD_CD); LCD_init(); Clr_LCD(); Serial.begin(9600); } unsigned hour=0, mi=0, sec=0; char dispstr[16] = ""; void loop() { // put your main code here, to run repeatedly: sprintf(dispstr, "%d:%d:%d",hour,mi,sec); sec++; if (sec >= 60) { sec = 0; mi++; } if (mi>=60) { mi = 0; hour++; } Dis_str(0,0,"------TIMER-----"); Dis_str(1,0, dispstr); delay(1000); }