Hi,
(I'm French)
I Try to do a i2c with Pic 16f887 and mpu6050. But I can't read the register $43(0x43)...
Init the i2c is 400khz.
program MPU6050_16F887_i2c_mikrobasic
' Lcd module connections
dim LCD_RS as sbit at RB4_bit
LCD_EN as sbit at RB5_bit
LCD_D4 as sbit at RB0_bit
LCD_D5 as sbit at RB1_bit
LCD_D6 as sbit at RB2_bit
LCD_D7 as sbit at RB3_bit
LCD_RS_Direction as sbit at TRISB4_bit
LCD_EN_Direction as sbit at TRISB5_bit
LCD_D4_Direction as sbit at TRISB0_bit
LCD_D5_Direction as sbit at TRISB1_bit
LCD_D6_Direction as sbit at TRISB2_bit
LCD_D7_Direction as sbit at TRISB3_bit
' End Lcd module connections
dim txt1 as char[16]
txt2 as char[4]
txt3 as char[8]
txt4 as char[7]
i as word
j as word
txt as string [5]
i2c as word
accx_out_h as word
accx_out_l as word
accx_out as word
gyrox_out_h as integer
gyrox_out_l as integer
gyrox_out as integer
gyroy_out_h as word
gyroy_out_l as word
gyroy_out as word
gyroz_out_h as word
gyroz_out_l as word
gyroz_out as word
MPU6050_ADDRESS as byte
MPU6050_ADDRESS_read as byte
sub procedure Move_Delay() ' Function used for text moving
Delay_ms(500) ' You can change the moving speed here
end sub
sub procedure init_lecturei2c_acc
I2C1_Start() ' issue I2C start signal
I2C1_Wr(MPU6050_ADDRESS) ' send byte via I2C (device address + W) adresse mpu6050
I2C1_Wr($1C) ' send byte (address of EEPROM location)
I2C1_Wr($10) ' send data (data to be written)
I2C1_Stop() ' issue I2C stop signal
I2C1_Start() ' issue I2C start signal
I2C1_Wr(MPU6050_ADDRESS) ' send byte via I2C (device address + W) adresse mpu6050
I2C1_Wr($6A) ' send byte (address of EEPROM location)
I2C1_Wr(%00100000) ' send data (data to be written)
I2C1_Stop() ' issue I2C stop signal
I2C1_Start() ' issue I2C start signal
I2C1_Wr(MPU6050_ADDRESS) ' send byte via I2C (device address + W)
I2C1_Wr($37) ' send byte (address of EEPROM location)
I2C1_Wr($00) ' send data (data to be written)
I2C1_Stop() ' issue I2C stop signal
I2C1_Start() ' issue I2C start signal
I2C1_Wr(MPU6050_ADDRESS) ' send byte via I2C (device address + W)
I2C1_Wr($24) ' send byte (address of EEPROM location)
I2C1_Wr($0D) ' send data (data to be written)
I2C1_Stop() ' issue I2C stop signal
I2C1_Start() ' issue I2C start signal
I2C1_Wr(MPU6050_ADDRESS) ' send byte via I2C (device address + W)
I2C1_Wr($25) ' send byte (address of EEPROM location)
I2C1_Wr($80) ' send data (data to be written)
I2C1_Stop() ' issue I2C stop signal
I2C1_Start() ' issue I2C start signal
I2C1_Wr(MPU6050_ADDRESS) ' send byte via I2C (device address + W)
I2C1_Wr($26) ' send byte (address of EEPROM location)
I2C1_Wr($01) ' send data (data to be written)
I2C1_Stop() ' issue I2C stop signal
I2C1_Start() ' issue I2C start signal
I2C1_Wr(MPU6050_ADDRESS) ' send byte via I2C (device address + W)
I2C1_Wr($27) ' send byte (address of EEPROM location)
I2C1_Wr($86) ' send data (data to be written)
I2C1_Stop() ' issue I2C stop signal
end sub
sub procedure lecturei2c_acc
I2C1_Start() ' issue I2C start signal
I2C1_Wr(MPU6050_ADDRESS) ' send byte via I2C (device address + W)
I2C1_Wr($3B) ' send byte (data address)
I2C1_repeated_Start() ' issue I2C signal repeated start
I2C1_Wr(MPU6050_ADDRESS_read) ' send byte (device address + R)
accx_out_l = I2C1_Rd(0) ' read the data (NO acknowledge)
'gyrox_out_l = word(gyrox_out_l <<8)
'gyrox_out_h = I2C1_Rd(1)/4 ' read the data (NO acknowledge)
'gyroy_out_h = I2C1_Rd(2) ' read the data (NO acknowledge)
'gyroy_out_h = I2C1_Rd(3)/4 ' read the data (NO acknowledge)
'gyroz_out_h = I2C1_Rd(4) ' read the data (NO acknowledge)
'gyroz_out_h = I2C1_Rd(5)/4 ' read the data (NO acknowledge)
I2C1_Stop() ' issue I2C stop signal
end sub
sub procedure init_lecturei2c_gyro
I2C1_Start() ' issue I2C start signal
I2C1_Wr(MPU6050_ADDRESS) ' send byte via I2C (device address + W) adresse mpu6050
I2C1_Wr($6B) ' send byte (address of EEPROM location)
I2C1_Wr($80) ' send data (data to be written)
I2C1_Stop() ' issue I2C stop signal
delay_ms(5)
I2C1_Start() ' issue I2C start signal
I2C1_Wr(MPU6050_ADDRESS) ' send byte via I2C (device address + W) adresse mpu6050
I2C1_Wr($6B) ' send byte (address of EEPROM location)
I2C1_Wr($03) ' send data (data to be written)
I2C1_Stop() ' issue I2C stop signal
I2C1_Start() ' issue I2C start signal
I2C1_Wr(MPU6050_ADDRESS) ' send byte via I2C (device address + W)
I2C1_Wr($1A) ' send byte (address of EEPROM location)
I2C1_Wr($01) ' send data (data to be written)
I2C1_Stop() ' issue I2C stop signal
I2C1_Start() ' issue I2C start signal
I2C1_Wr(MPU6050_ADDRESS) ' send byte via I2C (device address + W)
I2C1_Wr($1B) ' send byte (address of EEPROM location)
I2C1_Wr($18) ' send data (data to be written)
I2C1_Stop() ' issue I2C stop signal
end sub
sub procedure lecturei2c_gyro
I2C1_Start() ' issue I2C start signal
I2C1_Wr(MPU6050_ADDRESS) ' send byte via I2C (device address + W)
I2C1_Wr($43) ' send byte (data address)
I2C1_repeated_Start() ' issue I2C signal repeated start
I2C1_Wr(MPU6050_ADDRESS_read) ' send byte (device address + R)
gyrox_out_l = I2C1_Rd(0) ' read the data (NO acknowledge)
'gyrox_out_l = word(gyrox_out_l <<8)
'gyrox_out_h = I2C1_Rd(1)/4 ' read the data (NO acknowledge)
'gyroy_out_l = I2C1_Rd(2) ' read the data (NO acknowledge)
'gyroy_out_h = I2C1_Rd(3)/4 ' read the data (NO acknowledge)
'gyroz_out_l = I2C1_Rd(4) ' read the data (NO acknowledge)
'gyroz_out_h = I2C1_Rd(5)/4 ' read the data (NO acknowledge)
I2C1_Stop() ' issue I2C stop signal
'gyrox_out= (gyrox_out_h << 8)+(gyrox_out_l)
end sub
main:
SSPSTAT=%00011100
SSPADD=$09
SSPCON=$28
TRISA=255
TRISB = 0
PORTA=0
PORTB = $FF
TRISB = $FF
TRISC = 0
MPU6050_ADDRESS=$68
MPU6050_ADDRESS_read= $69
gyrox_out_l=5
accx_out_l=254
adcon1=%10001110
ANSEL = 0 ' Configure AN pins as digital I/O
ANSELH = 0
I2C1_Init(400000) ' initialize I2C communication 400khz pour mpu6050 verifier si horloge pic 20 mhz
txt1 = "afficheur"
txt2 = "sam BO"
txt3 = "gyroscope"
txt4 = "Altimetre"
Lcd_Init() ' Initialize Lcd
Lcd_Cmd(_LCD_CLEAR) ' Clear display
Lcd_Cmd(_LCD_CURSOR_OFF) ' Cursor off
Lcd_Out(1,3,txt3) ' Write text in first row
Lcd_Out(2,6,txt2) ' Write text in second row
Delay_ms(2000)
Lcd_Cmd(_LCD_CLEAR) ' Clear display
Lcd_Out(1,1,txt1) ' Write text in first row
Lcd_Out(2,6,"sam") ' Write text in second row
Delay_ms(500)
Lcd_Cmd(_LCD_CLEAR) ' Clear display
Lcd_Out(1,6,"gyro")
'init_lecturei2c_acc()
'init_lecturei2c_gyro()
while TRUE ' boucle
init_lecturei2c_acc()
init_lecturei2c_gyro()
lecturei2c_gyro()
lecturei2c_acc()
wordToStr(accx_out_l, txt)
Lcd_out(2,5,txt)
'delay_ms(200)
wend
end.
- Log in to post comments
phpbb Topic ID
14937