51单片机+2个74hc595+12864液晶显示 40
2个回答
2010-05-05
展开全部
程#include <at89x51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit sk=P1^5;
sbit stcp1=P1^4;
sbit shcp1=P1^3;
sbit stcp2=P1^1;
sbit shcp2=P1^0;
sbit flag=P1^2;
unsigned char getdata;
unsigned int temp;
unsigned int temp1;
uchar m = 0;
sbit st=P1^6;
sbit oe=P3^1; //adc0809的引脚控制端
sbit eoc=P3^0;
sbit CLK=P1^7;
sbit DQ = P0^6;
sbit PinA=P0^0;
sbit PinB=P0^1;
sbit PinC=P0^2;
sbit P03=P0^3;
sbit P34=P3^4;
sbit P35=P3^5;
sbit P36=P3^6;
uchar code dis1[] = {"voltage of ch1:"};
uchar code dis4[] = {"voltage of ch2:"};
uchar code dis3[] = {"current of ch3:"};
uchar code dis5[] = {"tempera of ch4:"};
uchar code dis2[] = {".VmAC " };
unsigned char code dispbitcode[]={"0123456789"};
unsigned char dispbuf[4];
uchar DS[7];
uchar code td[]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07};//通道先择数组
uint ad_0809,ad_data1,ad_data2,ad_data3,ad_data0;
uchar m,number;
uchar x[8];//八通道数据待存数组
uchar num=0,j,y=0,k=0;
uint num2[2]={0,0},num1=0;
uchar n=0;
void init();
void delay(uint z);
void write_com(uchar com);
void write_data(uchar date);
void fc595(uchar a);
void fc5951(uchar b);
void startadc(uchar address);
void delaynms(uint x);//nms延时程序
void display();//显示程序
void ad0809();//芯片启动程序
void key();//键扫描程序
void ad0809_1();
void ad0809_2();
void displayMA();
void delay22(unsigned int i) ;
Init_DS18B20(void);
ReadOneChar(void) ;
WriteOneChar(unsigned char dat) ;
ReadTemperature(void);
void wendu();
void TimeInitial()
{ TMOD=0x10;
TH1=(65536-200)/256;
TL1=(65536-200)%256;
ET1=1;
TR1=1;
}
void t1(void) interrupt 3 using 0
{
TH1=(65536-200)/256;
TL1=(65536-200)%256;
CLK=~CLK;
}
/*******************************************************************/
/* */
/* 主函数 */
/* */
/*******************************************************************/
void main()
{
EX1 = 1;
IT1 = 1;
EA=1;
P1=0x00;
init();
TimeInitial();
while(1)
{
switch(m)
{
case 0:
ad0809();
break;
case 1:
ad0809_1();
break;
case 2:
ad0809_2();
break;
case 3:
wendu();
break ;
default : break;
}
}
}
/*******************************************************************/
/* */
/* 子函数 */
/* */
/*******************************************************************/
void init()
{
delay(15);
write_com(0x38);
delay(5);
write_com(0x38);
delay(5);
write_com(0x38);
write_com(0x38);
write_com(0x08);
write_com(0x01);
write_com(0x06);
write_com(0x0c);
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_data(uchar date)
{
fc5951(4);
fc595(date);
delay(5);
fc5951(5);
delay(5);
fc5951(4);
}
void write_com(uchar com)
{
fc5951(0);
fc595(com);
delay(5);
fc5951(1);
delay(5);
fc5951(0);
}
void fc595(uchar a)
{
for(j=0;j<8;j++)
{
DS[j]=a%0x02;
a=a/0x02;
}
stcp1=0;
for(j=8;j>0;j--)
{
sk=DS[j-1];
shcp1=0;
shcp1=1;
}
stcp1=0;
stcp1=1;
}
void fc5951(uchar b)
{
stcp2=0;
for(j=0;j<7;j++)
{
flag=b%0x02;
b=b/0x02;
shcp2=0;
shcp2=1;
}
stcp2=0;
stcp2=1;
}
/*******************************************************************/
/* */
/* ADC0809的子函数 */
/* */
/*******************************************************************/
//nms 延时程序
void delaynms(uint x)
{
uchar i;
while(x-->0)
{
for(i=0;i<125;i++)
{;}
}
}
/*******************************************************************/
/* */
/* 显示程序 */
/* */
/*******************************************************************/
void display()
{
write_com(0x80+0x40+2) ;
write_data(dispbitcode[dispbuf[3]]);
write_com(0x80+0x40+3) ;
write_data(dispbitcode[dispbuf[2]]);
write_com(0x80+0x40+4) ;
write_data(dis2[0]);
write_com(0x80+0x40+5) ;
write_data(dispbitcode[dispbuf[1]]);
write_com(0x80+0x40+6) ;
write_data(dispbitcode[dispbuf[0]]);
write_com(0x80+0x40+8) ;
write_data(dis2[1]);
write_com(0x80+0x40+9) ;
write_data(dis2[6]);
}
void displayMA()
{
uchar i = 0,mm;
write_com(0x80);
i = 0;
while(dis3[i] != '\0')
{
write_data(dis3[i]);
i++;
}
write_com(0x80+0x40+2) ;
write_data(dispbitcode[dispbuf[3]]);
write_com(0x80+0x40+3) ;
write_data(dispbitcode[dispbuf[2]]);
write_com(0x80+0x40+4) ;
write_data(dis2[0]);
write_com(0x80+0x40+5) ;
write_data(dispbitcode[dispbuf[1]]);
write_com(0x80+0x40+6) ;
write_data(dispbitcode[dispbuf[0]]);
write_com(0x80+0x40+8) ;
write_data(dis2[2]);
write_com(0x80+0x40+9) ;
write_data(dis2[3]);
}
void ad0809()
{
uchar i = 0,mm;
write_com(0x80);
i = 0;
while(dis1[i] != '\0')
{
write_data(dis1[i]);
i++;
}
startadc(0);
st=0;
oe=0;
st=1;
st=0;
while(eoc==0);
oe=1;
getdata=P2;
oe=0;
temp=getdata*1.0/255*500;
dispbuf[0]=temp%10;
dispbuf[1]=temp/10%10;
dispbuf[2]=temp/100%10;
dispbuf[3]=temp/1000;
display();
}
void ad0809_1()
{
uchar i = 0,mm;
write_com(0x80);
i = 0;
while(dis4[i] != '\0')
{
write_data(dis4[i]);
i++;
}
startadc(1);
st=0;
oe=0;
st=1;
st=0;
while(eoc==0);
oe=1;
getdata=P2;
oe=0;
temp=getdata*1.0/255*500;
dispbuf[0]=temp%10;
dispbuf[1]=temp/10%10;
dispbuf[2]=temp/100%10;
dispbuf[3]=temp/1000;
display();
}
void ad0809_2()
{
startadc(2);
st=0;
oe=0;
st=1;
st=0;
while(eoc==0);
oe=1;
getdata=P2;
oe=0;
temp=(getdata*1.0/255*500)*10;
dispbuf[0]=temp%10;
dispbuf[1]=temp/10%10;
dispbuf[2]=temp/100%10;
dispbuf[3]=temp/1000;
displayMA();
}
void wendu()
{ uchar i = 0,mm;
write_com(0x80);
i = 0;
while(dis3[i] != '\0')
{
write_data(dis5[i]);
i++;
}
temp =ReadTemperature();//读温度
dispbuf[1] = temp/10/16;
dispbuf[0] = temp/16%10;
dispbuf[2] = temp/16%10/10;
dispbuf[3] = temp/16%10/100;
write_com(0x80+0x40+2) ;
write_data(dispbitcode[dispbuf[1]]);
write_com(0x80+0x40+3) ;
write_data(dispbitcode[dispbuf[0]]);
write_com(0x80+0x40+4) ;
write_data(dis2[0]);
write_com(0x80+0x40+5) ;
write_data(dispbitcode[dispbuf[2]]);
write_com(0x80+0x40+6) ;
write_data(dispbitcode[dispbuf[3]]);
write_com(0x80+0x40+8) ;
write_data(dis2[4]);
write_com(0x80+0x40+9) ;
write_data(dis2[5]);
}
/*******************************************************************/
/* */
/* 通道选择 */
/* */
/*******************************************************************/
void startadc(uchar address)
{
PinC=(bit)(address&0x04); //选择输入通道
PinB=(bit)(address&0x02);
PinA=(bit)(address&0x01);
}
/*******************************************************************/
/* */
/* 温度测量 */
/* */
/*******************************************************************/
//延时函数
void delay22(unsigned int i)
{
while(i--);
}
//初始化函数
Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1; //DQ复位
delay22(8); //稍做延时
DQ = 0; //单片机将DQ拉低
delay22(80); //精确延时 大于 480us
DQ = 1; //拉高总线
delay22(14);
x=DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
delay22(20);
}
//读一个字节
ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; // 给脉冲信号
dat>>=1;
DQ = 1; // 给脉冲信号
if(DQ)
dat|=0x80;
delay22(4);
}
return(dat);
}
//写一个字节
WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
delay22(5);
DQ = 1;
dat>>=1;
}
//delay(4);
}
//读取温度
ReadTemperature(void)
{
unsigned char a=0;
unsigned char b=0;
unsigned int t=0;
float tt=0;
Init_DS18B20();
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
a=ReadOneChar();
b=ReadOneChar();
t=b;
t<<=8;
t=t|a;
tt=t*0.0625;
//t= tt*10+0.5; //放大10倍输出并四舍五入---此行没用
return(t);
}
/*******************************************************************/
/* */
/* 按键切换 */
/* */
/*******************************************************************/
void t2(void) interrupt 2 using 3
{
m++;
if(m == 4) m=0;
}
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit sk=P1^5;
sbit stcp1=P1^4;
sbit shcp1=P1^3;
sbit stcp2=P1^1;
sbit shcp2=P1^0;
sbit flag=P1^2;
unsigned char getdata;
unsigned int temp;
unsigned int temp1;
uchar m = 0;
sbit st=P1^6;
sbit oe=P3^1; //adc0809的引脚控制端
sbit eoc=P3^0;
sbit CLK=P1^7;
sbit DQ = P0^6;
sbit PinA=P0^0;
sbit PinB=P0^1;
sbit PinC=P0^2;
sbit P03=P0^3;
sbit P34=P3^4;
sbit P35=P3^5;
sbit P36=P3^6;
uchar code dis1[] = {"voltage of ch1:"};
uchar code dis4[] = {"voltage of ch2:"};
uchar code dis3[] = {"current of ch3:"};
uchar code dis5[] = {"tempera of ch4:"};
uchar code dis2[] = {".VmAC " };
unsigned char code dispbitcode[]={"0123456789"};
unsigned char dispbuf[4];
uchar DS[7];
uchar code td[]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07};//通道先择数组
uint ad_0809,ad_data1,ad_data2,ad_data3,ad_data0;
uchar m,number;
uchar x[8];//八通道数据待存数组
uchar num=0,j,y=0,k=0;
uint num2[2]={0,0},num1=0;
uchar n=0;
void init();
void delay(uint z);
void write_com(uchar com);
void write_data(uchar date);
void fc595(uchar a);
void fc5951(uchar b);
void startadc(uchar address);
void delaynms(uint x);//nms延时程序
void display();//显示程序
void ad0809();//芯片启动程序
void key();//键扫描程序
void ad0809_1();
void ad0809_2();
void displayMA();
void delay22(unsigned int i) ;
Init_DS18B20(void);
ReadOneChar(void) ;
WriteOneChar(unsigned char dat) ;
ReadTemperature(void);
void wendu();
void TimeInitial()
{ TMOD=0x10;
TH1=(65536-200)/256;
TL1=(65536-200)%256;
ET1=1;
TR1=1;
}
void t1(void) interrupt 3 using 0
{
TH1=(65536-200)/256;
TL1=(65536-200)%256;
CLK=~CLK;
}
/*******************************************************************/
/* */
/* 主函数 */
/* */
/*******************************************************************/
void main()
{
EX1 = 1;
IT1 = 1;
EA=1;
P1=0x00;
init();
TimeInitial();
while(1)
{
switch(m)
{
case 0:
ad0809();
break;
case 1:
ad0809_1();
break;
case 2:
ad0809_2();
break;
case 3:
wendu();
break ;
default : break;
}
}
}
/*******************************************************************/
/* */
/* 子函数 */
/* */
/*******************************************************************/
void init()
{
delay(15);
write_com(0x38);
delay(5);
write_com(0x38);
delay(5);
write_com(0x38);
write_com(0x38);
write_com(0x08);
write_com(0x01);
write_com(0x06);
write_com(0x0c);
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_data(uchar date)
{
fc5951(4);
fc595(date);
delay(5);
fc5951(5);
delay(5);
fc5951(4);
}
void write_com(uchar com)
{
fc5951(0);
fc595(com);
delay(5);
fc5951(1);
delay(5);
fc5951(0);
}
void fc595(uchar a)
{
for(j=0;j<8;j++)
{
DS[j]=a%0x02;
a=a/0x02;
}
stcp1=0;
for(j=8;j>0;j--)
{
sk=DS[j-1];
shcp1=0;
shcp1=1;
}
stcp1=0;
stcp1=1;
}
void fc5951(uchar b)
{
stcp2=0;
for(j=0;j<7;j++)
{
flag=b%0x02;
b=b/0x02;
shcp2=0;
shcp2=1;
}
stcp2=0;
stcp2=1;
}
/*******************************************************************/
/* */
/* ADC0809的子函数 */
/* */
/*******************************************************************/
//nms 延时程序
void delaynms(uint x)
{
uchar i;
while(x-->0)
{
for(i=0;i<125;i++)
{;}
}
}
/*******************************************************************/
/* */
/* 显示程序 */
/* */
/*******************************************************************/
void display()
{
write_com(0x80+0x40+2) ;
write_data(dispbitcode[dispbuf[3]]);
write_com(0x80+0x40+3) ;
write_data(dispbitcode[dispbuf[2]]);
write_com(0x80+0x40+4) ;
write_data(dis2[0]);
write_com(0x80+0x40+5) ;
write_data(dispbitcode[dispbuf[1]]);
write_com(0x80+0x40+6) ;
write_data(dispbitcode[dispbuf[0]]);
write_com(0x80+0x40+8) ;
write_data(dis2[1]);
write_com(0x80+0x40+9) ;
write_data(dis2[6]);
}
void displayMA()
{
uchar i = 0,mm;
write_com(0x80);
i = 0;
while(dis3[i] != '\0')
{
write_data(dis3[i]);
i++;
}
write_com(0x80+0x40+2) ;
write_data(dispbitcode[dispbuf[3]]);
write_com(0x80+0x40+3) ;
write_data(dispbitcode[dispbuf[2]]);
write_com(0x80+0x40+4) ;
write_data(dis2[0]);
write_com(0x80+0x40+5) ;
write_data(dispbitcode[dispbuf[1]]);
write_com(0x80+0x40+6) ;
write_data(dispbitcode[dispbuf[0]]);
write_com(0x80+0x40+8) ;
write_data(dis2[2]);
write_com(0x80+0x40+9) ;
write_data(dis2[3]);
}
void ad0809()
{
uchar i = 0,mm;
write_com(0x80);
i = 0;
while(dis1[i] != '\0')
{
write_data(dis1[i]);
i++;
}
startadc(0);
st=0;
oe=0;
st=1;
st=0;
while(eoc==0);
oe=1;
getdata=P2;
oe=0;
temp=getdata*1.0/255*500;
dispbuf[0]=temp%10;
dispbuf[1]=temp/10%10;
dispbuf[2]=temp/100%10;
dispbuf[3]=temp/1000;
display();
}
void ad0809_1()
{
uchar i = 0,mm;
write_com(0x80);
i = 0;
while(dis4[i] != '\0')
{
write_data(dis4[i]);
i++;
}
startadc(1);
st=0;
oe=0;
st=1;
st=0;
while(eoc==0);
oe=1;
getdata=P2;
oe=0;
temp=getdata*1.0/255*500;
dispbuf[0]=temp%10;
dispbuf[1]=temp/10%10;
dispbuf[2]=temp/100%10;
dispbuf[3]=temp/1000;
display();
}
void ad0809_2()
{
startadc(2);
st=0;
oe=0;
st=1;
st=0;
while(eoc==0);
oe=1;
getdata=P2;
oe=0;
temp=(getdata*1.0/255*500)*10;
dispbuf[0]=temp%10;
dispbuf[1]=temp/10%10;
dispbuf[2]=temp/100%10;
dispbuf[3]=temp/1000;
displayMA();
}
void wendu()
{ uchar i = 0,mm;
write_com(0x80);
i = 0;
while(dis3[i] != '\0')
{
write_data(dis5[i]);
i++;
}
temp =ReadTemperature();//读温度
dispbuf[1] = temp/10/16;
dispbuf[0] = temp/16%10;
dispbuf[2] = temp/16%10/10;
dispbuf[3] = temp/16%10/100;
write_com(0x80+0x40+2) ;
write_data(dispbitcode[dispbuf[1]]);
write_com(0x80+0x40+3) ;
write_data(dispbitcode[dispbuf[0]]);
write_com(0x80+0x40+4) ;
write_data(dis2[0]);
write_com(0x80+0x40+5) ;
write_data(dispbitcode[dispbuf[2]]);
write_com(0x80+0x40+6) ;
write_data(dispbitcode[dispbuf[3]]);
write_com(0x80+0x40+8) ;
write_data(dis2[4]);
write_com(0x80+0x40+9) ;
write_data(dis2[5]);
}
/*******************************************************************/
/* */
/* 通道选择 */
/* */
/*******************************************************************/
void startadc(uchar address)
{
PinC=(bit)(address&0x04); //选择输入通道
PinB=(bit)(address&0x02);
PinA=(bit)(address&0x01);
}
/*******************************************************************/
/* */
/* 温度测量 */
/* */
/*******************************************************************/
//延时函数
void delay22(unsigned int i)
{
while(i--);
}
//初始化函数
Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1; //DQ复位
delay22(8); //稍做延时
DQ = 0; //单片机将DQ拉低
delay22(80); //精确延时 大于 480us
DQ = 1; //拉高总线
delay22(14);
x=DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
delay22(20);
}
//读一个字节
ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; // 给脉冲信号
dat>>=1;
DQ = 1; // 给脉冲信号
if(DQ)
dat|=0x80;
delay22(4);
}
return(dat);
}
//写一个字节
WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
delay22(5);
DQ = 1;
dat>>=1;
}
//delay(4);
}
//读取温度
ReadTemperature(void)
{
unsigned char a=0;
unsigned char b=0;
unsigned int t=0;
float tt=0;
Init_DS18B20();
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
a=ReadOneChar();
b=ReadOneChar();
t=b;
t<<=8;
t=t|a;
tt=t*0.0625;
//t= tt*10+0.5; //放大10倍输出并四舍五入---此行没用
return(t);
}
/*******************************************************************/
/* */
/* 按键切换 */
/* */
/*******************************************************************/
void t2(void) interrupt 2 using 3
{
m++;
if(m == 4) m=0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询