Using the I2C bus

ok… so i am trying to incorporate using the SRF08 ultrasonic sensor to my hex. Im using the usual setup - BB2 and basic atom pro.
I found some sample code for the basic atom on the net and changed it slightly for the pro (SDA and SCL pin numbers) but having problems compiling it!

SDA Con P6 ’ Define Data pin
SCL Con P7 ’ Define Clk pin
SRF08 Con 0xE0 ’ Sonar I2C Address

CmdReg Con 0 ’ Sonar Command register
LightReg Con 1 ’ Sonar Light sensor register
RangeReg Con 2 ’ Sonar 1st Range register

Light Var Byte ’ Light sensor
Range Var Word ’ 16 bit variable for Range

Main
i2cout SDA, SCL, SRF08, CmdReg, [81]
pause 66
i2cin SDA, SCL, SRF08, LightReg, [Light, Range.HighByte, Range.LowByte]
Serout S_OUT, i9600, “Light1 = 0x”, HEX Light, ", Range = ", DEC Range, " cm, "] ’ Use terminal window

goto main

the problem is:

Error: FILE C:\PROGRAM FILES\BASICMICRO\BASIC MICRO STUDIO\TESTMORE\I2C.BAS(LINE 26) : [TOKEN CMDREG] : Expected

line 26 corresponding to the i2cout part - at the CmdReg bit i think. as far as i can tell its in the right format for an i2cout command but if im missing something could you guys please help? many thanks.

the ‘81’ at the end corresponds to a request for the result in centimeters from the sensor.
Any help would be greatly appreciated

Compiles for BasicAtom, ask basic micro for new command syntax. I looked at manual as I’m sure you did, but couldn’t guess it.

Alan KM6VV

I know that Basic Micro was in the process of changing the I2C Commands. Looking at their current Work In Progress document up on their forum: forums.basicmicro.net/viewtopic. … 3&start=15

I see the command format for I2COut is:
i2cout sda, scl, controlbyte, [addressbyte1, addressbyte2, databyte]
• SDA - is a variable or constant that defines the serial data pin (SDA).
• SCL - is a variable or constant that defines the serial clock pin (SCL).
• ControlByte - is a byte sized variable or constant (0-255) that sets the device ID and the device
address of the I2C device.
• AddressByte1 - is a byte sized variable or constant (0-255) that specifi es the address in a
byte addressing i2c scheme. In word sized i2x addressing schemes it specifi es high byte.
• AddressByte2 - is a byte sized variable or constant (0-255) that specifi es the low byte of a word
sized i2x addressing scheme. AddressByte2 can be ignored in a byte size address scheme.
• DataByte - is a byte sized variable or constant (0-255)

and the Format for I2CIn is:
i2cin sda, scl, controlbyte, [databyte]
• SDA - is a variable or constant that defi nes the serial data pin (SDA).
• SCL - is a variable or constant that defi nes the serial clock pin (SCL).
• ControlByte - is a byte sized variable or constant (0-255) that sets the device ID and the device
address of the I2C device.
• DataByte - is a byte sized variable or constant (0-255).
Supported
• BA - Supported.
• BAN - Supported.
• BAP - Supported.
• BAP40 - Supported.

I am not sure which build this went into…
Kurt

As of .15 Studio i2c syntax has had the eeprom address argument removed. This has always caused a lot of problems when using i2c commands with non-eeprom i2c devices so I finally removed it. Atom and Nano syntax follow the new syntax now too(as of .16). For i2c eeproms you now have to use i2cout (to send the address) and then an i2cin to read the memory location(or block or locations). Eeprom writes still work with a single i2cout but the eeprom address is now part of the data(eg [highbyte,lowbyte,data1,data2,…,datan] for 16bit addressed i2c eeprom).

I’ve nearly completed updating the next release of Studio. The main change is a complete replacement of the old editor. Whats taking some time is I’m adding some intellisense like features. Mouse over a label,var,con or table name anywhere in your code and you’ll get a tooltip with the definition. While typing a command you will now get a tooltip with the syntax just below where you are typing just like in Visual Studio. This will also allow you to get a detailed description of the command and it’s arguments(by clicking the tooltip). Eventually we should have full on the fly parsing for error checking though that won’t be in this next release.

I’m a little confused… According to this website:

robot-electronics.co.uk/htm/srf08tech.html

‘To the programmer the SRF08 behaves in the same way as the ubiquitous 24xx series eeprom’s, except that the I2C address is different’

However, This website contradicts that in that it doesnt mention any of the EEPROM syntax in the program.

robot-electronics.co.uk/acat … atom.shtml

I am struggling to find the correct syntax to use in this command, could anyone please shed some light on how i could read in a simple value from the register of the SRF08?

I know i am probably missing something fairly straight forward but I can’t seem to figure it out! I am now using Basic Micro Studio 1.0.0.15
Thanks!

I have managed to get rid of the I2cout error, but am now getting an error with the i2cin

SDA Con P6 ’ Define Data pin
SCL Con P7 ’ Define Clk pin
SRF08 Con 0xE0 ’ Sonar I2C Address

CmdReg Con 0 ’ Sonar Command register
LightReg Con 1 ’ Sonar Light sensor register
RangeReg Con 2 ’ Sonar 1st Range register

Light Var Byte ’ Light sensor
Range Var Word ’ 16 bit variable for Range

Main
i2cout SDA, SCL, %10100000, [SRF08, 81]
pause 66
i2cin SDA, SCL, %10100001, [SRF08, LightReg, Light, Range.HighByte, Range.LowByte]
Serout S_OUT, i9600, “Light1 = 0x”, HEX Light, ", Range = ", DEC Range, " cm, "] ’ Use terminal window

goto main

Error: FILE C:\USERS\DANIELSON\DOCUMENTS\I2CTESTING.BAS(LINE 15) : [TOKEN SRF08] : Expected variable

Any Help??

You are still sort of using the old format of command… Don’t hold me to it, but I think it might work better something like:


SDA	 Con	 P6	 ' Define Data pin
SCL	 Con	 P7	 ' Define Clk pin 
SRF08	 Con	 0xE0	' Sonar I2C Address

CmdReg	 Con	 0	 ' Sonar Command register
LightReg	Con	 1	 ' Sonar Light sensor register
RangeReg	Con	 2	 ' Sonar 1st Range register

Light	 Var	Byte	 ' Light sensor
Range	 Var	Word	 ' 16 bit variable for Range

Main
    i2cout	SDA, SCL, SRF08, [81]    ; third param is the I2c device address
    pause	66
    i2cout	SDA, SCL, SRF08, [LightReg]    ; Do a write to the I2c Saying we want this variable
    i2cin	SDA, SCL,SRF08, [Range.HighByte, Range.LowByte]
    Serout	S_OUT, i9600, "Light1 = 0x", HEX Light, ", Range = ", DEC Range, " cm, "] ' Use terminal window

goto	main

Again I am rusty on my I2C stuff. But I think the normal way is to do a write of the register number that you want and then do a read or read multiple…

Good Luck
Kurt

thanks for the help! I am nolonger getting compile problems, however another issue has arised! :unamused: :unamused:

SDA Con P6 ’ Define Data pin
SCL Con P7 ’ Define Clk pin
SRF08 Con 0xE0 ’ Sonar I2C Address

CmdReg Con 0 ’ Sonar Command register
LightReg Con 1 ’ Sonar Light sensor register
RangeReg Con 2 ’ Sonar 1st Range register

Light Var Byte ’ Light sensor
Range Var Word ’ 16 bit variable for Range

Main
Serout S_OUT, i9600, “before”] ’ Use terminal window

i2cout   SDA, SCL, SRF08, [81]    ; third param is the I2c device address
Serout   S_OUT, i9600, "after 1"] ' Use terminal window

pause   66
i2cout   SDA, SCL, SRF08, [LightReg]    ; Do a write to the I2c Saying we want this variable
Serout   S_OUT, i9600, "after 2"] ' Use terminal window

i2cin   SDA, SCL,SRF08, [Range.HighByte, Range.LowByte]
Serout   S_OUT, i9600, "after 3"] ' Use terminal window

Serout   S_OUT, i9600, "Light1 = 0x", HEX Light, ", Range = ", DEC Range, " cm, "] ' Use terminal window
Serout   S_OUT, i9600, "after 4"] ' Use terminal window

goto main

so i have put some serout commands to track the flow of the program… It is only outputting the ‘before’ and none of the others…
this suggests that it is getting held up at the first i2cout command.

Could this mean that my setup is incorrect with the pullup resistors etc, or could the syntax still be incorrect? i dont know why the program is getting stuck there :question: :open_mouth:

i used the following guide to help with the setup:

robot-electronics.co.uk/acat … atom.shtml

only adjusting the pin numbers, to 6 and 7, and also putting another pullup resistor on the SCL line aswell as the SDA.

some info on the srf08:

robot-electronics.co.uk/htm/srf08tech.html

some info on the i2c:

robot-electronics.co.uk/htm/ … 2c_bus.htm

Thanks again for the help, as I am very confused into this matter!
Thanks again,
Dan

I have used the SRF08 as well as the SRF10, …

My first suggestions is to change to other IO pins. I have tried in the past to use the hardware SCL/SDA lines (6/7) and have had problems as I think the bit bang I2C code may interfere with the hardware I2C which I think is used to talk to the onboard EEPROM. I used external Pull-up resistors.

Kurt

you say you have used the srf08 and srf10? do you mean you have tested them with the code you suggested and they worked? or you have used them previously with the old syntax?

I will try using other I/O pins, but i thought that they had to use the hardware I2C pins, SDA (pin 6) and SCL (pin 7).

would it just be the same code but change the pin numbers to the corresponding pins i chose?

Tried it, and the program is still freezing at that point!
:cry:

any more suggestions?

Thank you!

I am guessing that because there is no error escape label, the atom is just sitting there indefinately waiting for an ACK from the SRF08.
Do you think this is what is happening?
I can’t figure out why it’s not responding!

forums.basicmicro.net/viewtopic. … 9&start=15

this forum suggests that the basic atom pro has problems with its i2cin and i2cout commands… cyberbound says:

'I have given up getting the I2Cin and I2Cout commands working. They do work for EEPROM… but that’s about it. ’

‘I2Cin and I2Cout do not work properly on the Atom Pro with the exception of simple EEPROM stuff (and even then it’s not perfect or consistant)’

do you think the problem could be with the atom pro? i paid 65 quid for it! :angry:

What version of the IDE are you using?

Did you change to other IO pins? Did you put in the recommended Pull-up resistors? I did get this to work a long time ago (old format), but not on those IO pins…

Who is cyberbound? What version was he running? Someday I will reintegrate my SRF08/10 on one of the robots, but right now have my plate full.

Kurt

i am using the basic micro studio 1.0.0.15 IDE, what IDE do you use?
cyberbound is just a user on the forum, a lot of people seem to have issues using the I2C bus with anything other than EEPROM, and i just mentioned his alias as he said the mentioned quotes.

I did try briefly changing to the other inputs, but had the same problem.
I am using 1k8 pull up resistors for both the SDA and SCL lines to Vdd / Vcc (the middle pin of the set of 3 on the BB2)

Please see the screenshots!

can you see any problems here?

White = SDA
Yellow = SCL
Red = 5v from regulator / from the pull up resistors to the BB2 Vdd
Black = ground



here is another pic

the prototype board i am using is called vari-board, similar to breadboarding the tracks run linear in parallel

Ok so i have managed to stop it getting hung up after the i2cout / in commands., which is good news, however i am recieving values of 0 from the sensor!!!

please see the pic:

Can you think of any reason why it would be doing this?

Thanks again, Dan

Just thought id let you know, managed to get the range sensor working (eventually). :smiley: :smiley:

I Can’t seem to get the light sensor working however, keeps giving a reading of around 250. This is not essential but if i could get it to work that would be nice!

SDA Con P6 ’ Define Data pin
SCL Con P7 ’ Define Clk pin
SRF08 Con 0xE0 ’ Sonar I2C Address

CmdReg Con 0 ’ Sonar Command register
LightReg Con 1 ’ Sonar Light sensor register
RangeReg Con 2 ’ Sonar 1st Range register

Light Var Byte ’ Light sensor
Range Var Word ’ 16 bit variable for Range

main

gosub change

loop
;Serout S_OUT, i9600, “before”] ’ Use terminal window

i2cout   SDA, SCL, SRF08, [CmdReg,81]    ; third param is the I2c device address

; Serout S_OUT, i9600, “after 1”] ’ Use terminal window

pause   66
i2cout   SDA, SCL, SRF08, [RangeReg]    ; Do a write to the I2c Saying we want this variable

; Serout S_OUT, i9600, “after 2”] ’ Use terminal window

i2cin   SDA, SCL,SRF08, [Range.HighByte, Range.LowByte]

; Serout S_OUT, i9600, “after 3”] ’ Use terminal window

i2cout   SDA, SCL, SRF08, [Lightreg]    ; Do a write to the I2c Saying we want this variable

; Serout S_OUT, i9600, “after 2”] ’ Use terminal window

i2cin   SDA, SCL,SRF08, [Light]

; Serout S_OUT, i9600, “after 3”] ’ Use terminal window

Serout   S_OUT, i9600, "Light1 = ", dec Light, ", Range = ", DEC Range, " cm, "] ' Use terminal window

; Serout S_OUT, i9600, “after 4”] ’ Use terminal window

goto loop

;;;;;Change the i2c address (make sure it is at 0xE0) ;;;;;;;;;;;
change:

i2cout   SDA, SCL, SRF08, [CmdReg, 0xA0]    
i2cout   SDA, SCL,SRF08, [CmdReg, 0xAA]
i2cout   SDA, SCL,SRF08, [CmdReg, 0xA5]
i2cout   SDA, SCL,SRF08, [SRF08]


Serout   S_OUT, i9600, "change address "] ' Use terminal window

return

this is my code (working for range, but not for light sensor)

If you can see any problems with the way i am requesting the data from the light register please let me know!

Thanks for your time to those who helped - Greatly appreciated!

Dan

Again try a differeint IO pin. 6/7 are hardware I2C. Also they are 3.3V not 5V so all bets are off. (Although it looks like my old code was using these??? )

Also I discussed some stuff with the Basic Micro people and there are a few issues with the new command syntax. That is there is no provision for the commands to error out. That is it should give you an error if the device fails, (Not there or busy). I don’t think the current version has that in it.

I did find some old code that I had working on the old api. Also I then wrote my own version in a combination Assembly language/ Basic that used the hardware I2C support that is built into the BAP. The program is pretty primitive so not sure if it would help or not… I have an assembly language TimerW handler which I use to do a count down and fail the command… Again not tested!!!

Kurt
i2c test of srf08.bas (17.4 KB)

Um… :unamused:

That post is from 07… It’s not even using the same IDE as you. Please be careful when reading old posts, as they have obviously been fixed since then…