You are here: Home FORUM Support Sending 24-bit Data to SPI Device

nqBASIC

  :: Support
Welcome Guest   [Register]  [Login]
 Subject :Sending 24-bit Data to SPI Device.. 17-07-2010 16:20:03 
ajbeal
Joined: 01-11-2009 11:18:04
Posts: 20
Location: Knoxville, TN USA

I'm using a NanoCore connected to a MC14489 7-Segment LED decoder/driver for a LED display. The MC14489 needs 8-bits sent to it for configuration setup and 24-bits sent to it for the display data. I understand the SPI_Transfer() function is used for sending 8-bit bytes, but how can I send 24-bits of data via SPI? I tried using the SPI_Transfer() function three times sequentially, but no successful display digits are showing.

IP Logged
 Subject :Re:Sending 24-bit Data to SPI Device.. 23-07-2010 11:52:33 
DaveD
Joined: 02-06-2010 10:42:56
Posts: 5
Location
I'll take a wild guess at this one ( I haven't tried the SPI routines in nqbasic yet but I do have a bit of experience with SPI transfers and the Nanocore/9s12 in other languages). Have a look at the timing diagrams on page 9 of this: http://www.rasmicro.com/FTP/mc14489rev4.pdf . I'm going to make a wild assumption that you've got the SS pin from your nanocore connected as the enable line on the 14489. The nqbasic API says the SPI routines are designed for byte transfers so they are likely pulling SS low at the beginning of each byte and then setting it high at the end. This works when devices are only looking for one byte at a time so the 14489 will be fine when you are sending the config byte. The problem comes when you try to send the 24 bits of display data. The timing diagram shows that the 14489 needs the enable to remain low while all 3 data bytes are being received. The rising edge of the enable then latches the 24 bits into the display register. Since the enable line is likely going high after each byte, it doesn't work. A work around might be to define one of your unused pins as a DIO output and connect it to the enable of the 14489 instead of using the SS pin. Drive the output low, initiate as many SPI transfers as you need (1 if sending 14489 configuration, 3 if sending display data), and then drive it high again. We've done this with SPI slaves that required 16, 32, and as many as 192 bits of data, albeit using assemby code instead of nqbasic. Your mileage may vary depending on your actual h/w connections and my interpretation of the nqbasic API. Let me know if you have any luck with this. Dave
IP Logged
 Subject :Re:Sending 24-bit Data to SPI Device.. 28-07-2010 15:47:07 
ajbeal
Joined: 01-11-2009 11:18:04
Posts: 20
Location: Knoxville, TN USA
Dave-Thanks for your reply and suggestion. I tried your suggestion without success. I used a 2-channel o-scope to view the enable(ss), data(mosi) and clock(sck) lines. I made the enable line on (low), then made the three successive nqBasic SPI transfers, then made the enable high (off). The enable line was as you suggested and lasted the full 24-bit duration as needed. However, the clock was only 8 cycles in length for each of the three nqBasic 8-bit SPI data transfers rather than a full 24 cycles as desired. In other words, the clock cycled 8 times then turned off (went low) at the end of each of the 8-bit transfers. Of course the clock needs to continue for the full 24-bits rather than three sets of 8 with a 'pause' in between each set. I'm still stuck... I'm hoping the nqBasic guys will provide a SPI transfer that will allow us to set a variable for the number of data bits (and associated clock cycles) to accomodate the various SPI slave devices you mentioned; i.e. 8, 16, 24, 32 up to 192. Thanks Again! AJ
IP Logged
 Subject :Re:Sending 24-bit Data to SPI Device.. 28-07-2010 16:27:06 
DaveD
Joined: 02-06-2010 10:42:56
Posts: 5
Location

Hi AJ, You are seeing exactly what I would expect. The long explanation is below so you can read or skip it as you choose. At this point, use your scope to make sure that the rising edge of the clock pulses are falling in the middle of the data bits on the MOSI line. If it's not, change the in the SPI SETUP statement (SPI_CLK_HIGHxxx or SPI_CLK_LOWxxx). Make sure that the <BIT DIRECTION> is set to 1 for high bits first. Lastly, make sure you're sending the bytes in the correct order. The chip is looking for the byte with the active banks / bank 5 value first, then the bank 4 /bank 3 value, and then the bank 2/bank 1 value. Again, give it a try and let me know. Dave (Long explanation: The nanocore is based on the Freescale/Motorola 9S12C microcontroller. The SPI hardware on the 9S12 is designed to handle 8 bit transfers. If we want to do any other length, we generate our own SS line and just use the SPI to send the clock and data. There are always 8 clocks, there are always 8 data bits in an byte sized SPI H/W transfer. The slave device, in this case your decoder/driver, just uses the clock to move incoming data through a shift register. There is no minimum clock speed; as long as each data bit has an associated clock edge, the data gets clocked into the shift register. You could send 8 bits today, 8 tomorrow, and 8 the next day and the slave will still respond AS LONG AS YOU DON"T RAISE THE SS LINE until all the bits have been clocked in. It's actually quite cool. If your slave needs a weird number of bits, say 21, you just make sure that those 21 bits are the last 21 out of 24 clocked in. 43 bit device? You transfer 6 bytes (48 bits) with your data as the last 43 sent.)

IP Logged
Last Edited On: 28-07-2010 16:28:18 By DaveD for the Reason missed a word or two...
 Subject :Re:Sending 24-bit Data to SPI Device.. 31-07-2010 03:37:34 
ajbeal
Joined: 01-11-2009 11:18:04
Posts: 20
Location: Knoxville, TN USA
Dave - It Works!!! I checked the clock and it was transitioning from High to Low in the middle of the data bit rather than Low to High as it should be. So I changed the clock polarity in the SETUP statement and it worked. Here's my code. I used the delay for a repetitive scope display during my degugging. I used 0x55 for data so each data bit would toggle for the scope display. THANKS SO MUCH FOR YOUR HELP! Regards-AJ //Objects dim SR as new SPI(PM2,PM4,PM5,PM3) //PM2=MISO, PM4=MOSI, PM5=SCK, PM3=SS dim SS as new DIO(PAD07) //use this for SS (SPI enable line) //Variables dim i as new word dim dummy as new byte sub Delay(in word usec) for i=0 to usec system.Delay(1) //microsec next end sub Main //Setup SPI - mas/slave,prescaler,finediv, mode bitdirection SR.SPI_Setup(SPI_MASTER,SPI_DIV_256,0,SPI_CLK_LOW_SS_LOW,SPI_HIGH_BIT_FIRST) //Send config byte to configuration register SS.PIN_Out(PAD07,HIGH) //MC14489 enable hi SS.PIN_Out(PAD07,LOW) SR.SPI_Transfer(0x01,1,dummy) //normal mode SS.PIN_Out(PAD07,HIGH) while(1) //Send 24-bits of data SS.PIN_Out(PAD07,LOW) SR.SPI_Transfer(0x55,1,dummy) SR.SPI_Transfer(0x55,1,dummy) SR.SPI_Transfer(0x55,1,dummy) SS.PIN_Out(PAD07,HIGH) Delay(1000) end while end main
IP Logged
 Subject :Re:Sending 24-bit Data to SPI Device.. 02-08-2010 20:30:53 
DaveD
Joined: 02-06-2010 10:42:56
Posts: 5
Location
Good to hear AJ, I'm glad I could help. Dave
IP Logged
Page # 


Powered by ccBoard