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

HMC6352 (compass) in 16f628a

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



Joined: 27 Mar 2010
Posts: 167

View user's profile Send private message

HMC6352 (compass) in 16f628a
PostPosted: Mon Jun 04, 2012 2:10 pm     Reply with quote

Hello people.

I'm trying to get a digital compass connected to PIC16F628A microcontroller, but I have no success. The PIC 16F628A has no I2C, so I will have to develop a software ... Could someone help me or have something ready to work to read I2C on PIC 16F628A?

compass: http://www.sparkfun.com/products/7915

Thank you all for your help!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 04, 2012 2:46 pm     Reply with quote

CCS has i2c library code you can use. You can certainly do i2c on the
16F628A. This post shows it can work:
http://www.ccsinfo.com/forum/viewtopic.php?t=32570&start=13

This is a 4-page thread which discusses how to make a driver for the
HMC6352, and the various problems that can occur:
http://www.ccsinfo.com/forum/viewtopic.php?t=27179
lucasromeiro



Joined: 27 Mar 2010
Posts: 167

View user's profile Send private message

PostPosted: Mon Jun 04, 2012 8:10 pm     Reply with quote

I understand, thank you for the answer!

Using the first link I use the i2c 16F628A without problems?

on the second link I'll check with aplomb. The doubt is if I use the library for the first item I communicate normally?

because I've communicated with the compass using another pic and it worked normally, the pic had i2c. now I want to run the code in 16F628A, would the same code?

Thank you!!!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 04, 2012 8:37 pm     Reply with quote

What was the previous PIC that you used ?

Post the #use i2c() statement from your previous program.
lucasromeiro



Joined: 27 Mar 2010
Posts: 167

View user's profile Send private message

my code
PostPosted: Mon Jun 04, 2012 11:48 pm     Reply with quote

Code:

#define HMC6352_I2C_WRITE_ADDRESS 0x42
#define HMC6352_I2C_READ_ADDRESS  0x43
#define HMC6352_GET_DATA_COMMAND 0x41

// Read the compass heading.  This is in units
// of 1/10's of a degree, from 0 to 3599.
int16 HMC6352_read_heading(void)
{
int8 lsb;
int8 msb;

i2c_start();
i2c_write(HMC6352_I2C_WRITE_ADDRESS);
i2c_write(HMC6352_GET_DATA_COMMAND);
i2c_stop();

delay_ms(7);  // Allow 1 ms extra for safety

i2c_start();
i2c_write(HMC6352_I2C_READ_ADDRESS);
msb = i2c_read();
lsb = i2c_read(0);

i2c_stop();

return((int16)lsb | ((int16)msb << 8));
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jun 05, 2012 12:16 am     Reply with quote

You didn't answer any of the questions that I asked.
lucasromeiro



Joined: 27 Mar 2010
Posts: 167

View user's profile Send private message

sorry for erro.
PostPosted: Tue Jun 05, 2012 12:35 pm     Reply with quote

#use i2c(Master, sda=PIN_a2, scl=PIN_a3, FORCE_HW)

i use pic 18f4550
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jun 05, 2012 1:19 pm     Reply with quote

Quote:
#use i2c(Master, sda=PIN_a2, scl=PIN_a3, FORCE_HW)

Pins A2 and A3 are not the hardware i2c pins on the 18F4550. (The actual
hardware i2c pins are B0 and B1). This means that the compiler will
ignore the FORCE_HW parameter, and you are in fact doing software i2c
on pins A2 and A3.

So if it works OK on the 18F4550, it will also work OK on the 16F628A.
lucasromeiro



Joined: 27 Mar 2010
Posts: 167

View user's profile Send private message

TKS
PostPosted: Tue Jun 05, 2012 1:22 pm     Reply with quote

understand, so I can use exactly the same code?
amazing!
great to know that! is there any risk?
thank you
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jun 05, 2012 1:28 pm     Reply with quote

You can use the same code. There should no problems with using
software i2c.
lucasromeiro



Joined: 27 Mar 2010
Posts: 167

View user's profile Send private message

PostPosted: Tue Jun 05, 2012 1:32 pm     Reply with quote

Thanks for the tips!
Today I'll do the tests!
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