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

spi_xfer()

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



Joined: 16 Jul 2014
Posts: 7

View user's profile Send private message

spi_xfer()
PostPosted: Wed Jul 16, 2014 10:57 am     Reply with quote

I am trying to communicate over SPI to an LCD driver (MM5452V) which I have used before with another type of controller, so I know it works. I am using a PIC 18F14K22 controller this time. I have an array named LCD_output[] which has 5 bytes of data that I need to send via SPI to the driver. I'm having trouble getting any of the SPI write or Xfer commands to work. I would like to be able to use array with the spi_xfer() routine and tell it to send 40 bits, but this isn't working. What is the best way to send these 5 bytes of information over the SPI bus short of writing my own routine to control the clock output and toggle it high/low and send each bit without using the SPI routines?

for your information, each byte in my array has the information for one digit, [0] = ones, [1] = tens, [2] = hundreds, [3] = thousands and [4] is all zeros. I have set up the SPI bus using the
#use SPI(SPI1,BAUD=250000,STREAM=SPI_STREAM) line and am using a command: setup_spi(SPI_MASTER | SPI_L_TO_H); to set up mode 0 before trying to write to the LCD.

I am going to drag out the scope to hook to the SPI lines, but since I have never used SPI with these controllers, I'm having a little trouble figuring out which function to use.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jul 16, 2014 1:27 pm     Reply with quote

There are two methods of using spi with CCS. Don't mix the two methods.
Use either setup_spi() or #use spi(). Each method has it's own associated
functions. #use spi() has spi_xfer(). It's the most modern method.
Look in the CCS manual. It has many parameters. One of them is the
SPI mode. You put the mode in #use spi() statement.

#use spi() can run hardware or software SPI. The fclock (SCK)
frequency of the MM5452V is fairly low. It depends upon Vdd voltage.
What is the Vdd voltage of the MM5452V and the PIC on your board ?
What's the oscillator frequency of the PIC ? Do you insist on running
hardware SPI, or can you run software ?

Years ago, I posted routines to run the MM5453V (a very similar chip)
with bit-banging:
http://www.ccsinfo.com/forum/viewtopic.php?t=17643
davidb_csi



Joined: 16 Jul 2014
Posts: 7

View user's profile Send private message

PostPosted: Wed Jul 16, 2014 2:12 pm     Reply with quote

I am running the PIC at 8Mhz using the internal oscillator. the circuit is running on 3.3vdc. I have two devices using the SPI bus, the LCD display driver and an LTC1655Cs8 16-bit D/A converter. No reads, only writes. The A/D converter is working. Here is the code I have written to write to it. My 4-20ma converter that it is running seems stable, so I have not checked it with the scope at this time, but am connecting wires for this as we speak. My scope can decode SPI bus lines.

By the way, I'm using the hardware lines that are the defaults for the chip for the SPI bus (clock= RB6, MOSI= RC7, MISO=RB4). My AO-CS line is RB5 and LCD_CS line is RB7.
Code:

void WriteAO(int16 value)
{
   //16-bit D/A: 4ma =12800 and 20ma=64000 (0-4.096v (20.48ma) output)

   setup_spi(SPI_MASTER | SPI_H_TO_L);  // SPI mode 1

   output_low(AO_CS);               // Take AO /CS low
   spi_xfer(SPI_STREAM,value,16);       //write 2 bytes (16 bits) of data
   output_high(AO_CS);               //Take AO_CS HIGH
}

I don't remember why right now, but when I first used these devices in another application, the A/D used SPI mode 1 and the LCD used mode 0. That is why I included the setup_spi() function in my two subroutines (one for the analog output and the other for the LCD)

I used the #use SPI(SPI1,BAUD=250000,STREAM=SPI_STREAM) line because my old code had the SPI running at 250Khz.

Are you telling me I can't use the setup_spi() if I am using the #use SPI ?

I'll take a look at the code you mentioned too.

By the way, the CCS manual isn't very informative sometimes. For example, with the spi_xfer() I don't see where it tells me what how many bits is the 'data' parameter or how many bits can be sent?

I tried sending each array bit one at a time using the spi_write() function in between the two lines controlling the CS line and it was trying to work, but not correctly. I will know more when I hook up the scope.

Thanks for the help
Ttelmah



Joined: 11 Mar 2010
Posts: 19562

View user's profile Send private message

PostPosted: Wed Jul 16, 2014 2:35 pm     Reply with quote

Yes.

Mixing the modes caused problems.

Look in the manual. If you look at the entry for '#use spi', it refers you to 'spi_xfer'.
If you look at the entry for 'setup_spi', it refers to 'spi_read', and 'spi_write'.

The two modes are not cross compatible (_if_ you use the hardware SSP peripheral, and only use single byte transfers, then you can use #use, and spi_read and spi_write, but this is the only combination where 'cross operation' can work, and it is not worth the risk...).

Either use 'setup_spi', and 'spi_read', and 'spi_write', or use '#use', and 'spi_xfer'. They are two different libraries, and the manual is 'right' in avoiding mixing.
davidb_csi



Joined: 16 Jul 2014
Posts: 7

View user's profile Send private message

PostPosted: Wed Jul 23, 2014 11:12 am     Reply with quote

For now, I was still having trouble getting the SPI routines to work with my LCD display driver. The D/A chip works fine. In order to get things moving and to prove that my hardware is working, I decided to drop the SPI routines and use my own. Works like a charm. Both devices are working fine now. However, I have another problem that I can't figure out and that has to do with the PIC Port A lines RA0 through RA3 not working as digital inputs. RA4 and RA5 work fine. I'll post another subject in regards to this, but thanks for the help with the SPI.
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