CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

L3G4200D
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
hamid9543



Joined: 31 Jan 2013
Posts: 63

View user's profile Send private message

L3G4200D
PostPosted: Sun Mar 03, 2013 4:57 am     Reply with quote

hi to all member
I read below post carefully
https://www.ccsinfo.com/forum/viewtopic.php?t=48415&highlight=l3g42%2A
https://www.ccsinfo.com/forum/viewtopic.php?t=49712&highlight=l3g42%2A
https://www.ccsinfo.com/forum/viewtopic.php?t=46798&highlight=l3g42%2A
and write new code
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 correctly.
Ttelmah



Joined: 11 Mar 2010
Posts: 19506

View user's profile Send private message

PostPosted: Sun Mar 03, 2013 5:43 am     Reply with quote

On a multi byte I2C read the last transfer should use:

xM=i2c_read(0);

This does not acknowledge the last byte, and tells the device that the transaction is complete.
PCM_programmer points this out in one of the threads you claim to have read....

Then 'of course' it is fast. The I2C transactions, and display, probably total 1mSec, then you delay 50mSec, so it will be repeating nearly 20* per second. This is faster than the LCD can update, so all you will see is a blur....

Then the value will fluctuate a little. Expect a few counts even when still.

You are enabling the data ready output, but never use this.

Best Wishes
hamid9543



Joined: 31 Jan 2013
Posts: 63

View user's profile Send private message

L3G4200D
PostPosted: Sun Mar 03, 2013 1:14 pm     Reply with quote

i will make changes that you suppose and i report the result
would you explain more about
Quote:
You are enabling the data ready output, but never use this.

Thx
Ttelmah



Joined: 11 Mar 2010
Posts: 19506

View user's profile Send private message

PostPosted: Sun Mar 03, 2013 1:46 pm     Reply with quote

Read the data sheet, and look what the patterns you send to initialise the chip do. One enables the data ready output on the chip, allowing this to be connected to a processor pin, and synchronise the transactions, but you then don't use this.

Best Wishes
hamid9543



Joined: 31 Jan 2013
Posts: 63

View user's profile Send private message

update code
PostPosted: Sun Mar 03, 2013 11:38 pm     Reply with quote

update code
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.

and can you explain about:
Quote:

Read the data sheet, and look what the patterns you send to initialise the chip do. One enables the data ready output on the chip, allowing this to be connected to a processor pin, and synchronise the transactions, but you then don't use this.

would you specify the problem in this code
best regard
hamid9543



Joined: 31 Jan 2013
Posts: 63

View user's profile Send private message

what problem!
PostPosted: Fri Jul 05, 2013 2:53 am     Reply with quote

i write code
who am i is correct !
slave address code is correct!
but code not working properly and output is not valid
x=514 ,x=53456 ,x=2827,x=48830 ,.......
output change in 1sec and not valid!!!
please help
Code:

#Include <16f873a.h>
#fuses NOWDT,HS
#use delay(xtal=20Mhz)
#use i2c(Master,sda=PIN_c4,scl=PIN_c3)

// Define Reg L3G4200d*************
#define gyro_ADD_R 0xD3
#define gyro_ADD_W 0xD2
#define X_LSB 0x28
#define X_MSB 0x29
#define Y_LSB 0x2A
#define Y_MSB 0x2B
#define Z_LSB 0x2C
#define Z_MSB 0x2D
#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23
#define CTRL_REG5 0x24
#define FIFO_CTRL_REG 0x2E

//Define LCD ******************
#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_c5                                   
#define LCD_DATA7       PIN_c6
#include <LCD.c>



int16 value=0;
int8 M=0,L=0;

void writeRegister(int8 reg,int8 config)
 {
    i2c_start();
    i2c_write(gyro_ADD_W);     
    i2c_write(reg);       
    i2c_write(config);           
    i2c_stop();             
 }
 
int16 readregister(int8 Reg)
 {
    i2c_start();
    i2c_write(gyro_ADD_W);             
    i2c_write(Reg);
    i2c_start(); 
    i2c_write(gyro_ADD_R);
    L=i2c_read();
    M=i2c_read(0);
    value=make16(M,L);
    i2c_stop();
    return value;
 }

void init_gyro()
{
writeRegister(CTRL_REG1,0b10101111);
writeRegister(CTRL_REG2,0b00000000);
writeRegister(CTRL_REG3,0b00001000);
writeRegister(CTRL_REG4,0b00000000);     
writeRegister(CTRL_REG5,0b00000000);
writeRegister(FIFO_CTRL_REG,0b00000000);
}

void main()
{
init_gyro();
lcd_init();
lcd_gotoxy(1,1);
printf(lcd_putc,"starting up L3G4200D");
delay_ms(1000);
 

while(true)
{
    readregister(X_LSB);
    printf(lcd_putc"\f");
    lcd_gotoxy(1,2); 
    printf(lcd_putc,"x=%Lu     ",value);
    delay_ms(1000);
}

}

help help help please


Last edited by hamid9543 on Sat Jul 06, 2013 7:07 am; edited 2 times in total
oxo



Joined: 13 Nov 2012
Posts: 219
Location: France

View user's profile Send private message

PostPosted: Fri Jul 05, 2013 4:10 am     Reply with quote

Read the data sheet!

Table 14, 15 and 16. p22-23
hamid9543



Joined: 31 Jan 2013
Posts: 63

View user's profile Send private message

update code
PostPosted: Fri Jul 05, 2013 7:58 am     Reply with quote

update code, but output is not valid and change same before!!!!!
oxo



Joined: 13 Nov 2012
Posts: 219
Location: France

View user's profile Send private message

PostPosted: Fri Jul 05, 2013 8:41 am     Reply with quote

That code there ^^ has obvious errors which you will find by reading the data, and doing it the way they describe.
hamid9543



Joined: 31 Jan 2013
Posts: 63

View user's profile Send private message

no find!!!!!!!!
PostPosted: Fri Jul 05, 2013 9:03 am     Reply with quote

thx for reply oxo
Would you specify the problem in this code please.
I read code but can not find error.
if code have error why "who am i" code and "slave address" working very good?
thanx for your help
dyeatman



Joined: 06 Sep 2003
Posts: 1933
Location: Norman, OK

View user's profile Send private message

PostPosted: Fri Jul 05, 2013 9:31 am     Reply with quote

One other problem that has not been mentioned, you have the order of the
first few lines wrong. Here is how it should be
Code:
#Include <16f873a.h>
#fuses NOWDT,HS
#use delay(xtal=20Mhz)
#use i2c(Master,sda=PIN_c4,scl=PIN_c3)


The processor definition should always be first followed by the Fuses then
use delay then use RS232 and/or use I2c etc.
_________________
Google and Forum Search are some of your best tools!!!!
hamid9543



Joined: 31 Jan 2013
Posts: 63

View user's profile Send private message

l3g4200d
PostPosted: Fri Jul 05, 2013 9:40 am     Reply with quote

thanx but your answer can not solve my problem.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jul 05, 2013 1:14 pm     Reply with quote

Quote:

int16 readregister(int8 Reg)
{
i2c_start();
i2c_write(gyro_ADD_W);
i2c_write(Reg);
i2c_start();
i2c_write(gyro_ADD_R);
L=i2c_read();
M=i2c_read(0);
value=make16(M,L);
i2c_stop();
return value;
}


main()
{


while(true)
{
readregister(X_LSB);
printf(lcd_putc"\f");
lcd_gotoxy(1,2);
printf(lcd_putc,"x=%Lu ",value);
delay_ms(1000);
}

You claim that you read this thread:
https://www.ccsinfo.com/forum/viewtopic.php?t=48415&start=5
But you missed my comments near the end of the post about the
return value from the function. You are making the same mistake
as the original poster in that thread. Get a book on the C language
and read about how a function returns a value, and how to use it
when you call the function, and look at my example code in the link.
hamid9543



Joined: 31 Jan 2013
Posts: 63

View user's profile Send private message

without function
PostPosted: Fri Jul 05, 2013 11:03 pm     Reply with quote

me seems my code is correct and function have no error.
but write new code without function:
Code:

while(true)
{
    i2c_start();
    i2c_write(0xD2);             
    i2c_write(0x28);
    i2c_start(); 
    i2c_write(0xD3);
    L=i2c_read();
    M=i2c_read(0);
    value=make16(M,L);
    i2c_stop();
    printf(lcd_putc"\f");
    lcd_gotoxy(1,2); 
    printf(lcd_putc,"x=%Lu     ",value);
    delay_ms(1000);
}


output is:(do not move sensor!!)
x=257 x=63222 x=51914 x=55512 and......
hamid9543



Joined: 31 Jan 2013
Posts: 63

View user's profile Send private message

try again!!!
PostPosted: Sun Jul 07, 2013 7:01 am     Reply with quote

I replace 16f873a with 18f452 and bought new IMU(GY 80). Sad
But the result not valid and Has the same output changes.
When the sensor is standing still, the output should be zero and when we move the x-axis of the sensor's output value must change. But when the sensor is fixed, the output is changed as follows:
x=50372 x=50629 x=47031..................
However, these changes are random and with reconnect and connect power the other output is generated randomly.


please help I'm very confused Crying or Very sad
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
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