第71节:液晶屏的字符,16点阵,24点阵和32点阵的显示程序
扫描二维码
随时随地手机看文章
从业近十年!手把手教你单片机程序框架 第71讲
开场白:
这一节要教会大家二个知识点:
第一个:如何利用任意点阵字体显示函数display_lattice来显示8x16的字符,16点阵汉字,24点阵汉字和32点阵汉字。
第二个:纠正上一节的一个小错误。C51编译器跟其它单片机的编译器有点不一样。想把常量数据保存在ROM程序存储区里并不是用const关键字,而是是用code关键字。
具体内容,请看源代码讲解。
(1)硬件平台:
基于朱兆祺51单片机学习板。
(2)实现功能:开机上电后,可以看到液晶屏分别显示32点阵,24点阵和16点阵的“馒头”两个字,还有“V5”这两个8x16点阵的字符。
(3)源代码讲解如下:
#include "REG52.H"
sbit LCDCS_dr = P1^6; //片选线
sbit LCDSID_dr = P1^7; //串行数据线
sbit LCDCLK_dr = P3^2; //串行时钟线
sbit LCDRST_dr = P3^4; //复位线
void SendByteToLcd(unsigned char ucData); //发送一个字节数据到液晶模块
void SPIWrite(unsigned char ucWData, unsigned char ucWRS); //模拟SPI发送一个字节的命令或者数据给液晶模块的底层驱动
void WriteCommand(unsigned char ucCommand); //发送一个字节的命令给液晶模块
void LCDWriteData(unsigned char ucData); //发送一个字节的数据给液晶模块
void LCDInit(void); //初始化 函数内部包括液晶模块的复位
void display_lattice(unsigned int x,unsigned int y,const unsigned char *ucArray,unsigned char ucFbFlag,unsigned int x_amount,unsigned int y_amount); //显示任意点阵函数
void display_clear(void); // 清屏
void delay_short(unsigned int uiDelayshort); //延时
/* 注释一:
* 纠正上一节的一个小错误。C51编译器跟其它的编译器有点不一样。
* 存在ROM程序存储区里的常量数据并不是用const关键字,而是是用code关键字。
*/
code unsigned char Hz3232_man[]= /*馒 横向取模 32x32点阵 */
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x07,0x03,0x00,0x0F,0x87,0xFF,0x80,
0x0F,0x07,0x03,0x80,0x0E,0x07,0x03,0x80,0x0E,0x37,0xFF,0x80,0x1C,0x7F,0x03,0x80,
0x1F,0xFF,0x03,0x80,0x18,0x77,0xFF,0x00,0x38,0xE0,0x00,0xC0,0x36,0xDF,0xFF,0xF0,
0x77,0x9C,0xCE,0xE0,0x67,0x1C,0xCE,0xE0,0xC7,0x1C,0xCE,0xE0,0x07,0x1C,0xCE,0xE0,
0x07,0x1F,0xFF,0xE0,0x07,0x18,0x00,0x00,0x07,0x00,0x03,0x80,0x07,0x0F,0xFF,0xC0,
0x07,0x71,0x8F,0x00,0x07,0xE0,0xDE,0x00,0x07,0xC0,0xFC,0x00,0x07,0x80,0x78,0x00,
0x0F,0x01,0xFE,0x00,0x07,0x03,0x8F,0xE0,0x00,0x1E,0x03,0xF0,0x00,0xF8,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
code unsigned char Hz3232_tou[]= /*头 横向取模 32x32点阵 */
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xC0,0x00,0x00,0x03,0xE0,0x00,
0x03,0xC3,0xC0,0x00,0x00,0xF3,0x80,0x00,0x00,0x7B,0x80,0x00,0x00,0x7B,0x80,0x00,
0x00,0x3B,0x80,0x00,0x0E,0x03,0x80,0x00,0x07,0x83,0x80,0x00,0x03,0xC3,0x80,0x00,
0x01,0xE3,0x80,0x00,0x01,0xE3,0x80,0x00,0x00,0xC3,0x80,0x00,0x00,0x03,0x81,0xE0,
0x7F,0xFF,0xFF,0xF0,0x00,0x07,0x80,0x30,0x00,0x07,0x00,0x00,0x00,0x07,0x80,0x00,
0x00,0x0E,0xE0,0x00,0x00,0x1E,0x7C,0x00,0x00,0x3C,0x1F,0x00,0x00,0x78,0x0F,0xC0,
0x00,0xF0,0x03,0xC0,0x03,0xC0,0x01,0xE0,0x0F,0x00,0x00,0xE0,0x78,0x00,0x00,0x00,
0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
code unsigned char Hz2424_man[]= /*馒 横向取模 24x24点阵 */
{
0x00,0x00,0x00,0x00,0x00,0x00,0x0C,0x18,0x30,0x1E,0x1F,0xF8,0x1C,0x1C,0x38,0x1C,
0x1F,0xF8,0x19,0xFC,0x38,0x3F,0xFF,0xF8,0x31,0x98,0x30,0x7B,0xE0,0x0E,0x6F,0x7F,
0xFE,0x6E,0x76,0xEE,0xCC,0x76,0xEE,0x0C,0x7F,0xFE,0x0C,0x70,0x0C,0x0C,0x00,0x38,
0x0C,0x3F,0xF8,0x0D,0xCE,0x70,0x0F,0x87,0xE0,0x0F,0x03,0x80,0x1E,0x07,0xE0,0x0C,
0x1C,0x7E,0x01,0xF0,0x1F,0x00,0x00,0x00,
};
code unsigned char Hz2424_tou[]= /*头 横向取模 24x24点阵 */
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0E,0x00,0x06,0x0F,0x00,0x07,0x8E,0x00,0x01,
0xEE,0x00,0x00,0xEE,0x00,0x00,0xEC,0x00,0x1C,0x0C,0x00,0x0F,0x0C,0x00,0x07,0x9C,
0x00,0x03,0x9C,0x00,0x00,0x1C,0x0C,0x00,0x1C,0x1E,0x7F,0xFF,0xF6,0x00,0x1C,0x00,
0x00,0x3C,0x00,0x00,0x3F,0x80,0x00,0x71,0xE0,0x00,0xE0,0xF8,0x01,0xC0,0x3C,0x07,
0x00,0x1C,0x3C,0x00,0x0C,0x70,0x00,0x00,
};
code unsigned char Hz1616_man[]= /*馒 横向取模 16X16点阵 */
{
0x21,0xF8,0x21,0x08,0x21,0xF8,0x3D,0x08,0x45,0xF8,0x48,0x00,0x83,0xFC,0x22,0x94,
0x23,0xFC,0x20,0x00,0x21,0xF8,0x20,0x90,0x28,0x60,0x30,0x90,0x23,0x0E,0x00,0x00,
};
code unsigned char Hz1616_tou[]= /*头 横向取模 16X16点阵 */
{
0x00,0x80,0x10,0x80,0x0C,0x80,0x04,0x80,0x10,0x80,0x0C,0x80,0x08,0x80,0x00,0x80,
0xFF,0xFE,0x00,0x80,0x01,0x40,0x02,0x20,0x04,0x30,0x08,0x18,0x10,0x0C,0x20,0x08,
};
code unsigned char Zf816_V[]= /*V 横向取模 8x16点阵 */
{
0x00,0x00,0x00,0xE7,0x42,0x42,0x44,0x24,0x24,0x28,0x28,0x18,0x10,0x10,0x00,0x00,
};
code unsigned char Zf816_5[]= /*5 横向取模 8x16点阵 */
{
0x00,0x00,0x00,0x7E,0x40,0x40,0x40,0x58,0x64,0x02,0x02,0x42,0x44,0x38,0x00,0x00,
};
void main()
{
LCDInit(); //初始化12864 内部包含液晶模块的复位
display_clear(); // 清屏
display_lattice(0,0,Hz3232_man,0,4,32); //显示32点阵的<馒>字
display_lattice(2,0,Hz3232_tou,0,4,32); //显示32点阵的<头>字
display_lattice(4,0,Hz2424_man,0,3,24); //显示24点阵的<馒>字
display_lattice(6,0,Hz2424_tou,0,3,24); //显示24点阵的<头>字
display_lattice(8,0,Hz1616_man,0,2,16); //显示16点阵的<馒>字
display_lattice(9,0,Hz1616_tou,0,2,16); //显示16点阵的<头>字
display_lattice(11,0,Zf816_V,0,1,16); //显示8x16点阵的
display_lattice(12,0,Zf816_5,0,1,16); //显示8x16点阵的<5>字符
while(1)
{
;
}
}
void display_clear(void) // 清屏
{
unsigned char x,y;
WriteCommand(0x34); //关显示缓冲指令
WriteCommand(0x34); //关显示缓冲指令 故意写2次,怕1次关不了 这个是因为我参考到某厂家的驱动程序也是这样写的
y=0;
while(y<32) //y轴的范围0至31
{
WriteCommand(y+0x80); //垂直地址
WriteCommand(0x80); //水平地址
for(x=0;x<32;x++) //256个横向点,有32个字节
{
LCDWriteData(0x00);
}
y++;
}
WriteCommand(0x36); //开显示缓冲指令
}
/* 注释二:本节的核心函数,读者尤其要搞懂x_amount和y_amount对应的显示关系。
* 第1,2个参数x,y是坐标体系。x的范围是0至15,y的范围是0至31.
* 第3个参数*ucArray是字模的数组。
* 第4个参数ucFbFlag是反白显示标志。0代表正常显示,1代表反白显示。
* 第5,6个参数x_amount,y_amount分别代表字模数组的横向有多少个字节,纵向有几横。
*/
void display_lattice(unsigned int x,unsigned int y,const unsigned char *ucArray,unsigned char ucFbFlag,unsigned int x_amount,unsigned int y_amount)
{
unsigned int j=0;
unsigned int i=0;
unsigned char ucTemp;
WriteCommand(0x34); //关显示缓冲指令
WriteCommand(0x34); //关显示缓冲指令 故意写2次,怕1次关不了 这个是因为我参考到某厂家的驱动程序也是这样写的
for(j=0;j
{
WriteCommand(y+j+0x80); //垂直地址
WriteCommand(x+0x80); //水平地址
for(i=0;i
{
ucTemp=ucArray[j*x_amount+i];
if(ucFbFlag==1) //反白显示
{
ucTemp=~ucTemp;
}
LCDWriteData(ucTemp);
// delay_short(30000); //把上一节这个延时函数去掉,加快刷屏速度
}
}
WriteCommand(0x36); //开显示缓冲指令
}
void SendByteToLcd(unsigned char ucData) //发送一个字节数据到液晶模块
{
unsigned char i;
for ( i = 0; i < 8; i++ )
{
if ( (ucData << i) & 0x80 )
{
LCDSID_dr = 1;
}
else
{
LCDSID_dr = 0;
}
LCDCLK_dr = 0;
LCDCLK_dr = 1;
}
}
void SPIWrite(unsigned char ucWData, unsigned char ucWRS) //模拟SPI发送一个字节的命令或者数据给液晶模块的底层驱动
{
SendByteToLcd( 0xf8 + (ucWRS << 1) );
SendByteToLcd( ucWData & 0xf0 );
SendByteToLcd( (ucWData << 4) & 0xf0);
}
void WriteCommand(unsigned char ucCommand) //发送一个字节的命令给液晶模块
{
LCDCS_dr = 0;
LCDCS_dr = 1;
SPIWrite(ucCommand, 0);
delay_short(90);
}
void LCDWriteData(unsigned char ucData) //发送一个字节的数据给液晶模块
{
LCDCS_dr = 0;
LCDCS_dr = 1;
SPIWrite(ucData, 1);
}
void LCDInit(void) //初始化 函数内部包括液晶模块的复位
{
LCDRST_dr = 1; //复位
LCDRST_dr = 0;
LCDRST_dr = 1;
}
void delay_short(unsigned int uiDelayShort) //延时函数
{
unsigned int i;
for(i=0;i
{
;
}
}
总结陈词:
我们现在讲的字体显示都是横向的,如果某个项目要把整个液晶屏顺时针旋转90度,要求像对联一样纵向显示一串字体的时候,该怎么办?我前两个月就遇到了这样的项目,当时我的做法就是把字体的字库数组通过算法旋转90度就达到了目的。这种算法程序是怎样编写的?欲知详情,请听下回分解-----把字体顺时针旋转90度显示的算法程序。