Hi,
I am trying to interface ADC-cs5463 with P89v51rd2 micro controller using "bit banging SPI" code but not getting any result.
I am using below code if any one can see the code and if i am making some mistake or doing wrong than reply me.
Please help me to find the problem.
#include "p89v51rx2.h" #include <stdlib.h> #include <stdio.h> #include <string.h> #include <math.h>
//-----------------------------------ADC CS5463------------------ sbit nreset = P3^3; //RESET PIN OF CS5463 sbit cs = P3^4; //CHIP SELECT OF CS5463 sbit sclk = P3^7; //SERIAL CLOCK sbit sdo = P3^6; //SERIAL DATA OUT sbit sdi = P3^5; //SERIAL DATA IN
unsigned char sec_cnt=0; unsigned long longVar=0;
unsigned char mdatahigh; unsigned char mdatamid; unsigned char mdatalow;
//--------------------- Functions Related to CS5463 --------------------------------- void init_cs5463(void);
void main(void) {
init_cs5463();//initiallises the cs to function as per our requirements
}
void init_cs5463(void) {
write_to_reg(0x40,PCB,0x80,0x61); //write to config register(0x40)
cs=0;
transfer_byte(0xe8); // start continuous conversions..
cs=1;
//Here is my while loop and try to read the CS5463 register and disply it,but not able //to get any response from the it.
longVar = 0;
while(1)
{
read_a_reg(0x40,(unsigned char *)&longVar);
float2str(longVar,&disp_buffer[4],0);
float2str(mdatahigh,&disp_buffer[16],0);
float2str(mdatamid,&disp_buffer[19],0);
float2str(mdatalow,&disp_buffer[22],0);
void transfer_byte(unsigned char datum) {
unsigned char count,tempbyte;
sclk=0;
tempbyte=0x80;
for(count=0;count<8;count++) {
if(datum & tempbyte) {
sdi=1;
else {
sdi=0;
sclk=1;
tempbyte=tempbyte>>1;
delay(10);
unsigned char receive_byte(void) {
unsigned char count,tempbyte,datal;
datal=0x80;
tempbyte=0;
sdo=1;
if(sdo) {
tempbyte|=datal;
} sclk=1;
datal=datal>>1;
return(tempbyte);
void read_a_reg(unsigned char command,unsigned char * ptr) {
transfer_byte(command);
mdatahigh=receive_byte();
mdatamid=receive_byte();
mdatalow=receive_byte();
*ptr=mdatalow;
ptr++;
*ptr=mdatamid;
*ptr=mdatahigh;
void write_to_reg(unsigned char command,unsigned char high,unsigned char mid,unsigned char low) {
transfer_byte(high);
transfer_byte(mid);
transfer_byte(low);
Hope this will ok with you.
I really need the solution, it's urgency.
Hope experts will help.
#include "p89v51rx2.h" #include <stdlib.h> #include <stdio.h> #include <string.h> #include <math.h> //-----------------------------------ADC CS5463------------------ sbit nreset = P3^3; //RESET PIN OF CS5463 sbit cs = P3^4; //CHIP SELECT OF CS5463 sbit sclk = P3^7; //SERIAL CLOCK sbit sdo = P3^6; //SERIAL DATA OUT sbit sdi = P3^5; //SERIAL DATA IN unsigned char sec_cnt=0; unsigned long longVar=0; unsigned char mdatahigh; unsigned char mdatamid; unsigned char mdatalow; //--------------------- Functions Related to CS5463 --------------------------------- void init_cs5463(void); void read_a_reg(unsigned char command,unsigned char * ptr); void transfer_byte(unsigned char datum); unsigned char receive_byte(void); void write_to_reg(unsigned char command,unsigned char high,unsigned char mid,unsigned char low); void main(void) { init_cs5463();//initiallises the cs to function as per our requirements } void init_cs5463(void) { write_to_reg(0x40,PCB,0x80,0x61); //write to config register(0x40) cs=0; transfer_byte(0xe8); // start continuous conversions.. cs=1; //Here is my while loop and try to read the CS5463 register and disply it,but not able to get any response from the it. longVar = 0; while(1) { read_a_reg(0x40,(unsigned char *)&longVar); float2str(longVar,&disp_buffer[4],0); float2str(mdatahigh,&disp_buffer[16],0); float2str(mdatamid,&disp_buffer[19],0); float2str(mdatalow,&disp_buffer[22],0); } } void transfer_byte(unsigned char datum) { unsigned char count,tempbyte; sclk=0; tempbyte=0x80; for(count=0;count<8;count++) { if(datum & tempbyte) { sdi=1; } else { sdi=0; } sclk=1; sclk=0; tempbyte=tempbyte>>1; } sdi=1; delay(10); } unsigned char receive_byte(void) { unsigned char count,tempbyte,datal; sdi=1; datal=0x80; tempbyte=0; sdo=1; for(count=0;count<8;count++) { if(sdo) { tempbyte|=datal; } sclk=1; sclk=0; datal=datal>>1; } return(tempbyte); } void read_a_reg(unsigned char command,unsigned char * ptr) { cs=0; transfer_byte(command); mdatahigh=receive_byte(); mdatamid=receive_byte(); mdatalow=receive_byte(); cs=1; *ptr=mdatalow; ptr++; *ptr=mdatamid; ptr++; *ptr=mdatahigh; } void write_to_reg(unsigned char command,unsigned char high,unsigned char mid,unsigned char low) { cs=0; transfer_byte(command); transfer_byte(high); transfer_byte(mid); transfer_byte(low); cs=1; }
Now why would you - even for testing - hide an infinite loop inside init_cs5463()? Don't you think that your infinite loop should be in main()?
You have a function transfer_byte() that sends data - why then the generic name "transfer"?
You haven't defined somewhere what phase and polarity you are intending to have.
Are you aware that SPI is always dual-direction? When you listen, there is still data being sent. When you send, there is also data being received.
Why do you invent new names for signals, instead of using the traditional MOSI and MISO?
sbit sdo = P3^6; //SERIAL DATA OUT sbit sdi = P3^5; //SERIAL DATA IN
Have you verified all timing?