CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Software SPI not work !

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
aka211



Joined: 24 May 2010
Posts: 15

View user's profile Send private message

Software SPI not work !
PostPosted: Mon Jun 28, 2010 2:28 am     Reply with quote

I use PIC 16f946 to communicate with a slave through SPI.( software ) like this


This slave device have SPI diagram like this :

RN8209_SPI.doc

And here is my code but it still not work. I write the data to register 0x07h and then read it out and export to port B. But portB is always 0xFF, whatever the data I write.
I don't know where is the error ? plz tell me.
Code:

#include <16F946.h>
#fuses INTRC,NOWDT,PUT,NOBROWNOUT
#use delay(clock=8000000)
#use spi(DI=PIN_G1,DO=PIN_G2,CLK=PIN_G3,ENABLE_ACTIVE=0,SAMPLE_FALL,STREAM=SPI,BITS=8)
//////////////////////////////////////////
 #define RN_SELECT    PIN_G0
 
void main()
{
   int data;
   setup_oscillator(OSC_8MHZ);
   output_high(RN_SELECT);
 
while(1)
   {
   output_low(RN_SELECT); 
   SPI_XFER(SPI, 0xEA); // write enable register
   SPI_XFER(SPI, 0xE5); // write enable command
   SPI_XFER(SPI, 0x87); // 8-bit register address to write (0x07h)
   SPI_XFER(SPI, 0x12); // data to write
   SPI_XFER(SPI, 0xEA);// write enable register
   SPI_XFER(SPI, 0xDC);//close write command
   output_high(RN_SELECT);
   delay_us(50);
 
   output_low(RN_SELECT);
   SPI_XFER(SPI, 0x07); //address to read (0x07h)
   data=SPI_XFER(SPI,0xFF); //read the data and give to 'data' variable
   output_high(RN_SELECT);
   delay_us(50);
   
   output_b(data);
   delay_ms(100);
   }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 28, 2010 11:14 am     Reply with quote

What is your CCS compiler version ?
aka211



Joined: 24 May 2010
Posts: 15

View user's profile Send private message

PostPosted: Mon Jun 28, 2010 7:52 pm     Reply with quote

PCM programmer wrote:
What is your CCS compiler version ?


CCS_PCWH_4.093
I watched on oscillocope and found that the SDO pin of the slave was always high.(0xff)
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 28, 2010 9:58 pm     Reply with quote

This document that you posted shows that commands must begin
with 0xAE. All of the entries in the table show 0xAE. Only in one
sentence, it says to use "0xEA". But in 8 places it says to use 0xAE.
http://www.fileupyours.com/view/286927/RN8209_SPI.doc

I suggest that you change it to 0xAE and test it.
aka211



Joined: 24 May 2010
Posts: 15

View user's profile Send private message

PostPosted: Mon Jun 28, 2010 10:20 pm     Reply with quote

command that begins with 0xAE is used to enable write and to close write, because registers are write-protected.
read commands just begin with 0xxxxxxx (xxxxxxx is 7-bit address)
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 28, 2010 10:25 pm     Reply with quote

But what about your code that has all these 0xEA values in it ?
Quote:

output_low(RN_SELECT);
SPI_XFER(SPI, 0xEA); // write enable register
SPI_XFER(SPI, 0xE5); // write enable command
SPI_XFER(SPI, 0x87); // 8-bit register address to write (0x07h)
SPI_XFER(SPI, 0x12); // data to write
SPI_XFER(SPI, 0xEA);// write enable register
SPI_XFER(SPI, 0xDC);//close write command
aka211



Joined: 24 May 2010
Posts: 15

View user's profile Send private message

PostPosted: Tue Jun 29, 2010 1:11 am     Reply with quote

That because I want to write a data to 0x07h register and then read it out.
Commands begin with 0xEA is to make a register able to be written.
The line:
Code:
SPI_XFER(SPI, 0x87); // 8-bit register address to write (0x07h)

is to specify the address(0x07h): write command begins with 1xxxxxxx (1 plus 7 bit of the the address 0x07h --> 0x87h)
aka211



Joined: 24 May 2010
Posts: 15

View user's profile Send private message

PostPosted: Tue Jun 29, 2010 2:19 am     Reply with quote

PCM programmer wrote:
But what about your code that has all these 0xEA values in it ?
Quote:

output_low(RN_SELECT);
SPI_XFER(SPI, 0xEA); // write enable register
SPI_XFER(SPI, 0xE5); // write enable command
SPI_XFER(SPI, 0x87); // 8-bit register address to write (0x07h)
SPI_XFER(SPI, 0x12); // data to write
SPI_XFER(SPI, 0xEA);// write enable register
SPI_XFER(SPI, 0xDC);//close write command


oh, I'm sorry. 0xEA is the right one. Because the datasheet is in Chinese, I made some mistake when translate it into Enlish.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group