(二十八)树莓派3B+ Node.js硬件SPI驱动Nokia5110液晶

前言

在以前用wiringPi写过驱动Nokia液晶的例子《(十六)树莓派3B+ wiringPi库的使用–硬件SPI驱动NOKIA5110液晶》。现在使用的是Node.js驱动这个显示屏。

简介

Nokia5110液晶屏现在买到的都是之前Nokia5110手机的拆机屏幕,液晶驱动芯片是PCD8544。
在这里插入图片描述

1.液晶屏原理图

在这里插入图片描述
结合实物看,这个液晶屏的对外接口都是很简单的。

2.PCD8544芯片

特征

  • 单芯片 LCD 控制/驱动
  • 48 行,84 列输出
  • 显示数据 RAM 48*84 位
  • 芯片集成:
    ——LCD 电压发生器(也可以使用外部电压供应)
    ——LCD偏置电压发生器
    ——振荡器不需要外接元件(也可以使用外部时钟)
  • 外部 RES(复位)输入引脚
  • 串行界面最高 4.0Mbits/S
  • CMOS 兼容输入
  • 混合速率:48
  • 逻辑电压范围 VDD 到 VSS:2.7V~3.3V显示电压范围 VLCD 到 VSS:
    ——6.0~8.5V LCD内部电压发生器(充许电压发生器)
    ——6.0~9.0V LCD 外部电压供应(电压发生器关闭)
  • 低功耗,适用于电池供电系统
  • 关于 VLCD 的温度补偿
  • 使用温度范围:-25~70°C

概述

PCD8544 是一块低功耗的 CMOS LCD 控制驱动器,设计为驱动
48 行 84 列的图形显示。所有必须的显示功能集成在一块芯片上,包 括 LCD 电压及偏置电压发生器,只需很少外部元件且功耗小。
PCD8544 与微控制器的接口使用串行总线。
在这里插入图片描述
关于芯片的更多信息,大家可以去看芯片的数据手册。

驱动显示

这里的驱动显示使用的rpio这个库,之前也有使用的。7.树莓派3B+点亮第一个LED

let rpio = require('rpio');
let font = require('./font');
let os = require("os");

// 定义NOKIA5110 屏幕的引脚和树莓派GPIO的连接关系
const Nokia5110_pins = {
  RST: 40,    // 复位引脚  低电平有效
  CE:  24,   // 片选引脚  低电平有效
  DC:  38,   // 数据/指令选择  1:写数据 0:写命令
  DIN: 19,   // 串行数据线
  CLK: 23,   // 串行时钟线
}

// 寻址 
const NOKIA5110_address = {
  x_col_addr: 0x80,  // 定位列 地址 0-83
  y_row_addr: 0x40   // 定位行 地址 0-5
}
// 端口初始化
function gpioInit(){
  rpio.init({gpiomem: false});
  rpio.open(Nokia5110_pins.RST, rpio.OUTPUT); // 设置为输出 初始为低电平
  rpio.open(Nokia5110_pins.CE, rpio.OUTPUT); // 设置为输出 初始为低电平
  rpio.open(Nokia5110_pins.DC, rpio.OUTPUT); // 设置为输出 初始为低电平
}
// SPI初始化
function spiInit(){
  gpioInit();
  rpio.spiBegin();
  rpio.spiChipSelect(0);              /* Use CE0 */
  // rpio.spiSetCSPolarity(0, rpio.HIGH);  /* PCD8544 chip select is active-low */
  rpio.spiSetClockDivider(128);        /* PCD8544 最大 4MHz, 250MHz / 64 = 3.9MHz */
  rpio.spiSetDataMode(0);
}

// 往LCD写入数据
// wbyte: 需要写入的数据
// dat_cmd: 1-数据 0-命令 
function lcd_writeByte(wbyte, dat_cmd){
  let wb = Buffer.from([wbyte])
  if (dat_cmd == 1) { // 写数据
    rpio.write(Nokia5110_pins.DC, rpio.HIGH); // 输高电电平
  }
  else { // 写命令
    rpio.write(Nokia5110_pins.DC, rpio.LOW); // 输出低电平
  }
  rpio.spiWrite(wb, wb.length);
}

// 液晶清屏
function lcd_clear(){
  lcd_writeByte(NOKIA5110_address.x_col_addr,0);
  lcd_writeByte(NOKIA5110_address.y_row_addr,0);
  // 84*48的LCD点阵液晶  那么84x48=504
  for(i = 0; i < 504; i ++){
    lcd_writeByte(0,1);
  }
}

// 显示字节定位
// x: x轴坐标 0-83
// y: y轴坐标 0-5
function lcd_pos_byte(x,y){
  x |= NOKIA5110_address.x_col_addr;
  y |= NOKIA5110_address.y_row_addr;
  lcd_writeByte(x,0);
  lcd_writeByte(y,0);
}

// 显示一个6*8字符
// x: x轴坐标 0-13
// y: y轴坐标 0-5
function lcd_show_char(x, y, str=''){
  x = x * 6;
  lcd_pos_byte(x,y);
  let fontData = font[str];
  for(i = 0; i < 6; i ++){
    lcd_writeByte(fontData[i],1);
  }
}

// 显示字符串
// x: x轴坐标 0-13
// y: y轴坐标 0-5
function lcd_show_string(x, y, str=''){
  for (let i = 0; i < str.length; i++) {
    lcd_show_char(x++, y, str.charAt(i));
    if(x == 14){ x = 0; y ++;}
    if(y == 6){ y = 0;}
  }
}

// 初始化液晶
function lcd_init(){
  gpioInit();
  spiInit();
  rpio.write(Nokia5110_pins.RST, rpio.LOW); // 液晶复位
  rpio.msleep(500);
  rpio.write(Nokia5110_pins.RST, rpio.HIGH); // 液晶复位
  lcd_writeByte(0x21,0); // 工作模式 水平寻址  扩展指令
  lcd_writeByte(0x06,0); // VLCD温度系数2
  lcd_writeByte(0x13,0); // 设置偏置系统 1:48
  lcd_writeByte(0xc8,0); // 设置对比度
  lcd_writeByte(0x20,0); // 工作模式 水平寻址 常规指令
  lcd_writeByte(0x0c,0); // 普通模式
  lcd_clear(); // 清屏
}
function main(){
  lcd_init();
  lcd_show_string(0, 0,'0:' + os.hostname());
  lcd_show_string(0, 1,'1:' + os.type());
  lcd_show_string(0, 2,'2:' + os.platform());
  lcd_show_string(0, 3,'3:' + os.release());
  lcd_show_string(0, 4,'4:' + os.totalmem());
  lcd_show_string(0, 5,'5:' + os.freemem());
}
main()  

font.js文件是字库数据

let font = {
  ' ': [0x00,0x00,0x00,0x00,0x00,0x00],// (0)
  '!': [0x00,0x00,0x00,0x4F,0x00,0x00],//!(1)
  '"': [0x00,0x00,0x07,0x00,0x07,0x00],//"(2)
  '#': [0x00,0x14,0x7F,0x14,0x7F,0x14],//#(3)
  '$': [0x00,0x24,0x2A,0x7F,0x2A,0x12],//$(4)
  '%': [0x00,0x23,0x13,0x08,0x64,0x62],//%(5)
  '&': [0x00,0x36,0x49,0x55,0x22,0x50],//&(6)
  "'": [0x00,0x00,0x05,0x03,0x00,0x00],//'(7)
  '(': [0x00,0x00,0x1C,0x22,0x41,0x00],//((8)
  ')': [0x00,0x00,0x41,0x22,0x1C,0x00],//)(9)
  '*': [0x00,0x14,0x08,0x3E,0x08,0x14],//*(10)
  '+': [0x00,0x08,0x08,0x3E,0x08,0x08],//+(11)
  ',': [0x00,0x00,0x50,0x30,0x00,0x00],//,(12)
  '-': [0x00,0x08,0x08,0x08,0x08,0x08],//-(13)
  '.': [0x00,0x00,0x60,0x60,0x00,0x00],//.(14)
  '/': [0x00,0x20,0x10,0x08,0x04,0x02],///(15)
  '0': [0x00,0x3E,0x51,0x49,0x45,0x3E],//0(16)
  '1': [0x00,0x00,0x42,0x7F,0x40,0x00],//1(17)
  '2': [0x00,0x42,0x61,0x51,0x49,0x46],//2(18)
  '3': [0x00,0x21,0x41,0x45,0x4B,0x31],//3(19)
  '4': [0x00,0x18,0x14,0x12,0x7F,0x10],//4(20)
  '5': [0x00,0x27,0x45,0x45,0x45,0x39],//5(21)
  '6': [0x00,0x3C,0x4A,0x49,0x49,0x30],//6(22)
  '7': [0x00,0x01,0x71,0x09,0x05,0x03],//7(23)
  '8': [0x00,0x36,0x49,0x49,0x49,0x36],//8(24)
  '9': [0x00,0x06,0x49,0x49,0x29,0x1E],//9(25)
  ':': [0x00,0x00,0x36,0x36,0x00,0x00],//:(26)
  ';': [0x00,0x00,0x56,0x36,0x00,0x00],//;(27)
  '<': [0x00,0x08,0x14,0x22,0x41,0x00],//<(28)
  '=': [0x00,0x14,0x14,0x14,0x14,0x14],//=(29)
  '>': [0x00,0x00,0x41,0x22,0x14,0x08],//>(30)
  '?': [0x00,0x02,0x01,0x51,0x09,0x06],//?(31)
  '@': [0x00,0x32,0x49,0x79,0x41,0x3E],//@(32)
  'A': [0x00,0x7E,0x11,0x11,0x11,0x7E],//A(33)
  'B': [0x00,0x7F,0x49,0x49,0x49,0x3E],//B(34)
  'C': [0x00,0x3E,0x41,0x41,0x41,0x22],//C(35)
  'D': [0x00,0x7F,0x41,0x41,0x22,0x1C],//D(36)
  'E': [0x00,0x7F,0x49,0x49,0x49,0x41],//E(37)
  'F': [0x00,0x7F,0x09,0x09,0x09,0x01],//F(38)
  'G': [0x00,0x3E,0x41,0x49,0x49,0x7A],//G(39)
  'H': [0x00,0x7F,0x08,0x08,0x08,0x7F],//H(40)
  'I': [0x00,0x00,0x41,0x7F,0x41,0x00],//I(41)
  'J': [0x00,0x20,0x40,0x41,0x3F,0x01],//J(42)
  'K': [0x00,0x7F,0x08,0x14,0x22,0x41],//K(43)
  'L': [0x00,0x7F,0x40,0x40,0x40,0x40],//L(44)
  'M': [0x00,0x7F,0x02,0x04,0x02,0x7F],//M(45)
  'N': [0x00,0x7F,0x04,0x08,0x10,0x7F],//N(46)
  'O': [0x00,0x3E,0x41,0x41,0x41,0x3E],//O(47)
  'P': [0x00,0x7F,0x09,0x09,0x09,0x06],//P(48)
  'Q': [0x00,0x3E,0x41,0x51,0x21,0x5E],//Q(49)
  'R': [0x00,0x7F,0x09,0x19,0x29,0x46],//R(50)
  'S': [0x00,0x46,0x49,0x49,0x49,0x31],//S(51)
  'T': [0x00,0x01,0x01,0x7F,0x01,0x01],//T(52)
  'U': [0x00,0x3F,0x40,0x40,0x40,0x3F],//U(53)
  'V': [0x00,0x1F,0x20,0x40,0x20,0x1F],//V(54)
  'W': [0x00,0x3F,0x40,0x38,0x40,0x3F],//W(55)
  'X': [0x00,0x63,0x14,0x08,0x14,0x63],//X(56)
  'Y': [0x00,0x03,0x04,0x78,0x04,0x03],//Y(57)
  'Z': [0x00,0x61,0x51,0x49,0x45,0x43],//Z(58)
  '[': [0x00,0x00,0x7F,0x41,0x41,0x00],//[(59)
  "\\": [0x00,0x15,0x16,0x7C,0x16,0x15],  //\(60)
  ']': [0x00,0x00,0x41,0x41,0x7F,0x00],//](61)
  '^': [0x00,0x04,0x02,0x01,0x02,0x04],//^(62)
  '_': [0x00,0x40,0x40,0x40,0x40,0x40],//_(63)
  '`': [0x00,0x00,0x01,0x02,0x04,0x00],//`(64)
  'a': [0x00,0x20,0x54,0x54,0x54,0x78],//a(65)
  'b': [0x00,0x7F,0x48,0x44,0x44,0x38],//b(66)
  'c': [0x00,0x38,0x44,0x44,0x44,0x20],//c(67)
  'd': [0x00,0x38,0x44,0x44,0x48,0x7F],//d(68)
  'e': [0x00,0x38,0x54,0x54,0x54,0x18],//e(69)
  'f': [0x00,0x08,0x7E,0x09,0x01,0x02],//f(70)
  'g': [0x00,0x0C,0x52,0x52,0x52,0x3E],//g(71)
  'h': [0x00,0x7F,0x08,0x04,0x04,0x78],//h(72)
  'i': [0x00,0x00,0x44,0x7D,0x40,0x00],//i(73)
  'g': [0x00,0x20,0x40,0x44,0x3D,0x00],//j(74)
  'k': [0x00,0x7F,0x10,0x28,0x44,0x00],//k(75)
  'l': [0x00,0x00,0x41,0x7F,0x40,0x00],//l(76)
  'm': [0x00,0x7E,0x02,0x0C,0x02,0x7C],//m(77)
  'n': [0x00,0x7E,0x04,0x02,0x02,0x7C],//n(78)
  'o': [0x00,0x38,0x44,0x44,0x44,0x38],//o(79)
  'p': [0x00,0x7C,0x14,0x14,0x14,0x08],//p(80)
  'q': [0x00,0x08,0x14,0x14,0x18,0x7C],//q(81)
  'r': [0x00,0x7C,0x08,0x04,0x04,0x08],//r(82)
  's': [0x00,0x48,0x54,0x54,0x54,0x20],//s(83)
  't': [0x00,0x04,0x3F,0x44,0x40,0x20],//t(84)
  'u': [0x00,0x3C,0x40,0x40,0x20,0x7C],//u(85)
  'v': [0x00,0x1C,0x20,0x40,0x20,0x1C],//v(86)
  'w': [0x00,0x3C,0x40,0x30,0x40,0x3C],//w(87)
  'x': [0x00,0x44,0x28,0x10,0x28,0x44],//x(88)
  'y': [0x00,0x0C,0x50,0x50,0x50,0x3C],//y(89)
  'z': [0x00,0x44,0x64,0x54,0x4C,0x44],//z(90)
  '[': [0x00,0x00,0x08,0x36,0x41,0x00],//[(91)
  '|': [0x00,0x00,0x00,0x7F,0x00,0x00],//|(92)
  ']': [0x00,0x00,0x41,0x36,0x08,0x00],//](93)
  '~': [0x00,0x08,0x04,0x08,0x10,0x08],//~(94)
  '→': [0x00,0x08,0x08,0x2A,0x1C,0x08],//→(127)
  '←': [0x00,0x08,0x1C,0x2A,0x08,0x08],//←(128)
  '↑': [0x00,0x04,0x02,0x7F,0x02,0x04],//↑(129)
  '↓': [0x00,0x10,0x20,0x7F,0x20,0x10]//↓(130)
}
module.exports = font;

显示效果图

在这里插入图片描述
代码实现上和之前的C写的没什么区别,就是改动下驱动代码,其他基本不变。
在这里插入图片描述

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页