**这是本文档旧的修订版!**
< C code>
include "M10PWM.h"
include "M10SevenSeg.h"
define LCD_CSR_CMD 0
define LCD_CSR_DAT 1
define LCD_CSR_RST 2
static void lcdwritecmd (uint8t data) reentrant
{
LCDCSR = LCDCSRCMD;
LCDDATA = data;
nopdelay (5);
while(LCDCSR == 0);
}
static void lcdwritedat (uint8t data) reentrant
{
LCDCSR = LCDCSRDAT;
LCDDATA = data;
nopdelay (5);
while(LCDCSR == 0);
}
static void lcdreset () reentrant
{
LCDCSR = LCDCSRRST;
delay(50);
LCDCSR = 0;
delay(50);
}
static void lcdinit() reentrant
{
lcdreset();
lcdwrite_cmd (0x11);
delay(50);
lcdwritecmd(0xB1);
lcdwritedat(0x05);
lcdwritedat(0x3C);
lcdwritedat(0x3C);
lcdwritecmd(0xB2);
lcdwritedat(0x05);
lcdwritedat(0x3C);
lcdwritedat(0x3C);
lcdwritecmd(0xB3);
lcdwritedat(0x05);
lcdwritedat(0x3C);
lcdwritedat(0x3C);
lcdwritedat(0x05);
lcdwritedat(0x3C);
lcdwritedat(0x3C);
lcdwritecmd(0xB4); Column inversion
lcdwritedat(0x03);
lcdwritecmd(0xC0);
lcdwritedat(0x28);
lcdwritedat(0x08);
lcdwritedat(0x04);
lcdwritecmd(0xC1);
lcdwritedat(0xC0);
lcdwritecmd(0xC2);
lcdwritedat(0x0D);
lcdwritedat(0x00);
lcdwritecmd(0xC3);
lcdwritedat(0x8D);
lcdwritedat(0x2A);
lcdwritecmd(0xC4);
lcdwritedat(0x8D);
lcdwritedat(0xEE);
lcdwritecmd(0xC5); VCOM
lcdwritedat(0x1A);
lcdwritecmd(0x36); MX, MY, RGB mode
lcdwritedat(0xC0);
lcdwritecmd(0xe0);
lcdwritedat(0x04);
lcdwritedat(0x22);
lcdwritedat(0x07);
lcdwritedat(0x0a);
lcdwritedat(0x2e);
lcdwritedat(0x30);
lcdwritedat(0x25);
lcdwritedat(0x2a);
lcdwritedat(0x28);
lcdwritedat(0x26);
lcdwritedat(0x2e);
lcdwritedat(0x3a);
lcdwritedat(0x00);
lcdwritedat(0x01);
lcdwritedat(0x03);
lcdwritedat(0x13);
lcdwritecmd(0xe1);
lcdwritedat(0x04);
lcdwritedat(0x16);
lcdwritedat(0x06);
lcdwritedat(0x0d);
lcdwritedat(0x2d);
lcdwritedat(0x26);
lcdwritedat(0x23);
lcdwritedat(0x27);
lcdwritedat(0x27);
lcdwritedat(0x25);
lcdwritedat(0x2d);
lcdwritedat(0x3b);
lcdwritedat(0x00);
lcdwritedat(0x01);
lcdwritedat(0x04);
lcdwritedat(0x13);
lcdwritecmd(0x3a);
lcdwritedat(0x06);
lcdwritecmd(0x29);
}
void lcdSetRegion(uint8t xstart,uint8t ystart,uint8t xend,uint8t yend) reentrant
{
lcdwritecmd(0x2a);
lcdwritedat(0x0);
lcdwritedat(xstart);
lcdwritedat(0x0);
lcdwritedat(xend);
lcdwritecmd(0x2b);
lcdwritedat(0x0);
lcdwritedat(ystart);
lcdwritedat(0x0);
lcdwritedat(yend);
lcdwritecmd(0x2c);
}
void lcdwrite16bit(uint16t Data) reentrant
{
uint8_t t;
t = (Data»8) & 0xff;
lcdwritedat(t);
t = Data & 0xff;
lcdwritedat(t);
}
uint8t flashbyteread (uint32t addr)
{
uint8_t t;
t = (addr » 24) & 0xff;
FLASHREADDATA = t;
t = (addr » 16) & 0xff;
FLASHREADDATA = t;
t = (addr » 8) & 0xff;
FLASHREADDATA = t;
t = addr & 0xff;
FLASHREADDATA = t;
nopdelay (10);
FLASHREADCSR = 0;
nopdelay (10);
while(FLASHREADCSR == 0);
t = FLASHREADDATA;
return t;
}
static void singlecolor()
{
uint8t i,j,k,t;
const uint8t x = 128, y = 154;
const uint8t xtotal = 132, ytotal = 162;
uint32_t addr = 0x8000;
lcdSetRegion(0,0,xtotal-1,ytotal-1);
for (i = 0; i < xtotal; ++i) {
for (j = 0; j < ytotal; ++j) {
for (k = 0; k < 3; ++k) {
lcdwritedat(0xff);
}
}
}
lcdSetRegion1)
for (i=0;i<x;i++) { for (j=0;j<y;j++) { lcdwrite16bit(0x00ff); lcdwrite_dat (0);
for (k = 0; k < 3; ++k) { t = flash_byte_read (addr++); lcd_write_dat(t); // Serial.print (addr, HEX); // Serial.write (" "); // Serial.println(t, HEX); } } }} uint8t oldcount = 0; uint8t count = 0; uint32t big_count = 0; typedef struct {
void (*init) () __reentrant; uint8_t (*dataAvailable)() __reentrant; uint8_t (*read) () __reentrant;} PS2_STRUCT; extern const PS2_STRUCT PS2; —————————————————————————- ps2_init() Parameters: None
Return Value:
NoneRemarks: call this function to init the PS2 module —————————————————————————- static void ps2init() reentrant { PS2CSR = 0; PS2DATA = 0;
} End of ps2_init() —————————————————————————- ps2dataavailable() Parameters: None
Return Value: 1 when there is data in the FIFO 0 when FIFO is empty Remarks: call this function to see if there is data in the receiving FIFO —————————————————————————- static uint8t ps2dataavailable() reentrant {
if (PS2CSR) { return 1; } else { return 0; } } End of ps2dataavailable() static uint8t ps2read() reentrant { uint8t t;
t = PS2DATA; PS2_CSR = 0;
return t; } End of ps2_read() const PS2STRUCT PS2 = { .init = ps2init, .dataAvailable = ps2dataavailable, .read = ps2_read }; typedef struct { void (reset) () reentrant; void (writeCMD)(uint8t) reentrant; void (*writeDAT) (uint8t) __reentrant; } LCDSTRUCT; const LCDSTRUCT LCD = { .reset = lcdreset, .writeCMD = lcdwritecmd, .writeDAT = lcdwritedat }; void setup() { uint8_t t, i; Serial.begin (115200); SEVEN_SEG.init(); ROTARY_ENCODER = 0x0; P3_DIRECTION = 0xFF; PS2.init(); PWM.resolution (0, 938); lcdinit(); singlecolor();
PWM.dutyCycle (0, 255, 0); Serial.write (“Hello world!”); } void loop() { uint8t t;
Serial.println (big_count++); oldcount = count; count = ROTARYENCODER; SEVEN_SEG.byteHex (count); if (old_count != count) { PWM.dutyCycle (0, count, 255-count); } P3 = (1 « (count » 5)) - 1; if (PS2.dataAvailable()) { t = PS2.read();
if (t == 0xF0) { Serial.print(t, HEX); } else { Serial.println(t, HEX); } } } </code>