📄 128_64_lcd.c
字号:
#include <c8051f120.h>
#include <stdio.h>
#include <intrins.h>
#include <math.h>
#define PORT P0
sbit e=P2^0;
sbit rw=P2^1;
sbit di=P2^2;
sbit cs1=P2^3;
sbit cs2=P2^4;
sbit res=P2^5;
unsigned char bdata x;
sbit flag=x^7;
unsigned char xdata shuzimu[3][8]={
0x00,0x21,0x41,0x45,0x4b,0x31,0x00,0x00, //"3"
0x00,0x42,0x61,0x51,0x49,0x46,0x00,0x00, //"2"
0x00,0x27,0x45,0x45,0x45,0x39,0x00,0x00}; //"5"
unsigned char xdata hanzi[2][32]={
//0x00,0x00,0xF8,0x48,0x48,0x48,0x48,0xFF,
//0x48,0x48,0x48,0x48,0xF8,0x00,0x00,0x00,
//0x00,0x00,0x0F,0x04,0x04,0x04,0x04,0x3F,
//0x44,0x44,0x44,0x44,0x4F,0x40,0x70,0x00, //电
0x00,0xf8,0x48,0x48,0x48,0x48,0xff,0x48,
0x48,0x48,0x48,0xfc,0x08,0x00,0x00,0x00,
0x00,0x07,0x02,0x02,0x02,0x02,0x3f,0x42,
0x42,0x42,0x42,0x47,0x40,0x70,0x00,0x00, //电
//0x00,0x00,0x02,0x02,0x02,0x02,0x02,0xE2,
//0x12,0x0A,0x06,0x02,0x00,0x80,0x00,0x00,
//0x01,0x01,0x01,0x01,0x01,0x41,0x81,0x7F,
//0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00};//子
0x80,0x80,0x82,0x82,0x82,0x82,0x82,0xe2,
0xa2,0x92,0x8a,0x86,0x80,0xc0,0x80,0x00,
0x00,0x00,0x00,0x00,0x00,0x40,0x80,0x7f,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; //子
//C8051F124有关配置
config(){
WDTCN=0x07;
WDTCN=0xde; WDTCN=0xad;
SFRPAGE=0x0f;
XBR0=0x00;
XBR1=0x00;
XBR2=0x40;
SFRPAGE=0x0f;
P0MDOUT=0x00; //Output configuration to P0
P2MDOUT=0x00; //Output configuration to P2
SFRPAGE=0x00;
EMI0CF=0x03; //External Memory Configuration Register
SFRPAGE=0x00;
FLSCL=0x80; //FLASH Memory Control
SFRPAGE=0x0f;
OSCXCN=0x00; //EXTERNAL Oscillator Control Register
PLL0DIV=0x00; //PLL pre-divide Register
PLL0MUL=0x01; //PLL Clock scaler Register
PLL0FLT=0x31; //PLL Filter Register
PLL0CN=0x00; //PLL Control Register
CLKSEL=0x00; //Oscillator Clock Selector
OSCICN=0x86; //Internal Oscillator Control Register
SFRPAGE=0x00;
REF0CN=0x01; //Reference Control Register
return;
}
//液晶复位
reset(){
_nop_();
_nop_();
res=1;
_nop_();
_nop_();
res=0;
_nop_();
_nop_();
res=1;
_nop_();
return;
}
//左右屏幕写控制字
void wi1(char command){
cs1=1;
cs2=0;
di=0;
rw=1;
wibf1:
PORT=0xff;
e=1;
x=PORT;
if (flag==1) goto wibf1;
rw=0;
PORT=command;
e=1;
e=0;
return;
}
void wi2(char command){
cs1=0;
cs2=1;
di=0;
rw=1;
wibf2:
PORT=0xff;
e=1;
x=PORT;
if (flag==1) goto wibf2;
rw=0;
PORT=command;
e=1;
e=0;
return;
}
//左右屏幕写数据
void wd1(char date){
cs1=1;
cs2=0;
di=0;
rw=1;
wdbf1:
PORT=0xff;
e=1;
x=PORT;
if (flag==1) goto wdbf1;
di=1;
rw=0;
PORT=date;
e=1;
e=0;
return;
}
void wd2(char date){
cs1=0;
cs2=1;
di=0;
rw=1;
wdbf2:
PORT=0xff;
e=1;
x=PORT;
if (flag==1) goto wdbf2;
di=1;
rw=0;
PORT=date;
e=1;
e=0;
return;
}
//清LCD
clear_lcd(){
int i;
reset();
wi1(0x3f);
wi2(0x3f);
wi1(0xc0);
wi2(0xc0);
wi1(0xb8);
wi2(0xb8);
wi1(0x40);
wi2(0x40);
for(i=0;i<64;i++) {wd1(0xff);wd2(0xff);}//清第0页,共8页
wi1(0xb9);
wi2(0xb9);
wi1(0x40);
wi2(0x40);
for(i=0;i<64;i++) {wd1(0xff);wd2(0xff);}
wi1(0xba);
wi2(0xba);
wi1(0x40);
wi2(0x40);
for(i=0;i<64;i++) {wd1(0xff);wd2(0xff);}
wi1(0xbb);
wi2(0xbb);
wi1(0x40);
wi2(0x40);
for(i=0;i<64;i++) {wd1(0xff);wd2(0xff);}
wi1(0xbc);
wi2(0xbc);
wi1(0x40);
wi2(0x40);
for(i=0;i<64;i++) {wd1(0xff);wd2(0xff);}
wi1(0xbd);
wi2(0xbd);
wi1(0x40);
wi2(0x40);
for(i=0;i<64;i++) {wd1(0xff);wd2(0xff);}
wi1(0xbe);
wi2(0xbe);
wi1(0x40);
wi2(0x40);
for(i=0;i<64;i++) {wd1(0xff);wd2(0xff);}
wi1(0xbf);
wi2(0xbf);
wi1(0x40);
wi2(0x40);
for(i=0;i<64;i++) {wd1(0xff);wd2(0xff);}//清第7页,共8页
return;
}
//在x页(0~7),y列(0~127),写一列数据z
void writeto(int x,int y,int z){
x=x+0xb8;
y=y+0x40;
if(y<=0x7f)
{wi1(0xc0);
wi1(x);
wi1(y);
wd1(~z);}
else
{wi2(0xc0);
wi2(x);
wi2(y-0x40);
wd2(~z);}
return;
}
//在x8页(0~7),y8列(0~127),写一8*8数据阵
//注意:z8为汉字数组中的行值,汉字数组在data.c中定义 shuzimu[N][8];
void wto8(int x8,int y8,int z8){
int i;
for(i=0;i<8;i++)
writeto(x8,y8+i,shuzimu[z8][i]);
return;
}
//在x16页(0~7),y16列(0~127),写一16*16数据阵
//注意:z16为汉字数组中的行值,汉字数组在data.c中定义 hanzi[N][32];
void wto16(int x16,int y16,int z16){
int i;
for(i=0;i<16;i++)
writeto(x16,y16+i,hanzi[z16][i]);
for(i=16;i<32;i++)
writeto(x16+1,y16+i-16,hanzi[z16][i]);
return;
}
//显示数据
display(){
wto8(3,40,0);
wto8(3,48,1);
wto8(3,56,2);
wto16(2,64,0);
wto16(2,80,1);
return;
}
//主程序开始************************************************************************************
void main(void)
{SP=0x7f;
config();
reset();
wi1(0x3f);
wi2(0x3f);
clear_lcd();
_nop_();
clear_lcd();
//disp:
display();
//goto disp;
while(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -