View previous topic :: View next topic |
Author |
Message |
aka211
Joined: 24 May 2010 Posts: 15
|
Software SPI not work ! |
Posted: Mon Jun 28, 2010 2:28 am |
|
|
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
|
|
Posted: Mon Jun 28, 2010 11:14 am |
|
|
What is your CCS compiler version ? |
|
|
aka211
Joined: 24 May 2010 Posts: 15
|
|
Posted: Mon Jun 28, 2010 7:52 pm |
|
|
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
|
|
Posted: Mon Jun 28, 2010 9:58 pm |
|
|
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
|
|
Posted: Mon Jun 28, 2010 10:20 pm |
|
|
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
|
|
Posted: Mon Jun 28, 2010 10:25 pm |
|
|
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
|
|
Posted: Tue Jun 29, 2010 1:11 am |
|
|
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
|
|
Posted: Tue Jun 29, 2010 2:19 am |
|
|
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. |
|
|
|