hab mit range und gain herumexperimentiert, allerdings kommen keine sinnvolen werte heraus. das lowbyte wandert zwischen 0 und 165. manchmal kommen aber ziemlich viele nullen hintereinander! lese die werte mit dem terminal programm von windows ein (bei 9600baud)
timing-probleme?!
ich verwende zur ansteuerung das programm von H.J. Windt
unter http://www.robot-electronics.co.uk/shop/Examples.htm
Code:
'----------------------------------------------------------------------------------------------------'
'**********************************************************************'
'* WINDT SYSTEMS *'
'* SRF08 RANGE VIA EMULATED I2C BUS v1.3 for CCIUM2.01 with CCIAB2.0 *'
'* H.J. WINDT *'
'* 2005 *'
'**********************************************************************'
'----------------------------------------------------------------------------------------------------'
'This software, with the C-Control I Unit M2.01 mounted on the C-Control I Application Board 2.0,'
'will fire the SRF08 and read the light-sensor and 1st range and display the data on the LCD'
'as Light and Range.'
'Communication with the SRF08 is via an emulated I2C bus, this is due to the fact that the'
'standard I2C "IIC" software for the CCIUM2.01 does NOT check for clock stretching!'
'The emulated SDA line is via port 4.'
'The emulated SCL line is via port 5.'
'Feel free to use and share this software!'
'----------------------------------------------------------------------------------------------------'
'**************** I/O PORTS ****************'
define sda port[4]
define scl port[5]
define lcd_light_off port[16]
'*******************************************'
'**************** VARIABLES ****************'
define control_flags byte[1]
define i2c_nack bit[1]
define i2c_last bit[2]
define i2c_nack_count byte[2]
define i2c_byte byte[3]
define i2c_out_bit bit[24]
define i2c_in_bit bit[17]
define loop byte[4]
define light byte[6]
define range word[4]
define range_high_byte byte[7]
define range_low_byte byte[8]
'*******************************************'
'**************** CONSTANTS ****************'
define srf08_gain 31 'SRF08 GAIN / DEFAULT = 31'
define srf08_range 255 'SRF08 RANGE / DEFAULT = 255'
'*******************************************'
'****************** SETUP ******************'
print"#ON_LCD#"; : print"#INIT#"; : print"#CLR#"; : print"#OFF#"; : lcd_light_off = off
print"#ON_CONFIG#"; : put &b1000 : print"#OFF#";
#try_again
gosub initialize_srf08
if i2c_nack then goto try_again
'*******************************************'
'***************** PROGRAM *****************'
#start
gosub get_srf08_range
if i2c_nack then goto start
print"#ON_LCD#";
print"#L101#";
print"Light = ";light;" ";
print"#L201#";
print"Range = ";range;"cm";" ";
print"#OFF#";
goto start
'*******************************************'
'*************** SUBROUTINES ***************'
#get_srf08_range
#fire_srf08
gosub start_i2c
i2c_byte = &he0 : gosub write_byte_i2c
i2c_byte = 0 : gosub write_byte_i2c
i2c_byte = &h51 : gosub write_byte_i2c
gosub stop_i2c
if i2c_nack_count > 100 then goto no_ack_from_srf08
if i2c_nack then goto fire_srf08
#read_light_and_range
#srf08_wait_and_setup
gosub start_i2c
i2c_byte = &he0 : gosub write_byte_i2c
i2c_byte = 1 : gosub write_byte_i2c
gosub stop_i2c
#pass_srf08_setup
if i2c_nack_count > 100 then goto no_ack_from_srf08
if i2c_nack then goto srf08_wait_and_setup
#read_srf08_data
gosub start_i2c
i2c_byte = &he1 : gosub write_byte_i2c
gosub read_byte_i2c : light = i2c_byte
gosub read_byte_i2c : range_high_byte = i2c_byte
gosub read_last_byte_i2c : range_low_byte = i2c_byte
gosub stop_i2c
if i2c_nack then goto pass_srf08_setup
return
#start_i2c
sda = 0 : scl = 0
return
#stop_i2c
sda = 0 : deact scl : deact sda
return
#write_byte_i2c
for loop = 1 to 8
sda = i2c_out_bit
deact scl
#write_byte_i2c_clock_stretch
if not scl then goto write_byte_i2c_clock_stretch
scl = 0
i2c_byte = i2c_byte shl 1
next
deact sda
deact scl
#write_byte_i2c_ack_clock_stretch
if not scl then goto write_byte_i2c_ack_clock_stretch
i2c_nack = sda
scl = 0
if i2c_nack then goto i2c_error
i2c_nack_count = 0
return
#i2c_error
i2c_nack_count = i2c_nack_count + 1
goto stop_i2c
#read_byte_i2c
i2c_last = 0
#get_i2c_byte
deact sda
for loop = 1 to 8
deact scl
#read_byte_i2c_clock_stretch
if not scl then goto read_byte_i2c_clock_stretch
i2c_in_bit = sda
scl = 0
if loop < 8 then i2c_byte = i2c_byte shl 1
next
if i2c_last then deact sda else sda = 0
deact scl
#read_byte_i2c_ack_clock_stretch
if not scl then goto read_byte_i2c_ack_clock_stretch
scl = 0
return
#read_last_byte_i2c
i2c_last = 1
goto get_i2c_byte
'*******************************************'
'******* INITIALIZATION SUBROUTINES ********'
#initialize_srf08
gosub start_i2c
i2c_byte = &b11100000 : gosub write_byte_i2c
i2c_byte = &b00000001 : gosub write_byte_i2c
i2c_byte = srf08_gain : gosub write_byte_i2c
i2c_byte = srf08_range : gosub write_byte_i2c
#pass_initialize_srf08
gosub stop_i2c
if i2c_nack_count > 16 then goto no_ack_from_srf08
if i2c_nack then goto initialize_srf08
return
'*******************************************'
'****************** DATA *******************'
'*******************************************'
'************* ERROR MESSAGES **************'
#no_ack_from_srf08
beep 1,1,1:beep 10,1,1:beep 1,1,1:beep 10,1,1
print"#ON_LCD#";
print"#CLR#";
print"E-I2C ERROR:";
print"#L201#";
print"SRF08 not found";
pause 300
print"#CLR#";
print"#OFF#";
return
'*******************************************'
wie kann ich low-byte und high-byte zusammenfassen und als einen wert an die serielle shcnittstelle zurückgeben.
ist es möglich einen 2ten SRF10 an dieselbe c-control anzuschliessen
Lesezeichen