sbit io_LCD12864_RS = P1^0
sbit io_LCD12864_RW = P1^1
sbit io_LCD12864_EN = P1^2
#define io_LCD12864_DATAPORT P0
#define SET_DATAio_LCD12864_RS = 1
#define SET_INC io_LCD12864_RS = 0
#define SET_READ io_LCD12864_RW = 1
#define SET_WRITE io_LCD12864_RW = 0
#define SET_EN io_LCD12864_EN = 1
#define CLR_EN io_LCD12864_EN = 0
void v_Lcd12864CheckBusy_f( void ) //忙检信耐测函数
{
unsigned int nTimeOut = 0
SET_INC
SET_READ
CLR_EN
SET_EN
while( ( io_LCD12864_DATAPORT &0x80 ) &&( ++nTimeOut != 0 ) )
CLR_EN
SET_INC
SET_READ
}
void v_Lcd12864SendCmd_f( unsigned char byCmd ) //发送命令
{
v_Lcd12864CheckBusy_f()
SET_INC
SET_WRITE
CLR_EN
io_LCD12864_DATAPORT = byCmd
_nop_()
_nop_()
SET_EN
_nop_()
_nop_()
CLR_EN
SET_READ
SET_INC
}
void v_Lcd12864SendData_f( unsigned char byData ) //发送数据
{
v_Lcd12864CheckBusy_f()
SET_DATA
SET_WRITE
CLR_EN
io_LCD12864_DATAPORT = byData
_nop_()
_nop_()
SET_EN
_nop_()
_nop_()
CLR_EN
SET_READ
SET_INC
}
void v_DelayMs_f( unsigned int nDelay ) //延时
{
unsigned int i
for( nDelay >0 nDelay-- )
{
for( i = 125 i >0 i-- )
}
}
void v_Lcd12864Init_f( void ) //初始化
{
v_Lcd12864SendCmd_f( 0x30 ) /茄坦枝/基本指令集
v_DelayMs_f( 50 )
v_Lcd12864SendCmd_f( 0x01 ) //清颤敏屏
v_DelayMs_f( 50 )
v_Lcd12864SendCmd_f( 0x06 ) //光标右移
v_DelayMs_f( 50 )
v_Lcd12864SendCmd_f( 0x0c )//开显示
}
void v_Lcd12864SetAddress_f( unsigned char x, y )//地址转换
{
unsigned char byAddress
switch( y )
{
case 0 : byAddress = 0x80 + x
break
case 1 : byAddress = 0x90 + x
break
case 2 : byAddress = 0x88 + x
break
case 3 : byAddress = 0x98 + x
break
default :
break
}
v_Lcd12864SendCmd_f( byAddress )
}
void v_Lcd12864PutString_f( unsigned char x, unsigned char y, unsigned char *pData )
{
v_Lcd12864SetAddress_f( x, y )
while( *pData != '\0' )
{
v_Lcd12864SendData_f( *pData++ )
}
}
void main( void )
{
v_Lcd12864Init_f()
v_Lcd12864PutString_f( 0,0, "快乐随行")
v_Lcd12864PutString_f( 2,1, "分都不给")
v_Lcd12864PutString_f( 0,2, "*LCD12864ST7920*")
v_Lcd12864PutString_f( 0,3, "辛苦回答了半天")
while( 1 )
}
标准的一般都是这个程序#include <reg52.h>
#include <math.h>
#define lcd_data P1
sbit cs1=P2^3
sbit cs2=P2^4
sbit di=P2^0
sbit rw=P2^1
sbit e=P2^2
void delay(int t)
{
int i,j,k
for(i=0i<ti++)
{ for(j=0j<255j++)
k++}
}
void write_com_l(unsigned int command)
{
cs1=1
rw=0
di=0
lcd_data=command
e=1
e=0
cs1=0
}
void write_data_l(unsigned int data0)
{
cs1=1
rw=0
di=1
lcd_data=data0
e=1
e=0
cs1=0
}
void write_com_r(unsigned int command)
{
cs2=1
rw=0
di=0
lcd_data=command
e=1
e=0
cs2=0
}
void write_data_r(unsigned int data0)
{
cs2=1
rw=0
di=1
lcd_data=data0
e=1
e=0
cs2=0
}
void disp0()
{
unsigned int i,j
for (i=0i<8i++)
{write_com_l(0xb8+i)
write_com_r(0xb8+i)
write_com_l(0x40)
write_com_r(0x40)
for (j=0j<64j++)
{write_data_l(0xaa)
write_data_r(0xaa)
}
}
}
void disp1()
{
unsigned int i,j
for (i=0i<8i++)
{write_com_l(0xb8+i)
write_com_r(0xb8+i)
write_com_l(0x40)
write_com_r(0x40)
for (j=0j<64j++)
{write_data_l(0x55)
write_data_r(0x55)
}
}
}
void disp2()
{
unsigned int i,j
for (i=0i<8i++)
{write_com_l(0xb8+i)
write_com_r(0xb8+i)
write_com_l(0x40)
write_com_r(0x40)
for (j=0j<64j++)
{write_data_l(0x0f)
write_data_r(0x0f)
}
}
}
void disp3()
{
unsigned int i,j
for (i=0i<8i++)
{write_com_l(0xb8+i)
write_com_r(0xb8+i)
write_com_l(0x40)
write_com_r(0x40)
for (j=0j<64j++)
{write_data_l(0xf0)
write_data_r(0xf0)
}
}
}
void init_lcd() //初始化LCD清屏
{
write_com_l(0x3e)
write_com_r(0x3e)
write_com_l(0x3f)
write_com_r(0x3f)
write_com_l(0xc0)
write_com_r(0xc0)
write_com_l(0xb8)
write_com_r(0xb8)
write_com_l(0x40)
write_com_r(0x40)
for(1)
{
disp0()
delay(0x0ff)
disp1()
delay(0x0ff)
disp2()
delay(0x0ff)
disp3()
delay(0x0ff)
}
}
void main()
{
IE=0x81
init_lcd()
}
void innt0 () interrupt 0
{
do {}while(1)
}
#include<STC12.h>#define uchar unsigned char
#define uint unsigned int
uchar code seg_data1[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}
uchar code seg_data2[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef}
sbit p20=P2^0
sbit p21=P2^1
sbit p22=P2^2
/隐激/sfr ADC_CONTR=0xBC
//sfr P1ASF=0x9D
//sfr ADC_RES=0xBD
void delay(uint a1)
void input()
void display(uint count)
void main()
{
uint x
while(1)
{
input()
x=5*(ADC_RES*50)/255
display(x)
}
}
void input()
{
P1ASF=0x01
ADC_CONTR=0x88
delay(25)
IE=0xA0
}
void drop() interrupt 5
{
ADC_CONTR=0X88
}
void display(uint count)
{
uchar i
for (i=0i<5i++)
{
P0=seg_data1[count%10]
p20=0
delay(25)
p20=1
P0=seg_data2[count/10%10]
p21=0
delay(25)
p21=1
if(count>=100)
{
P0=seg_data1[count/100]
p22=0
delay(25)
p22=1
}
}
}
void delay(uint a1) //误差 0us
{
unsigned char a,b,c
for(c=a1c>0c--)
for(b=142b>0b--)
for(a=2a>0a--)
}
这是我自己写的,用数差誉码管显示,你只需要把void display(uint count)函数改为12864的显示函数灶庆袜即可
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)