I am currently testing SPI communication in ADS1148 evaluation kit and therefore trying to read back through SPI after sending values. I first send the SDATAC command = 0x16 and then send the write command 0x40 and then send 0x01. I have interfaced my microcontroller(Silabs C8051f020) with the pins of ADS1148 which are CS, SCLK, DIN, DRDY, DOUT/DRDY, START and RESET. I wanted to just check the SPI communication before starting to work on reading ADC values. But I am not able to read back the values that I have sent. For example if I have sent value = 0x40,0x01 I am getting value = 0x02 in variable valcheck instead of 0x01. I have checked the signals of SPI with oscilloscope and I am able to change the clk signal as required between 0 and 3.2V. I am also sending my program which is as below:
{
ChipSelect(LOW); //Now CS high to low indicating SPI communication enabled
STARTPIN = 1;
valcheck = ReadSPI();
WriteSPI(0x16,VALUE);
WriteSPI(0x00,COMMAND);
WriteSPI(0x01,VALUE);
valcheck = ReadSPI();
}
//////actual bit banging of SPI port in DOUT///////////
void WriteSPI(unsigned char dataout,unsigned char action)
{
unsigned char Counter;
void DelayTimer(unsigned int Delay);
unsigned char tempchar;
//DAT_OUT = 0;
if(action == COMMAND)
tempchar = 0x40|dataout;
else
tempchar = dataout;
//data sent for 8 bits into DATA IN line
for(Counter=0; Counter<8; Counter++)
{
CY = 0;
ACC=tempchar;
//ACC=0xff;
ACC = ACC << 1;
//data to be sent is given MSB first bitwise
DAT_IN = CY;
tempchar=ACC;
//delay of 1ms
DelayTimer(10);
//serialclock made high
SCLK = 1;
DelayTimer(10);
//serial clock made low. At falling edge of clock data is taken in through DATA IN line by ADS1148
SCLK = 0;
//DelayTimer(100);
//delay(1);
//SCLK = 1;
//delay(1);
}
DAT_IN = 0;
}
unsigned char ReadSPI(void)
{
unsigned char Counter,tempchar;
//initially tepchar is zero. any bit to be zadded can be ored into it
tempchar = 0;
//initially serial clock is 0
SCLK = 0;
//data recieved bit wise for 8 bits
for(Counter=0; Counter<8; Counter++)
{
//delay of 1ms
DelayTimer(10);
//serial clock made high. Data recieved on rising edge
SCLK = 1;
CY = 0;
//delay of 1ms
DelayTimer(10);
//data recieved through DATA OUT line into carry bitwise
CY = DAT_OUT;
if(CY == 1)
{
//if cy = 1,i.e. bit recieved = 1 then put that bit in LSB
tempchar |=1;
}
else
//else keep the bit = 0
{
tempchar |=0;
}
SCLK = 0;
//Counter value shows bit position. If bit position is less than 7 i.e. data is still to be recieved
if(Counter<7)
//shift byte containing bits recieved by 1
tempchar = tempchar << 1;
}
return tempchar;
}
void DelayTimer(unsigned int delay)
{
Timer1_Init(delay);
while(TF1 == 0);
TF1 = 0;
}
void ChipSelect(unsigned char state)
{
DelayTimer(10);
if(state == HIGH)
CS = 1;
else
CS = 0;
DelayTimer(10);
}
Please reply whether my program is OK? What else I should check or change?