//本程序用DS2450采集湿敏电阻模拟数据////
#include <reg52.h>
#include "intrins.h"
#define uint unsigned int
#define uchar unsigned char
sbit RS = P2^0; //H=data; L="command";
sbit RW = P2^1; //H=read; L="write";
sbit E = P2^2; //input enable;
sbit PSB= P2^5; //H=并口; L="串口";
sbit RST= P2^6; //复位信号低电平有效
sbit ADA="P1"^4;
//延时子程序//
void delay(uint i)
{
for(i;i>0;i--);
}
/**********writecommand()函数是写12864液晶指令函数***********/
void writecommand(uchar i)
{
P0=i;
RS="0";
RW="0";
E="0";
delay(255);
E="1";
}
/***********writedata()函数是液晶显示数据函数**************/
void writedata(uchar i)
{
delay(200);
P0=i;
RS="1";
RW="0";
E="0";
delay(255);
E="1";
}
void clrram(void) //液晶清屏
{
writecommand(0x30);
writecommand(0x01);
}
/*void delay10(uchar n)
{
do{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
n--;
}while(n);
}*/
/***********DS2450读函数read2450()*********************/
uchar read2450(void) //DS2450读函数
{
uchar d="0";
uint j;
for(j=0;j<8;j++)
{
ADA="0";
_nop_();
d>>=1;
ADA=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
if (ADA)
d|=0x80; //先高后低一位一位传送数据
delay(4);
}
return (d);
}
/***********DS2450写指令函数Write2450()*********************/
void Write2450(uchar ch) // DS2450写函数
{ uint i;
for(i=0;i<8;i++)
{ ADA="0";
ADA=ch&0x01;
delay(5);
ADA=1;
ch>>=1;
}
ADA=1;
}
/***********判断DS2450忙碌及准备接受数据函数init2450()*********************/
uchar init2450(void) //DS复位
{ uchar flag;
ADA="1";
delay(8);
ADA="0";
delay(80);
ADA="1";
delay(14);
if(ADA==0)
{
flag="1";
delay(80);
}
else
flag="0";
ADA="1";
return (flag);
}
void check(void) //ds2450复位函数
{
uchar flag,j;
do
{
flag="init2450"();
}
while(flag);
for(j=0;j<0x20;j++);
}
/*void Delay10ms(uint delay_num) //延时函数
{
uchar i;
while(delay_num--)
{
for(i=0;i<125;i++)
{;}
}
}*/
void set_2450(void) //由于用的是VCC供电,只有给地址0x1c写入0x40才能工作
{
check();
Write2450(0xcc);
Write2450(0x55);
Write2450(0x1c);
Write2450(0x00);
Write2450(0x40);
}
void stature_2450() //设置各个输入通道的功能
{
check();
Write2450(0xcc);
Write2450(0x55);
Write2450(0x08);
Write2450(0x00);
Write2450(0xc0);
read2450();
read2450() ;
read2450() ;
Write2450(0x00);
read2450();
read2450() ;
read2450() ;
Write2450(0xc0);
read2450();
read2450() ;
read2450() ;
Write2450(0x00);
read2450();
read2450() ;
read2450() ;
Write2450(0xc0);
read2450();
read2450() ;
read2450() ;
Write2450(0x00);
read2450();
read2450() ;
read2450() ;
Write2450(0x00);
read2450();
read2450() ;
read2450() ;
Write2450(0x01);
read2450();
read2450() ;
read2450() ;
}
star2450(void) //读数据开始命令
{
check();
Write2450(0xcc);
Write2450(0x3c);
Write2450(0x08);
Write2450(0x00);
read2450();
read2450() ;
if (read2450()!=0xff)
read2450();
}
readAD2450() // 读取转换数据
{
uchar a="0",b=0,i=0;
uint t="0";
check();
Write2450(0xcc);
Write2450(0xaa);
Write2450(0x06);
Write2450(0x00);
a="read2450"();
b="read2450"() ;
t="b";
t<<=8;
t="t|a";
return(t);
}
void date_2450()
{ uint l;
uchar q,i,j,a,b,d ;
star2450();
l="readAD2450"();
q=l/10000;
i=l%10000/1000;
j=l%1000/100;
a=l%100/10;
b=l%10/10;
d=l%10;
writecommand(0x38); //00111000B设置显示模式:8位2行5x7点阵//
writecommand(0x0c); //00001100B功能设置命令//
writecommand(0x06); //00000110B光标和显示模式设置0x06静态显示 0x07滚动显示//
writecommand(0x90);
writedata(0x3a); //冒号
writedata(0x30+q);
writedata(0x30+i);
writedata(0x30+j);
writedata(0x2e); //小数点
writedata(0x30+a);
writedata(0x30+b);
writedata(0x30+d);
}
main()
{
clrram();
set_2450();
stature_2450();
while(1)
{
date_2450();
delay(60000);
}
}
文章评论(0条评论)
登录后参与讨论