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 support@ccsinfo.com

hmc5883 lsb value

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
m4k



Joined: 27 Mar 2018
Posts: 13

View user's profile Send private message

hmc5883 lsb value
PostPosted: Tue Mar 27, 2018 8:50 am     Reply with quote

hi buddies

i try to run hmc5883 compass sensor (gy86 module) on dsp chip
but the output value for msb is zero...why?

this is my code :

Code:


#include <33FJ256GP506.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES CKSFSM                   //Clock Switching is enabled, fail Safe clock monitor is enabled
#FUSES NOJTAG                   //JTAG disabled

#device ICSP=1
#use delay(crystal=80M)
#use rs232(xmit=PIN_B0, baud=9600)
#INCLUDE <stdlib.h>
#use i2c(MASTER, I2C1, FAST)

#include <mpu.c>
#include <hmc1.c>


#define led1 pin_g13
#define led2 pin_g12
#define led3 pin_g14
#define led4 pin_g0
#define led5 pin_g1
void main()
{
config_mpu();
delay_ms(100);

char string[100];
hmc5883l_write_reg(HMC5883L_CFG_A_REG, 0x70);
hmc5883l_write_reg(HMC5883L_CFG_B_REG, 0xa0);
hmc5883l_write_reg(HMC5883L_MODE_REG, 0x00);

delay_ms(100);
unsigned char lsb = 0;
unsigned char msb = 0;
signed int16 x=0,y=0,z=0;
   while(TRUE)
   {

   
   
i2c_start();
i2c_write(0x3c);
i2c_write(0x03);  // Point to X-msb register
i2c_start();
i2c_write(0x3d);

msb = i2c_read();
lsb = i2c_read();

 
 x=make16(msb,lsb);

msb = i2c_read();
lsb = i2c_read();

 y=make16(msb,lsb);

msb = i2c_read();
lsb = i2c_read(0);

 z=make16(msb,lsb);

i2c_stop();
 
 
 
itoa(x,10, string);
   puts(string);

   output_toggle(led1);
   
     delay_ms(100); 
   }

}






tnx alote
Ttelmah



Joined: 11 Mar 2010
Posts: 13264

View user's profile Send private message

PostPosted: Tue Mar 27, 2018 9:39 am     Reply with quote

You are wasting a lot of power. Leave the chip in single operation mode.
Write 1 to the mode register, and wait for the DRDY pin to go low. Then read the data. Much easier and is how the chip is really assumed to work.
Alternatively just write, and wait for just slightly over 6mSec, then take the reading. The chip will then sleep till you next write to the mode register.

Now then a comment on your C.
Declare variables at the start of code segments or functions. C will accept declarations inside blocks as you have but this is not standard C, and can cause 'oddities'. Better to stick to the standard.

Are you really sure your chip is running right?. Crystal=80M, is not a legal rate for it. The maximum crystal frequency supported is 40MHz. To run above this it has to be running a PLL, which you are not setting up.

Why not just printf the number. Makes it a lot easier to understand what you are doing.

Why should the MSB be anything other than zero?. Chip sitting flat, the only value that will have any significant size, will be Z.
temtronic



Joined: 01 Jul 2010
Posts: 6154
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Mar 27, 2018 9:48 am     Reply with quote

couple of points
1) when posting about a peripheral device, you need to also post a link to the module or discrete device. With that we can download the correct datasheet for it....

2) according to the datasheet I found, the registers are starting from 03 offset..
x-msb
x-lsb
z-msb
z-lsb
y-msb
y-lsb

NOT x,y,x

3)you should have separate variables for the 6 bytes. This allows you to read all 6 bytes THEN combine into words (make() ). I'm confident the 33 series has lots of RAM....

4) When you run PCM P's 'I2C Scanner' program does it come back at base address 0x3c ? Some choose to use 7 bit addresses, others 8 bit. As well does the module have provision for changing the address ? This is why it's important to use the scanner program ! Just because 'someone' says it's address is XXX doesn't MEAN it really is ! Always confirm.

5) I2C devices require pullups on the I2C bus which depend upon VDD, # of devices,board design, bus length. So what are your pullups ?

this should be a start.....
there maybe more things to consider.
Jay
m4k



Joined: 27 Mar 2018
Posts: 13

View user's profile Send private message

PostPosted: Tue Mar 27, 2018 10:13 am     Reply with quote

Ttelmah wrote:

Are you really sure your chip is running right?. Crystal=80M, is not a legal rate for it. The maximum crystal frequency supported is 40MHz. To run above this it has to be running a PLL, which you are not setting up.


yes..it work properly without any problem

Ttelmah wrote:

Why should the MSB be anything other than zero?. Chip sitting flat, the only value that will have any significant size, will be Z.


can you explain more?
m4k



Joined: 27 Mar 2018
Posts: 13

View user's profile Send private message

PostPosted: Tue Mar 27, 2018 10:19 am     Reply with quote

temtronic wrote:
couple of points
1) when posting about a peripheral device, you need to also post a link to the module or discrete device. With that we can download the correct datasheet for it....


I explained above that use gy86 module

temtronic wrote:

3)you should have separate variables for the 6 bytes. This allows you to read all 6 bytes THEN combine into words (make() ). I'm confident the 33 series has lots of RAM....


What is my separation problem?

Code:

msb = i2c_read();
lsb = i2c_read();

 
 x=make16(msb,lsb);

msb = i2c_read();
lsb = i2c_read();

 y=make16(msb,lsb);

msb = i2c_read();
lsb = i2c_read(0);

 z=make16(msb,lsb);


i separate variable and make16 under each i2c_read Wink
temtronic



Joined: 01 Jul 2010
Posts: 6154
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Mar 27, 2018 11:10 am     Reply with quote

instead of this...
Quote:
msb = i2c_read();
lsb = i2c_read();

x=make16(msb,lsb);

msb = i2c_read();
lsb = i2c_read();

y=make16(msb,lsb);

msb = i2c_read();
lsb = i2c_read(0);

z=make16(msb,lsb);

I'd do something like this...
Code:

xmsb = i2c_read();
xlsb = i2c_read();
zmsb = i2c_read();
zlsb = i2c_read();
ymsb = i2c_read();
ylsb = i2c_read(0);
 
 x=make16(xmsb,xlsb);
 y=make16(ymsb,ylsb);

In your code the 2nd set of read is 'y'axis data, yet the datasheet I have says it is the 'Z' axis.
Also doing 'math' or other operations between I2C reads may cause timing problems, normally you read all the data into variables then do 'math'. It may or not affect your program, just something to be aware of.

Sorry, I didn't understand that 'gy86 module' was the gyroscope module.
Ttelmah



Joined: 11 Mar 2010
Posts: 13264

View user's profile Send private message

PostPosted: Tue Mar 27, 2018 11:11 am     Reply with quote

The point Temtronic was making is that a module is not the chip. There may be details of a particular module that need other data than the chip itself, so a direct link to the actual module being used can help.

A g sensor sitting flat will return small values for x and y. Typically x = perhaps 0.00x or -0.00x. Why should the MSB be anything other than 0?.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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