一派護法 十九級 |
[MyC.nc] module MyC { uses interface Boot; uses interface Leds; uses interface Timer<TMilli> as Timer0; uses interface Receive; uses interface SplitControl; } implementation { #define _BV(n) (1 << (n)) #define RST_0 P2OUT &= ~_BV(0) #define RST_1 P2OUT |= _BV(0) #define CE_0 P2OUT &= ~_BV(1) #define CE_1 P2OUT |= _BV(1) #define DC_0 P2OUT &= ~_BV(3) #define DC_1 P2OUT |= _BV(3) #define DIN_0 P2OUT &= ~_BV(6) #define DIN_1 P2OUT |= _BV(6) #define BL_1 P6OUT |= _BV(6) // 接ADC6接线柱即可 #define SCLK_0 P6OUT &= ~_BV(7) // 接ADC7接线柱即可 #define SCLK_1 P6OUT |= _BV(7) const unsigned char chAlphabetL[][6] = { {0x00, 0x20, 0x54, 0x54, 0x54, 0x78}, {0x00, 0x7F, 0x48, 0x44, 0x44, 0x38}, {0x00, 0x38, 0x44, 0x44, 0x44, 0x20}, {0x00, 0x38, 0x44, 0x44, 0x48, 0x7F}, {0x00, 0x38, 0x54, 0x54, 0x54, 0x18}, {0x00, 0x08, 0x7E, 0x09, 0x01, 0x02}, {0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C}, {0x00, 0x7F, 0x08, 0x04, 0x04, 0x78}, {0x00, 0x00, 0x44, 0x7D, 0x40, 0x00}, {0x00, 0x40, 0x80, 0x84, 0x7D, 0x00}, {0x00, 0x7F, 0x10, 0x28, 0x44, 0x00}, {0x00, 0x00, 0x41, 0x7F, 0x40, 0x00}, {0x00, 0x7C, 0x04, 0x18, 0x04, 0x78}, {0x00, 0x7C, 0x08, 0x04, 0x04, 0x78}, {0x00, 0x38, 0x44, 0x44, 0x44, 0x38}, {0x00, 0xFC, 0x24, 0x24, 0x24, 0x18}, {0x00, 0x18, 0x24, 0x24, 0x18, 0xFC}, {0x00, 0x7C, 0x08, 0x04, 0x04, 0x08}, {0x00, 0x48, 0x54, 0x54, 0x54, 0x20}, {0x00, 0x04, 0x3F, 0x44, 0x40, 0x20}, {0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C}, {0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C}, {0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C}, {0x00, 0x44, 0x28, 0x10, 0x28, 0x44}, {0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C}, {0x00, 0x44, 0x64, 0x54, 0x4C, 0x44} }; const unsigned char chAlphabetU[][6] = { {0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C}, {0x00, 0x7F, 0x49, 0x49, 0x49, 0x36}, {0x00, 0x3E, 0x41, 0x41, 0x41, 0x22}, {0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C}, {0x00, 0x7F, 0x49, 0x49, 0x49, 0x41}, {0x00, 0x7F, 0x09, 0x09, 0x09, 0x01}, {0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A}, {0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F}, {0x00, 0x00, 0x41, 0x7F, 0x41, 0x00}, {0x00, 0x20, 0x40, 0x41, 0x3F, 0x01}, {0x00, 0x7F, 0x08, 0x14, 0x22, 0x41}, {0x00, 0x7F, 0x40, 0x40, 0x40, 0x40}, {0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F}, {0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F}, {0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E}, {0x00, 0x7F, 0x09, 0x09, 0x09, 0x06}, {0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E}, {0x00, 0x7F, 0x09, 0x19, 0x29, 0x46}, {0x00, 0x46, 0x49, 0x49, 0x49, 0x31}, {0x00, 0x01, 0x01, 0x7F, 0x01, 0x01}, {0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F}, {0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F}, {0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F}, {0x00, 0x63, 0x14, 0x08, 0x14, 0x63}, {0x00, 0x07, 0x08, 0x70, 0x08, 0x07}, {0x00, 0x61, 0x51, 0x49, 0x45, 0x43} }; const unsigned char chColon[6] = {0x00, 0x00, 0x36, 0x36, 0x00, 0x00}; const unsigned char chDot[6] = {0x00, 0x00, 0x60, 0x60, 0x00, 0x00}; const unsigned char chMinus[6] = {0x00, 0x08, 0x08, 0x08, 0x08, 0x08}; const unsigned char chNum[][6] = { {0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E}, {0x00, 0x00, 0x42, 0x7F, 0x40, 0x00}, {0x00, 0x42, 0x61, 0x51, 0x49, 0x46}, {0x00, 0x21, 0x41, 0x45, 0x4B, 0x31}, {0x00, 0x18, 0x14, 0x12, 0x7F, 0x10}, {0x00, 0x27, 0x45, 0x45, 0x45, 0x39}, {0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30}, {0x00, 0x01, 0x71, 0x09, 0x05, 0x03}, {0x00, 0x36, 0x49, 0x49, 0x49, 0x36}, {0x00, 0x06, 0x49, 0x49, 0x29, 0x1E} }; const unsigned char img[][84] = { {0xdc, 0x32, 0x3a, 0xdc, 0x20, 0xa0, 0xa0, 0xc0, 0x0e, 0x30, 0x7e, 0x00, 0x40, 0x80, 0x00, 0x00, 0x00, 0x00, 0x60, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0xa0, 0x90, 0x88, 0xd4, 0xe4, 0x70, 0x72, 0xbc, 0x78, 0x38, 0x38, 0x3e, 0x7c, 0xf8, 0xf8, 0xf8, 0xf0, 0xf0, 0xf0, 0xe0, 0xc0, 0xc0, 0x40, 0x20, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xfa, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfc, 0xfe, 0xfe, 0x1e, 0x02}, {0x47, 0x41, 0x41, 0xd1, 0x23, 0x1d, 0x81, 0x40, 0x20, 0x98, 0x04, 0x83, 0x3c, 0x90, 0xf1, 0x3e, 0xce, 0x77, 0x63, 0xc3, 0xc3, 0x27, 0x3f, 0xfe, 0xfe, 0xfc, 0xf0, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x07, 0xf1, 0xfc, 0xfe, 0xff, 0xff, 0xfc, 0xf8, 0x98, 0x98, 0xe4, 0xe7, 0xff, 0xdc, 0xfc, 0xcf, 0xcf, 0xff, 0xbf, 0xff, 0xef, 0x1e, 0xf8, 0x10, 0x00, 0x40, 0x3f, 0x8f, 0x43, 0x71, 0x49, 0x49, 0x89, 0x01, 0x03, 0x0f, 0xf3, 0x00, 0x00, 0x88}, {0xf8, 0xf0, 0xf0, 0xf8, 0x77, 0xd1, 0x30, 0x90, 0x00, 0xaa, 0x00, 0x88, 0x00, 0x21, 0x8e, 0x70, 0x1f, 0xfe, 0xfe, 0xff, 0xff, 0x3f, 0x3f, 0x3f, 0x7f, 0x9f, 0xff, 0x3f, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0xc0, 0x00, 0x03, 0x0f, 0x3f, 0x7f, 0x7f, 0xff, 0xbf, 0x3f, 0x1f, 0x0f, 0x0f, 0x0f, 0x8f, 0x8f, 0xcf, 0xe3, 0xf7, 0x79, 0x1f, 0x07, 0x00, 0x00, 0x00, 0x80, 0x60, 0x80, 0x87, 0x04, 0x01, 0x00, 0x84, 0x83, 0x40, 0x30, 0x8c, 0x03, 0xa8, 0x00, 0x88}, {0x77, 0x65, 0xe3, 0xea, 0xc1, 0xca, 0x00, 0x88, 0x00, 0xaa, 0x00, 0x88, 0x00, 0xa0, 0x0f, 0xb0, 0x40, 0x82, 0x11, 0x2b, 0x4a, 0x4a, 0x72, 0x0a, 0x13, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x06, 0x1e, 0xfe, 0xfe, 0xfe, 0xfe, 0xff, 0x7f, 0x7f, 0x7f, 0x3f, 0x3f, 0xff, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x03, 0x03, 0x1b, 0x23, 0x23, 0x23, 0x23, 0x23, 0x25, 0x15, 0x0c, 0x80, 0x80, 0x40, 0x20, 0x70, 0x28, 0x46, 0x81, 0xca, 0x40, 0x28, 0x21, 0x39, 0x45, 0x82, 0x82, 0x82, 0x44, 0x78, 0x40, 0x4a, 0x40, 0x88}, {0x9a, 0x9d, 0x65, 0x65, 0x1a, 0x2d, 0x00, 0x88, 0x00, 0x2a, 0x00, 0x88, 0x00, 0x22, 0x00, 0x88, 0x00, 0x2a, 0x01, 0x8a, 0x02, 0x24, 0x04, 0x88, 0x08, 0x10, 0x10, 0x10, 0x20, 0xa0, 0xa0, 0x60, 0x40, 0x20, 0x21, 0x23, 0x25, 0x24, 0x24, 0x24, 0x44, 0x44, 0x42, 0xc1, 0xa0, 0xa0, 0x60, 0x20, 0x20, 0x30, 0x10, 0x10, 0x10, 0x18, 0x18, 0x18, 0x14, 0x14, 0x12, 0x22, 0x21, 0x40, 0x40, 0x40, 0x80, 0x80, 0x80, 0x80, 0x00, 0x03, 0x04, 0x04, 0x3c, 0xc4, 0x82, 0x83, 0x83, 0x47, 0xf8, 0x08, 0x08, 0x08, 0x18, 0x27}, {0x7f, 0x7f, 0x3e, 0x0e, 0x08, 0x0a, 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0a, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0a, 0x08, 0x0c, 0x0c, 0x0a, 0x0a, 0x09, 0x0d, 0x08, 0x10, 0x10, 0x20, 0x20, 0x20, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x20, 0x20, 0x1e, 0x21, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x20, 0x20, 0x20, 0x10, 0x08, 0x0c, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x09, 0x09, 0x09, 0x0a, 0x0a, 0x0d, 0x08, 0x08, 0x08, 0x08, 0x09, 0x0e, 0x08, 0x08, 0x08, 0x08} }; bool clearlcd = FALSE; uint8_t count = 4; struct node1_data data; void LCDWrite(unsigned char dat) { unsigned char i; CE_0; for (i = 0; i < 8; i++) { if (dat & 0x80) DIN_1; else DIN_0; SCLK_0; dat <<= 1; SCLK_1; } CE_1; } // 写入命令 void LCDWriteCmd(unsigned char cmd) { DC_0; LCDWrite(cmd); } // 写入数据 void LCDWriteData(unsigned char dat) { DC_1; LCDWrite(dat); } // 设置显示位置 void LCDSetPos(unsigned char x, unsigned char y) { LCDWriteCmd(0x40 | y); LCDWriteCmd(0x80 | x); } // 清屏 void LCDClear(void) { unsigned char i, j; LCDSetPos(0, 0); for (i = 0; i < 6; i++) { for (j = 0; j < 84; j++) LCDWriteData(0x00); } } // 初始化LCD void LCDInit(void) { // 复位 RST_0; _NOP(); _NOP(); _NOP(); _NOP(); RST_1; LCDWriteCmd(0x21); // 使用扩展命令设置LCD模式 LCDWriteCmd(0xa7); // 设置显示亮度 LCDWriteCmd(0x06); // 温度校正 LCDWriteCmd(0x13); // 1:48 LCDWriteCmd(0x20); // 使用基本命令, 且水平寻址 LCDClear(); LCDWriteCmd(0x0c); // 正常显示 CE_0; BL_1; // 开背光 } // 显示字符A void LCDWriteChar(const unsigned char *ch) { unsigned char i; for (i = 0; i < 6; i++) LCDWriteData(ch[i]); } void LCDDisplayImage(void) { unsigned char i, j; LCDSetPos(0, 0); for (i = 0; i < 6; i++) { for (j = 0; j < 84; j++) LCDWriteData(img[i][j]); } } void LCDWriteWord(const unsigned char *s) { while (*s) { if (*s >= 'a' && *s <= 'z') LCDWriteChar(chAlphabetL[*s - 'a']); else if (*s >= 'A' && *s <= 'Z') LCDWriteChar(chAlphabetU[*s - 'A']); s++; } } void LCDWriteSpace(void) { unsigned char i; for (i = 0; i < 6; i++) LCDWriteData(0x00); } void LCDWriteUINT(uint16_t num, bool dot) { LCDWriteChar(chNum[num / 10000]); LCDWriteChar(chNum[num % 10000 / 1000]); if (dot) LCDWriteChar(chDot); LCDWriteChar(chNum[num % 1000 / 100]); LCDWriteChar(chNum[num % 100 / 10]); LCDWriteChar(chNum[num % 10]); } event void Boot.booted(void) { LCDInit(); LCDDisplayImage(); call Leds.led2On(); call SplitControl.start(); } event void SplitControl.startDone(error_t err) { if (err == SUCCESS) call Timer0.startPeriodic(1000); else call SplitControl.start(); } event void SplitControl.stopDone(error_t err) { } event void Timer0.fired(void) { if (count >= 2) count--; else if (count == 1) call Leds.led2Off(); else if (count == 0) { if (!clearlcd) { LCDClear(); clearlcd = TRUE; } LCDSetPos(0, 0); LCDWriteWord("Light"); LCDWriteChar(chColon); LCDWriteSpace(); LCDWriteUINT(data.light, FALSE); LCDSetPos(0, 1); LCDWriteWord("Temperature"); LCDWriteChar(chColon); LCDWriteSpace(); LCDSetPos(0, 2); LCDWriteUINT(data.temperature, TRUE); LCDSetPos(0, 3); LCDWriteWord("Humidity"); LCDWriteChar(chColon); LCDWriteSpace(); LCDSetPos(0, 4); LCDWriteUINT(data.humidity, TRUE); count = 1; call Leds.led1Off(); } } event message_t *Receive.receive(message_t *pMsg, void *payload, uint8_t len) { if (len == sizeof(struct node1_data)) { memcpy(&data, payload, len); count = 0; call Leds.led1On(); } return pMsg; } }
|