Please help me convert this picaxe code to gcb.I did try but got it wrong and am not good at it.
The picaxe code may be faulty.
The device has i2c and serial out. How to use serial and send to terminal
'Picaxe code' hi2csetup i2cmaster, $A4, i2cslow, i2cbyte''Main:' hi2cout (0)' hi2cin (b2,b3)' w2=255*b2+b3' Debug'Goto Main;end of picaxe code;start of gcb code;TOF10120 Laser Range Sensor Module to terminal
#chipmega328p,16
#optionexplicit
#include<UNO_mega328p.h>;for terminal distance out
#defineUSART_BAUD_RATE9600
#defineUSART_TX_BLOCKING
#defineUSART_DELAY1ms;
#defineHI2C_DATAPORTC.4;sda
#defineHI2C_CLOCKPORTC.5;scl
#defineHI2C_BAUD_RATE400HI2CModeMaster;main Get Distance----dimdistance_lo,distance_hiasbytedoHI2CStartHI2CSend(0);HI2CStopHI2CReceive(distance_hi)HI2CReceive(distance_lo, NACK);read one byte and concludeHI2CStopwait500mshserprint"Distance= "+str(distance_hi*256+distance_lo)+" mm"HSerPrintCRLFloop
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thank you @mmotte for your code. It seems to work at 20cm to object and scrolls readings in the terminal which seem accurate but moving the object it freezes and starting at 10 cm or 30 cm and it only gives 1 reading.
It only arrived the day I posted. I have not used pull up resistors.
It also sends serial distance as 5 bytes distance "mm".
edit the data sheet is in chinese
When I ordered mine the colours didn't match.
Could be that with your TOF10120 you have to switch the I2C pins sda and scl.
Don't switch the power wires +V and Gnd, Unless you want to celebrate NewYear fireworks early, :)
This proves that TOF10120 is a time-of-flight sensing technology.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I got nowhere with this device
serial or i2c to spi display
thank you mmotte for your time replying.
I'll stick with vl0x units cos I can get the to work!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I think that the distance_hi*256+distance_lo exceeds it's limits and crashes!
Everytime the distance is <400 and >2000 it stops. and in between it get's false readings and stops too. By also restricting it with a SelectCase like I did in the V53LOX code we can prevent crashing.
I used the OnBoard LED to flash that it has not crashed yet. When that stops then only use the OnBoard button to reset.
TheTOF10120 code that I used also has an address fault like Stan mentioned scl and sda, so we have to clean it up a little to work correct:
;TOF10120 laser distance sensor
#chipmega328p, 16'Arduino Nano or Uno!
#optionexplicit
#include<UNO_mega328p.h>;for terminal distance out
#defineUSART_BAUD_RATE9600
#defineUSART_TX_BLOCKING
#defineUSART_DELAY1ms;I2C TOF10120
#defineHI2C_DATAPORTC.5;scl
#defineHI2C_CLOCKPORTC.4;sda
#defineHI2C_BAUD_RATE400HI2CModeMasterDimI2C_Address, RegAddressasByteI2C_Address=0xA4RegAddress=0x00wait500ms;OnBoard LED
#defineLedDIGITAL_13DirLedOut;Distance variablesdimdistance_lo,distance_hiasbytedimDistanceasword;main Get Distance----doSetLedOnHI2CStartHI2CSend(I2C_ADDRESS)HI2CSend(RegAddress)HI2CReStartHI2CSend(I2C_ADDRESS+1);indicate a read operationHI2CReceivedistance_hi, ACKHI2CReceivedistance_lo, NACKHI2CStopwait100msSetLedoffDistance=distance_hi*256+distance_lohserprint"Distance= "+str(Distance)+" mm"HSerPrintCRLFwait100msloop
Last edit: Haroen 2021-01-19
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
@Haroen Your code looks good. Thank you for shring.
The change I would recommend is to test that the sensor is responding by checking the HI2CAckPollState this should ensure the sensor is in a state to respond to the read request.
doHI2CStartHI2CSend(I2C_ADDRESS)HI2CSend(RegAddress)loopWhileHI2CAckPollStateHI2CReStartHI2CSend(I2C_ADDRESS+1);indicate a read operationHI2CReceivedistance_hi, ACKHI2CReceivedistance_lo, NACKHI2CStop
This do-loop could of course cause a continuous loop, so, you should also add counter or watch dog timer to catch this continuous loop situation.
:-)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I tested with dim distance_lo, distance_hi as word,
and also tried with deleting Distance=distance_hi*256 + distance_lo but still crashing occurs.
I don't think it's a variable limit problem anymore.
It has something to do with I2C like Anobium pointed out.
@Anobium, I tried your do-loop but made no difference, still think it should be in there too.
Is it needed to end the continuous loop with a counter or watch dog timer because it should loop until it is in state to respond to the read request?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
@Anobium, crashing like in all codes that I tried so far with certain distances the OnBoard LED does not flash anymore and the Terminal just stops giving "Distance=xxxmm".
Still crashing (Freezing) now faster with this code:
#chipmega328p, 16'Arduino Nano or Uno!
#optionexplicit
#include<UNO_mega328p.h>;for terminal distance out
#defineUSART_BAUD_RATE9600
#defineUSART_TX_BLOCKING
#defineUSART_DELAY1ms;I2C TOF10120
#defineHI2C_DATAPORTC.5;scl
#defineHI2C_CLOCKPORTC.4;sda
#defineHI2C_BAUD_RATE400HI2CModeMasterDimI2C_Address, RegAddressasByteI2C_Address=0xA4RegAddress=0x00wait500ms;I used the OnBoard LED to flash that it has not crashed yet.;When LED stops flashing then use the OnBoard button to reset.
#defineLedDIGITAL_13DirLedOut;Distance variablesdimdistance_lo, distance_hiasworddimDistanceasworddim_li2cCountasbytedim_lAckStateasbyte;main Get Distance----do_li2cCount=0SetLedOndoHI2CStartHI2CSend(I2C_ADDRESS)HI2CSend(RegAddress)_lAckState=HI2CAckPollState_li2cCount++if_lAckState=truethenwait1msEndifloopWhile_lAckStateand_li2cCount<255if_lAckState=falsethenHI2CReStartHI2CSend(I2C_ADDRESS+1);indicate a read operationHI2CReceivedistance_hi, ACKHI2CReceivedistance_lo, NACKHI2CStopwait100msSetLedoffDistance=distance_hi*256+distance_lohserprint"Distance= "+str(Distance)+" mm"HSerPrintCRLFelsehserprint"I2C Ack error"HSerPrintCRLFendifwait100msloop
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
My error... the test for HI2CAckPollState is in wrong place, and, I have moved the hI2CSend(RegAddress) to the correct place (please ensure this is correct).
Is the sensor really 400 kbts? Is it really 100 kbts?
Do you need a delay between the reads? The datasheet will reveal this.
And, do you have a protocol analyser? As this would reveal the lockup.
Revised Code
#chipmega328p, 16'Arduino Nano or Uno!
#optionexplicit
#include<UNO_mega328p.h>;for terminal distance out
#defineUSART_BAUD_RATE9600
#defineUSART_TX_BLOCKING
#defineUSART_DELAY1ms;I2C TOF10120
#defineHI2C_DATAPORTC.5;scl
#defineHI2C_CLOCKPORTC.4;sda* #defineHI2C_BAUD_RATE400HI2CModeMasterDimI2C_Address, RegAddressasByteI2C_Address=0xA4RegAddress=0x00wait500ms;I used the OnBoard LED to flash that it has not crashed yet.;When LED stops flashing then use the OnBoard button to reset.
#defineLedDIGITAL_13DirLedOut;Distance variablesdimdistance_lo, distance_hiasworddimDistanceasworddim_li2cCountasbytedim_lAckStateasbyte;main Get Distance----do_li2cCount=0SetLedOndoHI2CStartHI2CSend(I2C_ADDRESS)_lAckState=HI2CAckPollState_li2cCount++if_lAckState=truethenwait1msEndifloopWhile_lAckStateand_li2cCount<255if_lAckState=falsethenHI2CSend(RegAddress)HI2CReStartHI2CSend(I2C_ADDRESS+1);indicate a read operationHI2CReceivedistance_hi, ACKHI2CReceivedistance_lo, NACKHI2CStopwait100msSetLedoffDistance=distance_hi*256+distance_lohserprint"Distance= "+str(Distance)+" mm"HSerPrintCRLFelsehserprint"I2C Ack error"HSerPrintCRLFendifwait100msloop
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
TOF10120 no fun.
It got serial out so connect tx to rx and rx to tx and try reading serial.
the device would be defined as usart0 and the terminal as usart1 dunno.
The serial info from device is supposed to be 5 bytes.3 distance and last 2 "m","m"
again dunno.
looking at "help" :(
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You have to provide a lot more info, and, there is an intended typo in the code to help with the frequency discussion.
what intended typo ? RegAddress
too old for games.
This was started on picaxe and if sorted another feather in gcb.
Is the device popular enough to be bothered with?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This code compiles here. But, I am not testing. I am recommending methods to isolate the lockup.
#chipmega328p, 16'Arduino Nano or Uno!
#optionexplicit
#include<UNO_mega328p.h>;for terminal distance out
#defineUSART_BAUD_RATE9600
#defineUSART_TX_BLOCKING
#defineUSART_DELAY1ms;I2C TOF10120
#defineHI2C_DATAPORTC.5;scl
#defineHI2C_CLOCKPORTC.4;sda
#defineHI2C_BAUD_RATE100HI2CModeMasterDimI2C_Address, RegAddressasByteI2C_Address=0xA4RegAddress=0x00wait500ms;I used the OnBoard LED to flash that it has not crashed yet.;When LED stops flashing then use the OnBoard button to reset.
#defineLedDIGITAL_13DirLedOut;Distance variablesdimdistance_lo, distance_hiasworddimDistanceasworddim_li2cCountasbytedim_lAckStateasbyte;main Get Distance----do_li2cCount=0SetLedOndoHI2CStartHI2CSend(I2C_ADDRESS)_lAckState=HI2CAckPollState_li2cCount++if_lAckState=truethenwait1msEndifloopWhile_lAckStateand_li2cCount<255if_lAckState=falsethenHI2CSend(RegAddress)HI2CReStartHI2CSend(I2C_ADDRESS+1);indicate a read operationHI2CReceivedistance_hi, ACKHI2CReceivedistance_lo, NACKHI2CStopwait100msSetLedoffDistance=distance_hi*256+distance_lohserprint"Distance= "+str(Distance)+" mm"HSerPrintCRLFelsehserprint"I2C Ack error"HSerPrintCRLFendifwait100msloop
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
@Anobium, your code worked fine as-it-is. I did not change anything.
when implementing this in a project the onboard led code can be eliminated.
@Stan, I think with your robot car you could eliminate false readings by double checking by a code-loop the distance values before excepting it to act on.
False readings are distance differences that couldn't be possible in the time past knowing the robot car max velosity.
Last edit: Haroen 2021-01-21
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
@Anobium, "And, do you have a protocol analyser? As this would reveal the lockup."
I have two oscilloscopes to analyse waves and pulses! But protocols?
I did a search on "protocol analysis tools".
Is there a Window based analyser software that could do this topic quicker (reveal the lockup)?!
Could it do Serial, I2C(and address finder), SPI, Protocol specification, etc...
What and how do you guy's use?
Is this only something for profs?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
If you have a PICkit2 you can use that as a simple Logic analyser.
It has no Protocol analysis software but can still show if you are getting a response on Serial or I2C communications.
I do have a Full 16 channel Logic analyser with Protocol analysts but 90% of the time the PICKIt2 has shown the issue faster than I could set up the advanced tool.
p.s. Looking at the amazon link, at less than 10 Quid there are some good tools there.
you could also look at the Sigrok Project for opensource Software: https://sigrok.org/
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have tried the Sigrok software, a few times, and, I always end up back with the Saleae logic Version 1.15 software. It is easy to use and the export features enable further decoding.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
There are so many different types, is there an all-in-one?
What do you have and is it suffient?
I don't know exactly what it does extra and if it is in my alley.
I use my two oscilloscopes often:
1) PCSU200 2-channel USB
2) Mini DS213 4-channel portable
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I was told on the forum that pickit could NOT be used as a logic analyser so bought a clone 8 channel analyser that uses a real ones free software but never used it.
I got a usb hantek 1Mb sampling dual channel scope but we are getting too technical for me.
I have not used pull up resistors for i2c with tof10120 yet. Should I ?
I will try again today with the data sheet. the gcb i2c address finder, with no pull up resistors shows it's address as 0xF4
;i2c address finder
#chipmega328p, 16
#optionExplicitdimdeviceIDasbyte' Define I2C settings
#defineHI2C_BAUD_RATE400
#defineHI2C_DATAPORTC.4
#defineHI2C_CLOCKPORTC.5'I2C pins need to be input for SSP module when used on Microchip PIC device; Dir HI2C_DATA in; Dir HI2C_CLOCK in'MASTER MODEHI2CModeMaster
#defineUSART_BAUD_RATE9600
#defineUSART_TX_BLOCKING; Dir PORTc.6 Out
#defineUSART_DELAY10msdoHSerPrintCRLF2HSerPrint"Hardware I2C Discover using the "HSerPrintCHipNameStrHSerPrintCRLF2fordeviceID=0to255HI2CStartHI2CSend(deviceID)ifHI2CAckPollState=falsethenif((deviceID&1)=0)thenHSerPrint"W"elseHSerPrint"R"endifHSerSend9HSerPrint"ID: 0x"HSerPrinthex(deviceID)HSerSend9HSerPrint"(d)"+str(deviceID)HSerPrintCRLFHI2CSend(0)HI2CSend(0)endifHI2CStopnextHSerPrintCRLFHSerPrint"End of Device Search"HSerPrintCRLF2loop
Please help me convert this picaxe code to gcb.I did try but got it wrong and am not good at it.
The picaxe code may be faulty.
The device has i2c and serial out. How to use serial and send to terminal
Thank you @mmotte for your code. It seems to work at 20cm to object and scrolls readings in the terminal which seem accurate but moving the object it freezes and starting at 10 cm or 30 cm and it only gives 1 reading.
It only arrived the day I posted. I have not used pull up resistors.
It also sends serial distance as 5 bytes distance "mm".
edit the data sheet is in chinese
Last edit: stan cartwright 2020-12-05
Here's a picaxe video about the working TOF10120 for reference only.
@Stan, pLease try it for checking distance accuracy at overall range 1,993m.
See here for circuit connections.
When I ordered mine the colours didn't match.
Could be that with your TOF10120 you have to switch the I2C pins sda and scl.
Don't switch the power wires +V and Gnd, Unless you want to celebrate NewYear fireworks early, :)
This proves that TOF10120 is a time-of-flight sensing technology.
I got nowhere with this device
serial or i2c to spi display
thank you mmotte for your time replying.
I'll stick with vl0x units cos I can get the to work!
I think that the
distance_hi*256+distance_lo
exceeds it's limits and crashes!Everytime the distance is <400 and >2000 it stops. and in between it get's false readings and stops too. By also restricting it with a SelectCase like I did in the V53LOX code we can prevent crashing.
I used the OnBoard LED to flash that it has not crashed yet. When that stops then only use the OnBoard button to reset.
TheTOF10120 code that I used also has an address fault like Stan mentioned scl and sda, so we have to clean it up a little to work correct:
Last edit: Haroen 2021-01-19
@Haroen Your code looks good. Thank you for shring.
The change I would recommend is to test that the sensor is responding by checking the
HI2CAckPollState
this should ensure the sensor is in a state to respond to the read request.This do-loop could of course cause a continuous loop, so, you should also add counter or watch dog timer to catch this continuous loop situation.
:-)
I tested with
dim distance_lo, distance_hi as word
,and also tried with deleting
Distance=distance_hi*256 + distance_lo
but still crashing occurs.I don't think it's a variable limit problem anymore.
It has something to do with I2C like Anobium pointed out.
@Anobium, I tried your do-loop but made no difference, still think it should be in there too.
Is it needed to end the continuous loop with a counter or watch dog timer because it should loop until it is in state to respond to the read request?
Can you describe 'crashing' ?
My code was an example for development... this may protect the sensor. Hopefully, improves the situation.
@Anobium, crashing like in all codes that I tried so far with certain distances the OnBoard LED does not flash anymore and the Terminal just stops giving "Distance=xxxmm".
Still crashing (Freezing) now faster with this code:
My error... the test for HI2CAckPollState is in wrong place, and, I have moved the
hI2CSend(RegAddress)
to the correct place (please ensure this is correct).Is the sensor really 400 kbts? Is it really 100 kbts?
Do you need a delay between the reads? The datasheet will reveal this.
And, do you have a protocol analyser? As this would reveal the lockup.
Revised Code
I tried your code but just got I2C Ack error
You have to provide a lot more info, and, there is an intended typo in the code to help with the frequency discussion.
TOF10120 no fun.
It got serial out so connect tx to rx and rx to tx and try reading serial.
the device would be defined as usart0 and the terminal as usart1 dunno.
The serial info from device is supposed to be 5 bytes.3 distance and last 2 "m","m"
again dunno.
looking at "help" :(
You have to provide a lot more info, and, there is an intended typo in the code to help with the frequency discussion.
what intended typo ? RegAddress
too old for games.
This was started on picaxe and if sorted another feather in gcb.
Is the device popular enough to be bothered with?
This code compiles here. But, I am not testing. I am recommending methods to isolate the lockup.
Looks fine by me.
Anobium: Is the sensor really 400 kbts? Is it really 100 kbts?
Me, I can't trust any chinese datasheet if any exist :-|
Anobium: Do you need a delay between the reads?
Me as a sloth, everything is going way toooo fast :)
Anobium: The datasheet will reveal this.
1) Official china datasheet,
2) un-official translated of-the-record datasheet.
The code is working great by-the-way! Stan enjoy!!!
Thanks Anobium!
Last edit: Haroen 2021-01-21
Excellent. Post your final code.
I would like to review to discuss your questions.
@Anobium, your code worked fine as-it-is. I did not change anything.
when implementing this in a project the onboard led code can be eliminated.
@Stan, I think with your robot car you could eliminate false readings by double checking by a code-loop the distance values before excepting it to act on.
False readings are distance differences that couldn't be possible in the time past knowing the robot car max velosity.
Last edit: Haroen 2021-01-21
I have two oscilloscopes to analyse waves and pulses! But protocols?
I did a search on "protocol analysis tools".
Is there a Window based analyser software that could do this topic quicker (reveal the lockup)?!
Could it do Serial, I2C(and address finder), SPI, Protocol specification, etc...
What and how do you guy's use?
Is this only something for profs?
If you have a PICkit2 you can use that as a simple Logic analyser.
It has no Protocol analysis software but can still show if you are getting a response on Serial or I2C communications.
I do have a Full 16 channel Logic analyser with Protocol analysts but 90% of the time the PICKIt2 has shown the issue faster than I could set up the advanced tool.
p.s. Looking at the amazon link, at less than 10 Quid there are some good tools there.
you could also look at the Sigrok Project for opensource Software: https://sigrok.org/
https://www.amazon.co.uk/s?k=logic+analyser+8ch+24mhz&sprefix=logic+anal&ref=nb_sb_ss_ts-a-p_2_10 8 channel @ 24mhz
This is very easy to use. I use almost daily. I use version 1.15 software.
I think this is a great tool to have.
Last edit: Anobium 2021-01-20
Good point on the PICkit2 capabilities.
I have tried the Sigrok software, a few times, and, I always end up back with the Saleae logic Version 1.15 software. It is easy to use and the export features enable further decoding.
There are so many different types, is there an all-in-one?
What do you have and is it suffient?
I don't know exactly what it does extra and if it is in my alley.
I use my two oscilloscopes often:
1) PCSU200 2-channel USB
2) Mini DS213 4-channel portable
I was told on the forum that pickit could NOT be used as a logic analyser so bought a clone 8 channel analyser that uses a real ones free software but never used it.
I got a usb hantek 1Mb sampling dual channel scope but we are getting too technical for me.
I have not used pull up resistors for i2c with tof10120 yet. Should I ?
I will try again today with the data sheet. the gcb i2c address finder, with no pull up resistors shows it's address as 0xF4
Last edit: stan cartwright 2021-01-20