|
|
View previous topic :: View next topic |
Author |
Message |
rajm
Joined: 06 Nov 2012 Posts: 29
|
L3GD20 problem in reading values *** Locked - No progress |
Posted: Mon Apr 29, 2013 10:27 pm |
|
|
hi
I have got Pololu minIMU-9V2, in which I'm trying to read gyroscope values. When i connect my sensor its always showing X=-1, and when i disconnect my sensor its showing X=10223 0r X=10877. Since it has got pull up resistor i didn't connect external pull up resistor.
my code is
Code: |
#Include <18f4550.h>
//#use delay(xtal=20Mhz)
//#fuses NOWDT,HS
#fuses HS,MCLR,NOWDT,NOPROTECT,NOLVP,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=2000000)
#use i2c(Master,sda=PIN_B0,scl=PIN_B1)
#define LCD_ENABLE_PIN PIN_C2
#define LCD_RS_PIN PIN_C1
#define LCD_RW_PIN PIN_C0
#define DATA_PORT PORTD
#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); //10110000
writeRegister(CTRL_REG5,0b00000000);
}
void main()
{
lcd_init();
lcd_gotoxy(1,1);
printf(lcd_putc," L3GD20"); // 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(0);
x=make16(xM,xL);
i2c_stop();
printf(lcd_putc"\f");
lcd_gotoxy(1,2);
printf(lcd_putc,"x=%Ld",x);
delay_ms(50);
}
}
|
kindly help me to overcome this problem.
regards |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Apr 29, 2013 11:10 pm |
|
|
Quote: | #Include <18f4550.h>
//#use delay(xtal=20Mhz)
//#fuses NOWDT,HS
#fuses HS,MCLR,NOWDT,NOPROTECT,NOLVP,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=2000000)
|
What is the frequency of your external crystal or oscillator ? |
|
|
rajm
Joined: 06 Nov 2012 Posts: 29
|
|
Posted: Tue Apr 30, 2013 12:23 am |
|
|
in my pic demo board it is 4mhz..but i dont know how to see in datasheet.. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Apr 30, 2013 11:24 am |
|
|
Quote: | in my pic demo board it is 4mhz |
Then why do you have #use delay() set for 2 MHz ? The #use delay()
frequency tells the compiler the PIC oscillator speed. Then the compiler
can correctly setup the internal delay routines and hardware modules. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Tue Apr 30, 2013 11:43 am |
|
|
and 'HS'....
Best Wishes |
|
|
vrs
Joined: 30 Apr 2013 Posts: 18
|
|
Posted: Tue Apr 30, 2013 5:26 pm |
|
|
Code: | #include "C:\Users\semerci\Desktop\dddd\imu.h"
#include <flexLCD.C>
#int_SSP
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use i2c(master, SDA=PIN_C4, SCL=PIN_C3)
void SSP_isr(void)
{
}
#define gyro_ADD_R 0b11010111
#define gyro_ADD_W 0b11010110
/*#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23
#define CTRL_REG5 0x24
#define L3G_DEVICE_AUTO 0
#define L3G4200D_DEVICE 1
#define L3GD20_DEVICE 2*/
// SA0 states
#define L3G_SA0_LOW 0
#define L3G_SA0_HIGH 1
#define L3G_SA0_AUTO 2
// register addresses
#define L3G_WHO_AM_I 0x0F
#define L3G_CTRL_REG1 0x20
#define L3G_CTRL_REG2 0x21
#define L3G_CTRL_REG3 0x22
#define L3G_CTRL_REG4 0x23
#define L3G_CTRL_REG5 0x24
#define L3G_REFERENCE 0x25
#define L3G_OUT_TEMP 0x26
#define L3G_STATUS_REG 0x27
#define L3G_OUT_X_L 0x28
#define L3G_OUT_X_H 0x29
#define L3G_OUT_Y_L 0x2A
#define L3G_OUT_Y_H 0x2B
#define L3G_OUT_Z_L 0x2C
#define L3G_OUT_Z_H 0x2D
#define L3G_FIFO_CTRL_REG 0x2E
#define L3G_FIFO_SRC_REG 0x2F
#define L3G_INT1_CFG 0x30
#define L3G_INT1_SRC 0x31
#define L3G_INT1_THS_XH 0x32
#define L3G_INT1_THS_XL 0x33
#define L3G_INT1_THS_YH 0x34
#define L3G_INT1_THS_YL 0x35
#define L3G_INT1_THS_ZH 0x36
#define L3G_INT1_THS_ZL 0x37
#define L3G_INT1_DURATION 0x38
int16 x=0;
int8 xM=0,xL=0;
int8 yM=0,yL=0;
int8 zM=0,zL=0;
void writeRegister(int8 reg,int8 value)
{ lcd_gotoxy(6, 1);
printf(lcd_putc, "1");
i2c_start(); lcd_gotoxy(6, 1);printf(lcd_putc, "test1");
i2c_write(gyro_ADD_W);lcd_gotoxy(7, 1);printf(lcd_putc, "test11");
i2c_write(reg); lcd_gotoxy(8, 1);printf(lcd_putc, "test11");
i2c_write(value);lcd_gotoxy(9, 1);printf(lcd_putc, "test1");
i2c_stop();lcd_gotoxy(10, 1);printf(lcd_putc, "test11");
lcd_gotoxy(0, 1);
printf(lcd_putc, "i2cstart çıkış");
delay_ms(50);
}
void init_gyro()
{ printf(lcd_putc, "\f");
lcd_gotoxy(0, 1);
printf(lcd_putc, "i2cstart test");
writeRegister(L3G_CTRL_REG1,0b00001111);
lcd_gotoxy(0, 2);
printf(lcd_putc, "i2cstart tes");
writeRegister(L3G_CTRL_REG2,0b00001000);
lcd_gotoxy(0, 3);
printf(lcd_putc, "i2cstart tes");
writeRegister(L3G_CTRL_REG3,0b00001000);
lcd_gotoxy(0, 4);
printf(lcd_putc, "i2cstart tes");
writeRegister(L3G_CTRL_REG4,0b10110000); //10110000
lcd_gotoxy(10, 1);
printf(lcd_putc, "tamam");
writeRegister(L3G_CTRL_REG5,0b00000000);
}
void main()
{
set_tris_c(0xFE);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_CLOCK_DIV_2);
setup_psp(PSP_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_spi(SPI_SS_DISABLED);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
lcd_init();
printf(lcd_putc, "\f");
lcd_gotoxy(0, 1);
printf(lcd_putc, "ekran deneme");
lcd_gotoxy(0, 2);
printf(lcd_putc, "ekran deneme2");
lcd_gotoxy(0, 3);
printf(lcd_putc,"ekran deneme3");
lcd_gotoxy(0, 4);
printf(lcd_putc, "6543210987654321");
delay_ms(1000);
init_gyro();
lcd_init();
printf(lcd_putc, "\f");
lcd_gotoxy(1,1);
printf(lcd_putc," L3GD20"); // l3g4200d
delay_ms(500);
while(1)
{
i2c_start();
i2c_write(0b11010110 );
i2c_write(L3G_OUT_x_H);
i2c_start();
i2c_write(0b11010111);
xL=i2c_read();
xM=i2c_read(0);
i2c_stop();
i2c_start();
i2c_write(0b11010110 );
i2c_write(L3G_OUT_y_H);
i2c_start();
i2c_write(0b11010111);
yL=i2c_read();
yM=i2c_read(0);
i2c_stop();
i2c_start();
i2c_write(0b11010110 );
i2c_write(L3G_OUT_z_H);
i2c_start();
i2c_write(0b11010111);
zL=i2c_read();
zM=i2c_read(0);
i2c_stop();
x=x+1;
if (x=1)
{
// x=make16(xM,xL);
printf(lcd_putc"\f");
lcd_gotoxy(1,2);
printf(lcd_putc,"\%Ld ",x); //x=%Ld
lcd_gotoxy(0, 3);
printf(lcd_putc,"\%d \%d \%d",xm,ym,zm);
lcd_gotoxy(0, 4);
printf(lcd_putc,"\%d \%d \%d",xl,yl,zl);
delay_ms(500);
x=0;
xM=0,xL=0;
yM=0,yL=0;
zM=0,zL=0;
}
}
}
|
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed May 01, 2013 5:31 am |
|
|
Dear user 'vrs',
Why are you posting this code? Without any comments we don't know what to do with it.
Are you the same person as the original topic starter 'rajm'? I do suspect so because you did also post in Rajm's other thread, but without any comments we don't know.
See Rajm's other thread where Ttelmah made some comments on the SSP interrupt. These same comments do apply here.
Then, why did you post the code?
Is it not working and do you want us to have a look at it?
If yes, then tell us what is working and what is not. What is the result you expected to get and what are the (wrong) results you do get. You are the one with the real hardware in front of you. We can't read minds so need all the clues you can give us.
ALWAYS post your compiler version number!
Code: | setup_spi(SPI_SS_DISABLED); | This line is wrongly generated by a wizard in the old compiler versions, so at least we know you have an old compiler, but how old?
The correct code should be:or in the newer versions: Code: | setup_spi(SPI_DISABLED); |
This is such a beginner's mistake... I suggest you get your C book and fix it yourself.
The code as posted can not be tested by us because the file imu.h is missing. Now we don't know which processor you are using.
For example the original Ramj code was using a PIC18F4550 which requires the NOPBADEN fuse if you want too use PortB for digital I/O. |
|
|
vrs
Joined: 30 Apr 2013 Posts: 18
|
|
Posted: Wed May 01, 2013 5:54 am |
|
|
Minumuv9 to read the value from the sample.
if(x=1) to test. I forgot to delete it.
code is incorrect. minimuv9 can read from from the value.
to to be an example. to Rajm |
|
|
vrs
Joined: 30 Apr 2013 Posts: 18
|
|
Posted: Wed May 01, 2013 6:01 am |
|
|
using pic 16F877.
codes are added for illustration.
To read the value from minimu9v2.
working.
I do not know English. I am writing the message using Google Translate. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed May 01, 2013 10:22 am |
|
|
Hi vrs,
Thank you for sharing your code.
Always nice when someone who doesn't speak English makes the effort to share his knowledge. |
|
|
rajm
Joined: 06 Nov 2012 Posts: 29
|
|
Posted: Wed May 01, 2013 10:41 pm |
|
|
hi
Thank you for your help. I don't know whether program updated by vrs will work for my sensor. For what purpose the void SSP_isr(void) is used ?
How to display these in 2x16 lcd? Its enough if it displays one after other on same line in lcd.
regards |
|
|
vrs
Joined: 30 Apr 2013 Posts: 18
|
|
Posted: Thu May 02, 2013 5:02 am |
|
|
http://www.ccsinfo.com/forum/viewtopic.php?t=50012&highlight=minimu9
here's the last I wrote the code is running.
LCD 4 x 16
row 2 gyro x y z
line 3 acce x y z
line 4 magn x y z
Values are not regulated. is intended to represent the screen.
You do the arrangements.
I ran into the hardware. Not on computer.
I did the installation to 16F877. I used 4.7k pullup resistor.
errors to be passed to the new ccs c. |
|
|
rajm
Joined: 06 Nov 2012 Posts: 29
|
|
Posted: Fri May 03, 2013 12:36 am |
|
|
hi
You wrote the code for gyro in the above post, then how it display all the three sensor values in LCD. I have changed that gyro code for my gyro sensor L3GD20, but it shows result as -1 -1 -1 and it is not changing when i change the sensor. How to overcome this?
regards |
|
|
rajm
Joined: 06 Nov 2012 Posts: 29
|
|
Posted: Fri May 03, 2013 2:27 am |
|
|
hi vrs
explain about first #include and int8 line in the last coding you wrote in above link..
regards |
|
|
vrs
Joined: 30 Apr 2013 Posts: 18
|
|
Posted: Fri May 03, 2013 2:49 am |
|
|
Code: |
#include "C:\Users\semerci\Desktop\dddd\lcdddd.h"
#include <flexLCD.C>
#include <16f877a>
#include <imu.C> \\
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use i2c(MASTER, SDA=PIN_C4, SCL=PIN_C3,fast)
int8 konum=0, hesapH=0,hesapL=0;
signed long hesap=0;
void writeRegister(int8 reg,int8 value,int tur)
{
i2c_start();
i2c_write(tur);
i2c_write(reg);
i2c_write(value);
i2c_stop();
}
void init_gyro()
{
writeRegister(L3G_CTRL_REG1,0b11101111,gyro_ADD_W);
writeRegister(L3G_CTRL_REG2,0b00000000,gyro_ADD_W);
writeRegister(L3G_CTRL_REG4,0b0010000,gyro_ADD_W);
}
void init_acce()
{
writeRegister(LSM303_CTRL_REG1_A,0x47,acce_ADD_W);
writeRegister(LSM303_CTRL_REG4_A,0b00101000,acce_ADD_W);
}
void init_magn()
{
writeRegister(LSM303_SR_REG_M, 0b00000001,magn_ADD_W);
writeRegister(LSM303_IRA_REG_M,0b01001000,magn_ADD_W);
writeRegister(LSM303_IRB_REG_M,0b00110100,magn_ADD_W);
writeRegister(LSM303_IRC_REG_M,0b00110011,magn_ADD_W);
writeRegister(LSM303_CRA_REG_M,0b10010000,magn_ADD_W);
writeRegister(LSM303_CRB_REG_M,0b10000000,magn_ADD_W);
writeRegister(LSM303_MR_REG_M, 0b00000000,magn_ADD_W);
}
int a;
void Hesapla_Yaz(int adresR,int adresW) //adresR=device Read adres,adresW =device write adres
{
int i;a=1;int b=0;char c;
for ( i=40;i<46;i+=2) //40=0x28 xHbit, 42=0x2b yHbit, 44=0x2d zHbit
{
b=i; //b=hbit, b+1=lbit. acce,gyro xHbit=0x28 xLbit=029
if (adresR==magn_ADD_R)
{
konum=2;
b=i-37;//magnetometer xLbit=3 xHbit=4,yLbit=5 yHbit=6,zHbit=7,zHbit=8
c="m";
}
i2c_start();i2c_write(adresW);i2c_write(b);i2c_start();i2c_write(adresR);hesapH=i2c_read(),i2c_read(0);i2c_stop(); //hesapH=device x or y or z hbit
i2c_start();i2c_write(adresW);i2c_write(b+1);i2c_start();i2c_write(adresR);hesapL=i2c_read(),i2c_read(0);i2c_stop(); //hesapL=device x or y or z Lbit
hesap=make16(hesapH,hesapL);//hesap =long variable
if (adresR==acce_ADD_R)
{
konum=3;//lcd line
hesap=hesap>>4; //Accelerometer: one 12-bit reading (left-justified) per axis
c="a";
}
if (adresR==gyro_ADD_R)
{
c="g";
konum=4;
}
Lcd_gotoxy(konum+8, 1); printf(lcd_putc,"\%c",c);Lcd_gotoxy(1, 1); printf(lcd_putc,"satirlar=");//lcd 2x16 for konum=1 or 2
Lcd_gotoxy(a, konum); printf(lcd_putc,"\%Ld",hesap);
a=a+6; //lcd row
}
}
void main()
{
set_tris_c(0xFE);
/*setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_CLOCK_DIV_2);
setup_psp(PSP_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_spi(SPI_SS_DISABLED);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);*/
lcd_init();
printf(lcd_putc, "\f");
lcd_gotoxy(1, 1);
printf(lcd_putc, "minimu9 v2");
lcd_gotoxy(1, 3);
printf(lcd_putc, "KARAMAN/ERMENEK");
lcd_gotoxy(1, 2);
printf(lcd_putc, "pic 16f877");
lcd_gotoxy(1, 2);
printf(lcd_putc, "4x16 lcd");
delay_ms(1000);
init_gyro();
init_acce();
init_magn();
lcd_init();
while(1)
{
hesapla_yaz(gyro_ADD_R,gyro_ADD_W); //only gyro
hesapla_yaz(acce_ADD_R,acce_ADD_W); //only accelometer
init_magn(); //not work Without this.//compas for
hesapla_yaz(magn_ADD_R,magn_ADD_W);//compas
delay_ms(100);
printf(lcd_putc"\f");
}
} |
imu.c dosyası
Code: | #define L3G_DEVICE_AUTO 0
#define L3G4200D_DEVICE 1
#define L3GD20_DEVICE 2
// SA0 states
#define L3G_SA0_LOW 0
#define L3G_SA0_HIGH 1
#define L3G_SA0_AUTO 2
// register addresses
#define L3G_WHO_AM_I 0x0F
#define L3G_CTRL_REG1 0x20
#define L3G_CTRL_REG2 0x21
#define L3G_CTRL_REG3 0x22
#define L3G_CTRL_REG4 0x23
#define L3G_CTRL_REG5 0x24
#define L3G_REFERENCE 0x25
#define L3G_OUT_TEMP 0x26
#define L3G_STATUS_REG 0x27
#define L3G_OUT_X_L 0x28
#define L3G_OUT_X_H 0x29
#define L3G_OUT_Y_L 0x2A
#define L3G_OUT_Y_H 0x2B
#define L3G_OUT_Z_L 0x2C
#define L3G_OUT_Z_H 0x2D
#define L3G_FIFO_CTRL_REG 0x2E
#define L3G_FIFO_SRC_REG 0x2F
#define L3G_INT1_CFG 0x30
#define L3G_INT1_SRC 0x31
#define L3G_INT1_THS_XH 0x32
#define L3G_INT1_THS_XL 0x33
#define L3G_INT1_THS_YH 0x34
#define L3G_INT1_THS_YL 0x35
#define L3G_INT1_THS_ZH 0x36
#define L3G_INT1_THS_ZL 0x37
#define L3G_INT1_DURATION 0x38
#define LSM303DLH_DEVICE 0
#define LSM303DLM_DEVICE 1
#define LSM303DLHC_DEVICE 2
#define LSM303_DEVICE_AUTO 3
// SA0_A states
#define LSM303_SA0_A_LOW 0
#define LSM303_SA0_A_HIGH 1
#define LSM303_SA0_A_AUTO 2
// register addresses
#define LSM303_CTRL_REG1_A 0x20
#define LSM303_CTRL_REG2_A 0x21
#define LSM303_CTRL_REG3_A 0x22
#define LSM303_CTRL_REG4_A 0x23
#define LSM303_CTRL_REG5_A 0x24
#define LSM303_CTRL_REG6_A 0x25 // DLHC only
#define LSM303_HP_FILTER_RESET_A 0x25 // DLH, DLM only
#define LSM303_REFERENCE_A 0x26
#define LSM303_STATUS_REG_A 0x27
#define LSM303_OUT_X_L_A 0x28
#define LSM303_OUT_X_H_A 0x29
#define LSM303_OUT_Y_L_A 0x2A
#define LSM303_OUT_Y_H_A 0x2B
#define LSM303_OUT_Z_L_A 0x2C
#define LSM303_OUT_Z_H_A 0x2D
#define LSM303_FIFO_CTRL_REG_A 0x2E // DLHC only
#define LSM303_FIFO_SRC_REG_A 0x2F // DLHC only
#define LSM303_INT1_CFG_A 0x30
#define LSM303_INT1_SRC_A 0x31
#define LSM303_INT1_THS_A 0x32
#define LSM303_INT1_DURATION_A 0x33
#define LSM303_INT2_CFG_A 0x34
#define LSM303_INT2_SRC_A 0x35
#define LSM303_INT2_THS_A 0x36
#define LSM303_INT2_DURATION_A 0x37
#define LSM303_CLICK_CFG_A 0x38 // DLHC only
#define LSM303_CLICK_SRC_A 0x39 // DLHC only
#define LSM303_CLICK_THS_A 0x3A // DLHC only
#define LSM303_TIME_LIMIT_A 0x3B // DLHC only
#define LSM303_TIME_LATENCY_A 0x3C // DLHC only
#define LSM303_TIME_WINDOW_A 0x3D // DLHC only
#define LSM303_CRA_REG_M 0x00
#define LSM303_CRB_REG_M 0x01
#define LSM303_MR_REG_M 0x02
#define LSM303_OUT_X_H_M 0x03
#define LSM303_OUT_X_L_M 0x04
#define LSM303_OUT_Y_H_M -1 // The addresses of the Y and Z magnetometer output registers
#define LSM303_OUT_Y_L_M -2 // are reversed on the DLM and DLHC relative to the DLH.
#define LSM303_OUT_Z_H_M -3 // These four defines have dummy values so the library can
#define LSM303_OUT_Z_L_M -4 // determine the correct address based on the device type.
#define LSM303_SR_REG_M 0x09
#define LSM303_IRA_REG_M 0x0A
#define LSM303_IRB_REG_M 0x0B
#define LSM303_IRC_REG_M 0x0C
#define LSM303_WHO_AM_I_M 0x0F // DLM only
#define LSM303_TEMP_OUT_H_M 0x31 // DLHC only
#define LSM303_TEMP_OUT_L_M 0x32 // DLHC only
#define LSM303DLH_OUT_Y_H_M 0x05
#define LSM303DLH_OUT_Y_L_M 0x06
#define LSM303DLH_OUT_Z_H_M 0x07
#define LSM303DLH_OUT_Z_L_M 0x08
#define LSM303DLM_OUT_Z_H_M 0x05
#define LSM303DLM_OUT_Z_L_M 0x06
#define LSM303DLM_OUT_Y_H_M 0x07
#define LSM303DLM_OUT_Y_L_M 0x08
#define LSM303DLHC_OUT_Z_H_M 0x05
#define LSM303DLHC_OUT_Z_L_M 0x06
#define LSM303DLHC_OUT_Y_H_M 0x07
#define LSM303DLHC_OUT_Y_L_M 0x08
|
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|