ADS1100的51单片机程序
#include<reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
/*****ADS1100****/
sbit Sda=P3^0;
sbit Scl=P3^1;
/****12864*****/
sbit rs=P2^0;
sbit rw=P2^1;
sbit psb=P2^2;
sbit e=P2^5;
sbit rst=P2^4;
sbit WEI=P2^7;//定义位选
void delay_xms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=112;j>0;j--);
}
/***************12864display********************/
write_com(uchar com)
{
rs=0;
rw=0;
delay_xms(5);
P0=com;
e=1;
delay_xms(5);
e=0;
}
write_date(uchar date)
{
rs=1;
rw=0;
delay_xms(5);
P0=date;
e=1;
delay_xms(5);
e=0;
}
void init_12864()
{
P0=0xff;
WEI=1;
WEI=0;
rs=0;
rw=0;
e=0;
psb=1;
rst=1;
delay_xms(50);
write_com(0X30);
delay_xms(1);
write_com(0X30);
delay_xms(1);
write_com(0X0C);
delay_xms(1);
write_com(0X01);
delay_xms(15);
write_com(0X06);
delay_xms(1);
}
/***************AD TRANSFER*********************/
/*------------------------------------------------
启动IIC总线
------------------------------------------------*/
void start(void)
{
Sda=1;
_nop_();
Scl=1;
_nop_();
Sda=0;
_nop_();
Scl=0;
}
/*------------------------------------------------
停止IIC总线
------------------------------------------------*/
void stop(void)
{
Sda=0;
_nop_();
Scl=1;
_nop_();
Sda=1;
_nop_();
Scl=0;
}
/*------------------------------------------------
应答IIC总线
------------------------------------------------*/
void ack(void)
{
Sda=0;
_nop_();
Scl=1;
_nop_();
Scl=0;
_nop_();
}
/*------------------------------------------------
非应答IIC总线
------------------------------------------------*/
void noack(void)
{
Sda=1;
_nop_();
Scl=1;
_nop_();
Scl=0;
_nop_();
}
/*------------------------------------------------
发送一个字节
------------------------------------------------*/
void send(unsigned char Data)
{
unsigned char BitCounter=8;
unsigned char temp;
do
{
temp=Data;
Scl=0;
_nop_();
if((temp&0x80)==0x80)
Sda=1;
else
Sda=0;
Scl=1;
temp=Data<<1;
Data=temp;
BitCounter--;
}
while(BitCounter);
Scl=0;
}
/*------------------------------------------------
读入一个字节并返回
------------------------------------------------*/
unsigned char read(void)
{
unsigned char temp=0;
unsigned char temp1=0;
unsigned char BitCounter=8;
Sda=1;
do
{
Scl=0;
_nop_();
Scl=1;
_nop_();
if(Sda)
temp=temp|0x01;
else
temp=temp&0xfe;
if(BitCounter-1)
{
temp1=temp<<1;
temp=temp1;
}
BitCounter--;
}
while(BitCounter);
return(temp);
}
void display(long k)
{
unsigned int temp;
unsigned long int v;
v=3125*k ;
v=v/2048;
temp=v/10000;
write_com(0x80);
write_date(0x30+temp);
temp=v/1000%10;
write_date(0x30+temp);
temp=v/100%10;
write_date(0x30+temp);
temp=v/10%10;
write_date(0x30+temp);
write_date(0x2e);
temp=v%10;
write_date(0x30+temp);
}
void main()
{
short m,n;
long k;
init_12864();
while(1)
{
start();
send(0x90);
ack();
send(0x8c);
ack();
stop();
start();
send(0x91);
ack();
m=read();
ack();
n=read();
noack();
stop();
k=(0x00ff & m)<<8|n;
display(k);
}
}
|