#include "cs5523.h"
uchar idata PartSelect = 1;
////////////////cs5532 command/////////////////////////////
uchar idata RWIOCR ;
uchar idata RWIGRR ;
uchar idata RWCR ;
uchar idata RWCSR ;
////////////////////
uchar idata PerformConver ; // 执行转换
uchar idata PerformCalib ; // 执行校准
uchar idata Sync1 ; // 串行口重新部分初始化顺序
uchar idata Sync0 ; // 串行口结束部分初始化顺序
uchar idata Null ; // 清除口标志,保持系统处于连续的转换模式
/////////////////////////////////////////////
uchar idata ReadBuffer[12];
void ResetChip();
uchar idata OutRate;
////////////////////////////////////////////
uchar SPI_HostReadByte()
{
uchar idata i,rByte;
rByte = 0;
for(i=0;i<8;i++)
{
SPISCK = 0;
SPISCK = 1;
rByte <<= 1;
rByte |= SPISO;
}
return rByte;
}
///////////////////////////////////
void SPI_HostWriteByte(uchar wByte)
{
uchar idata i;
for(i=0;i<8;i++)
{
if(((wByte << i ) & 0x80) == 0x80)
{
SPISI = 1;
}
else
{
SPISI = 0;
}
SPISCK = 0;
SPISCK = 1;
}
}
////////////////////////////////////
void Cs5523ReadAdData()
{
uchar i;
SPISCK = 0;
NOP;
if (PartSelect == 1)
{
CS1 = 0;
CS2 = 1;
}
else if (PartSelect == 2)
{
CS1 = 1;
CS2 = 0;
}
else
{
CS1 = 1;
CS2 = 1;
}
PerformConver = 0x98;
SPI_HostWriteByte(PerformConver);
SPISO = 1;
while(SPISO == 1);
PerformConver = 0x00;
SPI_HostWriteByte(PerformConver);
for (i = 0;i < 12 ;i++)
{
ReadBuffer[i]=SPI_HostReadByte();
}
}
///////////////////////////////////
void Cs5523WriteData(uchar GeShu)
{
uchar i;
for (i = 0;i < GeShu ;i++)
{
SPI_HostWriteByte(ReadBuffer[i]);
}
}
//////////////////////////////////
void WriteCommand(uchar Commandcs)
{
SPI_HostWriteByte(Commandcs);
}
/////////////////////////////////
void Cs5523Init()
{
uchar idata j;
bit flag22 = 0;
InitT0();
TR0=1;
ResetFlag = 0;
delayms(65000);
delayms(65000);
SPISCK = 0;
flag22 = 0;
SPISCK = 0;
CS1 = 0;
/////////////////////////////////////////////////////////////
while(flag22 == 0)
{
RWCR = 0x0b;
WriteCommand(RWCR);
ReadBuffer[0] = SPI_HostReadByte();
ReadBuffer[1] = SPI_HostReadByte();
ReadBuffer[2] = SPI_HostReadByte();
EA = 0;
for(j = 0;j<3;j++)
{
SBUF = ReadBuffer[j];
while (TI == 0);
TI = 0;
}
EA = 1;
if ((ReadBuffer[0] == 0x00)&&(ReadBuffer[1] == 0x00)&&(ReadBuffer[2] == 0xC0))
{
flag22 = 1;
RWCR = 0x03;
ReadBuffer[0] = 0x00;
ReadBuffer[1] = 0x00;
ReadBuffer[2] = 0x00;
WriteCommand(RWCR);
Cs5523WriteData(3);
P24 = !P24;
}
else
{
flag22 = 0;
ResetChip();
}
}
///////////////////////////////////////
ReadBuffer[0] = 0x04;
ReadBuffer[1] = 0x30;
ReadBuffer[2] = 0x00;
RWCR = 0x03;
WriteCommand(RWCR);
Cs5523WriteData(3);
//////////////////////
delayms(1000);
ReadBuffer[0] = 0x07;
ReadBuffer[1] = 0xb0;
ReadBuffer[2] = 0x7b; //7.5
ReadBuffer[3] = 0x0f;
ReadBuffer[4] = 0xb0;
ReadBuffer[5] = 0xfb;
RWCSR = 0x05;
WriteCommand(RWCSR);
Cs5523WriteData(6);
delayms(1000);
PerformCalib = 0x81;
WriteCommand(PerformCalib);
SPISO = 1;
while(SPISO == 1);
delayms(1000);
PerformCalib = 0x82;
WriteCommand(PerformCalib);
SPISO = 1;
while(SPISO == 1);
delayms(1000);
}
///////////////////////////////////////
void delayms(uint tickms)
{
uchar idata count,i;
_nop_();
_nop_();
_nop_();
for(i=tickms;i>0;i--)
{ for(count=0;count<=58;count++) {;}
_nop_();
}
}
///////////////////////////////////////
void ResetChip()
{
P24 = !P24;
//////////////////////////////
WriteCommand(0xff);
delayms(1000);
WriteCommand(0xfe);
//////////////////////////////
delayms(60000);
RWCR = 0x03;
ReadBuffer[0] = 0x00;
ReadBuffer[1] = 0x00;
ReadBuffer[2] = 0x80;
WriteCommand(RWCR);
Cs5523WriteData(3);
delayms(1000);
WriteCommand(0xff);
delayms(1000);
WriteCommand(0xfe);
}