Menu

SPI Questions

Help
BoomStick
2012-01-23
2013-05-30
  • BoomStick

    BoomStick - 2012-01-23

    Just a Few silly SPI Bus questions:

    1. What mode does GCB set the SPi port to? can I change the CPOL and CPHA values?

    For example I want CPOL = 1 and CPHA = 1 (AD calls this SPi mode 3)

    It might be sport on already I havent actually tried it…hrm….

    2. In the help file it says that it transfers a byte, so I assume a 8 bit 0-255 value and recieves a byte, again, 8 bit.

    Now I want a 16 bit number, sent and recieved.

    How do I do this, or is it as simple as just setting your return variable to a word, and sending a number higher than 255? I need all the way up to 65535….

    …or do I need to go into the CGB source code and start to fiddle?

    3. I'm playing with a ADIS16365 Gyroscope from AD, just incase anybody is interested….this has been quite a learning curve to be honest!

     
  • BoomStick

    BoomStick - 2012-01-23

    Uh, I tried to send a large number and the compiler spat out "cannot store <large number> in the byte variable SPI_TX"

    Bummer.

    I sorted out my mode problem in hwspi.h, Still looking through it for a solution to my byte problem…..the issue is that the request command for my gyro pack is 16 bit and by response is 16bit…..

     
  • kent_twt4

    kent_twt4 - 2012-01-23

    I would try two sucessive writes and reads first to satisfy the 16 bit registers if possible?  And if that didn't get it then I would use a software SPI to conform to the timing diagram of your device.  So something like:

    ...
    dim accel as word Alias spireadH, spireadL
    ...
    ...
    SPITransfer spiwriteH, 0        'register addr
    SPITransfer spiwriteL, 0        'data
    ...
    SPITransfer 0, spireadH     'MSB data first
    SPITransfer 0, spireadL     'LSB data 
    ...
    Print accel     'send reading out to lcd
    
     
  • Nobody/Anonymous

    I like it, if I read correctly:

    dim accel as word Alias spireadH, spireadL

    Does this bit of code combine my first 8 bit packet with the second 8 bit packet as one variable(word)?

    eg,

    (output from gyro)

    1111 1111 (FF, 255 in decimal) <-high packet

    1111 1111 (FF, 255 in decimal) <-Low packet

    And this code will combine them, create a word with a value 65535  eg, (1111 1111 1111 1111, 65535)

    ….Fair enough if it does, as I was worried about the intergration!

    .
    ..

    I'm keeping the CS line held down during the read process (two 8 bit bytes) We'll see how we go!

     
  • BoomStick

    BoomStick - 2012-01-23

    Oops forgot to log in :-/

    Just another question, I havent got too far into it, I assume its still o.k to run the hardware USART in both SPI mode and Serial mode at the same time? or can I have one and not the other?

     
  • BoomStick

    BoomStick - 2012-01-24

    O.K just a bit of a Update:

    I had a look with the CRO, you can run both SPI and RS232 at the same time, and kent_twt4, that declaration of the variable to intergrate two bytes to a word (and convert!) works brilliantly.

    I just need to knock up a cct board to interface with my Gyro now, as by PIC programmer board is playing silly buggers with my output lines….this should fix it!

     
  • BoomStick

    BoomStick - 2012-01-25

    More ofa update:

    I have the pack hooked up on a new test board. All good.

    Problem is no joy at the moment. I'm playing around with the timing (Eg holding down the CS line) and I'm not 100% sure I have my request command set correctly. The SPI pack is as quiet as a church mouse :-(

    ….I'll keep you updated!

     
  • BoomStick

    BoomStick - 2012-01-25

    Hrm, looking back and forward at the data sheet, it seems my SPI wave is upside down….both the SCLK and the Dout line! Argh!

    ….does anybody know if there is a flag to invert the SPI quare wave wave or do I have to use a 4049 to invert the output wave?

    *4049, is a CMOS IC with 6x inverter gates

     
  • BoomStick

    BoomStick - 2012-01-25

    O.K Software solution:

    In the include\lowlevel directory of GCB there is a file called ssp.h, down the bottom is the sub  SPIMode(SPICurrentMode)

    set SSPSTAT.SMP off
    set SSPSTAT.CKE off
    set SSPCON.CKP on

    Now this bit of code is repeated twice, if you set SSPCON.CKP to on it will invert the clock.

    .
    ..

    I was playing around with hwspi.h, and nothing sould happen. Quite a pain to find! :-)

     
  • Hugh Considine

    Hugh Considine - 2012-01-25

    Bit late, but I might jump in on this conversation anyway. If you have a newish update.zip (22/2/2011 or newer), there are some changes to the SPI routines. First, the ssp.h file doesn't do anything any more, so you possibly should delete it or move it to a folder where it doesn't get confusing. The correct file is (or should be) hwspi.h - have a look at the lowlevel.dat file (in the include folder) and see which one of the two is listed.

    Another thing is that the newer hwspi.h file provides a second SPIMode sub, which lets you set the clock polarity and phase. For mode 3 (CPOL = 1 and CPHA = 1), you can write either:

    SPIMode Master, 3

    or

    SPIMode Master, SPI_CPOL_1 + SPI_CPHA_1

    That is basically the same as setting the SSPSTAT bits as you suggested jonogeo, but it should give you the same results if you suddenly decide to start using an AVR and might be a bit easier to read. (It does make the program on the PIC a bit bigger, so changing the bits in SSPSTAT might be a better idea after all.) Hope that helps!

     
  • BoomStick

    BoomStick - 2012-01-25

    I did see that after my last post :-/  I went back to what you suggested, and it works really well.

    Yep you are spot on my man, I can now send data (in a simple loopback test) and everything comes out 100%

    Now for my ADIS unit…I swear I'm not reading the timing chart correctly :-/ I'll keep plugging away!

     
  • BoomStick

    BoomStick - 2012-01-27

    O.K after a bit more buggerising around I have got my clock and CS lines sorted out! Same with my request command.

    It works fine on the cro, and I can do a SPI loopback test etc to see if the data in is equal to the data out and the maths are taken care of correctly! Sorted!

    The send command looks really good, the issue is that it seems to mash up the data coming in: If you look at it on the cro it seems the return square waves is not in the defined SCLK areas…. :-/

    .
    ..

    On a similar note does anybody know how I would strip to bits of a byte? For example the output out of the Gyro requires
    that the first two bits from the most significant bit be stripped off:

    eg

    1111 111 1

    xx11 1111

    First two bits removed, and set to 0, so

    0011 1111

    Any idea?

     
  • Hugh Considine

    Hugh Considine - 2012-01-27

    The easy way to strip out unwanted bits it to use the AND operator. If you have a variable called SensorData and you want to get rid of the first 2 bits, write:

    SensorData = SensorData AND b'00111111'

    Another way is to use the . to set individual bits:

    SensorData.7 = 0
    SensorData.6 = 0

    or use the Set command. All are pretty much the same when sent to the PIC, so choose whichever one looks the nicest to you!

     
  • BoomStick

    BoomStick - 2012-01-27

    ….and we have function! I think…..SPI doesn't look correct on the CRO, but looks O.K (ish) in Hyperterminal! (which is what you'd expect with no movement)

    WooooooooooHooooooooooo! Thankyou boys!

    …next mission is to add in the math libary for negative numbers and decimal points, so I can create a position system (equations of motion). I spoze I better make a libary and tutorial for this as thanks to the community!

    You guys rock!

     
  • Hugh Considine

    Hugh Considine - 2012-01-27

    Good to hear it's starting to work!

    If you need negative numbers, GCBASIC does have some support. Create an Integer type variable, and then you should be able to store negative numbers in it. There are no doubt still some bugs, but it should be possible to add, subtract, multiply, divide and compare negative numbers if you have a fairly recent update of GCBASIC.

     
  • BoomStick

    BoomStick - 2012-01-28

    Ah cool, does it do decimal points as well? And I take it that the Hserprint command has no trouble formatting this value (With Negative sign and decimal point) as I send it to the RS232 port?

    (Sorry normally I'd try this, but alas I'm not near my programmer :-/ )

     
  • Hugh Considine

    Hugh Considine - 2012-01-28

    I'm not sure if HSerPrint can handle Integer variables yet or not. Print (for LCD) definitely does, but don't recall if the other print routines have been updated. As for the decimal point, you will need to do that yourself. I think there was some code posted on this forum that can do decimal points from a while back, but I could be wrong.

     
  • BoomStick

    BoomStick - 2012-02-05

    Sorry for the late reply, I just got from Queensland….

    I'll get into it and find out!

     
  • BoomStick

    BoomStick - 2012-02-07

    It didn't seem to want to work :-/ Its not exactly important at the moment, since I decided to do as much as the data mashing possible on my laptop! I'll have to look into it tho…

    On a related topic, a while ago I had trouble sending data out of the RS232 port, I kept on getting mashed characters and wierd bits of text…eg if I did this:

    HSerPrint "ADIS 16360"
    HserPrintCRLF
    HSerPrint "VER:  v7.250"

    I'll get the whole lot mashed up in hyperterminal :-/

    Now If I add a delay between these commands (just the USART_DELAY) I don't have a problem….so

    HSerPrint "ADIS 16360"
    Wait USART_DELAY
    HserPrintCRLF
    Wait USART_DELAY
    HSerPrint "VER:  v7.250"

    Wierd, but hey its all good (This data is just going through hyperterminal) I had all sorts of trouble sending raw bytes and the like, all it took was one little delay loop!

    .
    ..

    One more silly question it I cant seem to work out how to make a sort duration trigger pulse trigger a section of code…well its actually not a triger pulse its a RS232 signal, the problem is its just one pulse every so often (Its supposed to be 128) and when the port isn't being used it does not return to high….

    There is no way of knowing where the byte starts and finishes so I don't think I can use the Serial read command?

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.