第71節(jié):液晶屏的字符,16點陣,24點陣和32點陣的顯示程序
從業(yè)近十年!手把手教你單片機程序框架 第71講
開場白:
這一節(jié)要教會大家二個知識點:
第一個:如何利用任意點陣字體顯示函數(shù)display_lattice來顯示8x16的字符,16點陣漢字,24點陣漢字和32點陣漢字。
第二個:糾正上一節(jié)的一個小錯誤。C51編譯器跟其它單片機的編譯器有點不一樣。想把常量數(shù)據(jù)保存在ROM程序存儲區(qū)里并不是用const關(guān)鍵字,而是是用code關(guān)鍵字。
具體內(nèi)容,請看源代碼講解。
(1)硬件平臺:
基于朱兆祺51單片機學(xué)習(xí)板。
(2)實現(xiàn)功能:開機上電后,可以看到液晶屏分別顯示32點陣,24點陣和16點陣的“饅頭”兩個字,還有“V5”這兩個8x16點陣的字符。
(3)源代碼講解如下:
#include "REG52.H"
sbit LCDCS_dr = P1^6; //片選線
sbit LCDSID_dr = P1^7; //串行數(shù)據(jù)線
sbit LCDCLK_dr = P3^2; //串行時鐘線
sbit LCDRST_dr = P3^4; //復(fù)位線
void SendByteToLcd(unsigned char ucData); //發(fā)送一個字節(jié)數(shù)據(jù)到液晶模塊
void SPIWrite(unsigned char ucWData, unsigned char ucWRS); //模擬SPI發(fā)送一個字節(jié)的命令或者數(shù)據(jù)給液晶模塊的底層驅(qū)動
void WriteCommand(unsigned char ucCommand); //發(fā)送一個字節(jié)的命令給液晶模塊
void LCDWriteData(unsigned char ucData); //發(fā)送一個字節(jié)的數(shù)據(jù)給液晶模塊
void LCDInit(void); //初始化 函數(shù)內(nèi)部包括液晶模塊的復(fù)位
void display_lattice(unsigned int x,unsigned int y,const unsigned char *ucArray,unsigned char ucFbFlag,unsigned int x_amount,unsigned int y_amount); //顯示任意點陣函數(shù)
void display_clear(void); // 清屏
void delay_short(unsigned int uiDelayshort); //延時
/* 注釋一:
* 糾正上一節(jié)的一個小錯誤。C51編譯器跟其它的編譯器有點不一樣。
* 存在ROM程序存儲區(qū)里的常量數(shù)據(jù)并不是用const關(guān)鍵字,而是是用code關(guān)鍵字。
*/
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 內(nèi)部包含液晶模塊的復(fù)位
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); //關(guān)顯示緩沖指令
WriteCommand(0x34); //關(guān)顯示緩沖指令 故意寫2次,怕1次關(guān)不了 這個是因為我參考到某廠家的驅(qū)動程序也是這樣寫的
y=0;
while(y<32) //y軸的范圍0至31
{
WriteCommand(y+0x80); //垂直地址
WriteCommand(0x80); //水平地址
for(x=0;x<32;x++) //256個橫向點,有32個字節(jié)
{
LCDWriteData(0x00);
}
y++;
}
WriteCommand(0x36); //開顯示緩沖指令
}
/* 注釋二:本節(jié)的核心函數(shù),讀者尤其要搞懂x_amount和y_amount對應(yīng)的顯示關(guān)系。
* 第1,2個參數(shù)x,y是坐標(biāo)體系。x的范圍是0至15,y的范圍是0至31.
* 第3個參數(shù)*ucArray是字模的數(shù)組。
* 第4個參數(shù)ucFbFlag是反白顯示標(biāo)志。0代表正常顯示,1代表反白顯示。
* 第5,6個參數(shù)x_amount,y_amount分別代表字模數(shù)組的橫向有多少個字節(jié),縱向有幾橫。
*/
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); //關(guān)顯示緩沖指令
WriteCommand(0x34); //關(guān)顯示緩沖指令 故意寫2次,怕1次關(guān)不了 這個是因為我參考到某廠家的驅(qū)動程序也是這樣寫的
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); //把上一節(jié)這個延時函數(shù)去掉,加快刷屏速度
}
}
WriteCommand(0x36); //開顯示緩沖指令
}
void SendByteToLcd(unsigned char ucData) //發(fā)送一個字節(jié)數(shù)據(jù)到液晶模塊
{
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發(fā)送一個字節(jié)的命令或者數(shù)據(jù)給液晶模塊的底層驅(qū)動
{
SendByteToLcd( 0xf8 + (ucWRS << 1) );
SendByteToLcd( ucWData & 0xf0 );
SendByteToLcd( (ucWData << 4) & 0xf0);
}
void WriteCommand(unsigned char ucCommand) //發(fā)送一個字節(jié)的命令給液晶模塊
{
LCDCS_dr = 0;
LCDCS_dr = 1;
SPIWrite(ucCommand, 0);
delay_short(90);
}
void LCDWriteData(unsigned char ucData) //發(fā)送一個字節(jié)的數(shù)據(jù)給液晶模塊
{
LCDCS_dr = 0;
LCDCS_dr = 1;
SPIWrite(ucData, 1);
}
void LCDInit(void) //初始化 函數(shù)內(nèi)部包括液晶模塊的復(fù)位
{
LCDRST_dr = 1; //復(fù)位
LCDRST_dr = 0;
LCDRST_dr = 1;
}
void delay_short(unsigned int uiDelayShort) //延時函數(shù)
{
unsigned int i;
for(i=0;i
{
;
}
}
總結(jié)陳詞:
我們現(xiàn)在講的字體顯示都是橫向的,如果某個項目要把整個液晶屏順時針旋轉(zhuǎn)90度,要求像對聯(lián)一樣縱向顯示一串字體的時候,該怎么辦?我前兩個月就遇到了這樣的項目,當(dāng)時我的做法就是把字體的字庫數(shù)組通過算法旋轉(zhuǎn)90度就達到了目的。這種算法程序是怎樣編寫的?欲知詳情,請聽下回分解-----把字體順時針旋轉(zhuǎn)90度顯示的算法程序。