View previous topic :: View next topic |
Author |
Message |
Gerhard
Joined: 30 Aug 2007 Posts: 144 Location: South Africa
|
HMC6352 16 reading averaging |
Posted: Sun Oct 12, 2008 8:53 am |
|
|
Hi.
I am still using a HMC6352 compass and I want to remove some of the jitter by using the function in the chip to use the average of the past 16 values of the magnetometers.
On page 8 of the data sheet the following shows how to.
Quote: | Measurement Summing
This EEPROM summed measurement byte permits designers/users to back average or data smooth the output data
(heading, magnetometer values) to reduce the amount of jitter in the data presentation. The default value is 04(hex) which
is four measurements summed. A value of 00(hex) would be no summing. Up to 16 sets of magnetometer data may be
selected for averaging. This slave address is written into EEPROM address 06(hex) and loaded to RAM on the power up. |
However in the I2C protocol I have no idea where to place the 0x16 value to find the averaged readings.
I have gone through the data sheet several times but seem to struggle to find the exact order of the protocol.
At the moment I am doing it as follows:
Code: | int16 HMC6352_read_heading(void)
{
int8 lsb;
int8 msb;
i2c_start();
i2c_write(HMC6352_I2C_WRITE_ADDRESS); //0x42
i2c_write(HMC6352_GET_DATA_COMMAND); //0x41
i2c_stop();
delay_ms(7);
i2c_start();
i2c_write(HMC6352_I2C_READ_ADDRESS); //0x43
msb = i2c_read();
lsb = i2c_read(0);
i2c_stop();
return((int16)lsb | ((int16)msb << 8));
} |
Can someone please advise how the protocol falls into place in this chip. |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Sun Oct 12, 2008 9:53 am |
|
|
According to the data sheet:
1. I2c_write 0x42 // i2c_write(HMC6352_I2C_WRITE_ADDRESS)
2. I2c_write 0x77 // "w" command
3. I2c_write 0x06 // EEPROM Address
4. I2c_write 0x16 // number of summed readings
The writes are done in sequence, one after the other.
The chip should ACK each write command.
Send a start, the four commands then a stop
Last edited by dyeatman on Sun Oct 12, 2008 9:57 am; edited 2 times in total |
|
|
Gerhard
Joined: 30 Aug 2007 Posts: 144 Location: South Africa
|
|
Posted: Sun Oct 12, 2008 9:54 am |
|
|
I tried doing it as follows but something is amiss because it does not work.
Code: | int16 HMC6352_read_heading(void)
{
int8 lsb;
int8 msb;
i2c_start();
i2c_write(HMC6352_I2C_WRITE_ADDRESS);
i2c_write(0x77);//write to eeprom
delay_us(80);
i2c_write(0x06);//write to eeprom adress 0x06
i2c_write(0x16);//enable 16 value averageing
i2c_stop();
i2c_start();
i2c_write(HMC6352_I2C_WRITE_ADDRESS);//0x42
i2c_write(HMC6352_GET_DATA_COMMAND); //0x41
i2c_stop();
delay_ms(7);
i2c_start();
i2c_write(HMC6352_I2C_READ_ADDRESS);//0x43
msb = i2c_read();
lsb = i2c_read(0);
i2c_stop();
return((int16)lsb | ((int16)msb << 8));
} |
|
|
|
Gerhard
Joined: 30 Aug 2007 Posts: 144 Location: South Africa
|
|
Posted: Sun Oct 12, 2008 9:56 am |
|
|
Thanks dyeatman.
Should it be in sequence with one i2c_start at beginning of each comand and stop at each comand or one start comand, 3 data comands and then 1 stop comand. |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Sun Oct 12, 2008 9:58 am |
|
|
I replied to your question by editing my original response and adding one command I omitted... sorry.. |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Sun Oct 12, 2008 10:02 am |
|
|
The data sheet does say you need 70usec afte the "w' command before sending the 0x06. It seems to imply another delay needed after the 0x06 as well. I would add it just in case.... |
|
|
Gerhard
Joined: 30 Aug 2007 Posts: 144 Location: South Africa
|
|
Posted: Sun Oct 12, 2008 10:14 am |
|
|
Just to ensure that I am understanding correctly. Is this what you suggest?
Code: | i2c_start();
i2c_write(HMC6352_I2C_WRITE_ADDRESS);
i2c_write(0x77);//write to eeprom
delay_us(80);
i2c_write(0x16);
delay_us(80);
i2c_write(0x16);//enable 16 value averageing
i2c_stop(); |
Thanks for the help. |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Sun Oct 12, 2008 10:19 am |
|
|
Actually:
Code: | i2c_start();
i2c_write(HMC6352_I2C_WRITE_ADDRESS);
i2c_write(0x77);//write to eeprom
delay_us(80);
i2c_write(0x06);
delay_us(80);
i2c_write(0x16);//enable 16 value averageing
i2c_stop();
|
You also have to power the compass unit off and back on for this to take effect. |
|
|
Ttelmah Guest
|
|
Posted: Sun Oct 12, 2008 12:25 pm |
|
|
I'd also think that the number used to set the average, would need to be 0x10 (16), not 0x16 (22). Since 16 is the maximum allowed, 22, would probably not work at all.
Best Wishes |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Sun Oct 12, 2008 12:29 pm |
|
|
Thanks Ttelmah
I had intended to make it decimal for clarity...but I got carried away with the hex. That's what I get for doing several things at once while also trying to pay attention to the forum! |
|
|
Ttelmah Guest
|
|
Posted: Mon Oct 13, 2008 2:10 am |
|
|
Know the feeling all too well....
Best Wishes |
|
|
_Gerhard Guest
|
|
Posted: Mon Oct 13, 2008 6:00 am |
|
|
Thanks dyeatman and Ttelmah. I got it working with your help.
Good Day Gerhard |
|
|
|