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).
;ChipSettings#chip 16F1825,32#config OSC=INTOSC#include<glcd.h>'I2CrequiredfortheOLED#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'USARTsettings#define USART_BAUD_RATE 9600 'Initializes USART port with 9600 baud'#defineUSART_BLOCKING'Bothoftheseblockingstatementswill#define USART_TX_BLOCKING ' wait for tx register to be emptyDirPORTC.1In'SDIDirPORTC.2Out'SDOWait250msGLCDINITGLCDCLSGLCDPrint(30,0,"Hello")Wait1s;VariablesDimbuffasbyteDimbuff2asbyte'takeameasurementanddisplayontheLCDHSerSend0x00'deviceaddressWait70msHSerSend0x50'takeameasurementininchesWait70ms'devicetakes70mstotakethereadingbuff=HSerReceive'readthefirstbytebackbuff2=HSerReceive'readthesecondbytebackGLCDPrint(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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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:
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:
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.
Hi. Have you tried using the Hardware SPI methods?
Yes, I've tried the SPI method following the example in the help:
I get 0 for everything I read back:

Ok I will try here. I need time to get setup etc.
@Peter. Is this all sorted?
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
Which ultrasonic sensor?
It's an SRF02.
I'll give it another go this evening or at the weekend.
Please look a the demos within your installation- ..GCB@Syn\GreatCowBasic\Demos\UltraSonic Sensor Solutions