I2C-It BASCOM-AVR
Пример измерения растояния I2C-It для MiniBot'a на BASCOM-AVR
Это первая приблизительная версия, написана на основе примеров и реально на железе не проверялась. Т.к. я в Грузии а МиниБот в России ;-).
<source lang="vb"> '------------------------------------------------------------------------------- 'name : i2c_IRF.bas 'copyright : (c) 2008, Konstantin Kostyuk 'purpose : read data from I2C-It - InfraRed Fainder for I2C bus 'micro : Mega32 'device : MiniBot - Russian National Class 'commercial addon needed : no '------------------------------------------------------------------------------- $prog &HFF , &HE4 , &HD9 , &H00 $regfile = "m32def.dat" $crystal = 8000000 $baud = 2400 '------------------------------------------------------------------------------- ' Hardvare ' ' PC0 - I2c SLC ' PC1 - I2c SDA Config Scl = Portc.0 Config Sda = Portc.1 '------------------------------------------------------------------------------- 'Declarations Declare Sub Get_distance(byval Adres As Byte , Value As Byte) '------------------------------------------------------------------------------- 'Constants ' 'Default Write address for I2C-It. 'You Can set new adress by hardware pins, see datashit Const Waddress = &H20 'Default Read address for I2C-It. Const Raddress = &H21 'Units Type (1=inches) (2=cm) (3=Raw) Const Range_inch = 1 Const Range_cm = 2 Const Range_raw = 3 '------------------------------------------------------------------------------- 'Variables Dim Unit As Byte Dim Value As Byte '-------------------------------------------------------------------------------
Do
'Read and Print to UART Call Get_distance(range_cm , Value) : Print Value Waitms 500
Loop End
'------------------------------------------------------------------------------- 'Implementation
Sub Get_distance(byval Unit As Byte , Value As Byte)
I2cstart 'generate start I2cwbyte Waddress 'write adsress I2cwbyte Unit 'Unit type I2cstart 'repeated start I2cwbyte Raddress 'read address I2crbyte Value , Nack 'read byte I2cstop 'generate stop
End Sub </source>