AD采样以及12864液晶显示成功程序

#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define comm 0 //表示指令
#define dat 1 //表示数据
//sbit CS=P0^5;
//sbit SID=P0^6;
//sbit SCLK=P0^7;
sbit eoc=P0^0;
sbit oe=P0^1;
sbit st=P0^2;
sbit ABC=P0^3;

sbit cs = P0^5;//串行的片选,高选中
sbit sid = P0^6;//串行的数据口
sbit clk = P0^7;//串行的同步时钟
sbit psb = P0^4; //H=并口; L=串口;
void delay(uint x);
/*void send_cmd(uchar cmd_data);
void send_data(uchar cmd_data);
void lcd_init();
void display1(uint x,uint y,uchar dis[]);
void display2(uint x,uint y,uchar dis[]);
void display_dy(uint x,uint y,uint dat);*/
uint ad0809_dy();
uint ad0809_dl();
//uchar keyscan();

uint a[4];

//uint i,add;
/*延时1ms*/
void delay(uint x)
{
uint i,j;
for(j=0;j<x;j++)
for(i=0;i<110;i++);
}
/*-------------延迟-------------*/
void delay2 (uint us) //delay time
{
while(us--);
}
/*void delay1 (uint ms)
{
uint i,j;
for(i=0;i<ms;i++)
for(j=0;j <15;j++)
delay2(1);
}
*/
/*-------------读写液晶----------------*/
void wr_lcd (uchar dat_comm,uchar content)
{
uchar a,i,j;
a=content;
cs=1;//首先片选选中
clk=0;//时钟置低
sid=1;//传送5个"1"
for(i=0;i<5;i++)//产生5个上升沿,传送5个数据,最后时钟置低
{
clk=1;
clk=0;
}
sid=0;//
clk=1;//产生一个上升沿,传送一个数据
clk=0;//
if(dat_comm)
sid=1; //数据
else
sid=0; //指令
clk=1;// rw完成
clk=0;//进行传送
sid=0;// rs=0,显示指令数据
clk=1;//
clk=0;//传送'0'
for(j=0;j<2;j++)
{
for(i=0;i<4;i++)
{
a=a<<1;
sid=CY;//应该是个实时数值,把此位从寄存器中赋给sid
clk=1;//
clk=0;//
}
sid=0;
for(i=0;i<4;i++)
{
clk=1;
clk=0;
}
}
} /*一次攒的数据八位*/

/*---------------显示汉字或字符----------------*/
void lcd_disp (uchar *dis)
{ uint i;
i = 0;
while(dis[i] != '\0')
{
wr_lcd(dat,dis[i]); //显示字符
i++;
}
}

/*------------------初始化-----------------*/
void init_lcd (void)
{
psb=0;
wr_lcd (comm,0x30); /*30---基本指令动作*/
wr_lcd (comm,0x01); /*清屏,地址指针指向00H*/
delay2 (100);
wr_lcd (comm,0x06); /*光标的移动方向*/
wr_lcd (comm,0x0c); /*开显示,关游标*/
}

/*void display_dy(uint x,uint y,uint dat)
{
switch(x)
{
case 1:add=0x80+y;break;
case 2:add=0x90+y;break;
case 3:add=0x88+y;break;
case 4:add=0x98+y;break;
}
send_cmd(add);
for(i=0;i<3;i++)
{
a[i]=dat%10;
dat=dat/10;
}

send_data(a[2]+0x30);
send_data('.');
send_data(a[1]
+0x30);
send_data(a[0]+0x30);
send_data('V');
}
*/
/*uchar keyscan()//带返回值的子函数,需写明数据类型
{
P0=0XFE;
temp=P0;
temp=temp&0xfe;
while(te

mp!=0xf0)//有键按下
{
delay(5);
temp=P0;
temp=temp&0xfe;
while(temp!=0xf0)
{
temp=P0;
switch(temp)
{
case 0x7e:num=1;break;
case 0xbe:num=2;break;
case 0xde:num=3;break;
case 0xee:num=10;break;
}
while(temp!=0xf0)//松手检测
{
temp=P0;
temp=temp&0xf0;
}

}
}
P0=0XFD; //第二行扫描
temp=P0;
temp=temp&0xfe;
while(temp!=0xf0)//有键按下
{
delay(5);
temp=P0;
temp=temp&0xfe;
while(temp!=0xf0)
{
temp=P0;
switch(temp)
{
case 0x7d:num=4;break;
case 0xbd:num=5;break;
case 0xdd:num=6;break;
case 0xed:num=11;break;
}
while(temp!=0xf0)//松手检测
{
temp=P0;
temp=temp&0xf0;
}
}
}
P1=0XFB;
temp=P0;
temp=temp&0xfe;
while(temp!=0xf0)//有键按下
{
delay(5);
temp=P0;
temp=temp&0xfe;
while(temp!=0xf0)
{
temp=P0;
switch(temp)
{
case 0x7b:num=7;break;
case 0xbb:num=8;break;
case 0xdb:num=9;break;
case 0xeb:num=12;break;
}
while(temp!=0xf0)//松手检测
{
temp=P0;
temp=temp&0xf0;
}
}
}
P1=0XF7;
temp=P0;
temp=temp&0xfe;
while(temp!=0xf0)//有键按下
{
delay(5);
temp=P0;
temp=temp&0xfe;
while(temp!=0xf0)
{
temp=P0; //必须有,否则示数不变
switch(temp)
{
case 0x77:num=14;break;//14 *
case 0xB7:num=0;break;
case 0xD7:num=15;break;//15 #
case 0xE7:num=13;break;//13 F4
}
while(temp!=0xf0)//松手检测
{
temp=P0;
temp=temp&0xf0;
}
}
return num;
}
}*/

uint ad0809_dy()
{
uint ad_0809;
ABC=0;//选通通道,ABC同时接A8
oe=0;//以下三条指令为起动AD0809
st=0;
st=1;
st=0;
delay(1);
while(!eoc);//等待转换结束
oe=1;//取出读得的数据
ad_0809=P2;
oe=0;
return ad_0809;
}

/*uint ad0809_dl()
{
uint ad_0809;
ABC=1;//选通通道,ABC同时接A8
oe=0;//以下三条指令为起动AD0809
st=0;
st=1;
st=0;
delay(1);
while(!eoc);//等待转换结束
oe=1;//取出读得的数据
ad_0809=P2;
oe=0;
return ad_0809;
} */
main()
{
//uint dat;
uint dat1;
double a=0.0;
int temp;
uchar date[5]={'0'};
init_lcd ();
while(1)
{
wr_lcd (comm,0x80);//设定ddrm地址
lcd_disp("青岛理工大学");
wr_lcd (comm,0x90);//设定ddrm地址
lcd_disp("LED 电源");
/*display1(1,0,"青岛理工大学");
//delay(5);
display2(2,0," LED 电源");
//delay(5);*/
dat1=ad0809_dy();
a=dat1*4.70/255;
temp=(unsigned int)(a*100);
date[0]=temp/100+48;
date[1]='.';
date[2]=(temp%100)/10+48;
date[3]=temp%10+48;
date[4]='\0';
wr_lcd (comm,0x88);
//设定ddrm地址
lcd_disp(date);
//display_dy(4,0,dat1);
//delay(100);
//dat=0xff;
//dat=dat*49/25;
//display_dl(3,0,dat);
//dat1=0xff; //2.5V
}
}

相关文档
最新文档