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 support@ccsinfo.com

Transform SPI Code for MMA7455

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



Joined: 22 Aug 2011
Posts: 6
Location: Thailand

View user's profile Send private message

Transform SPI Code for MMA7455
PostPosted: Fri Sep 02, 2011 11:38 am     Reply with quote

I want to transfrom this code to ccsc code for PIC16f877, but this code and function makes me confused. Can you help me? Thank you very much.

while (!SPI2S_SPTEF),SPI2S,SPI2D,SPI2C2_BIDIROE = 0,

Code:
// Read function SPI 3 Wire Mode or 4 Wire Mode
byte spi_read(byte reg){
byte x;
CS=0;
x=SPI2S;
x=SPI2D;
while (!SPI2S_SPTEF);
SPI2D= ((reg &0x3F)<<1); // write in the register address with the read command
while(!SPI2S_SPRF); //wait for transfer
x=SPI2D;
SPI2C2_BIDIROE = 0; // MOSI become input when 3 wire mode
SPI2D = 0x00; // send 2nd byte
while (!SPI2S_SPRF); // wait transfer done
x = SPI2D;
SPI2C2_BIDIROE = 1; //change direction back to output when in 3 wire mode
CS=1;
return (x);
}


and x=SPI2D; //dummy read
Code:

//Write function SPI 3 Wire or SPI 4 Wire Mode
void spi_write(byte reg, byte data){
byte x;
CS=0;
SPI2D=(((reg &0x3F)<<1)|0x80);
while (!SPI2S_SPRF); //wait for transmission complete
x=SPI2D; //dummy read
SPI2D=data;
while (!SPI2S_SPRF); //wait for transmission complete
x=SPI2D;
CS=1;
}
Shocked Shocked Very Happy
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Sep 02, 2011 2:20 pm     Reply with quote

It appears that you are trying to convert the SPI routines from this
Freescale appnote, on page 12:
http://www.freescale.com/files/sensors/doc/app_note/AN3468.pdf
This code is written for some Freescale processor. The appnote
doesn't show the #include files with the definitions of those bits.

If you go to the main page for the MMA7455 and click on "Application
Notes", you will see a Zip file of source code that can be downloaded:
http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=MMA745xL
You have to register on the Freescale website to be allowed to download it.
I didn't do that. I believe the file you want is called: AN3468SW.zip

You will also have to look at the Freescale processor documentation.
The appnote says they use the Freescale S08 processor. I don't know
the exact one. But here is a link for one of them:
http://www.freescale.com/files/microcontrollers/doc/data_sheet/MC9S08JM16.pdf
This document lists the bits in the SPI control registers that mostly
correspond to the symbols in the sample code.

For example, this symbol refers to the the SPTEF bit (bit 5) of the SPI2S
register which is listed on page 43 of the MC9S08JM16.pdf file (in the link above):
Code:
SPI2S_SPTEF

Then on page 241 it says this:
Quote:

Module Use:
After SPI master initiates transfer by checking that SPTEF = 1 and then
writing data to SPIDH/L:
Wait for SPRF, then read from SPIDH/L
Wait for SPTEF, then write to SPIDH/L
Data transmissions can be 8- or 16-bits long


Then on pages 249 and 250 it explains the SPTEF bit in detail:
Quote:

15.3.4 SPI Status Register (SPIxS)

SPTEF -
SPI Transmit Buffer Empty Flag — This bit is set when the transmit data
buffer is empty. It is cleared by reading SPIxS with SPTEF set,
.
.
.

0 SPI transmit buffer not empty
1 SPI transmit buffer empty



I think that, effectively, this whole block shown below,
Code:
x=SPI2S;
x=SPI2D;
while (!SPI2S_SPTEF);
SPI2D= ((reg &0x3F)<<1); // write in the register address with the read command
while(!SPI2S_SPRF); //wait for transfer
x=SPI2D;

can be replaced by this line of CCS code:
Code:

x = spi_read((reg &0x3F)<<1);

It's a little different than the Freescale code, because the spi_read()
ASM code doesn't check for buffer empty before writing to it. But it
does check the Buffer Full status after writing the data, before it
reads the data that was received.

The complete function would look as shown below.
Code:

// Do SPI read (4-Wire SPI).

byte spi_read(byte reg)
{
byte x;

output_low(CS);

x = spi_read((reg &0x3F)<<1);  // Send command

x = spi_read(0);  // Read data

output_high(CS);

return (x);
}

The CS constant must be defined as a CCS pin number above main()
Example:
Code:

#define CS  PIN_B0

Also, you must leave the compiler in "standard i/o" mode. Don't enable
fast i/o mode.

You didn't say if you are using the Parallax module. If you are, that
module only supports 3-wire SPI, and the above code would have to
be modified.


But anyway, this shows how to do it. You have to analyze the Freescale
code and read their SPI documentation. Then you have to compare it to
the 16F877 documentation, and study the .LST file of the CCS compiler
(using Symbolic mode, to make it easier to understand), and make a
judgement of how compatible it is. And also, make a judgement of what
is really important to include in the translated code.

I don't want to do this for you. I already spent too much time writing this
up. You must do it. I just want to make that clear.
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