//测出的电压经0804转换后在12864上显示
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define port P1 //DB0~DB7数据口
sbit e=P2^0;//使能
sbit rw=P2^1;//读写
sbit rs=P2^2;//数据/命令
sbit cs2=P2^3;//片选
sbit cs1=P2^4;
sbit busy=P1^7;//检测忙
#define date P0
sbit cs=P2^5; //片选信号,控制芯片的启动,低电平有效
sbit rd=P2^6; //读数据控制,低电平有效
sbit rw1=P2^7; //AD转换启动控制,上升沿有效
sbit intr=P3^3; //AD转换结束输出低电平
uchar code hz[][16]={
{0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00},
//"0",0
{0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00},
//"1",1
{0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00},
//"2",2
{0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00},
//"3",3
{0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00},
//"4",4
{0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00},
//"5",5
{0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00},
//"6",6
{0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00},
//"7",7
{0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00},
//"8",8
{0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00} //"9",9
};
//延时
void delay(uint n)
{
uint i;
for(;n>0;n--)
for(i=200;i>0;i--);
}
void check()
{
rs=0;rw=1;
port=0x00;
e=1;
while(busy);
e=0;
}
//写指令
void wreg(uchar c)
{
check();
rs=0; //指令
rw=0; //写 e=0;
port=c;
e=1;
e=0; //写入指令
}
//写数据
void wdata(uchar dat)
{
check();
rs=1; //数据
rw=0;
port=dat;
e=1;
e=0;
}
//选屏幕 0--全屏,1--左屏,2--右屏;
void select(uint n)
{
switch(n)
{
case 0:cs1=0;cs2=0;break; //低电平选中
case 1:cs1=0;cs2=1;break; //cs1左屏
case 2:cs1=1;cs2=0;break; //cs2右屏
}
}
//页
void setpage(uchar page)
{
//page=page&0x07;
page=page|0xb8;
check();
wreg(page);
}
//列
void setcolumn(uchar column)
{
// column=column&0x3f;
column=column|0x40;
wreg(column);
}
//清屏 0--全屏,1--左屏,2--右屏;
void clear(uint n)
{
uchar i,j;
select(n);
//wreg(0x3f);
for(i=0;i<8;i++)
{
setpage(i);
setcolumn(0);
for(j=0;j<64;j++)
wdata(0x00); //置0清空
}
}
void show16(uchar page,uchar column,uchar screen,uchar*s)
{
uchar i,j;
select(screen);
j=0;
setpage(page);
setcolumn(column);
for(i=0;i<16;i++)
{
wdata(s[j++]);
}
setpage(page+1);
setcolumn(column);
for(i=0;i<16;i++)
{
wdata(s[j++]);
}
}
void show8(uchar page,uchar column,uchar screen,uchar *str)
{
uchar i,j;
select(screen);j=0;
setpage(page);
setcolumn(column);
for(i=0;i<8;i++)
{ wdata(str[j++]);
}
setpage(page+1);
setcolumn(column);
for(i=0;i<8;i++)
{ wdata(str[j++]);
}
}
// ad 转换
void start_ad( )
{
date=0xff;
cs=0;
rw1=0;
rd=1;
delay(3);
cs=1;
rw1=1;
rd=1;
}
uint ad_read( )
{
uint temp;
cs=0;
rd=0;
delay(3);
temp=date;
rd=1;
cs=1;
return(temp);
}
void ad_init( )
{
cs=1;rw1=1;rd=1;
}
main( )
{
uint a1,a2,a3,a4;
uint ad_data;
ad_init( );
start_ad( ) ;
while(intr==1);
ad_data=ad_read();
ad_data=ad_data*196;
a1=ad_data/10000; //第四位数码管(最高位)
a2=ad_data%10000/1000;//第三位数码管?
a3=ad_data%1000/100;//第二位数码管?
a4=ad_data%100/10;//第一位数码管(最低位)
clear(0);
show8(0,8,1,hz[a1]);
show8(0,16,1,hz[a2]);
show8(0,24,1,hz[a3]);
show8(0,32,1,hz[a4]);
show8(0,8,2,hz[5]);
show8(0,16,2,hz[6]);
show8(0,24,2,hz[7]);
show8(0,32,2,hz[8]);
delay(5000);
while(1);
} |