hamid9543
Active Member
Offline
Posts: 124
Thank You
-Given: 80
-Receive: 7
|
|
« on: February 10, 2013, 01:23:43 13:23 » |
|
hi i write code l3g4200d #Include <16f873a.h> #use delay (clock = 20000000) #Use I2C (master, sda = PIN_C3, scl = PIN_C4) #define LCD_RS_PIN PIN_b1 //// #define LCD_RW_PIN PIN_b2 //// #define LCD_ENABLE_PIN PIN_b3 //// #define LCD_DATA4 PIN_b4 //// #define LCD_DATA5 PIN_b5 //// #define LCD_DATA6 PIN_b6 //// #define LCD_DATA7 PIN_b7 #define CTRL_REG1 0x20 #define CTRL_REG2 0x21 #define CTRL_REG3 0x22 #define CTRL_REG4 0x23 #define CTRL_REG5 0x24 #include <LCD.c> int L3G4200D_Address = 0xD2; //I2C address of the L3G4200D int x,y,z; void writeRegister(int deviceAddress, int address,int val) { i2c_start(); i2c_write(deviceAddress); // start transmission to device i2c_write(address); // send register address i2c_write(val); // send value to write i2c_stop(); // end transmission }
void setupL3G4200D(int scale) { writeRegister(L3G4200D_Address, CTRL_REG3, 0b00001000); if(scale == 250) { writeRegister(L3G4200D_Address, CTRL_REG4, 0b00000000); } else if(scale == 500) { writeRegister(L3G4200D_Address, CTRL_REG4, 0b00010000); } else { writeRegister(L3G4200D_Address, CTRL_REG4, 0b00110000); } }
void main() { lcd_init(); lcd_gotoxy(1,1); printf(lcd_putc,"starting up L3G4200D"); delay_ms (1000); setupL3G4200D(2000); //Configure L3G4200 - 250, 500 or 2000 deg/sec writeRegister(L3G4200D_Address,0x20,0x0F); delay_ms(1500); //wait for the sensor to be ready
while(true) { i2c_start(); i2c_write(L3G4200D_Address); i2c_write(0xD3); i2c_start(); int xLSB=i2c_read(0x28); // register to read int xHSB=i2c_read(0x29); // register to read x = make16(xHSB,xLSB); int yLSB=i2c_read(0x2A); int yHSB=i2c_read(0x2B); y = make16(yHSB,yLSB); int zLSB=i2c_read(0x2C); int zHSB=i2c_read(0x2D); z = make16(zHSB,zLSB); i2c_stop(); //This will update x, y, and z with new values lcd_gotoxy(1,2); printf(lcd_putc,"x=%03u",x); lcd_gotoxy(6,1); printf(lcd_putc,"y=%03u",y); lcd_gotoxy(12,1); printf(lcd_putc,"z=%03u",z); delay_ms(100); } } but lcd show x=-1 y=-1 z=-1 can you help me?
|
|
|
Logged
|
|
|
|
robotai
Junior Member
Offline
Posts: 60
Thank You
-Given: 27
-Receive: 23
|
|
« Reply #1 on: February 10, 2013, 03:15:03 15:15 » |
|
The code you used to read I2C seems not correct. Maybe refer to datasheet in below link http://www.st.com/internet/com/TECHNICAL_RESOURCES/TECHNICAL_LITERATURE/DATASHEET/CD00265057.pdfOn P22~23 has correct command tables. Especially Table 17. Part of your code may look like while(true) { i2c_start(); i2c_write(L3G4200D_Address); // write first i2c_write(0x28); // set register to start read i2c_start(); i2c_write(L3G4200D_Address+1); // start read then int xLSB=i2c_read(); // read register in sequence int xHSB=i2c_read(); x = make16(xHSB,xLSB); int yLSB=i2c_read(); int yHSB=i2c_read(); y = make16(yHSB,yLSB); int zLSB=i2c_read(); int zHSB=i2c_read(); z = make16(zHSB,zLSB); i2c_stop(); //This will update x, y, and z with new values lcd_gotoxy(1,2); printf(lcd_putc,"x=%03u",x); lcd_gotoxy(6,1); printf(lcd_putc,"y=%03u",y); lcd_gotoxy(12,1); printf(lcd_putc,"z=%03u",z); delay_ms(100); }
|
|
|
Logged
|
|
|
|
hamid9543
Active Member
Offline
Posts: 124
Thank You
-Given: 80
-Receive: 7
|
|
« Reply #2 on: March 03, 2013, 10:56:16 10:56 » |
|
i chang code and write new code #Include <16f873a.h> #use delay(xtal=20Mhz) #fuses NOWDT,HS #use i2c(Master,sda=PIN_c4,scl=PIN_c3) #define LCD_RS_PIN PIN_b1 #define LCD_RW_PIN PIN_b2 #define LCD_ENABLE_PIN PIN_b3 #define LCD_DATA4 PIN_b4 #define LCD_DATA5 PIN_b5 #define LCD_DATA6 PIN_b6 #define LCD_DATA7 PIN_b7 #include <LCD.c> #define gyro_ADD_R 0xD3 #define gyro_ADD_W 0xD2 #define CTRL_REG1 0x20 #define CTRL_REG2 0x21 #define CTRL_REG3 0x22 #define CTRL_REG4 0x23 #define CTRL_REG5 0x24
int16 x=0; int8 xM=0,xL=0;
void writeRegister(int8 reg,int8 value) { i2c_start(); i2c_write(gyro_ADD_W); i2c_write(reg); i2c_write(value); i2c_stop(); }
void init_gyro() { writeRegister(CTRL_REG1,0b00001111); writeRegister(CTRL_REG2,0b00001000); writeRegister(CTRL_REG3,0b00001000); writeRegister(CTRL_REG4,0b10110000); writeRegister(CTRL_REG5,0b00000000); }
void main() { lcd_init(); lcd_gotoxy(1,1); printf(lcd_putc,"starting up L3G4200D"); delay_ms(1000); init_gyro();
while(true) { i2c_start(); i2c_write(0XD2); i2c_write(0x28); i2c_start(); i2c_write(0XD3); xL=i2c_read(); xM=i2c_read(); x=make16(xM,xL); i2c_stop(); printf(lcd_putc"\f"); lcd_gotoxy(1,2); printf(lcd_putc,"x=%Ld",x); delay_ms(50); }
} but lcd do not display fix value( x ) and this value changing very fast. attention:scan i2c bus works corectly
|
|
|
Logged
|
|
|
|
robotai
Junior Member
Offline
Posts: 60
Thank You
-Given: 27
-Receive: 23
|
|
« Reply #3 on: March 03, 2013, 03:42:15 15:42 » |
|
Seems you made it worked. I suppose the value would change but should within some range. Maybe set CTRL_REG4 to lower dps may help.
|
|
|
Logged
|
|
|
|
flo0319
Junior Member
Offline
Posts: 81
Thank You
-Given: 7
-Receive: 17
|
|
« Reply #4 on: March 03, 2013, 06:44:13 18:44 » |
|
You read a new value and display it with a rate of 20 times / second , you can put a half second delay in main loop and you will can see "normal" changes
|
|
|
Logged
|
|
|
|
hamid9543
Active Member
Offline
Posts: 124
Thank You
-Given: 80
-Receive: 7
|
|
« Reply #5 on: March 04, 2013, 05:47:12 05:47 » |
|
update code while(true) { i2c_start(); i2c_write(0XD2); i2c_write(0x28); i2c_start(); i2c_write(0XD3); xL=i2c_read(); xM=i2c_read(0); x=make16(xM,xL); i2c_stop(); printf(lcd_putc"\f"); lcd_gotoxy(1,2); printf(lcd_putc,"x=%Lu",x); delay_ms(1500); } i do not any move l3g4200d but the value of lcd output is not fixed.
|
|
|
Logged
|
|
|
|
flo0319
Junior Member
Offline
Posts: 81
Thank You
-Given: 7
-Receive: 17
|
|
« Reply #6 on: March 04, 2013, 09:48:49 09:48 » |
|
It's normal to not be a fixed value. You have set a high sensitivity and also the HP filter is disabled. Any variation in magnetic field from your environment will be present in sensor measurement
Any data need to be filtered because noises.
you can change the REG4 for a lower dsp (ex. 0b10010000 for 500 scale)
but I recommend you to use the high pass filter
|
|
|
Logged
|
|
|
|
hamid9543
Active Member
Offline
Posts: 124
Thank You
-Given: 80
-Receive: 7
|
|
« Reply #7 on: March 05, 2013, 07:26:57 07:26 » |
|
update code,but not working......
|
|
|
Logged
|
|
|
|
demure
Newbie
Offline
Posts: 19
Thank You
-Given: 5
-Receive: 19
|
|
« Reply #8 on: March 26, 2013, 01:45:51 13:45 » |
|
L3G4200D.h /*
/************************* L3G4200D Registers *************************/ #define gyro_WHO_AM_I 0x0F #define gyro_CTRL_REG1 0x20 #define gyro_CTRL_REG2 0x21 #define gyro_CTRL_REG3 0x22 #define gyro_CTRL_REG4 0x23 #define gyro_CTRL_REG5 0x24 #define gyro_REFERENCE 0x25 #define gyro_OUT_TEMP 0x26 #define gyro_STATUS_REG 0x27 #define gyro_OUT_X_L 0x28 #define gyro_OUT_X_H 0x29 #define gyro_OUT_Y_L 0x2A #define gyro_OUT_Y_H 0x2B #define gyro_OUT_Z_L 0x2C #define gyro_OUT_Z_H 0x2D #define gyro_FIFO_CTRL_REG 0x2E #define gyro_FIFO_SRC_REG 0x2F #define gyro_INT1_CFG 0x30 #define gyro_INT1_SRC 0x31 #define gyro_INT1_TSH_XH 0x32 #define gyro_INT1_TSH_XL 0x33 #define gyro_INT1_TSH_YH 0x34 #define gyro_INT1_TSH_YL 0x35 #define gyro_INT1_TSH_ZH 0x36 #define gyro_INT1_TSH_ZL 0x37 #define gyro_INT1_DURATION 0x38 i2c rutine you have to define address #define gyro_write_addr 0xD2 #define gyro_read_addr 0xD3
|
|
|
Logged
|
|
|
|
hamid9543
Active Member
Offline
Posts: 124
Thank You
-Given: 80
-Receive: 7
|
|
« Reply #9 on: July 07, 2013, 03:53:37 15:53 » |
|
hi When the l3g4200d is fixed and do not move it out how it is. thanx
|
|
|
Logged
|
|
|
|
|