View previous topic :: View next topic |
Author |
Message |
lucasromeiro
Joined: 27 Mar 2010 Posts: 167
|
HMC6352 (compass) in 16f628a |
Posted: Mon Jun 04, 2012 2:10 pm |
|
|
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
|
|
|
lucasromeiro
Joined: 27 Mar 2010 Posts: 167
|
|
Posted: Mon Jun 04, 2012 8:10 pm |
|
|
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
|
|
Posted: Mon Jun 04, 2012 8:37 pm |
|
|
What was the previous PIC that you used ?
Post the #use i2c() statement from your previous program. |
|
|
lucasromeiro
Joined: 27 Mar 2010 Posts: 167
|
my code |
Posted: Mon Jun 04, 2012 11:48 pm |
|
|
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
|
|
Posted: Tue Jun 05, 2012 12:16 am |
|
|
You didn't answer any of the questions that I asked. |
|
|
lucasromeiro
Joined: 27 Mar 2010 Posts: 167
|
sorry for erro. |
Posted: Tue Jun 05, 2012 12:35 pm |
|
|
#use i2c(Master, sda=PIN_a2, scl=PIN_a3, FORCE_HW)
i use pic 18f4550 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jun 05, 2012 1:19 pm |
|
|
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
|
TKS |
Posted: Tue Jun 05, 2012 1:22 pm |
|
|
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
|
|
Posted: Tue Jun 05, 2012 1:28 pm |
|
|
You can use the same code. There should no problems with using
software i2c. |
|
|
lucasromeiro
Joined: 27 Mar 2010 Posts: 167
|
|
Posted: Tue Jun 05, 2012 1:32 pm |
|
|
Thanks for the tips!
Today I'll do the tests! |
|
|
|