Menu

Can't get SPI working

Help
Peter
2016-09-11
2016-09-29
  • Peter

    Peter - 2016-09-11

    Hi

    I'm trying to get my SRF02 ultrasonic sensors to work over a serial connection. I have tested the device using I2C but I can't get the SPI to work (I've been trying for a couple of years and always fall back to I2C).

    Details of the device are here:
    https://www.robot-electronics.co.uk/htm/srf02techSer.htm
    The 'mode' pin is connected to 0v to put it into serial mode (for I2C you leave it floating). This is the code I'm using:

    ;Chip Settings
    #chip 16F1825,32
    #config OSC=INTOSC
    
    #include <glcd.h>
    
    'I2C required for the OLED
    #define I2C_DATA PORTA.4
    #define I2C_CLOCK PORTA.5
    #define I2C_BIT_DELAY 0 us
    #define I2C_CLOCK_DELAY 0 us
    #define I2C_END_DELAY 0 us
    #define I2C_DISABLE_INTERRUPTS ON
    #define GLCD_TYPE GLCD_TYPE_SSD1306
    #define GLCD_I2C_Address 0x78
    #define I2C_MODE Master
    
    'USART settings
    #define USART_BAUD_RATE 9600    'Initializes USART port with 9600 baud
    '#define USART_BLOCKING ' Both of these blocking statements will
    #define USART_TX_BLOCKING   ' wait for tx register to be empty
    
    Dir PORTC.1 In   'SDI
    Dir PORTC.2 Out  'SDO
    
    Wait 250 ms
    GLCDINIT
    GLCDCLS
    GLCDPrint(30,0,"Hello")
    Wait 1 s
    
    ;Variables
    Dim buff as byte
    Dim buff2 as byte
    
    'take a measurement and display on the LCD
      HSerSend 0x00           'device address
      Wait 70 ms
      HSerSend 0x50           'take a measurement in inches
      Wait 70 ms              'device takes 70 ms to take the reading
      buff = HSerReceive      'read the first byte back
      buff2 = HSerReceive     'read the second byte back
      GLCDPrint(0,10,"Result")
      GLCDPrint(80,10,buff)
      GLCDPrint(100,10,buff2)
    

    The LCD just displays 80 and 80 for buff and buff2 i.e. the last variables sent. When the SRF02 receives a command telling it to take a measurement, the built in LED should flash which it doesn't so it is not receiving the command correctly.
    I've also tried using SPITransfer and using the software serial using:

    InitSer 1, r9600, 1, 8, 2, none, normal
    

    HWSerial doesn't seem to let you set the protocol details (1 start bit, 2 stop bits, assumed 8 data bits)

    The device is connected with rx > PORTC.2 and tx > PORTC.1, which is correct for the hardware serial. There is no clock signal required for the SRF02 so PORTC.0 is left unconnected.

    Am I doing something totally wrong? I've haven't managed to get serial/SPI to work with any device so far.
    Do I need pullups/pulldowns on the serial lines like with I2C? (I didn't think you did with serial).

    I currently have Great Cow BASIC (0.94 2015-04-02) installed. I can try updating to the newer version if there's been a bug change since then.

     
  • Anobium

    Anobium - 2016-09-13

    Hi. Have you tried using the Hardware SPI methods?

     
  • Peter

    Peter - 2016-09-13

    Yes, I've tried the SPI method following the example in the help:

    ;Chip Settings
    #chip 16F1825,32
    #config OSC=INTOSC
    
    #include <glcd.h>
    
    'I2C required for the OLED
    #define I2C_DATA PORTA.4
    #define I2C_CLOCK PORTA.5
    #define I2C_BIT_DELAY 0 us
    #define I2C_CLOCK_DELAY 0 us
    #define I2C_END_DELAY 0 us
    #define I2C_DISABLE_INTERRUPTS ON
    #define GLCD_TYPE GLCD_TYPE_SSD1306
    #define GLCD_I2C_Address 0x78
    #define I2C_MODE Master
    
    'HW SPI settings
    SPIMode MasterSlow
    Dir PORTC.1 In   'SDI
    Dir PORTC.2 Out  'SDO
    
    Wait 250 ms
    GLCDINIT
    GLCDCLS
    GLCDPrint(30,0,"Hello")
    
    ;Variables
    Dim dummy as byte
    Dim buff as byte
    Dim buff2 as byte
    Dim swVer as byte
    
    'take a measurement
      SPITransfer 0x00, dummy 'send the device address.  Device should not send a reply
      SPITransfer 0x50, dummy 'send the command to take a measurement.
                              'Device should not send a reply
    
      Wait 80 ms              'device takes 75 ms to take the reading
      SPITransfer 0x5E, buff  'request the first byte and receive it
      SPITransfer 0, buff2    'read the second byte (nothing to send)
    
    'display the result on the LCD
      GLCDPrint(0,10,"Result")
      GLCDPrint(80,10,buff)
      GLCDPrint(100,10,buff2)
      GLCDPrint(0,20,"Dummy val")
      GLCDPrint(80,20,dummy)
    
    'read the software version
      SPITransfer 0x00, dummy 'send the device address.  Device should not send a reply
      SPITransfer 0x5D, swVer 'request the software version and read reply
    
    'display on LCD
      GLCDPrint(0,30,"Version")
      GLCDPrint(80,30,swVer)
    

    I get 0 for everything I read back:

     
  • Anobium

    Anobium - 2016-09-13

    Ok I will try here. I need time to get setup etc.

     
    • Anobium

      Anobium - 2016-09-24

      @Peter. Is this all sorted?

       
      • Peter

        Peter - 2016-09-28

        No, however I've not tried again since (and I only saw your reply today).

        I've hooked up the ultrasonic to the USB>TTL converter and couldn't get it to do anything in PuTTY or TerraTerm. I then tried in Powershell (a bit like this http://stackoverflow.com/questions/22997293/use-powershell-to-write-chars-to-serial-port ) and the ultrasonic receives commands from the PC.
        The PowerShell method sends the data as a byte, not as a string (i.e. 80 not "80") which I think is where PuTTY etc. were going wrong.

        Do SPITransfer and HSerSend transmit 0x50 as a string or as a byte?

        I've also realised that this is serial device not SPI and there are 2 stop bits not 1 so plenty for me to check.

         

        Last edit: Peter 2016-09-28
        • Anobium

          Anobium - 2016-09-29

          Which ultrasonic sensor?

           
          • Peter

            Peter - 2016-09-29

            It's an SRF02.
            I'll give it another go this evening or at the weekend.

             
            • Anobium

              Anobium - 2016-09-29

              Please look a the demos within your installation- ..GCB@Syn\GreatCowBasic\Demos\UltraSonic Sensor Solutions

               

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.