极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 14831|回复: 1

[TPYBoard-Micropython之会python就能做硬件 2] 利用micropython控制NOKIA 5110屏

[复制链接]
发表于 2017-1-24 22:46:34 | 显示全部楼层 |阅读模式
转载请注明:@小五义 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个引脚,不同的板子可能标注的英文略有不同,具体含义如下:


3、Tpyboard V102连接5110的方法

    这里使用的是SPI连接方法,如果你对什么是SPI连接还不是很清楚,你可以百度一下,这样有利于你从原理上搞明白为什么这么连接。具体针脚对应如下:(由于tpyboard v102有两个spi接口,这里我们选用x1对应接口)


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文件中(该文件可从文章后面的网址下载),如下:
  1. class CN_UTF8:
  2.     """docstring for CN_UTF8"""
  3.     #key:values
  4.     #key: 使用汉字的UTF-8码
  5.     #values: 16*16
  6.     #   [0] 8*16 上半部分
  7.     #   [1] 8*16 下半部分
  8.     UTF8_CHINESE = {
  9.         0xe6aca2:[
  10.                     [0x04,0x24,0x44,0x84,0x64,0x9C,0x40,0x30,0x0F,0xC8,0x08,0x08,0x28,0x18,0x00,0x00],
  11.                     [0x10,0x08,0x06,0x01,0x82,0x4C,0x20,0x18,0x06,0x01,0x06,0x18,0x20,0x40,0x80,0x00]
  12.                 ],#欢
  13.         0xe8bf8e:[
  14.                     [0x40,0x40,0x42,0xCC,0x00,0x00,0xFC,0x04,0x02,0x00,0xFC,0x04,0x04,0xFC,0x00,0x00],
  15.                     [0x00,0x40,0x20,0x1F,0x20,0x40,0x4F,0x44,0x42,0x40,0x7F,0x42,0x44,0x43,0x40,0x00]
  16.                 ],#迎
  17.         0xe4bdbf:[
  18.                     [0x80,0x60,0xF8,0x07,0x04,0xE4,0x24,0x24,0x24,0xFF,0x24,0x24,0x24,0xE4,0x04,0x00],
  19.                     [0x00,0x00,0xFF,0x00,0x80,0x81,0x45,0x29,0x11,0x2F,0x41,0x41,0x81,0x81,0x80,0x00]
  20.                 ],#使
  21.         0xe794a8:[
  22.                     [0x00,0x00,0xFE,0x22,0x22,0x22,0x22,0xFE,0x22,0x22,0x22,0x22,0xFE,0x00,0x00,0x00],
  23.                     [0x80,0x60,0x1F,0x02,0x02,0x02,0x02,0x7F,0x02,0x02,0x42,0x82,0x7F,0x00,0x00,0x00]
  24.                 ],#用
  25.         0xe78eb0:[
  26.                     [0x04,0x84,0x84,0xFC,0x84,0x84,0x00,0xFE,0x02,0x02,0xF2,0x02,0x02,0xFE,0x00,0x00],
  27.                     [0x20,0x60,0x20,0x1F,0x10,0x90,0x40,0x23,0x18,0x06,0x01,0x7E,0x80,0x83,0xE0,0x00]
  28.                 ],#现
  29.         0xe59ca8:[
  30.                     [0x08,0x08,0x88,0xC8,0x38,0x0C,0x0B,0x08,0x08,0xE8,0x08,0x08,0x08,0x08,0x08,0x00],
  31.                     [0x02,0x01,0x00,0xFF,0x40,0x41,0x41,0x41,0x41,0x7F,0x41,0x41,0x41,0x41,0x40,0x00]
  32.                 ],#在
  33.         0xe697b6:[
  34.                     [0x00,0xFC,0x84,0x84,0x84,0xFC,0x00,0x10,0x10,0x10,0x10,0x10,0xFF,0x10,0x10,0x00],
  35.                     [0x00,0x3F,0x10,0x10,0x10,0x3F,0x00,0x00,0x01,0x06,0x40,0x80,0x7F,0x00,0x00,0x00]
  36.                 ],#时
  37.         0xe997b4:[
  38.                     [0x00,0xF8,0x01,0x06,0x00,0xF0,0x12,0x12,0x12,0xF2,0x02,0x02,0x02,0xFE,0x00,0x00],
  39.                     [0x00,0xFF,0x00,0x00,0x00,0x1F,0x11,0x11,0x11,0x1F,0x00,0x40,0x80,0x7F,0x00,0x00]
  40.                 ], #间   
  41.         0xe68891:[
  42.                     [0x20,0x24,0x24,0x24,0xFE,0x23,0x22,0x20,0x20,0xFF,0x20,0x22,0x2C,0xA0,0x20,0x00],
  43.                     [0x00,0x08,0x48,0x84,0x7F,0x02,0x41,0x40,0x20,0x13,0x0C,0x14,0x22,0x41,0xF8,0x00]
  44.                 ], #我
  45.         0xe8909d:[
  46.                     [0x02,0x02,0xF2,0x92,0x97,0xF2,0x92,0x92,0x92,0xF2,0x97,0x92,0xF2,0x02,0x02,0x00],
  47.                     [0x00,0x80,0x88,0x88,0x44,0x4A,0x53,0x22,0x22,0x12,0x0A,0x06,0x00,0x00,0x00,0x00]
  48.                 ], #萝
  49.         0xe58d9c:[
  50.                     [0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x20,0x40,0x80,0x00,0x00,0x00,0x00,0x00],
  51.                     [0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x00]
  52.                 ], #卜
  53.         0xe699ba:[
  54.                     [0x10,0x94,0x53,0x32,0x1E,0x32,0x52,0x10,0x00,0x7E,0x42,0x42,0x42,0x7E,0x00,0x00],
  55.                     [0x00,0x00,0x00,0xFF,0x49,0x49,0x49,0x49,0x49,0x49,0x49,0xFF,0x00,0x00,0x00,0x00]
  56.                 ], #智
  57.         0xe883bd:[
  58.                     [0x08,0xCC,0x4A,0x49,0x48,0x4A,0xCC,0x18,0x00,0x7F,0x88,0x88,0x84,0x82,0xE0,0x00],
  59.                     [0x00,0xFF,0x12,0x12,0x52,0x92,0x7F,0x00,0x00,0x7E,0x88,0x88,0x84,0x82,0xE0,0x00]
  60.                 ],#能
  61.         0xe682a8:[
  62.                     [0x20,0x10,0x08,0xFC,0x23,0x10,0x88,0x67,0x04,0xF4,0x04,0x24,0x54,0x8C,0x00,0x00],
  63.                     [0x40,0x30,0x00,0x77,0x80,0x81,0x88,0xB2,0x84,0x83,0x80,0xE0,0x00,0x11,0x60,0x00]
  64.                 ],#您
  65.         0xe5bd93:[
  66.                     [0x00,0x40,0x42,0x44,0x58,0x40,0x40,0x7F,0x40,0x40,0x50,0x48,0xC6,0x00,0x00,0x00],
  67.                     [0x00,0x40,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0xFF,0x00,0x00,0x00]
  68.                 ],#当
  69.         0xE5898D:[
  70.                     [0x08,0x08,0xE8,0x29,0x2E,0x28,0xE8,0x08,0x08,0xC8,0x0C,0x0B,0xE8,0x08,0x08,0x00],
  71.                     [0x00,0x00,0xFF,0x09,0x49,0x89,0x7F,0x00,0x00,0x0F,0x40,0x80,0x7F,0x00,0x00,0x00]
  72.                 ]#前
  73.    
  74.                  
  75.          

  76.     }
  77.     #key 汉字的UTF-8码
  78.     #isBottom 确定这次是获取 某个字的 上半部分(0) 还是下半部分(1)
  79.     def get_chinese_utf8(self, key,isBottom = 0):
  80.         values = self.UTF8_CHINESE[key]
  81.         return values[isBottom]
复制代码


三、显示“萝卜智能”的小实例
    按照上面连接后,将tpyboard v102插入电脑usb,在TPFLASH内建立以下文件(可从文章后地址下载):

#font.py
  1. class FONT6_8:
  2.     """docstring for FONT6_8"""
  3.     FONTTYPE6_8 = [
  4.         [0x00, 0x00, 0x00, 0x00, 0x00, 0x00] # 20
  5.         ,[0x00, 0x00, 0x00, 0x5f, 0x00, 0x00] # 21 !
  6.         ,[0x00, 0x00, 0x07, 0x00, 0x07, 0x00] # 22 "
  7.         ,[0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14] # 23 #
  8.         ,[0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12] # 24 $
  9.         ,[0x00, 0x23, 0x13, 0x08, 0x64, 0x62] # 25 %
  10.         ,[0x00, 0x36, 0x49, 0x55, 0x22, 0x50] # 26 &
  11.         ,[0x00, 0x00, 0x05, 0x03, 0x00, 0x00] # 27 '
  12.         ,[0x00, 0x00, 0x1c, 0x22, 0x41, 0x00] # 28 (
  13.         ,[0x00, 0x00, 0x41, 0x22, 0x1c, 0x00] # 29 )
  14.         ,[0x00, 0x14, 0x08, 0x3e, 0x08, 0x14] # 2a *
  15.         ,[0x00, 0x08, 0x08, 0x3e, 0x08, 0x08] # 2b +
  16.         ,[0x00, 0x00, 0x50, 0x30, 0x00, 0x00] # 2c ,
  17.         ,[0x00, 0x08, 0x08, 0x08, 0x08, 0x08] # 2d -
  18.         ,[0x00, 0x00, 0x60, 0x60, 0x00, 0x00] # 2e .
  19.         ,[0x00, 0x20, 0x10, 0x08, 0x04, 0x02] # 2f /
  20.         ,[0x00, 0x3e, 0x51, 0x49, 0x45, 0x3e] # 30 0
  21.         ,[0x00, 0x00, 0x42, 0x7f, 0x40, 0x00] # 31 1
  22.         ,[0x00, 0x42, 0x61, 0x51, 0x49, 0x46] # 32 2
  23.         ,[0x00, 0x21, 0x41, 0x45, 0x4b, 0x31] # 33 3
  24.         ,[0x00, 0x18, 0x14, 0x12, 0x7f, 0x10] # 34 4
  25.         ,[0x00, 0x27, 0x45, 0x45, 0x45, 0x39] # 35 5
  26.         ,[0x00, 0x3c, 0x4a, 0x49, 0x49, 0x30] # 36 6
  27.         ,[0x00, 0x01, 0x71, 0x09, 0x05, 0x03] # 37 7
  28.         ,[0x00, 0x36, 0x49, 0x49, 0x49, 0x36] # 38 8
  29.         ,[0x00, 0x06, 0x49, 0x49, 0x29, 0x1e] # 39 9
  30.         ,[0x00, 0x00, 0x36, 0x36, 0x00, 0x00] # 3a :
  31.         ,[0x00, 0x00, 0x56, 0x36, 0x00, 0x00] # 3b ;
  32.         ,[0x00, 0x08, 0x14, 0x22, 0x41, 0x00] # 3c <
  33.         ,[0x00, 0x14, 0x14, 0x14, 0x14, 0x14] # 3d =
  34.         ,[0x00, 0x00, 0x41, 0x22, 0x14, 0x08] # 3e >
  35.         ,[0x00, 0x02, 0x01, 0x51, 0x09, 0x06] # 3f ?
  36.         ,[0x00, 0x32, 0x49, 0x79, 0x41, 0x3e] # 40 @
  37.         ,[0x00, 0x7e, 0x11, 0x11, 0x11, 0x7e] # 41 A
  38.         ,[0x00, 0x7f, 0x49, 0x49, 0x49, 0x36] # 42 B
  39.         ,[0x00, 0x3e, 0x41, 0x41, 0x41, 0x22] # 43 C
  40.         ,[0x00, 0x7f, 0x41, 0x41, 0x22, 0x1c] # 44 D
  41.         ,[0x00, 0x7f, 0x49, 0x49, 0x49, 0x41] # 45 E
  42.         ,[0x00, 0x7f, 0x09, 0x09, 0x09, 0x01] # 46 F
  43.         ,[0x00, 0x3e, 0x41, 0x49, 0x49, 0x7a] # 47 G
  44.         ,[0x00, 0x7f, 0x08, 0x08, 0x08, 0x7f] # 48 H
  45.         ,[0x00, 0x00, 0x41, 0x7f, 0x41, 0x00] # 49 I
  46.         ,[0x00, 0x20, 0x40, 0x41, 0x3f, 0x01] # 4a J
  47.         ,[0x00, 0x7f, 0x08, 0x14, 0x22, 0x41] # 4b K
  48.         ,[0x00, 0x7f, 0x40, 0x40, 0x40, 0x40] # 4c L
  49.         ,[0x00, 0x7f, 0x02, 0x0c, 0x02, 0x7f] # 4d M
  50.         ,[0x00, 0x7f, 0x04, 0x08, 0x10, 0x7f] # 4e N
  51.         ,[0x00, 0x3e, 0x41, 0x41, 0x41, 0x3e] # 4f O
  52.         ,[0x00, 0x7f, 0x09, 0x09, 0x09, 0x06] # 50 P
  53.         ,[0x00, 0x3e, 0x41, 0x51, 0x21, 0x5e] # 51 Q
  54.         ,[0x00, 0x7f, 0x09, 0x19, 0x29, 0x46] # 52 R
  55.         ,[0x00, 0x46, 0x49, 0x49, 0x49, 0x31] # 53 S
  56.         ,[0x00, 0x01, 0x01, 0x7f, 0x01, 0x01] # 54 T
  57.         ,[0x00, 0x3f, 0x40, 0x40, 0x40, 0x3f] # 55 U
  58.         ,[0x00, 0x1f, 0x20, 0x40, 0x20, 0x1f] # 56 V
  59.         ,[0x00, 0x3f, 0x40, 0x38, 0x40, 0x3f] # 57 W
  60.         ,[0x00, 0x63, 0x14, 0x08, 0x14, 0x63] # 58 X
  61.         ,[0x00, 0x07, 0x08, 0x70, 0x08, 0x07] # 59 Y
  62.         ,[0x00, 0x61, 0x51, 0x49, 0x45, 0x43] # 5a Z
  63.         ,[0x00, 0x00, 0x7f, 0x41, 0x41, 0x00] # 5b [
  64.         ,[0x00, 0x02, 0x04, 0x08, 0x10, 0x20] # 5c \
  65.         ,[0x00, 0x00, 0x41, 0x41, 0x7f, 0x00] # 5d ]
  66.         ,[0x00, 0x04, 0x02, 0x01, 0x02, 0x04] # 5e ^
  67.         ,[0x00, 0x40, 0x40, 0x40, 0x40, 0x40] # 5f _
  68.         ,[0x00, 0x00, 0x01, 0x02, 0x04, 0x00] # 60 `
  69.         ,[0x00, 0x20, 0x54, 0x54, 0x54, 0x78] # 61 a
  70.         ,[0x00, 0x7f, 0x48, 0x44, 0x44, 0x38] # 62 b
  71.         ,[0x00, 0x38, 0x44, 0x44, 0x44, 0x20] # 63 c
  72.         ,[0x00, 0x38, 0x44, 0x44, 0x48, 0x7f] # 64 d
  73.         ,[0x00, 0x38, 0x54, 0x54, 0x54, 0x18] # 65 e
  74.         ,[0x00, 0x08, 0x7e, 0x09, 0x01, 0x02] # 66 f
  75.         ,[0x00, 0x0c, 0x52, 0x52, 0x52, 0x3e] # 67 g
  76.         ,[0x00, 0x7f, 0x08, 0x04, 0x04, 0x78] # 68 h
  77.         ,[0x00, 0x00, 0x44, 0x7d, 0x40, 0x00] # 69 i
  78.         ,[0x00, 0x20, 0x40, 0x44, 0x3d, 0x00] # 6a j
  79.         ,[0x00, 0x7f, 0x10, 0x28, 0x44, 0x00] # 6b k
  80.         ,[0x00, 0x00, 0x41, 0x7f, 0x40, 0x00] # 6c l
  81.         ,[0x00, 0x7c, 0x04, 0x18, 0x04, 0x78] # 6d m
  82.         ,[0x00, 0x7c, 0x08, 0x04, 0x04, 0x78] # 6e n
  83.         ,[0x00, 0x38, 0x44, 0x44, 0x44, 0x38] # 6f o
  84.         ,[0x00, 0x7c, 0x14, 0x14, 0x14, 0x08] # 70 p
  85.         ,[0x00, 0x08, 0x14, 0x14, 0x18, 0x7c] # 71 q
  86.         ,[0x00, 0x7c, 0x08, 0x04, 0x04, 0x08] # 72 r
  87.         ,[0x00, 0x48, 0x54, 0x54, 0x54, 0x20] # 73 s
  88.         ,[0x00, 0x04, 0x3f, 0x44, 0x40, 0x20] # 74 t
  89.         ,[0x00, 0x3c, 0x40, 0x40, 0x20, 0x7c] # 75 u
  90.         ,[0x00, 0x1c, 0x20, 0x40, 0x20, 0x1c] # 76 v
  91.         ,[0x00, 0x3c, 0x40, 0x30, 0x40, 0x3c] # 77 w
  92.         ,[0x00, 0x44, 0x28, 0x10, 0x28, 0x44] # 78 x
  93.         ,[0x00, 0x0c, 0x50, 0x50, 0x50, 0x3c] # 79 y
  94.         ,[0x00, 0x44, 0x64, 0x54, 0x4c, 0x44] # 7a z
  95.         ,[0x00, 0x00, 0x08, 0x36, 0x41, 0x00] # 7b [
  96.         ,[0x00, 0x00, 0x00, 0x7f, 0x00, 0x00] # 7c |
  97.         ,[0x00, 0x00, 0x41, 0x36, 0x08, 0x00] # 7d ]
  98.         ,[0x00, 0x10, 0x08, 0x08, 0x10, 0x08] # 7e ~
  99.         ,[0x00, 0x78, 0x46, 0x41, 0x46, 0x78] # 7f (delete)
  100.     ]
  101.      

  102.     def get_font6_8(self, data):
  103.         return self.FONTTYPE6_8[bytearray(data)[0] - 0x20]
复制代码


#chinese.py

//转载请注明:@小五义http://www.cnblogs.com/xiaowuyi  QQ群:64770604
  1. class CN_UTF8:
  2.     """docstring for CN_UTF8"""
  3.     #key:values
  4.     #key: 使用汉字的UTF-8码
  5.     #values: 16*16
  6.     #   [0] 8*16 上半部分
  7.     #   [1] 8*16 下半部分
  8.     UTF8_CHINESE = {
  9.         0xe6aca2:[
  10.                     [0x04,0x24,0x44,0x84,0x64,0x9C,0x40,0x30,0x0F,0xC8,0x08,0x08,0x28,0x18,0x00,0x00],
  11.                     [0x10,0x08,0x06,0x01,0x82,0x4C,0x20,0x18,0x06,0x01,0x06,0x18,0x20,0x40,0x80,0x00]
  12.                 ],#欢
  13.         0xe8bf8e:[
  14.                     [0x40,0x40,0x42,0xCC,0x00,0x00,0xFC,0x04,0x02,0x00,0xFC,0x04,0x04,0xFC,0x00,0x00],
  15.                     [0x00,0x40,0x20,0x1F,0x20,0x40,0x4F,0x44,0x42,0x40,0x7F,0x42,0x44,0x43,0x40,0x00]
  16.                 ],#迎
  17.         0xe4bdbf:[
  18.                     [0x80,0x60,0xF8,0x07,0x04,0xE4,0x24,0x24,0x24,0xFF,0x24,0x24,0x24,0xE4,0x04,0x00],
  19.                     [0x00,0x00,0xFF,0x00,0x80,0x81,0x45,0x29,0x11,0x2F,0x41,0x41,0x81,0x81,0x80,0x00]
  20.                 ],#使
  21.         0xe794a8:[
  22.                     [0x00,0x00,0xFE,0x22,0x22,0x22,0x22,0xFE,0x22,0x22,0x22,0x22,0xFE,0x00,0x00,0x00],
  23.                     [0x80,0x60,0x1F,0x02,0x02,0x02,0x02,0x7F,0x02,0x02,0x42,0x82,0x7F,0x00,0x00,0x00]
  24.                 ],#用
  25.         0xe78eb0:[
  26.                     [0x04,0x84,0x84,0xFC,0x84,0x84,0x00,0xFE,0x02,0x02,0xF2,0x02,0x02,0xFE,0x00,0x00],
  27.                     [0x20,0x60,0x20,0x1F,0x10,0x90,0x40,0x23,0x18,0x06,0x01,0x7E,0x80,0x83,0xE0,0x00]
  28.                 ],#现
  29.         0xe59ca8:[
  30.                     [0x08,0x08,0x88,0xC8,0x38,0x0C,0x0B,0x08,0x08,0xE8,0x08,0x08,0x08,0x08,0x08,0x00],
  31.                     [0x02,0x01,0x00,0xFF,0x40,0x41,0x41,0x41,0x41,0x7F,0x41,0x41,0x41,0x41,0x40,0x00]
  32.                 ],#在
  33.         0xe697b6:[
  34.                     [0x00,0xFC,0x84,0x84,0x84,0xFC,0x00,0x10,0x10,0x10,0x10,0x10,0xFF,0x10,0x10,0x00],
  35.                     [0x00,0x3F,0x10,0x10,0x10,0x3F,0x00,0x00,0x01,0x06,0x40,0x80,0x7F,0x00,0x00,0x00]
  36.                 ],#时
  37.         0xe997b4:[
  38.                     [0x00,0xF8,0x01,0x06,0x00,0xF0,0x12,0x12,0x12,0xF2,0x02,0x02,0x02,0xFE,0x00,0x00],
  39.                     [0x00,0xFF,0x00,0x00,0x00,0x1F,0x11,0x11,0x11,0x1F,0x00,0x40,0x80,0x7F,0x00,0x00]
  40.                 ], #间   
  41.         0xe68891:[
  42.                     [0x20,0x24,0x24,0x24,0xFE,0x23,0x22,0x20,0x20,0xFF,0x20,0x22,0x2C,0xA0,0x20,0x00],
  43.                     [0x00,0x08,0x48,0x84,0x7F,0x02,0x41,0x40,0x20,0x13,0x0C,0x14,0x22,0x41,0xF8,0x00]
  44.                 ], #我
  45.         0xe8909d:[
  46.                     [0x02,0x02,0xF2,0x92,0x97,0xF2,0x92,0x92,0x92,0xF2,0x97,0x92,0xF2,0x02,0x02,0x00],
  47.                     [0x00,0x80,0x88,0x88,0x44,0x4A,0x53,0x22,0x22,0x12,0x0A,0x06,0x00,0x00,0x00,0x00]
  48.                 ], #萝
  49.         0xe58d9c:[
  50.                     [0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x20,0x40,0x80,0x00,0x00,0x00,0x00,0x00],
  51.                     [0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x00]
  52.                 ], #卜
  53.         0xe699ba:[
  54.                     [0x10,0x94,0x53,0x32,0x1E,0x32,0x52,0x10,0x00,0x7E,0x42,0x42,0x42,0x7E,0x00,0x00],
  55.                     [0x00,0x00,0x00,0xFF,0x49,0x49,0x49,0x49,0x49,0x49,0x49,0xFF,0x00,0x00,0x00,0x00]
  56.                 ], #智
  57.         0xe883bd:[
  58.                     [0x08,0xCC,0x4A,0x49,0x48,0x4A,0xCC,0x18,0x00,0x7F,0x88,0x88,0x84,0x82,0xE0,0x00],
  59.                     [0x00,0xFF,0x12,0x12,0x52,0x92,0x7F,0x00,0x00,0x7E,0x88,0x88,0x84,0x82,0xE0,0x00]
  60.                 ],#能
  61.         0xe682a8:[
  62.                     [0x20,0x10,0x08,0xFC,0x23,0x10,0x88,0x67,0x04,0xF4,0x04,0x24,0x54,0x8C,0x00,0x00],
  63.                     [0x40,0x30,0x00,0x77,0x80,0x81,0x88,0xB2,0x84,0x83,0x80,0xE0,0x00,0x11,0x60,0x00]
  64.                 ],#您
  65.         0xe5bd93:[
  66.                     [0x00,0x40,0x42,0x44,0x58,0x40,0x40,0x7F,0x40,0x40,0x50,0x48,0xC6,0x00,0x00,0x00],
  67.                     [0x00,0x40,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0xFF,0x00,0x00,0x00]
  68.                 ],#当
  69.         0xE5898D:[
  70.                     [0x08,0x08,0xE8,0x29,0x2E,0x28,0xE8,0x08,0x08,0xC8,0x0C,0x0B,0xE8,0x08,0x08,0x00],
  71.                     [0x00,0x00,0xFF,0x09,0x49,0x89,0x7F,0x00,0x00,0x0F,0x40,0x80,0x7F,0x00,0x00,0x00]
  72.                 ]#前
  73.    
  74.                  
  75.          

  76.     }
  77.     #key 汉字的UTF-8码
  78.     #isBottom 确定这次是获取 某个字的 上半部分(0) 还是下半部分(1)
  79.     def get_chinese_utf8(self, key,isBottom = 0):
  80.         values = self.UTF8_CHINESE[key]
  81.         return values[isBottom]
复制代码


#upcd8544.py

//转载请注明:@小五义http://www.cnblogs.com/xiaowuyi  QQ群:64770604
  1. # -*- coding: utf-8 -*-
  2. """
  3. MicroPython PCD8544 driver
  4. (for Nokia 5110 displays)
  5. """

  6. __author__     = "Markus Birth"
  7. __copyright__  = "Copyright 2015, Markus Birth"
  8. __credits__    = ["Markus Birth"]
  9. __license__    = "MIT"
  10. __version__    = "1.0"
  11. __maintainer__ = "Markus Birth"
  12. __email__      = "[email protected]"
  13. __status__     = "Production"

  14. # Datasheet: https://www.sparkfun.com/datasheets/LCD/Monochrome/Nokia5110.pdf
  15. # Inspiration from:
  16. #   - https://github.com/inaugurator/upyd5110
  17. #   - https://github.com/rm-hull/pcd8544/blob/master/src/lcd.py
  18. #
  19. # PINOUT
  20. # WiPy/pyBoard      display   function
  21. #
  22. # 3V3 or any Pin => VCC       3.3V logic voltage (0=off, 1=on)
  23. # MOSI           => DIN       data flow (Master out, Slave in)
  24. # SCK            => CLK       SPI clock
  25. # any Pin        => RST       Reset pin (0=reset, 1=normal)
  26. # any Pin        => CE        Chip Enable (0=listen for input, 1=ignore input)
  27. # any Pin        => DC        Data/Command (0=commands, 1=data)
  28. # any Pin        => LIGHT     Light (0=on, 1=off)
  29. # GND            => GND
  30. #
  31. # pyBoard "Y" side
  32. # SPI    = pyb.SPI(1)
  33. # RST    = pyb.Pin('Y4')
  34. # CE     = pyb.Pin('Y5')
  35. # DC     = pyb.Pin('Y3')
  36. # LIGHT  = pyb.Pin('Y2')
  37. # PWR    = pyb.Pin('Y1')
  38. #
  39. # pyBoard "X" side
  40. # SPI    = pyb.SPI(2)
  41. # RST    = pyb.Pin('X4')
  42. # CE     = pyb.Pin('X5')
  43. # DC     = pyb.Pin('X3')
  44. # LIGHT  = pyb.Pin('X2')
  45. # PWR    = pyb.Pin('X1')
  46. #
  47. # WiPy (on Exp board, SD and User-LED jumper have to be removed!)
  48. # SPI    = machine.SPI(0)   # GP14 (CLK) + GP16 (MOSI->DIN), User-LED jumper removed!
  49. # RST    = machine.Pin('GP24')
  50. # CE     = machine.Pin('GP12')
  51. # DC     = machine.Pin('GP22')
  52. # LIGHT  = machine.Pin('GP23')
  53. # PWR    = directly from 3V3 pin of the WiPy

  54. try:
  55.     import pyb as machine
  56. except:
  57.     # WiPy
  58.     import machine
  59.      
  60. import sys
  61. import struct
  62. import time
  63. import font
  64. import chinese

  65. class PCD8544:
  66.     ADDRESSING_HORIZ = 0x00
  67.     ADDRESSING_VERT  = 0x02
  68.     INSTR_BASIC = 0x00
  69.     INSTR_EXT   = 0x01
  70.     POWER_UP   = 0x00
  71.     POWER_DOWN = 0x04
  72.     DISPLAY_BLANK   = 0x08
  73.     DISPLAY_ALL     = 0x09
  74.     DISPLAY_NORMAL  = 0x0c
  75.     DISPLAY_INVERSE = 0x0d
  76.     TEMP_COEFF_0 = 0x04
  77.     TEMP_COEFF_1 = 0x05
  78.     TEMP_COEFF_2 = 0x06
  79.     TEMP_COEFF_3 = 0x07
  80.     BIAS_1_4  = 0x17   # 1/4th
  81.     BIAS_1_5  = 0x16   # 1/5th
  82.     BIAS_1_6  = 0x15   # 1/6th
  83.     BIAS_1_7  = 0x14   # 1/7th
  84.     BIAS_1_8  = 0x13   # 1/8th
  85.     BIAS_1_9  = 0x12   # 1/9th
  86.     BIAS_1_10 = 0x11   # 1/10th
  87.     BIAS_1_11 = 0x10   # 1/11th

  88.     def __init__(self, spi, rst, ce, dc, light, pwr=None):
  89.         self.width  = 84
  90.         self.height = 48
  91.         self.power      = self.POWER_DOWN
  92.         self.addressing = self.ADDRESSING_HORIZ
  93.         self.instr      = self.INSTR_BASIC
  94.         self.display_mode = self.DISPLAY_BLANK
  95.         self.temp_coeff = self.TEMP_COEFF_0
  96.         self.bias       = self.BIAS_1_11
  97.         self.voltage    = 3060

  98.         # init the SPI bus and pins
  99.         spi.init(spi.MASTER, baudrate=328125, bits=8, polarity=0, phase=1, firstbit=spi.MSB)
  100.         if "OUT_PP" in dir(rst):
  101.             # pyBoard style
  102.             rst.init(rst.OUT_PP, rst.PULL_NONE)  # Reset line
  103.             ce.init(ce.OUT_PP, ce.PULL_NONE)     # Chip Enable
  104.             dc.init(dc.OUT_PP, dc.PULL_NONE)     # Data(1) / Command(0) mode
  105.             light.init(light.OUT_PP, light.PULL_NONE)
  106.             if pwr:
  107.                 pwr.init(pwr.OUT_PP, pwr.PULL_NONE)
  108.         else:
  109.             # WiPy style
  110.             rst.init(rst.OUT, None)
  111.             ce.init(ce.OUT, None)
  112.             dc.init(dc.OUT, None)
  113.             light.init(light.OUT, None)
  114.             if pwr:
  115.                 pwr.init(pwr.OUT, None)

  116.         self.spi   = spi
  117.         self.rst   = rst
  118.         self.ce    = ce
  119.         self.dc    = dc
  120.         self.light = light
  121.         self.pwr   = pwr

  122.         self.light_off()
  123.         self.power_on()
  124.         self.ce.value(1)  # set chip to disable (don't listen to input)
  125.         self.reset()
  126.         self.set_contrast(0xbf)
  127.         self.clear()
  128.         self.lcd_font = font.FONT6_8()
  129.         self.chinese = chinese.CN_UTF8()


  130.     def _set_function(self):
  131.         """ Write current power/addressing/instructionset values to lcd. """
  132.         value = 0x20 | self.power | self.addressing | self.instr
  133.         self.command([value])

  134.     def set_power(self, power, set=True):
  135.         """ Sets the power mode of the LCD controller """
  136.         assert power in [self.POWER_UP, self.POWER_DOWN], "Power must be POWER_UP or POWER_DOWN."
  137.         self.power = power
  138.         if set:
  139.             self._set_function()

  140.     def set_adressing(self, addr, set=True):
  141.         """ Sets the adressing mode """
  142.         assert addr in [self.ADDRESSING_HORIZ, self.ADDRESSING_VERT], "Addressing must be ADDRESSING_HORIZ or ADDRESSING_VERT."
  143.         self.addressing = addr
  144.         if set:
  145.             self._set_function()

  146.     def set_instr(self, instr, set=True):
  147.         """ Sets instruction set (basic/extended) """
  148.         assert instr in [self.INSTR_BASIC, self.INSTR_EXT], "Instr must be INSTR_BASIC or INSTR_EXT."
  149.         self.instr = instr
  150.         if set:
  151.             self._set_function()

  152.     def set_display(self, display_mode):
  153.         """ Sets display mode (blank, black, normal, inverse) """
  154.         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."
  155.         assert self.instr == self.INSTR_BASIC, "Please switch to basic instruction set first."
  156.         self.display_mode = display_mode
  157.         self.command([display_mode])

  158.     def set_temp_coeff(self, temp_coeff):
  159.         """ Sets temperature coefficient (0-3) """
  160.         assert 4 <= temp_coeff < 8, "Temperature coefficient must be one of TEMP_COEFF_0..TEMP_COEFF_3."
  161.         assert self.instr == self.INSTR_EXT, "Please switch to extended instruction set first."
  162.         self.temp_coeff = temp_coeff
  163.         self.command([temp_coeff])

  164.     def set_bias(self, bias):
  165.         """ Sets the LCD bias. """
  166.         assert 0x10 <= bias <= 0x17, "Bias must be one of BIAS_1_4..BIAS_1_11."
  167.         assert self.instr == self.INSTR_EXT, "Please switch to extended instruction set first."
  168.         self.bias = bias
  169.         self.command([bias])

  170.     def set_voltage(self, millivolts):
  171.         """ Sets the voltage of the LCD charge pump in millivolts. """
  172.         assert 3060 <= millivolts <= 10680, "Voltage must be between 3,060 and 10,680 mV."
  173.         assert self.instr == self.INSTR_EXT, "Please switch to extended instruction set first."
  174.         self.voltage = millivolts
  175.         basevoltage = millivolts - 3060
  176.         incrementor = basevoltage // 60
  177.         code = 0x80 & incrementor
  178.         self.command([code])

  179.     def set_contrast(self, value):
  180.         """ set LCD voltage, i.e. contrast """
  181.         assert 0x80 <= value <= 0xff, "contrast value must be between 0x80 and 0xff"
  182.         self.command([0x21, self.TEMP_COEFF_2, self.BIAS_1_7, value, 0x20, self.DISPLAY_NORMAL])
  183.         # 0x21 - enter extended instruction set (H=1)
  184.         # 0x06 - set temperature coefficient 2
  185.         # 0x14 - set BIAS system to n=3 (recomm. mux rate 1:40/1:34)
  186.         # value - (80-ff) - set Vop (80 = 3.00V, ff = 10.68V), 8b seems to work (0x3b/d70: 3.00+(70*0.06)=7.2V)
  187.         # 0x20 - back to basic instruction set
  188.         # 0x0c - normal display mode

  189.     def position(self, x, y):
  190.         """ set cursor to bank y, column x """
  191.         assert 0 <= x < self.width, "x must be between 0 and 83"
  192.         assert 0 <= y < self.height // 8, "y must be between 0 and 5"
  193.         assert self.instr == self.INSTR_BASIC, "Please switch to basic instruction set first."
  194.         self.command([x + 0x80, y + 0x40])

  195.     def clear(self):
  196.         """ clear screen """
  197.         self.position(0, 0)
  198.         self.data([0] * (self.height * self.width // 8))
  199.         self.position(0, 0)

  200.     def sleep_ms(self, mseconds):
  201.         try:
  202.             time.sleep_ms(mseconds)
  203.         except AttributeError:
  204.             machine.delay(mseconds)

  205.     def sleep_us(self, useconds):
  206.         try:
  207.             time.sleep_us(useconds)
  208.         except AttributeError:
  209.             machine.udelay(useconds)

  210.     def power_on(self):
  211.         if self.pwr:
  212.             self.pwr.value(1)
  213.         self.reset()

  214.     def reset(self):
  215.         """ issue reset impulse to reset the display """
  216.         self.rst.value(0)  # RST on
  217.         self.sleep_us(100) # reset impulse has to be >100 ns and <100 ms
  218.         self.rst.value(1)  # RST off
  219.         # Defaults after reset:
  220.         self.power      = self.POWER_DOWN
  221.         self.addressing = self.ADDRESSING_HORIZ
  222.         self.instr      = self.INSTR_BASIC
  223.         self.display_mode = self.DISPLAY_BLANK
  224.         self.temp_coeff = self.TEMP_COEFF_0
  225.         self.bias       = self.BIAS_1_11
  226.         self.voltage    = 3060

  227.     def power_off(self):
  228.         self.clear()
  229.         self.command([0x20, 0x08])
  230.         # 0x20 - basic instruction set
  231.         # 0x08 - set display to blank (doesn't delete contents)
  232.         self.sleep_ms(10)
  233.         if self.pwr:
  234.             self.pwr.value(0) # turn off power

  235.     def command(self, arr):
  236.         """ send bytes in command mode """
  237.         self.bitmap(arr, 0)

  238.     def data(self, arr):
  239.         """ send bytes in data mode """
  240.         self.bitmap(arr, 1)

  241.     def bitmap(self, arr, dc):
  242.         self.dc.value(dc)
  243.         buf = struct.pack('B'*len(arr), *arr)
  244.         self.ce.value(0) # set chip to listening/enable
  245.         try:
  246.             self.spi.send(buf)
  247.         except AttributeError:
  248.             self.spi.write(buf)
  249.         self.ce.value(1) # set chip to disable

  250.     def light_on(self):
  251.         self.light.value(0)  # pull to GND

  252.     def light_off(self):
  253.         self.light.value(1)  # set to HIGH

  254.     def lcd_write_string(self, string, x, y):
  255.         self.position(x,y)
  256.         for i in string:
  257.             self.data(self.lcd_font.get_font6_8(i))
  258.      
  259.     def lcd_write_chineses(str,x,y,space = 9):
  260.         # i,j=0,0
  261.         # lsLen = len(str)
  262.         # while (j<lsLen)
  263.             # self.lcd_write_chinese(str[j],x+(i*space),y)
  264.             # i+=1
  265.             # j+=1
  266.         return 0
  267.      
  268.     def lcd_write_chinese(self,data,x,y):
  269.         #获取 字 的UTF8码
  270.         code = 0x00 #赋初值
  271.         data_code = data.encode("UTF-8")
  272.         code |= data_code[0]<<16
  273.         code |= data_code[1]<<8
  274.         code |= data_code[2]
  275.         #获取 字 的UTF8码 END
  276.         self.position(x,y)
  277.         self.data(self.chinese.get_chinese_utf8(code,0))
  278.         self.position(x,y+1)
  279.         self.data(self.chinese.get_chinese_utf8(code,1))
复制代码


#boot.py

//转载请注明:@小五义http://www.cnblogs.com/xiaowuyi  QQ群:64770604
  1. # boot.py -- run on boot-up
  2. # can run arbitrary Python, but best to keep it minimal

  3. import machine
  4. import pyb
  5. #pyb.main('main.py') # main script to run after this one
  6. #pyb.usb_mode('CDC+MSC') # act as a serial and a storage device
  7. #pyb.usb_mode('CDC+HID') # act as a serial device and a mouse
复制代码


#main.py
  1. # main.py -- put your code here!
  2. import pyb
  3. import upcd8544
  4. from machine import SPI,Pin

  5. def main():
  6.     lcd_5110.lcd_write_chinese('萝',14,0)
  7.     lcd_5110.lcd_write_chinese('卜',30,0)
  8.     lcd_5110.lcd_write_chinese('智',46,0)
  9.     lcd_5110.lcd_write_chinese('能',62,0)


  10.     pyb.delay(1000)

  11. if __name__ == '__main__':

  12.     SPI = pyb.SPI(1) #DIN=>X8-MOSI/CLK=>X6-SCK

  13.     RST    = pyb.Pin('X1')
  14.     CE     = pyb.Pin('X2')
  15.     DC     = pyb.Pin('X3')
  16.     LIGHT  = pyb.Pin('X4')
  17.     lcd_5110 = upcd8544.PCD8544(SPI, RST, CE, DC, LIGHT)
  18.     while(1):
  19.      main()
复制代码


实现效果如图:


以上提到的程序原代码及工具下载地址:https://pan.baidu.com/s/1eRZZd1C

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2017-1-25 09:22:01 | 显示全部楼层
楼主辛苦谢谢分享!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 07:21 , Processed in 0.042548 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表