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

PIC16F887 SPI help
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
searuff



Joined: 22 Mar 2012
Posts: 13

View user's profile Send private message

PIC16F887 SPI help
PostPosted: Thu Mar 22, 2012 4:25 pm     Reply with quote

Hi, guys. I am trying to work on a project which involved a PIC16887 SPI communication with TDC-GP21. On my code I try this
Code:
 
#include <16F887.h>
#FUSES INTRC,NOWDT,NOPUT,NOMCLR,NOPROTECT,NOCPD,NOBROWNOUT,NOIESO,NOFCMEN,NOLVP
#use delay(internal=4000000)
#use spi(DI=PIN_C4, DO=PIN_C5, CLK=PIN_C3, ENABLE=PIN_C0, BITS=16)
#define SSEL   PIN_C0
#define D0     PIN_C5
#define DI     PIN_C4
#define CLK    PIN_C3
main()
{
setup_spi( SPI_L_TO_H | SPI_CLK_DIV_16);
while(1){
SPI_WRITE(0x020F);
spi_write(0x0101);
}
}


It seems like that nothing is writing on the chip. I hook PIN C3 and C5 to a oscilloscope and they are always low. Did I miss anything? How can I make sure SPI work on PIC16F887.

Thanks for anyone's help
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Thu Mar 22, 2012 4:32 pm     Reply with quote

You are mixing up two different and incompatible ways of setting up/using the SPI.
_Either_ use #use SPI, in which case you should also select the mode required in this, and then use spi_xfer to send/receive data, _or_ use setup_spi, and spi_write. The latter mode _only_ supports 8bit transfers, and will always talk to the hardware SPI pins.

Best Wishes
searuff



Joined: 22 Mar 2012
Posts: 13

View user's profile Send private message

PostPosted: Thu Mar 22, 2012 4:52 pm     Reply with quote

Ttelmah wrote:
You are mixing up two different and incompatible ways of setting up/using the SPI.
_Either_ use #use SPI, in which case you should also select the mode required in this, and then use spi_xfer to send/receive data, _or_ use setup_spi, and spi_write. The latter mode _only_ supports 8bit transfers, and will always talk to the hardware SPI pins.

Best Wishes


Hi, Thanks for the information. I am not familiar with spi. If I have to send 32 bit data to TDC chip, I have to use #use SPI right? I got some code using spi_write and spi_read. I wonder how can I send/read 32bit data using spi_write and spi_read. I will take a look at spi_xfer. Thanks for the help.
dyeatman



Joined: 06 Sep 2003
Posts: 1934
Location: Norman, OK

View user's profile Send private message

PostPosted: Thu Mar 22, 2012 5:02 pm     Reply with quote

If you read the datasheet data is sent in 8 bit bytes. Also the chip is
3.3V NOT 5V so be careful!
_________________
Google and Forum Search are some of your best tools!!!!
searuff



Joined: 22 Mar 2012
Posts: 13

View user's profile Send private message

PostPosted: Thu Mar 22, 2012 5:17 pm     Reply with quote

dyeatman wrote:
If you read the datasheet data is sent in 8 bit bytes. Also the chip is
3.3V NOT 5V so be careful!


I read the datasheet I think the opcode is 8bits, and the register I need to initial is 32 bits. So can I just send out 32bit in 4 times? Thanks for the tips. I was about to send 5V to the Chip.
cheeloh1



Joined: 24 Mar 2012
Posts: 3

View user's profile Send private message

PostPosted: Sat Mar 24, 2012 3:06 pm     Reply with quote

I am working with searuff on the project. So in our case, we need two types of communication the spi_read and the spi_write. From what I understand, we need the spi_read to read the data from the TDC chip into the PIC. We need the spi_write to send the read data from the PIC to a 7 segment display.

In this case, we should only use the setup_spi command and disregard the #use spi?

Also, one other urgent question that we had was where can we find the definitions of spi_write and spi_read? We believed they would be located in the 16F887.h file, but they are not there. There is no complaints from the compiler about not finding the functions, but we would like to see how they are defined and simply cannot find them anywhere.

Thank you for any help.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Mar 24, 2012 3:35 pm     Reply with quote

The functions are built-in to the compiler. There is no compilable C source
code in a driver file. But see this thread. It shows the ASM code for
spi_read() and spi_write(). You can look at the .LST file and see what
the compiler is doing:
http://www.ccsinfo.com/forum/viewtopic.php?t=35442

With regard to your questions about the SPI interface to theTDC-GP21,
you need to post a link to the data sheet for that chip.
cheeloh1



Joined: 24 Mar 2012
Posts: 3

View user's profile Send private message

PostPosted: Sat Mar 24, 2012 3:59 pm     Reply with quote

Great thank you. The TDC chip datasheet can be found here:

http://www.acam.de/uploads/media/DB_GP21_en_04.pdf
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Mar 24, 2012 4:40 pm     Reply with quote

Yes, you can call spi_write() multiple times to write a value larger than
a byte.

Don't do this. It won't work:
Quote:

SPI_WRITE(0x020F);

Instead, do this:
Code:

spi_write(0x02);
spi_write(0x0F);

Of course, you also have to follow the rest of the protocol. If a command
byte is required, then you have to write that first. Also, you have to set
the Slave Select line to a low level during the complete SPI transaction
(command + data bytes). The above example also assumes the MSB
is sent first. You need to carefully read the TDC chip's documentation
and follow it exactly.

Examples of how to write an SPI driver:
http://www.ccsinfo.com/forum/viewtopic.php?t=44786&start=3
http://www.ccsinfo.com/forum/viewtopic.php?t=41059
http://www.ccsinfo.com/forum/viewtopic.php?t=38199
searuff



Joined: 22 Mar 2012
Posts: 13

View user's profile Send private message

PostPosted: Mon Mar 26, 2012 8:49 pm     Reply with quote

Hi, now I have a problem of using 2 different interface. One of them is SPI_H_TO_L another one is SPI_L_TO_H. I try to use
Code:

setup_spi(SPI_MASTER | SPI_CLK_DIV_16|SPI_H_TO_L);
setup_spi2(SPI_MASTER | SPI_CLK_DIV_16|SPI_L_TO_H);

and change any spi_write() that use the second one to spi_write2(). but it seems like it doesn't write any data out.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 26, 2012 9:36 pm     Reply with quote

The 16F887 only has one hardware SPI module, so you can't use
spi_write2() with that PIC.

I tested compiling that function with vs. 4.130 with a 16F887 and it
doesn't give an error message. It should. That's a bug and I'll report
it to CCS. It generates a few lines of useless ASM code, but it doesn't
do anything.

If you want a 2nd SPI port, it will have to be a software port. You can
do that with the #use spi() statement. You must use the spi_xfer()
function to send or receive SPI data. See the CCS manual.
Also, you can't use software SPI to make the PIC be an SPI slave, if
that is your intention. Only hardware SPI should be used for that.
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Tue Mar 27, 2012 4:15 am     Reply with quote

It is perhaps worth pointing out that you can mix devices using different SPI standards, onto a single SPI bus, _provided_ they have chip select lines. Sequence is:

setup spi for the device you want to talk to.
_Then_ lower the CS line for this device
Handle the comms to this device
Raise the CS line

So long as you make your 'communication' routine, perform the required setup before the CS is activated, what has happened on the bus the rest of the time, doesn't matter. This also applies to mixing device speeds.
Generally makes things a lot easier than having to implement both a soft, and a hardware SPI, and saves pins as well.

Best Wishes
searuff



Joined: 22 Mar 2012
Posts: 13

View user's profile Send private message

PostPosted: Sat Mar 31, 2012 6:07 pm     Reply with quote

I use spi seting as this:
Code:
setup_spi(SPI_MASTER |SPI_CLK_DIV_4|SPI_L_TO_H);

when I do spi_write. I check the output of my PIC and it did have the clk and data I intend to write. I want to check the respond of TDC-GP21. Then I do a read. My read function is as below:
Code:

int32 read_4byte(byte address)
{
   int32 result = 0;
   int i;
   output_low(tcs);
   spi_write(address);
   result |= spi_read(0xFF);
   result = result<<8;
   result |= spi_read(0xFF);
   result = result<<8;   
   result |= spi_read(0xFF);
   result = result<<8;
   result |= spi_read(0xFF);
   output_high(tcs);
   return result;
}


Seems like I am always reading 0. I don't think I ever successfully wrote data in TDC-GP21. I need some help for the spi interface. Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Mar 31, 2012 6:18 pm     Reply with quote

Are you running the 16F887 at +5v and the TDC chip at +3.3v ?
With no level translators ? If so, hardware SPI will not work.
You probably would read all 0's or some other bad data.

You should get the "LF" version of the PIC, such as 16LF887 and run it
at 3.3v, so both the PIC and the TDC chip are at +3.3v. Then hardware
SPI will work.
searuff



Joined: 22 Mar 2012
Posts: 13

View user's profile Send private message

PostPosted: Sat Mar 31, 2012 7:53 pm     Reply with quote

PCM programmer wrote:
Are you running the 16F887 at +5v and the TDC chip at +3.3v ?
With no level translators ? If so, hardware SPI will not work.
You probably would read all 0's or some other bad data.

You should get the "LF" version of the PIC, such as 16LF887 and run it
at 3.3v, so both the PIC and the TDC chip are at +3.3v. Then hardware
SPI will work.


I source 16F887 using 3.3 V. I measure the output of SI pin of it. PIC is giving out 3.3 V data. But seems like TDC chip doesn't respond. I tried initialize the register in TDC chip and start a measure. But the Fire_UP pin of TDC never generate any respond. I assumed data was not written. I tried spi_write to a Max7219. Only changing setup to SPI_H_to_L, and I can write on max7219. So I think SPI_write works, but it doesn't work on TDC chip?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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