Keil Logo

P89v51rd2 spi not working-help

Next Thread | Thread List | Previous Thread Start a Thread | Settings

Details Message
Read-Only
Author
japs ele
Posted
8-Apr-2011 07:28 GMT
Toolset
C51
New! P89v51rd2 spi not working-help

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;

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;

}

Read-Only
Author
Per Westermark
Posted
8-Apr-2011 07:32 GMT
Toolset
C51
New! RE: P89v51rd2 spi not working-help

Didn't you see the note about how to post source code?

Don't you see a difference between the "code" in your post, and this code?

#include <stdio.h>

int main(void) {
    printf("Hello world\n");
    return 0;
}

Now, what do you think you would have to do to get your code properly indented and with the line breaks where they should be? How about giving it a try?

Read-Only
Author
Andrew Neil
Posted
8-Apr-2011 07:55 GMT
Toolset
None
New! RE: Didn't you see the note about how to post source code?

It really is quite clearly stated:

http://www.danlhenry.com/caps/keil_code.png

If you can miss something that obvious, perhaps you should go back and re-read the P89v51rd2 SPI documentation to make sure you haven't missed any other important details in there...

Read-Only
Author
japs ele
Posted
8-Apr-2011 08:16 GMT
Toolset
None
New! RE: Didn't you see the note about how to post source code?

Hi,

sorry for not posting the code in wrong format.
Now i had understand and re-post it.

I am using Bit Banging instead of On chip SPI module.

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;

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;

}

.

Read-Only
Author
Per Westermark
Posted
8-Apr-2011 09:20 GMT
Toolset
None
New! RE: Didn't you see the note about how to post source code?

Did you just cut/paste the code from the original post? Didn't you notice that you still have the exact same source code lines merged to a single line? If that had been possible, then _I_ could have reposted your source code in a readable format.

You have to go all the way back to your source code editor, since the code in your first post is already permanently ruined. Next thing - avoid tab characters for indenting.

Read-Only
Author
japs ele
Posted
8-Apr-2011 09:31 GMT
Toolset
None
New! RE: Didn't you see the note about how to post source code?

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;
}
Read-Only
Author
Per Westermark
Posted
8-Apr-2011 09:55 GMT
Toolset
None
New! RE: Didn't you see the note about how to post source code?

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?

Read-Only
Author
Andrew Neil
Posted
8-Apr-2011 09:34 GMT
Toolset
None
New! RE: Now i had understand and re-post it.

No, you haven't!

Look again: where is the indentation? Note how some lines have merged into one!

Again, if you can't spot simple, obvious things like this, how do you expect to find bugs in your code?!

Attention to detail is key!

"not getting any result"

So what debugging have you done to find out why not?

Have you verified that your 'bit-banging' code does actually set the output lines high & low?

Have you verified that your 'bit-banging' code does actually read the input lines correctly?

Have you verified that your 'bit-banging' timing is correct?

Here are some debugging tips & techniques:

http://www.8052.com/faqs/120313

http://www.techonlineindia.com/article/09-09-23/Developing_a_good_bedside_manner.aspx

Read-Only
Author
Andrew Neil
Posted
8-Apr-2011 09:37 GMT
Toolset
None
New! RE: No, you haven't!

OK - while I was writing that, you finally did manage to re-post it correctly!

See how much time you could have saved by taking care in the first place?

Read-Only
Author
Andrew Neil
Posted
8-Apr-2011 10:25 GMT
Toolset
None
New! RE: SPI is always dual-direction?

Not always - some things are "half-duplex".

To the OP: A link to the datasheet is always helpful...

Read-Only
Author
Per Westermark
Posted
8-Apr-2011 12:23 GMT
Toolset
None
New! RE: SPI is always dual-direction?

SPI is dupex even if quite a number of _protocols_ are half-duplex. For half-duplex protocols, it's common to keep the outgoing data line high - the idle state - when receiving data.

Obviously, the datasheets should never be far away. And the source code should contain very clear documentation what SPI mode (polarity, phase), bit rates etc the code is expected to fulfill.

Read-Only
Author
erik malund
Posted
8-Apr-2011 13:55 GMT
Toolset
None
New! RE: SPI is always dual-direction?

I am using Bit Banging instead of On chip SPI module.

Why on earth are you doing that?

Erik

Read-Only
Author
Per Westermark
Posted
8-Apr-2011 15:20 GMT
Toolset
None
New! RE: SPI is always dual-direction?

"I am using Bit Banging instead of On chip SPI module."

Why on earth are you doing that?

Erik

And why on earth do you think that using bit banging frees you from caring about phase, polarity, timing etc? With bit banging, you must still perform exactly the same actions as the SPI hardware would do.

Read-Only
Author
Andrew Neil
Posted
8-Apr-2011 16:44 GMT
Toolset
None
New! RE: I really need the solution, it's urgency.

Why does the "urgency" seem to have disappeared...?

Read-Only
Author
japs ele
Posted
9-Apr-2011 05:28 GMT
Toolset
None
New! RE: I really need the solution, it's urgency.

somebody have on chip spi code for 89v51rd2 than pls share.

Thanks in advance.

Read-Only
Author
Per Westermark
Posted
9-Apr-2011 10:26 GMT
Toolset
None
New! RE: I really need the solution, it's urgency.

With the high intensity put into solving problems with the above code, I have to kind of suspect that the bitbanged code was the first code "found" somewhere. And when it didn't work, then the OP got stuck. So now it's time to try to find code making use of the SPI device.

But whatever code is used, it's still important to figure out what is needed regarding timing, configuration, ... Debugging is always needed when implementing programs. How else can it be verified that the code does as expected and that all requirements specified in the datasheets are fulfilled?

Read-Only
Author
Andrew Neil
Posted
9-Apr-2011 11:19 GMT
Toolset
None
New! RE: suspect that the bitbanged code was the first code "found" somewhere

My thoughts exactly.

Read-Only
Author
erik malund
Posted
11-Apr-2011 14:48 GMT
Toolset
None
New! and ...

I guess (s)he could not find "webcode" for the HW SPI and thus chose the "clumsy" solution of bit-banging when HW SPI is available on the chip.

Erik

Read-Only
Author
Per Westermark
Posted
11-Apr-2011 15:46 GMT
Toolset
None
New! RE: and ...

Guess he/she didn't even consider hardware SPI until this thread started to ask about it.

Read-Only
Author
japs ele
Posted
12-Apr-2011 05:37 GMT
Toolset
None
New! RE: and ...

Why you all people experts-authors are passing time on comment to other thing...why not think or write solution of my problem??

I didn't find any single person who had see my code and give any comment on it...i got comment on align meant...!!! hey you all why not focus on problem and try to solve it....right??

I know it had on chip module of spi...but i didn't have any reference code of it...right??

people talk about on chip spi but no body give reference code or any link which guide me....right??

I think this forum is useless....there is no focus on solving the problem...but here people like to only discuss it.

Read-Only
Author
Andrew Neil
Posted
12-Apr-2011 06:51 GMT
Toolset
None
New! RE: I think this forum is useless

You should always take care to understand the purpose of a forum before posting.

The purpose of this forum is clearly stated here: http://www.keil.com/forum/

"The Keil Discussion Forum is an open forum where you may post questions and comments about Keil products."

It is not a free consultancy service.

It is not a place where people will do your homework for you.

If you want someone to do your work for you, then you should be posting on sites like these:

http://www.vworker.com/RentACoder/misc/BidRequests/ShowBidRequest.asp?lngBidRequestId=1573293

http://www.zintro.com

Or try http://www.keil.com/condb

"I didn't find any single person who had see my code and give any comment on it"

Then you must be blind!

You have been given plenty of pointers for where you should be focussing your debugging attention:

* That the IO pins are actually working;

* The SPI phase and polarity;

* Timing;

* Correct handling of received data.

Read-Only
Author
PerT Westermark
Posted
12-Apr-2011 09:11 GMT
Toolset
None
New! RE: and ...

The amount of help given normally depends on the amount of feedback we receive. We know that people who don't give any feedback will probably not listen to what we write. So we don't give any more help.

You have not given any feedback in this thread. So why should we try to remote-debug something if we don't feel that you are an active termination on the other end of this communications link?

We don't know if you have read the datasheet yet, because of the lack of feedback? Have you tried resynchronize the port? Have you considered the Auto-boot sequence? Have you compared your signals with the SDI Write Timing and SDO Read Timing in the datasheet?

Read-Only
Author
erik malund
Posted
12-Apr-2011 14:35 GMT
Toolset
None
New! here is a "solution of my problem"

Why you all people experts-authors are passing time on comment to other thing...why not think or write solution of my problem??

throw the code out and use the Hardware SPI interface

Erik

Read-Only
Author
erik malund
Posted
12-Apr-2011 15:21 GMT
Toolset
None
New! most likely

(possibly with minor modifications) the SPI code generated by the CodeArchitect (free from http://www.esacademy.com) for the LPC936 will, most likely, work for the RD2. Compare the datasheets, the chance is so great that it is 10 times worth the effort.

Erik

Read-Only
Author
Andrew Neil
Posted
12-Apr-2011 16:48 GMT
Toolset
None
New! RE: Compare the datasheets

That would require some study of two datasheets...

Read-Only
Author
erik malund
Posted
12-Apr-2011 18:12 GMT
Toolset
None
New! RE: Compare the datasheets

sure, an excellent excesize for a beginner. Anyhow the SPI chapter will not be HUGE

Erik

Next Thread | Thread List | Previous Thread Start a Thread | Settings

  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.