Menu

picaxe conversion

Help
2020-12-04
2021-02-18
1 2 3 .. 7 > >> (Page 1 of 7)
  • stan cartwright

    stan cartwright - 2020-12-04

    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
    #chip mega328p,16
    #option explicit
    #include <UNO_mega328p.h>
    
    ;for terminal distance out
    #define USART_BAUD_RATE 9600
    #define USART_TX_BLOCKING
    #define USART_DELAY 1 ms
    ;
    #define HI2C_DATA PORTC.4 ;sda
    #define HI2C_CLOCK PORTC.5 ;scl
    #define HI2C_BAUD_RATE 400
    HI2CMode Master
    ;main Get Distance----
    dim distance_lo,distance_hi as byte
    do
    HI2CStart
    HI2CSend(0)
    ;HI2CStop
    
    HI2CReceive(distance_hi)
    HI2CReceive(distance_lo, NACK) ;read one byte and conclude
    HI2CStop
    wait 500 ms
    hserprint "Distance= "+str(distance_hi*256+distance_lo)+" mm"
    HSerPrintCRLF
    loop
    
     
  • mmotte

    mmotte - 2020-12-05
    ;start of gcb code
    ;TOF10120 Laser Range Sensor Module to terminal
    #chip mega328p,16
    #option explicit
    #include <UNO_mega328p.h>
    
    ;for terminal distance out
    #define USART_BAUD_RATE 9600
    #define USART_TX_BLOCKING
    #define USART_DELAY 1 ms
    ;
    #define HI2C_DATA PORTC.4 ;sda
    #define HI2C_CLOCK PORTC.5 ;scl
    #define HI2C_BAUD_RATE 400
    HI2CMode Master
    Dim I2C_Address, RegAddress as Byte
    I2C_Address = 0xA4
    RegAddress  = 0x00
    
    ;main Get Distance----
    dim distance_lo,distance_hi as byte
    do
              HI2CStart
              HI2CSend(I2C_ADDRESS)
              HI2CSend(RegAddress)
              HI2CReStart
              HI2CSend(I2C_ADDRESS+1)               ;indicate a read operation
              HI2CReceive distance_hi, ACK
              HI2CReceive distance_lo, NACK
              HI2CStop
    
    wait 500 ms
    hserprint "Distance= "+str(distance_hi*256+distance_lo)+" mm"
    HSerPrintCRLF
    loop
    
     
  • stan cartwright

    stan cartwright - 2020-12-05

    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
  • Haroen

    Haroen - 2020-12-17

    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.

     
  • stan cartwright

    stan cartwright - 2020-12-31

    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!

     
  • Haroen

    Haroen - 2021-01-19

    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
    #chip mega328p, 16  'Arduino Nano or Uno!
    #option explicit
    #include <UNO_mega328p.h>
    
    ;for terminal distance out
    #define USART_BAUD_RATE 9600
    #define USART_TX_BLOCKING
    #define USART_DELAY 1 ms
    ;I2C TOF10120
    #define HI2C_DATA PORTC.5;scl
    #define HI2C_CLOCK PORTC.4 ;sda
    #define HI2C_BAUD_RATE 400
    HI2CMode Master
    
    Dim I2C_Address, RegAddress as Byte
    I2C_Address = 0xA4
    RegAddress  = 0x00
    wait 500 ms
    
    ;OnBoard LED
    #define Led DIGITAL_13
    Dir Led Out
    
    ;Distance variables
    dim distance_lo,distance_hi as byte
    dim Distance as word
    
    ;main Get Distance----
    do
      Set Led On
      HI2CStart
      HI2CSend(I2C_ADDRESS)
      HI2CSend(RegAddress)
      HI2CReStart
      HI2CSend(I2C_ADDRESS+1)               ;indicate a read operation
      HI2CReceive distance_hi, ACK
      HI2CReceive distance_lo, NACK
      HI2CStop
      wait 100 ms
      Set Led off
      Distance=distance_hi*256+distance_lo
      hserprint "Distance= "+str(Distance)+" mm"
      HSerPrintCRLF
      wait 100 ms
    loop
    
     

    Last edit: Haroen 2021-01-19
  • Anobium

    Anobium - 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.

    do
        HI2CStart
        HI2CSend(I2C_ADDRESS)
        HI2CSend(RegAddress)
    loop While HI2CAckPollState
    HI2CReStart
    HI2CSend(I2C_ADDRESS+1)               ;indicate a read operation
    HI2CReceive distance_hi, ACK
    HI2CReceive distance_lo, NACK
    HI2CStop
    

    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.

    :-)

     
  • Haroen

    Haroen - 2021-01-19

    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?

     
    • Anobium

      Anobium - 2021-01-19

      Can you describe 'crashing' ?

      My code was an example for development... this may protect the sensor. Hopefully, improves the situation.

      dim _li2cCount as byte
      dim _lAckState as byte
      
      ;main Get Distance----
      do
        _li2cCount = 0
        Set Led On
          do
              HI2CStart
              HI2CSend(I2C_ADDRESS)
              HI2CSend(RegAddress)
             _lAckState = HI2CAckPollState 
             _li2cCount++
             if _lAckState = true then
                 wait 1 ms
             End if
          loop While _lAckState  and  i2cCount < 255
      
        if _lAckState = false  then   
                HI2CReStart
                HI2CSend(I2C_ADDRESS+1)               ;indicate a read operation
                HI2CReceive distance_hi, ACK
                HI2CReceive distance_lo, NACK
                HI2CStop
                wait 100 ms
                Set Led off
                Distance=distance_hi*256+distance_lo
                hserprint "Distance= "+str(Distance)+" mm"
                HSerPrintCRLF
         else
             hserprint "I2C Ack error"
             HSerPrintCRLF
      
        end if
          wait 100 ms
      loop
      
       
  • Haroen

    Haroen - 2021-01-19

    @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:

    #chip mega328p, 16  'Arduino Nano or Uno!
    #option explicit
    #include <UNO_mega328p.h>
    
    ;for terminal distance out
    #define USART_BAUD_RATE 9600
    #define USART_TX_BLOCKING
    #define USART_DELAY 1 ms
    ;I2C TOF10120
    #define HI2C_DATA PORTC.5;scl
    #define HI2C_CLOCK PORTC.4 ;sda
    #define HI2C_BAUD_RATE 400
    HI2CMode Master
    
    Dim I2C_Address, RegAddress as Byte
    I2C_Address = 0xA4
    RegAddress  = 0x00
    wait 500 ms
    
    ;I used the OnBoard LED to flash that it has not crashed yet.
    ;When LED stops flashing then use the OnBoard button to reset.
    #define Led DIGITAL_13
    Dir Led Out
    
    ;Distance variables
    dim distance_lo, distance_hi as word
    dim Distance as word
    
    dim _li2cCount as byte
    dim _lAckState as byte
    
    ;main Get Distance----
    do
      _li2cCount = 0
      Set Led On
        do
            HI2CStart
            HI2CSend(I2C_ADDRESS)
            HI2CSend(RegAddress)
           _lAckState = HI2CAckPollState
           _li2cCount++
           if _lAckState = true then
               wait 1 ms
           End if
        loop While _lAckState  and  _li2cCount < 255
    
      if _lAckState = false  then
              HI2CReStart
              HI2CSend(I2C_ADDRESS+1)               ;indicate a read operation
              HI2CReceive distance_hi, ACK
              HI2CReceive distance_lo, NACK
              HI2CStop
              wait 100 ms
              Set Led off
              Distance=distance_hi*256+distance_lo
              hserprint "Distance= "+str(Distance)+" mm"
              HSerPrintCRLF
       else
           hserprint "I2C Ack error"
           HSerPrintCRLF
    
      end if
        wait 100 ms
    loop
    
     
  • Anobium

    Anobium - 2021-01-19

    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

    #chip mega328p, 16  'Arduino Nano or Uno!
    #option explicit
    #include <UNO_mega328p.h>
    
    ;for terminal distance out
    #define USART_BAUD_RATE 9600
    #define USART_TX_BLOCKING
    #define USART_DELAY 1 ms
    ;I2C TOF10120
    #define HI2C_DATA PORTC.5;scl
    #define HI2C_CLOCK PORTC.4 ;sda
    * #define HI2C_BAUD_RATE 400
    HI2CMode Master
    
    Dim I2C_Address, RegAddress as Byte
    I2C_Address = 0xA4
    RegAddress  = 0x00
    wait 500 ms
    
    ;I used the OnBoard LED to flash that it has not crashed yet.
    ;When LED stops flashing then use the OnBoard button to reset.
    #define Led DIGITAL_13
    Dir Led Out
    
    ;Distance variables
    dim distance_lo, distance_hi as word
    dim Distance as word
    
    dim _li2cCount as byte
    dim _lAckState as byte
    
    ;main Get Distance----
    do
      _li2cCount = 0
      Set Led On
        do
            HI2CStart
            HI2CSend(I2C_ADDRESS)
           _lAckState = HI2CAckPollState
           _li2cCount++
           if _lAckState = true then
               wait 1 ms
           End if
        loop While _lAckState  and  _li2cCount < 255
    
      if _lAckState = false  then
             HI2CSend(RegAddress)
             HI2CReStart
              HI2CSend(I2C_ADDRESS+1)               ;indicate a read operation
              HI2CReceive distance_hi, ACK
              HI2CReceive distance_lo, NACK
              HI2CStop
              wait 100 ms
              Set Led off
              Distance=distance_hi*256+distance_lo
              hserprint "Distance= "+str(Distance)+" mm"
              HSerPrintCRLF
       else
           hserprint "I2C Ack error"
           HSerPrintCRLF
    
      end if
        wait 100 ms
    loop
    
     
  • stan cartwright

    stan cartwright - 2021-01-19

    I tried your code but just got I2C Ack error

     
    • Anobium

      Anobium - 2021-01-19

      You have to provide a lot more info, and, there is an intended typo in the code to help with the frequency discussion.

       
  • stan cartwright

    stan cartwright - 2021-01-19

    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" :(

     
  • stan cartwright

    stan cartwright - 2021-01-19

    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?

     
  • Anobium

    Anobium - 2021-01-19

    This code compiles here. But, I am not testing. I am recommending methods to isolate the lockup.

    #chip mega328p, 16  'Arduino Nano or Uno!
    #option explicit
    #include <UNO_mega328p.h>
    
    ;for terminal distance out
    #define USART_BAUD_RATE 9600
    #define USART_TX_BLOCKING
    #define USART_DELAY 1 ms
    ;I2C TOF10120
    #define HI2C_DATA PORTC.5;scl
    #define HI2C_CLOCK PORTC.4 ;sda
    #define HI2C_BAUD_RATE 100
    HI2CMode Master
    
    Dim I2C_Address, RegAddress as Byte
    I2C_Address = 0xA4
    RegAddress  = 0x00
    wait 500 ms
    
    ;I used the OnBoard LED to flash that it has not crashed yet.
    ;When LED stops flashing then use the OnBoard button to reset.
    #define Led DIGITAL_13
    Dir Led Out
    
    ;Distance variables
    dim distance_lo, distance_hi as word
    dim Distance as word
    
    dim _li2cCount as byte
    dim _lAckState as byte
    
    ;main Get Distance----
    do
      _li2cCount = 0
      Set Led On
        do
            HI2CStart
            HI2CSend(I2C_ADDRESS)
           _lAckState = HI2CAckPollState
           _li2cCount++
           if _lAckState = true then
               wait 1 ms
           End if
        loop While _lAckState  and  _li2cCount < 255
    
      if _lAckState = false  then
             HI2CSend(RegAddress)
             HI2CReStart
              HI2CSend(I2C_ADDRESS+1)               ;indicate a read operation
              HI2CReceive distance_hi, ACK
              HI2CReceive distance_lo, NACK
              HI2CStop
              wait 100 ms
              Set Led off
              Distance=distance_hi*256+distance_lo
              hserprint "Distance= "+str(Distance)+" mm"
              HSerPrintCRLF
       else
           hserprint "I2C Ack error"
           HSerPrintCRLF
    
      end if
        wait 100 ms
    loop
    
     
  • Haroen

    Haroen - 2021-01-20

    Anobium: I have moved the hI2CSend(RegAddress) to the correct place (please ensure this is correct).

    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
    • Anobium

      Anobium - 2021-01-20

      Excellent. Post your final code.

      I would like to review to discuss your questions.

       
      • Haroen

        Haroen - 2021-01-20

        @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
  • Haroen

    Haroen - 2021-01-20

    @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?

     
    • Chris Roper

      Chris Roper - 2021-01-20

      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/

       
  • Anobium

    Anobium - 2021-01-20

    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
    • Anobium

      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.

       
    • Haroen

      Haroen - 2021-01-21

      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

       
  • stan cartwright

    stan cartwright - 2021-01-20

    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
    #chip mega328p, 16
    #option Explicit
    dim deviceID as byte
    
        ' Define I2C settings
        #define HI2C_BAUD_RATE 400
        #define HI2C_DATA PORTC.4
        #define HI2C_CLOCK PORTC.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 MODE
        HI2CMode Master
    
        #define USART_BAUD_RATE 9600
        #define USART_TX_BLOCKING
    ;    Dir PORTc.6 Out
        #define USART_DELAY 10 ms
    
    do
        HSerPrintCRLF 2
        HSerPrint "Hardware I2C Discover using the "
        HSerPrint CHipNameStr
        HSerPrintCRLF 2
    
        for deviceID = 0 to 255
          HI2CStart
          HI2CSend ( deviceID )
    
          if HI2CAckPollState = false then
    
             if (( deviceID & 1 ) = 0 ) then
             HSerPrint "W"
            else
             HSerPrint "R"
            end if
            HSerSend 9
            HSerPrint   "ID: 0x"
            HSerPrint   hex(deviceID)
            HSerSend 9
            HSerPrint "(d)"+str(deviceID)
            HSerPrintCRLF
    
            HI2CSend ( 0 )
            HI2CSend ( 0 )
          end if
    
          HI2CStop
        next
        HSerPrintCRLF
        HSerPrint   "End of Device Search"
        HSerPrintCRLF 2
    loop
    
     

    Last edit: stan cartwright 2021-01-20
1 2 3 .. 7 > >> (Page 1 of 7)

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.