#include”reg52.h”
sbit SCLK=P1^5;
sbit MISO=P3^5;
sbit MOSI=P3^2;
unsigned long int ADS1255_data;
//接收一字节程序
unsigned char SPIreceiveByte()
{
unsignedchar idata n=8;
unsigned char tdata;
SCLK=1;
while(n--)
{
delayNOP();
SCLK=0;
delayNOP();
tdata=tdata<<1;
if(MISO==1)
tdata=tdata|0x01;
else
tdata=tdata&0xfe;
SCLK=1;
}
return(tdata);
}
//发送一字节程序
void SPISendByte(unsigned char ch)
{
unsignedchar idata n=8;
SCLK=1;
while(n--)
{
delayNOP();
SCLK=0;
if((ch&0x80)==0x80)
{
MOSI=1;
}
else
{
MOSI=0;
}
delayNOP();
ch=ch<<1;
SCLK=1;
}
}
/******************写入多个字节**************************/
void do_n_byte(uint length,uint*pSend)
{
while(length--)
{
SPISendByte(*pSend);
pSend++;
}
}
/*****************读出一个24位数据********************************/
unsigned long int SPIreceive24Bits()
{
ucharsend[]={0x03,0xff,0xff,0xff};
unsignedlong int tdata;
do_n_byte(sizeof(send)-3,send); //
delayNOP(); //¿
delayNOP();
delayNOP();
send[1]=SPIreceiveByte(); //MSB
send[2]=SPIreceiveByte(); //Mid-Byte
send[3]=SPIreceiveByte(); //LSB
tdata=send[1]*65536+send[2]*256+send[3];
return(tdata);
}
/********************************************************/
//校准
/********************************************************/
void set_selfcal(void)
{
SPISendByte(0xf0); //lfcal command byte(in order to get the bestperformance)
}
void set_wakeup(void)
{
SPISendByte(0x00); //wakeup command byte(0xff can too)
}
void set_standby(void)
{
SPISendByte(0xfd);
}
void set_sync(void)
{
SPISendByte(0xfc);
}
void set_reset(void)
{
SPISendByte(0xfe);
}
/**********************往寄存器中写数据*********************************/
void write_register(uchar addr,uchardate)
{
uchar send[] = {0x50,0x00,0x00};//write 1 register(as used 0x00)
send[0] |= addr;//star
ting from register addr(as used 0101"addr"b)
send[2] |= date;//data writed
do_n_byte(sizeof(send),send);
}
/*******************从寄存器中读数据*************************/
uchar read_register(uchar addr)
{
uchar send[] = {0x10,0x00,0xff}; //read 1 register(as used 0x00)
send[0] |= addr; //starting from register addr(as used 0001"addr"b)
do_n_byte(sizeof(send)-1,send);
delayNOP();
delayNOP();
delayNOP();
send[2]=SPIreceiveByte(); //½«¼Ä´æÆ÷ÖеÄÊý¾Ý¶Á³öÀ´
return send[2]; //content saved in the send[2]
}
/********************初始化ADS1255********************/
void init_ads1255(void)
{
write_register(0x00,0xf6);
write_register(0x01,0x01);
write_register(0x02,0x00);
write_register(0x03,0xf0);
}
/***********************ads255校准*****************************/
void SPIcalibration(void)
{
set_sync();
delayNOP();
set_wakeup();
delayNOP();
set_selfcal();
delayNOP();
set_standby();
delayNOP();
}
void main()
{
init_ads1255();
SPIcalibration();
ADS1255_data=SPIreceive24Bits()
}
一周热门 更多>