[TPYBoard - Micropython之会python就能做硬件 2] 利用micropython控制NOKIA 5110屏
轉載請注明:@小五義 http://www.cnblogs.com/xiaowuyi?
歡迎加入討論群?64770604一、本次實驗所需器材
1、TPYboard V102板??一塊
2、DS3231模塊???一塊
3、NOKIA 5110 LCD?屏?一塊
4、杜邦線:若干
===感謝某寶“蘿卜城”提供的TPYboard V102豪華版套餐===
二、Nokia 5110 LCD屏使用方法
1、什么是Nokia 5110 LCD?
????? Nokia 5110?LCD是NOKIA 公司生產的可用于其5110、6150,6100 等系列移動電話的液晶顯示模塊,國內廠家也生產有類似的兼容產品。該產品除應用于移動電話外,也可廣泛應用于各類便攜式設備的顯示系統。與其它類型的產品相比,該模塊具有以下特點:
●84x48 的點陣LCD,可以顯示4 行漢字,
●采用串行接口與主處理器進行通信,接口信號線數量大幅度減少,包括電源和地在內的信號線僅有9 條。支持多種串行通信協議(如AVR 單片機的SPI、MCS51 的串口模式0等),傳輸速率高達4Mbps,可全速寫入顯示數據,無等待時間。
●可通過導電膠連接模塊與印制版
●LCD 控制器/驅動器芯片已綁定到LCD 晶片上,模塊的體積很小。
●采用低電壓供電,正常顯示時的工作電流在200μA 以下,且具有掉電模式。
2、Nokia 5110 LCD?引腳說明
??? Nokia 5110 LCD共有8個引腳,不同的板子可能標注的英文略有不同,具體含義如下:
| 標注 | 含義 |
| GND | 接地 |
| BL | 背景燈 |
| Vcc | 3.3V |
| CLK | 時鐘信號 |
| DIN | 數據流 |
| DC | 當為0時表示命令狀態,當為1時表示數據狀態 |
| CE | 使能端 |
| RST | RESET |
3、Tpyboard?V102連接5110的方法
????這里使用的是SPI連接方法,如果你對什么是SPI連接還不是很清楚,你可以百度一下,這樣有利于你從原理上搞明白為什么這么連接。具體針腳對應如下:(由于tpyboard v102有兩個spi接口,這里我們選用x1對應接口)
| TPYboard | 5110LCD | SPI |
| GND | GND | ? |
| X4 | BL | ? |
| 3.3V | Vcc | ? |
| X6 | CLK | SCK |
| X8 | DIN | MOSI |
| X3 | DC | ? |
| X2 | CE | ? |
| X1 | RST | ? |
4、5110?LCD字模的獲取
(1)英文及數據的顯示
?????本節提供了font.py文件,已經將英文、數字及符號的字模做好,可以直接調用。具體下載見文章最后的下載地址。
(2)漢字取模
?????漢字取模需要用到字模提取工具,該工具在文章后面的下載地址中可以下載。這里以“歡”為例具體說明使用方法如下:
第一步,打開工具:
第二步,點擊新建圖像,寬度與高度均為16,點擊確定。如圖:
第三步,在文字輸入區寫入要取模的漢字“歡”,按下ctrl+enter。
第四步,點左邊菜單里的取模方式,點c51格式,于是得到:
0x04,0x24,0x44,0x84,0x64,0x9C,0x40,0x30,0x0F,0xC8,0x08,0x08,0x28,0x18,0x00,0x00,
0x10,0x08,0x06,0x01,0x82,0x4C,0x20,0x18,0x06,0x01,0x06,0x18,0x20,0x40,0x80,0x00,
第五步,對應填寫到chinese.py文件中(該文件可從文章后面的網址下載),如下:
class CN_UTF8:"""docstring for CN_UTF8"""#key:values#key: 使用漢字的UTF-8碼#values: 16*16# [0] 8*16 上半部分# [1] 8*16 下半部分UTF8_CHINESE = {0xe6aca2:[[0x04,0x24,0x44,0x84,0x64,0x9C,0x40,0x30,0x0F,0xC8,0x08,0x08,0x28,0x18,0x00,0x00],[0x10,0x08,0x06,0x01,0x82,0x4C,0x20,0x18,0x06,0x01,0x06,0x18,0x20,0x40,0x80,0x00]],#歡 0xe8bf8e:[[0x40,0x40,0x42,0xCC,0x00,0x00,0xFC,0x04,0x02,0x00,0xFC,0x04,0x04,0xFC,0x00,0x00],[0x00,0x40,0x20,0x1F,0x20,0x40,0x4F,0x44,0x42,0x40,0x7F,0x42,0x44,0x43,0x40,0x00]],#迎0xe4bdbf:[[0x80,0x60,0xF8,0x07,0x04,0xE4,0x24,0x24,0x24,0xFF,0x24,0x24,0x24,0xE4,0x04,0x00],[0x00,0x00,0xFF,0x00,0x80,0x81,0x45,0x29,0x11,0x2F,0x41,0x41,0x81,0x81,0x80,0x00]],#使0xe794a8:[[0x00,0x00,0xFE,0x22,0x22,0x22,0x22,0xFE,0x22,0x22,0x22,0x22,0xFE,0x00,0x00,0x00],[0x80,0x60,0x1F,0x02,0x02,0x02,0x02,0x7F,0x02,0x02,0x42,0x82,0x7F,0x00,0x00,0x00]],#用0xe78eb0:[[0x04,0x84,0x84,0xFC,0x84,0x84,0x00,0xFE,0x02,0x02,0xF2,0x02,0x02,0xFE,0x00,0x00],[0x20,0x60,0x20,0x1F,0x10,0x90,0x40,0x23,0x18,0x06,0x01,0x7E,0x80,0x83,0xE0,0x00]],#現0xe59ca8:[[0x08,0x08,0x88,0xC8,0x38,0x0C,0x0B,0x08,0x08,0xE8,0x08,0x08,0x08,0x08,0x08,0x00],[0x02,0x01,0x00,0xFF,0x40,0x41,0x41,0x41,0x41,0x7F,0x41,0x41,0x41,0x41,0x40,0x00]],#在0xe697b6:[[0x00,0xFC,0x84,0x84,0x84,0xFC,0x00,0x10,0x10,0x10,0x10,0x10,0xFF,0x10,0x10,0x00],[0x00,0x3F,0x10,0x10,0x10,0x3F,0x00,0x00,0x01,0x06,0x40,0x80,0x7F,0x00,0x00,0x00]],#時0xe997b4:[[0x00,0xF8,0x01,0x06,0x00,0xF0,0x12,0x12,0x12,0xF2,0x02,0x02,0x02,0xFE,0x00,0x00],[0x00,0xFF,0x00,0x00,0x00,0x1F,0x11,0x11,0x11,0x1F,0x00,0x40,0x80,0x7F,0x00,0x00]], #間 0xe68891:[[0x20,0x24,0x24,0x24,0xFE,0x23,0x22,0x20,0x20,0xFF,0x20,0x22,0x2C,0xA0,0x20,0x00],[0x00,0x08,0x48,0x84,0x7F,0x02,0x41,0x40,0x20,0x13,0x0C,0x14,0x22,0x41,0xF8,0x00]], #我0xe8909d:[[0x02,0x02,0xF2,0x92,0x97,0xF2,0x92,0x92,0x92,0xF2,0x97,0x92,0xF2,0x02,0x02,0x00],[0x00,0x80,0x88,0x88,0x44,0x4A,0x53,0x22,0x22,0x12,0x0A,0x06,0x00,0x00,0x00,0x00]], #蘿0xe58d9c:[[0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x20,0x40,0x80,0x00,0x00,0x00,0x00,0x00],[0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x00]], #卜0xe699ba:[[0x10,0x94,0x53,0x32,0x1E,0x32,0x52,0x10,0x00,0x7E,0x42,0x42,0x42,0x7E,0x00,0x00],[0x00,0x00,0x00,0xFF,0x49,0x49,0x49,0x49,0x49,0x49,0x49,0xFF,0x00,0x00,0x00,0x00]], #智0xe883bd:[[0x08,0xCC,0x4A,0x49,0x48,0x4A,0xCC,0x18,0x00,0x7F,0x88,0x88,0x84,0x82,0xE0,0x00],[0x00,0xFF,0x12,0x12,0x52,0x92,0x7F,0x00,0x00,0x7E,0x88,0x88,0x84,0x82,0xE0,0x00]],#能0xe682a8:[[0x20,0x10,0x08,0xFC,0x23,0x10,0x88,0x67,0x04,0xF4,0x04,0x24,0x54,0x8C,0x00,0x00],[0x40,0x30,0x00,0x77,0x80,0x81,0x88,0xB2,0x84,0x83,0x80,0xE0,0x00,0x11,0x60,0x00]],#您0xe5bd93:[[0x00,0x40,0x42,0x44,0x58,0x40,0x40,0x7F,0x40,0x40,0x50,0x48,0xC6,0x00,0x00,0x00],[0x00,0x40,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0xFF,0x00,0x00,0x00]],#當0xE5898D:[[0x08,0x08,0xE8,0x29,0x2E,0x28,0xE8,0x08,0x08,0xC8,0x0C,0x0B,0xE8,0x08,0x08,0x00],[0x00,0x00,0xFF,0x09,0x49,0x89,0x7F,0x00,0x00,0x0F,0x40,0x80,0x7F,0x00,0x00,0x00]]#前}#key 漢字的UTF-8碼#isBottom 確定這次是獲取 某個字的 上半部分(0) 還是下半部分(1)def get_chinese_utf8(self, key,isBottom = 0):values = self.UTF8_CHINESE[key]return values[isBottom]三、顯示“蘿卜智能”的小實例
????按照上面連接后,將tpyboard v102插入電腦usb,在TPFLASH內建立以下文件(可從文章后地址下載):
#font.py
//轉載請注明:@小五義http://www.cnblogs.com/xiaowuyi??QQ群:64770604 class FONT6_8:"""docstring for FONT6_8"""FONTTYPE6_8 = [[0x00, 0x00, 0x00, 0x00, 0x00, 0x00] # 20,[0x00, 0x00, 0x00, 0x5f, 0x00, 0x00] # 21 !,[0x00, 0x00, 0x07, 0x00, 0x07, 0x00] # 22 ",[0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14] # 23 #,[0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12] # 24 $,[0x00, 0x23, 0x13, 0x08, 0x64, 0x62] # 25 %,[0x00, 0x36, 0x49, 0x55, 0x22, 0x50] # 26 &,[0x00, 0x00, 0x05, 0x03, 0x00, 0x00] # 27 ',[0x00, 0x00, 0x1c, 0x22, 0x41, 0x00] # 28 ( ,[0x00, 0x00, 0x41, 0x22, 0x1c, 0x00] # 29 ),[0x00, 0x14, 0x08, 0x3e, 0x08, 0x14] # 2a *,[0x00, 0x08, 0x08, 0x3e, 0x08, 0x08] # 2b +,[0x00, 0x00, 0x50, 0x30, 0x00, 0x00] # 2c ,,[0x00, 0x08, 0x08, 0x08, 0x08, 0x08] # 2d -,[0x00, 0x00, 0x60, 0x60, 0x00, 0x00] # 2e .,[0x00, 0x20, 0x10, 0x08, 0x04, 0x02] # 2f /,[0x00, 0x3e, 0x51, 0x49, 0x45, 0x3e] # 30 0,[0x00, 0x00, 0x42, 0x7f, 0x40, 0x00] # 31 1,[0x00, 0x42, 0x61, 0x51, 0x49, 0x46] # 32 2,[0x00, 0x21, 0x41, 0x45, 0x4b, 0x31] # 33 3,[0x00, 0x18, 0x14, 0x12, 0x7f, 0x10] # 34 4,[0x00, 0x27, 0x45, 0x45, 0x45, 0x39] # 35 5,[0x00, 0x3c, 0x4a, 0x49, 0x49, 0x30] # 36 6,[0x00, 0x01, 0x71, 0x09, 0x05, 0x03] # 37 7,[0x00, 0x36, 0x49, 0x49, 0x49, 0x36] # 38 8,[0x00, 0x06, 0x49, 0x49, 0x29, 0x1e] # 39 9,[0x00, 0x00, 0x36, 0x36, 0x00, 0x00] # 3a :,[0x00, 0x00, 0x56, 0x36, 0x00, 0x00] # 3b ;,[0x00, 0x08, 0x14, 0x22, 0x41, 0x00] # 3c <,[0x00, 0x14, 0x14, 0x14, 0x14, 0x14] # 3d =,[0x00, 0x00, 0x41, 0x22, 0x14, 0x08] # 3e >,[0x00, 0x02, 0x01, 0x51, 0x09, 0x06] # 3f ?,[0x00, 0x32, 0x49, 0x79, 0x41, 0x3e] # 40 @,[0x00, 0x7e, 0x11, 0x11, 0x11, 0x7e] # 41 A,[0x00, 0x7f, 0x49, 0x49, 0x49, 0x36] # 42 B,[0x00, 0x3e, 0x41, 0x41, 0x41, 0x22] # 43 C,[0x00, 0x7f, 0x41, 0x41, 0x22, 0x1c] # 44 D,[0x00, 0x7f, 0x49, 0x49, 0x49, 0x41] # 45 E,[0x00, 0x7f, 0x09, 0x09, 0x09, 0x01] # 46 F,[0x00, 0x3e, 0x41, 0x49, 0x49, 0x7a] # 47 G,[0x00, 0x7f, 0x08, 0x08, 0x08, 0x7f] # 48 H,[0x00, 0x00, 0x41, 0x7f, 0x41, 0x00] # 49 I,[0x00, 0x20, 0x40, 0x41, 0x3f, 0x01] # 4a J,[0x00, 0x7f, 0x08, 0x14, 0x22, 0x41] # 4b K,[0x00, 0x7f, 0x40, 0x40, 0x40, 0x40] # 4c L,[0x00, 0x7f, 0x02, 0x0c, 0x02, 0x7f] # 4d M,[0x00, 0x7f, 0x04, 0x08, 0x10, 0x7f] # 4e N,[0x00, 0x3e, 0x41, 0x41, 0x41, 0x3e] # 4f O,[0x00, 0x7f, 0x09, 0x09, 0x09, 0x06] # 50 P,[0x00, 0x3e, 0x41, 0x51, 0x21, 0x5e] # 51 Q,[0x00, 0x7f, 0x09, 0x19, 0x29, 0x46] # 52 R,[0x00, 0x46, 0x49, 0x49, 0x49, 0x31] # 53 S,[0x00, 0x01, 0x01, 0x7f, 0x01, 0x01] # 54 T,[0x00, 0x3f, 0x40, 0x40, 0x40, 0x3f] # 55 U,[0x00, 0x1f, 0x20, 0x40, 0x20, 0x1f] # 56 V,[0x00, 0x3f, 0x40, 0x38, 0x40, 0x3f] # 57 W,[0x00, 0x63, 0x14, 0x08, 0x14, 0x63] # 58 X,[0x00, 0x07, 0x08, 0x70, 0x08, 0x07] # 59 Y,[0x00, 0x61, 0x51, 0x49, 0x45, 0x43] # 5a Z,[0x00, 0x00, 0x7f, 0x41, 0x41, 0x00] # 5b [,[0x00, 0x02, 0x04, 0x08, 0x10, 0x20] # 5c \ ,[0x00, 0x00, 0x41, 0x41, 0x7f, 0x00] # 5d ],[0x00, 0x04, 0x02, 0x01, 0x02, 0x04] # 5e ^,[0x00, 0x40, 0x40, 0x40, 0x40, 0x40] # 5f _,[0x00, 0x00, 0x01, 0x02, 0x04, 0x00] # 60 `,[0x00, 0x20, 0x54, 0x54, 0x54, 0x78] # 61 a,[0x00, 0x7f, 0x48, 0x44, 0x44, 0x38] # 62 b,[0x00, 0x38, 0x44, 0x44, 0x44, 0x20] # 63 c,[0x00, 0x38, 0x44, 0x44, 0x48, 0x7f] # 64 d,[0x00, 0x38, 0x54, 0x54, 0x54, 0x18] # 65 e,[0x00, 0x08, 0x7e, 0x09, 0x01, 0x02] # 66 f,[0x00, 0x0c, 0x52, 0x52, 0x52, 0x3e] # 67 g,[0x00, 0x7f, 0x08, 0x04, 0x04, 0x78] # 68 h,[0x00, 0x00, 0x44, 0x7d, 0x40, 0x00] # 69 i,[0x00, 0x20, 0x40, 0x44, 0x3d, 0x00] # 6a j,[0x00, 0x7f, 0x10, 0x28, 0x44, 0x00] # 6b k,[0x00, 0x00, 0x41, 0x7f, 0x40, 0x00] # 6c l,[0x00, 0x7c, 0x04, 0x18, 0x04, 0x78] # 6d m,[0x00, 0x7c, 0x08, 0x04, 0x04, 0x78] # 6e n,[0x00, 0x38, 0x44, 0x44, 0x44, 0x38] # 6f o,[0x00, 0x7c, 0x14, 0x14, 0x14, 0x08] # 70 p,[0x00, 0x08, 0x14, 0x14, 0x18, 0x7c] # 71 q,[0x00, 0x7c, 0x08, 0x04, 0x04, 0x08] # 72 r,[0x00, 0x48, 0x54, 0x54, 0x54, 0x20] # 73 s,[0x00, 0x04, 0x3f, 0x44, 0x40, 0x20] # 74 t,[0x00, 0x3c, 0x40, 0x40, 0x20, 0x7c] # 75 u,[0x00, 0x1c, 0x20, 0x40, 0x20, 0x1c] # 76 v,[0x00, 0x3c, 0x40, 0x30, 0x40, 0x3c] # 77 w,[0x00, 0x44, 0x28, 0x10, 0x28, 0x44] # 78 x,[0x00, 0x0c, 0x50, 0x50, 0x50, 0x3c] # 79 y,[0x00, 0x44, 0x64, 0x54, 0x4c, 0x44] # 7a z,[0x00, 0x00, 0x08, 0x36, 0x41, 0x00] # 7b [,[0x00, 0x00, 0x00, 0x7f, 0x00, 0x00] # 7c |,[0x00, 0x00, 0x41, 0x36, 0x08, 0x00] # 7d ],[0x00, 0x10, 0x08, 0x08, 0x10, 0x08] # 7e ~,[0x00, 0x78, 0x46, 0x41, 0x46, 0x78] # 7f (delete)]def get_font6_8(self, data):return self.FONTTYPE6_8[bytearray(data)[0] - 0x20]#chinese.py
//轉載請注明:@小五義http://www.cnblogs.com/xiaowuyi??QQ群:64770604 class CN_UTF8:"""docstring for CN_UTF8"""#key:values#key: 使用漢字的UTF-8碼#values: 16*16# [0] 8*16 上半部分# [1] 8*16 下半部分UTF8_CHINESE = {0xe6aca2:[[0x04,0x24,0x44,0x84,0x64,0x9C,0x40,0x30,0x0F,0xC8,0x08,0x08,0x28,0x18,0x00,0x00],[0x10,0x08,0x06,0x01,0x82,0x4C,0x20,0x18,0x06,0x01,0x06,0x18,0x20,0x40,0x80,0x00]],#歡 0xe8bf8e:[[0x40,0x40,0x42,0xCC,0x00,0x00,0xFC,0x04,0x02,0x00,0xFC,0x04,0x04,0xFC,0x00,0x00],[0x00,0x40,0x20,0x1F,0x20,0x40,0x4F,0x44,0x42,0x40,0x7F,0x42,0x44,0x43,0x40,0x00]],#迎0xe4bdbf:[[0x80,0x60,0xF8,0x07,0x04,0xE4,0x24,0x24,0x24,0xFF,0x24,0x24,0x24,0xE4,0x04,0x00],[0x00,0x00,0xFF,0x00,0x80,0x81,0x45,0x29,0x11,0x2F,0x41,0x41,0x81,0x81,0x80,0x00]],#使0xe794a8:[[0x00,0x00,0xFE,0x22,0x22,0x22,0x22,0xFE,0x22,0x22,0x22,0x22,0xFE,0x00,0x00,0x00],[0x80,0x60,0x1F,0x02,0x02,0x02,0x02,0x7F,0x02,0x02,0x42,0x82,0x7F,0x00,0x00,0x00]],#用0xe78eb0:[[0x04,0x84,0x84,0xFC,0x84,0x84,0x00,0xFE,0x02,0x02,0xF2,0x02,0x02,0xFE,0x00,0x00],[0x20,0x60,0x20,0x1F,0x10,0x90,0x40,0x23,0x18,0x06,0x01,0x7E,0x80,0x83,0xE0,0x00]],#現0xe59ca8:[[0x08,0x08,0x88,0xC8,0x38,0x0C,0x0B,0x08,0x08,0xE8,0x08,0x08,0x08,0x08,0x08,0x00],[0x02,0x01,0x00,0xFF,0x40,0x41,0x41,0x41,0x41,0x7F,0x41,0x41,0x41,0x41,0x40,0x00]],#在0xe697b6:[[0x00,0xFC,0x84,0x84,0x84,0xFC,0x00,0x10,0x10,0x10,0x10,0x10,0xFF,0x10,0x10,0x00],[0x00,0x3F,0x10,0x10,0x10,0x3F,0x00,0x00,0x01,0x06,0x40,0x80,0x7F,0x00,0x00,0x00]],#時0xe997b4:[[0x00,0xF8,0x01,0x06,0x00,0xF0,0x12,0x12,0x12,0xF2,0x02,0x02,0x02,0xFE,0x00,0x00],[0x00,0xFF,0x00,0x00,0x00,0x1F,0x11,0x11,0x11,0x1F,0x00,0x40,0x80,0x7F,0x00,0x00]], #間 0xe68891:[[0x20,0x24,0x24,0x24,0xFE,0x23,0x22,0x20,0x20,0xFF,0x20,0x22,0x2C,0xA0,0x20,0x00],[0x00,0x08,0x48,0x84,0x7F,0x02,0x41,0x40,0x20,0x13,0x0C,0x14,0x22,0x41,0xF8,0x00]], #我0xe8909d:[[0x02,0x02,0xF2,0x92,0x97,0xF2,0x92,0x92,0x92,0xF2,0x97,0x92,0xF2,0x02,0x02,0x00],[0x00,0x80,0x88,0x88,0x44,0x4A,0x53,0x22,0x22,0x12,0x0A,0x06,0x00,0x00,0x00,0x00]], #蘿0xe58d9c:[[0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x20,0x40,0x80,0x00,0x00,0x00,0x00,0x00],[0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x00]], #卜0xe699ba:[[0x10,0x94,0x53,0x32,0x1E,0x32,0x52,0x10,0x00,0x7E,0x42,0x42,0x42,0x7E,0x00,0x00],[0x00,0x00,0x00,0xFF,0x49,0x49,0x49,0x49,0x49,0x49,0x49,0xFF,0x00,0x00,0x00,0x00]], #智0xe883bd:[[0x08,0xCC,0x4A,0x49,0x48,0x4A,0xCC,0x18,0x00,0x7F,0x88,0x88,0x84,0x82,0xE0,0x00],[0x00,0xFF,0x12,0x12,0x52,0x92,0x7F,0x00,0x00,0x7E,0x88,0x88,0x84,0x82,0xE0,0x00]],#能0xe682a8:[[0x20,0x10,0x08,0xFC,0x23,0x10,0x88,0x67,0x04,0xF4,0x04,0x24,0x54,0x8C,0x00,0x00],[0x40,0x30,0x00,0x77,0x80,0x81,0x88,0xB2,0x84,0x83,0x80,0xE0,0x00,0x11,0x60,0x00]],#您0xe5bd93:[[0x00,0x40,0x42,0x44,0x58,0x40,0x40,0x7F,0x40,0x40,0x50,0x48,0xC6,0x00,0x00,0x00],[0x00,0x40,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0xFF,0x00,0x00,0x00]],#當0xE5898D:[[0x08,0x08,0xE8,0x29,0x2E,0x28,0xE8,0x08,0x08,0xC8,0x0C,0x0B,0xE8,0x08,0x08,0x00],[0x00,0x00,0xFF,0x09,0x49,0x89,0x7F,0x00,0x00,0x0F,0x40,0x80,0x7F,0x00,0x00,0x00]]#前}#key 漢字的UTF-8碼#isBottom 確定這次是獲取 某個字的 上半部分(0) 還是下半部分(1)def get_chinese_utf8(self, key,isBottom = 0):values = self.UTF8_CHINESE[key]return values[isBottom]#upcd8544.py
//轉載請注明:@小五義http://www.cnblogs.com/xiaowuyi??QQ群:64770604 # -*- coding: utf-8 -*- """ MicroPython PCD8544 driver (for Nokia 5110 displays) """__author__ = "Markus Birth" __copyright__ = "Copyright 2015, Markus Birth" __credits__ = ["Markus Birth"] __license__ = "MIT" __version__ = "1.0" __maintainer__ = "Markus Birth" __email__ = "markus@birth-online.de" __status__ = "Production"# Datasheet: https://www.sparkfun.com/datasheets/LCD/Monochrome/Nokia5110.pdf # Inspiration from: # - https://github.com/inaugurator/upyd5110 # - https://github.com/rm-hull/pcd8544/blob/master/src/lcd.py # # PINOUT # WiPy/pyBoard display function # # 3V3 or any Pin => VCC 3.3V logic voltage (0=off, 1=on) # MOSI => DIN data flow (Master out, Slave in) # SCK => CLK SPI clock # any Pin => RST Reset pin (0=reset, 1=normal) # any Pin => CE Chip Enable (0=listen for input, 1=ignore input) # any Pin => DC Data/Command (0=commands, 1=data) # any Pin => LIGHT Light (0=on, 1=off) # GND => GND # # pyBoard "Y" side # SPI = pyb.SPI(1) # RST = pyb.Pin('Y4') # CE = pyb.Pin('Y5') # DC = pyb.Pin('Y3') # LIGHT = pyb.Pin('Y2') # PWR = pyb.Pin('Y1') # # pyBoard "X" side # SPI = pyb.SPI(2) # RST = pyb.Pin('X4') # CE = pyb.Pin('X5') # DC = pyb.Pin('X3') # LIGHT = pyb.Pin('X2') # PWR = pyb.Pin('X1') # # WiPy (on Exp board, SD and User-LED jumper have to be removed!) # SPI = machine.SPI(0) # GP14 (CLK) + GP16 (MOSI->DIN), User-LED jumper removed! # RST = machine.Pin('GP24') # CE = machine.Pin('GP12') # DC = machine.Pin('GP22') # LIGHT = machine.Pin('GP23') # PWR = directly from 3V3 pin of the WiPytry:import pyb as machine except:# WiPyimport machineimport sys import struct import time import font import chineseclass PCD8544:ADDRESSING_HORIZ = 0x00ADDRESSING_VERT = 0x02INSTR_BASIC = 0x00INSTR_EXT = 0x01POWER_UP = 0x00POWER_DOWN = 0x04DISPLAY_BLANK = 0x08DISPLAY_ALL = 0x09DISPLAY_NORMAL = 0x0cDISPLAY_INVERSE = 0x0dTEMP_COEFF_0 = 0x04TEMP_COEFF_1 = 0x05TEMP_COEFF_2 = 0x06TEMP_COEFF_3 = 0x07BIAS_1_4 = 0x17 # 1/4thBIAS_1_5 = 0x16 # 1/5thBIAS_1_6 = 0x15 # 1/6thBIAS_1_7 = 0x14 # 1/7thBIAS_1_8 = 0x13 # 1/8thBIAS_1_9 = 0x12 # 1/9thBIAS_1_10 = 0x11 # 1/10thBIAS_1_11 = 0x10 # 1/11thdef __init__(self, spi, rst, ce, dc, light, pwr=None):self.width = 84self.height = 48self.power = self.POWER_DOWNself.addressing = self.ADDRESSING_HORIZself.instr = self.INSTR_BASICself.display_mode = self.DISPLAY_BLANKself.temp_coeff = self.TEMP_COEFF_0self.bias = self.BIAS_1_11self.voltage = 3060# init the SPI bus and pinsspi.init(spi.MASTER, baudrate=328125, bits=8, polarity=0, phase=1, firstbit=spi.MSB)if "OUT_PP" in dir(rst):# pyBoard stylerst.init(rst.OUT_PP, rst.PULL_NONE) # Reset linece.init(ce.OUT_PP, ce.PULL_NONE) # Chip Enabledc.init(dc.OUT_PP, dc.PULL_NONE) # Data(1) / Command(0) modelight.init(light.OUT_PP, light.PULL_NONE)if pwr:pwr.init(pwr.OUT_PP, pwr.PULL_NONE)else:# WiPy stylerst.init(rst.OUT, None)ce.init(ce.OUT, None)dc.init(dc.OUT, None)light.init(light.OUT, None)if pwr:pwr.init(pwr.OUT, None)self.spi = spiself.rst = rstself.ce = ceself.dc = dcself.light = lightself.pwr = pwrself.light_off()self.power_on()self.ce.value(1) # set chip to disable (don't listen to input)self.reset()self.set_contrast(0xbf)self.clear()self.lcd_font = font.FONT6_8()self.chinese = chinese.CN_UTF8()def _set_function(self):""" Write current power/addressing/instructionset values to lcd. """value = 0x20 | self.power | self.addressing | self.instrself.command([value])def set_power(self, power, set=True):""" Sets the power mode of the LCD controller """assert power in [self.POWER_UP, self.POWER_DOWN], "Power must be POWER_UP or POWER_DOWN."self.power = powerif set:self._set_function()def set_adressing(self, addr, set=True):""" Sets the adressing mode """assert addr in [self.ADDRESSING_HORIZ, self.ADDRESSING_VERT], "Addressing must be ADDRESSING_HORIZ or ADDRESSING_VERT."self.addressing = addrif set:self._set_function()def set_instr(self, instr, set=True):""" Sets instruction set (basic/extended) """assert instr in [self.INSTR_BASIC, self.INSTR_EXT], "Instr must be INSTR_BASIC or INSTR_EXT."self.instr = instrif set:self._set_function()def set_display(self, display_mode):""" Sets display mode (blank, black, normal, inverse) """assert display_mode in [self.DISPLAY_BLANK, self.DISPLAY_ALL, self.DISPLAY_NORMAL, self.DISPLAY_INVERSE], "Mode must be one of DISPLAY_BLANK, DISPLAY_ALL, DISPLAY_NORMAL or DISPLAY_INVERSE."assert self.instr == self.INSTR_BASIC, "Please switch to basic instruction set first."self.display_mode = display_modeself.command([display_mode])def set_temp_coeff(self, temp_coeff):""" Sets temperature coefficient (0-3) """assert 4 <= temp_coeff < 8, "Temperature coefficient must be one of TEMP_COEFF_0..TEMP_COEFF_3."assert self.instr == self.INSTR_EXT, "Please switch to extended instruction set first."self.temp_coeff = temp_coeffself.command([temp_coeff])def set_bias(self, bias):""" Sets the LCD bias. """assert 0x10 <= bias <= 0x17, "Bias must be one of BIAS_1_4..BIAS_1_11."assert self.instr == self.INSTR_EXT, "Please switch to extended instruction set first."self.bias = biasself.command([bias])def set_voltage(self, millivolts):""" Sets the voltage of the LCD charge pump in millivolts. """assert 3060 <= millivolts <= 10680, "Voltage must be between 3,060 and 10,680 mV."assert self.instr == self.INSTR_EXT, "Please switch to extended instruction set first."self.voltage = millivoltsbasevoltage = millivolts - 3060incrementor = basevoltage // 60code = 0x80 & incrementorself.command([code])def set_contrast(self, value):""" set LCD voltage, i.e. contrast """assert 0x80 <= value <= 0xff, "contrast value must be between 0x80 and 0xff"self.command([0x21, self.TEMP_COEFF_2, self.BIAS_1_7, value, 0x20, self.DISPLAY_NORMAL])# 0x21 - enter extended instruction set (H=1)# 0x06 - set temperature coefficient 2# 0x14 - set BIAS system to n=3 (recomm. mux rate 1:40/1:34)# value - (80-ff) - set Vop (80 = 3.00V, ff = 10.68V), 8b seems to work (0x3b/d70: 3.00+(70*0.06)=7.2V)# 0x20 - back to basic instruction set# 0x0c - normal display modedef position(self, x, y):""" set cursor to bank y, column x """assert 0 <= x < self.width, "x must be between 0 and 83"assert 0 <= y < self.height // 8, "y must be between 0 and 5"assert self.instr == self.INSTR_BASIC, "Please switch to basic instruction set first."self.command([x + 0x80, y + 0x40])def clear(self):""" clear screen """self.position(0, 0)self.data([0] * (self.height * self.width // 8))self.position(0, 0)def sleep_ms(self, mseconds):try:time.sleep_ms(mseconds)except AttributeError:machine.delay(mseconds)def sleep_us(self, useconds):try:time.sleep_us(useconds)except AttributeError:machine.udelay(useconds)def power_on(self):if self.pwr:self.pwr.value(1)self.reset()def reset(self):""" issue reset impulse to reset the display """self.rst.value(0) # RST onself.sleep_us(100) # reset impulse has to be >100 ns and <100 msself.rst.value(1) # RST off# Defaults after reset:self.power = self.POWER_DOWNself.addressing = self.ADDRESSING_HORIZself.instr = self.INSTR_BASICself.display_mode = self.DISPLAY_BLANKself.temp_coeff = self.TEMP_COEFF_0self.bias = self.BIAS_1_11self.voltage = 3060def power_off(self):self.clear()self.command([0x20, 0x08])# 0x20 - basic instruction set# 0x08 - set display to blank (doesn't delete contents)self.sleep_ms(10)if self.pwr:self.pwr.value(0) # turn off powerdef command(self, arr):""" send bytes in command mode """self.bitmap(arr, 0)def data(self, arr):""" send bytes in data mode """self.bitmap(arr, 1)def bitmap(self, arr, dc):self.dc.value(dc)buf = struct.pack('B'*len(arr), *arr)self.ce.value(0) # set chip to listening/enabletry:self.spi.send(buf)except AttributeError: self.spi.write(buf)self.ce.value(1) # set chip to disabledef light_on(self):self.light.value(0) # pull to GNDdef light_off(self):self.light.value(1) # set to HIGHdef lcd_write_string(self, string, x, y):self.position(x,y)for i in string:self.data(self.lcd_font.get_font6_8(i))def lcd_write_chineses(str,x,y,space = 9):# i,j=0,0# lsLen = len(str)# while (j<lsLen)# self.lcd_write_chinese(str[j],x+(i*space),y)# i+=1# j+=1return 0def lcd_write_chinese(self,data,x,y):#獲取 字 的UTF8碼code = 0x00 #賦初值data_code = data.encode("UTF-8")code |= data_code[0]<<16code |= data_code[1]<<8code |= data_code[2]#獲取 字 的UTF8碼 ENDself.position(x,y)self.data(self.chinese.get_chinese_utf8(code,0))self.position(x,y+1)self.data(self.chinese.get_chinese_utf8(code,1))#boot.py
//轉載請注明:@小五義http://www.cnblogs.com/xiaowuyi??QQ群:64770604 # boot.py -- run on boot-up # can run arbitrary Python, but best to keep it minimalimport machine import pyb #pyb.main('main.py') # main script to run after this one #pyb.usb_mode('CDC+MSC') # act as a serial and a storage device #pyb.usb_mode('CDC+HID') # act as a serial device and a mouse#main.py
//轉載請注明:@小五義http://www.cnblogs.com/xiaowuyi??QQ群:64770604 # main.py -- put your code here! import pyb import upcd8544 from machine import SPI,Pindef main():lcd_5110.lcd_write_chinese('蘿',14,0)lcd_5110.lcd_write_chinese('卜',30,0)lcd_5110.lcd_write_chinese('智',46,0)lcd_5110.lcd_write_chinese('能',62,0)pyb.delay(1000)if __name__ == '__main__':SPI = pyb.SPI(1) #DIN=>X8-MOSI/CLK=>X6-SCKRST = pyb.Pin('X1')CE = pyb.Pin('X2')DC = pyb.Pin('X3')LIGHT = pyb.Pin('X4')lcd_5110 = upcd8544.PCD8544(SPI, RST, CE, DC, LIGHT)while(1):main()實現效果如圖:
???
以上提到的程序原代碼及工具下載地址:https://pan.baidu.com/s/1eRZZd1C
總結
以上是生活随笔為你收集整理的[TPYBoard - Micropython之会python就能做硬件 2] 利用micropython控制NOKIA 5110屏的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 51nod 1766
- 下一篇: 你不能错过这些美食!