#include <reg52.h>
#include "intrins.h"
#include <stdio.h>
#include "lcd1602.h"
sbit CLOCK=P2^3;//时钟
sbit D_IN=P2^2;//地址
sbit D_OUT=P2^1;//数据输出
sbit _CS=P2^0;//片选
typedef unsigned char BYTE;
typedef unsigned int WORD;
BYTE flag,aa;
void init()
{
TMOD=0X20;
TH1=0XFD;
TL1=0XFD;
TR1=1;
SM0=0;
SM1=1;
REN=1;
EA=1;
ES=1;
RI=0;
TI=0;
}
void comm(char *pp)
{ ES=0;
do
{
SBUF=*pp++;
while(!TI);
TI=0;
}while(*pp);
ES=1;
}
void ser()interrupt 4
{
ES=0;
while(!RI);
RI=0;
aa=SBUF;
flag=1;
ES=1;
}
WORD read1543(BYTE port)
{
WORD data ad;
WORD data i;
BYTE data al=0,ah=0;
CLOCK=0;
_CS=0;
port<<=4;
for (i=0;i<4;i++) //
{
D_IN=(bit)(port&0x80);CLOCK=1;CLOCK=0;
port<<=1;
}
for (i=0;i<6;i++) //??6?CLOCK
{
CLOCK=1;CLOCK=0;
}
_CS=1;
_nop_();_nop_();
_CS=0; //
for (i=0;i<2;i++) //
{
D_OUT=1;
CLOCK=1;
ah<<=1;
if (D_OUT) ah|=0x01;
CLOCK=0;
}
for (i=0;i<8;i++) //D7--D0
{
D_OUT=1;
CLOCK=1;
al<<=1;
if (D_OUT) al|=0x01;
CLOCK=0;
}
_CS=1;
ad=(WORD)ah;ad<<=8;ad|=al; //AD?
return (ad);
}
float count(BYTE ch)
{
float temp;
temp=read1543(ch)*5.00;//5.00为参考电压 1024为10位adc的
temp=temp/1023;
return temp;
}
void Delay500ms() [url=]//@11.0592MHz[/url]
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 22;
j = 3;
k = 227;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main()
{
float val;
BYTE buff[5];
init();
lcd_init();
while(1)
{
val=count(0);
sprintf(buff,"%.2f",val);
comm(buff);
comm("\n");
// zifu_disp(0,0,buff);
Delay500ms();
}
|