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

weird readings in a HMC6352

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







weird readings in a HMC6352
PostPosted: Fri Jan 16, 2009 8:18 am     Reply with quote

hi!
I bought a digital compass, the hmc6352, it works fine.
I changed the operational mode, and then i try to read it from the EEPROM and from the RAM, but i get weird numbers like 12, 3, 4,
(I expect 82=0x52);


Code:

#include <16F873A.h>
#fuses HS, NOPROTECT, NOWRT, NOLVP
#use delay(clock=20000000)
#use i2c(Master, sda=PIN_C4, scl=PIN_C3)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, parity=N, bits=8)

void changemode(){

int raam;

i2c_start();
i2c_write(0x42);
i2c_write(0x47);//write to RAM
delay_us(80),
i2c_write(0x74);//RAM address
delay_us(80);
i2c_write(0x52);//continuous operational mode (0b01010010)
i2c_stop();
delay_us(80);

i2c_start();
i2c_write(0x42);
i2c_write(0x67);//read from RAM
delay_us(80);
i2c_write(0x74);//RAM address
i2c_stop();
delay_us(80);

i2c_start();
i2c_write(0x43);
raam=i2c_read();
i2c_stop();

printf(" RAM: %u\n\r", raam);

i2c_start();
i2c_write(0x42);
i2c_write(0x4C); //save mode
i2c_stop();
delay_us(130);

}

void readmode(){

int ee;
i2c_start();
i2c_write(0x42);
i2c_write(0x72); //read from EEPROM
delay_us(80);
i2c_write(0x08);//EEPROM address
i2c_stop();
delay_us(80);

i2c_start();
i2c_write(0x43);
ee=i2c_read();
i2c_stop();

printf(" EEPROM: %u\n\r", ee);
}

void main(){

  setup_adc_ports (NO_ANALOGS);
  setup_adc (ADC_OFF);
  setup_spi (SPI_SS_DISABLED);
  setup_timer_0 (RTCC_INTERNAL | RTCC_DIV_1);
  setup_timer_1 (T1_INTERNAL | T1_DIV_BY_8);
  setup_timer_2 (T2_DISABLED, 0, 1);
  setup_comparator (NC_NC_NC_NC);
  setup_vref (FALSE);

changemode(); //first I change the mode and then i read it
while(true){
   readmode();
   delay_ms(1000);
   }

}


thanks!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jan 16, 2009 12:49 pm     Reply with quote

I didn't look at your code too closely, but I did notice that you're not
giving a NACK on the last read operation. You need to do that.
Add a 0 parameter to i2c_read() statement as shown below:
Quote:
i2c_start();
i2c_write(0x43);
raam=i2c_read(0);
i2c_stop();


There are two places in your code (in total) where this needs to be done.
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