#include<stdio.h>
#include<aduc841.h>
/******************************************************************
DEFINE CONTROL PINS OF ADUC841 FOR THE PURPOSE OF AD7792 CONTROL.
Customers should define the pins according to their design.
******************************************************************/
sbit CS=0x0A0;
sbit SCLOCK=0x0A6;
sbit DIN=0x0A2;
sbit DOUT=0x0A3;
unsigned char DataRead[3];
void WriteToReg(unsigned char ByteData);
void ReadFromReg(unsigned char nByte);
void Delay(unsigned int Time)
{
while(Time)
{
Time--;
}
}
void main()
{
int ResetTime;
/* Set up UART */
T3CON = 0x086;
T3FD = 0x08;
SCON = 0x052;
printf("Hello\r\n");
/* PRECONFIGURE...*/
ResetTime=32;
SCLOCK=1;
CS=0; //to keep DIN=1 for 32 sclock to reset the part
DIN=1;
while(ResetTime--)
{
Delay(10);
SCLOCK=0;
Delay(10);
SCLOCK=1;
}
CS=1;
printf("Reset\r\n");
WriteToReg(0x10); //write to Communication register.The next step is writing to Configuration register.
WriteToReg(0x00); //set the Configuration bipolar mode.Gain=1.
WriteToReg(0x80); //Configuration internal reference selected.
while(1)
{
WriteToReg(0x08);//write to Communication register.The next step is writing to Mode register.
WriteToReg(0x20);//set the mode register as single conversion mode.
WriteToReg(0x00);//inter 64 kHZ clock.internal clock is not available at the clk pin.
WriteToReg(0x40);//write to Communication register.The next step is to read from Status register.
ReadFromReg(1);
while((DataRead[0]&0x80)==0x80)//wait for the end of convertion by polling the status register RDY bit
{
WriteToReg(0x40);
ReadFromReg(1);
}
WriteToReg(0x58);//write to Communication register.The next step is to read from Data register.
ReadFromReg(2);
printf("Data:%02BX %02BX\r\n",DataRead[0],DataRead[1]);
}
}
void WriteToReg(unsigned char ByteData) // write ByteData to the register
{
unsigned char temp;
int i;
CS=0;
temp=0x80;
for(i=0;i<8;i++)
{
if((temp & ByteData)==0)
{
DIN=0;
}
else
{
DIN=1;
}
SCLOCK=0;
Delay(10);
SCLOCK=1;
Delay(10);
temp=temp>>1;
}
CS=1;
}
void ReadFromReg(unsigned char nByte) // nByte is the number of bytes which need to be read
{
int i,j;
unsigned char temp;
DIN=1;
CS=0;
temp=0;
DOUT=1;
for(i=0; i<nByte; i++)
{
for(j=0; j<8; j++)
{
SCLOCK=0;
if(DOUT==0)
{
temp=temp<<1;
}else
{
temp=temp<<1;
temp=temp+0x01;
}
Delay(10);
SCLOCK=1;
Delay(10);
}
DataRead[i]=temp;
temp=0;
}
CS=1;
}