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

iis2mdc + pic16f887

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



Joined: 17 Apr 2020
Posts: 1
Location: Mexico

View user's profile Send private message MSN Messenger

iis2mdc + pic16f887
PostPosted: Fri Apr 17, 2020 6:34 pm     Reply with quote

Hi guys, I have this sensor IIS2MDC located on a evaluation board called STEVAL-MKI185V1. This board (slave) is linked in I2C to a PIC16F887 (master).

Code:
 
#include <16F887.h>
#device ADC=10
#FUSES  WDT, NOMCLR, NOBROWNOUT, PROTECT, CPD, PUT, NOLVP
#use delay(internal=8MHz)

#BYTE PORTA = 5
#BYTE PORTB = 6
#BYTE PORTC = 7

#define MAGN_SDA  PIN_B5
#define MAGN_SCL  PIN_B6
#use rs232(baud=9600,parity=N,xmit=PIN_C6, rcv=PIN_C7,bits=8)
#use i2c(MASTER, sda=MAGN_SDA, scl=MAGN_SCL)


#define OFFSET_X_REG_L 0x45
#define OFFSET_X_REG_H 0x46
#define OFFSET_Y_REG_L 0x47
#define OFFSET_Y_REG_H 0x48
#define OFFSET_Z_REG_L 0x49
#define OFFSET_Z_REG_H 0x4A
#define CFG_REG_A 0x60
#define CFG_REG_B 0x61
#define CFG_REG_C 0x62
#define STATUS_REG 0x67
#define X_axis_register_OUTX_L_REG 0x68

#define MagnetAddress_read 0x3D
#define MagnetAddress_write 0x3C

void iis2mdc_write_reg(int8 reg, int8 data)
{
i2c_start();
i2c_write(MagnetAddress_write);
i2c_write(reg);
i2c_write(data);
i2c_stop();
}

int8 iis2mdc_read_reg(int8 reg)
{
int8 retval;

i2c_start();
i2c_write(MagnetAddress_write);
i2c_write(reg);
i2c_start();
i2c_write(MagnetAddress_read);
retval = i2c_read(0);
i2c_stop();

return(retval);
}

typedef struct
{
int16 x;
int16 y;
int16 z;
}iis2mdc_result;
iis2mdc_result compass = {0,0,0};


void iis2mdc_read_data(void)
{
int8 x_lsb;
int8 x_msb;

int8 y_lsb;
int8 y_msb;

int8 z_lsb;
int8 z_msb;

i2c_start();
i2c_write(MagnetAddress_write);
i2c_write(X_axis_register_OUTX_L_REG);  // Point to X-msb register
i2c_start();
i2c_write(MagnetAddress_read);

x_lsb = i2c_read();
x_msb = i2c_read();

y_lsb = i2c_read();
y_msb = i2c_read();

z_lsb = i2c_read();
z_msb = i2c_read(0);   // do a NACK on last read

i2c_stop();
 
// Combine high and low bytes into 16-bit values.
compass.x = make16(x_msb<<8, x_lsb);
compass.y = make16(y_msb<<8, y_lsb);
compass.z = make16(z_msb<<8, z_lsb);
}

int result;
int regA;
int regB;
int regC;

void main()
{
output_float(PIN_B4);
delay_ms(100);  // A power-on init delay may be needed

printf("\n\rStart:\n\r");

// Init the HMC5883L.  Set Mode register for
// continuous measurements.
iis2mdc_write_reg(CFG_REG_A, 0x00);
iis2mdc_write_reg(CFG_REG_B, 0x01);
iis2mdc_write_reg(CFG_REG_C, 0x01);
regA = iis2mdc_read_reg(CFG_REG_A);
regB = iis2mdc_read_reg(CFG_REG_B);
regC = iis2mdc_read_reg(CFG_REG_C);
printf("%u", regA);
printf("%u", regB);
printf("%u", regC);

// Continuously read and display the x,y,z results.
// Wait at least 67 ms between reads, re the HMC5883L data sheet.

while(1)
  {

   result = iis2mdc_read_reg(STATUS_REG);
   printf("%u", result);
   if (result == 0b00001000){
   delay_ms(100);
   iis2mdc_read_data();   
   printf("%lu, %lu, %lu \n\r", compass.x, compass.y, compass.z);
  } else {}
  }
  delay_ms(670);
}



I can initialize the sensor and I get some values with the code above but these values are random and they are not what I´m expecting. I put the sensor to work with an arduino and library wire and it shows the expected values so it definitely works. I´ve been looking around the topics in this forum and I was avaible to create the code above but I think I´m missing something with the configuration registers because I´m not avaible to make it work properly. In page 14 of the AN5080 app note for this sensor https://www.st.com/resource/en/application_note/dm00434449-iis2mdc-highaccuracy-ultralowpower-3axis-digital-output-magnetometer-stmicroelectronics.pdf there is a startup sequence I ran it but I have the same issue. I believe my issue is with the config registers and/or the reading sequence . I also attach the datasheet https://www.st.com/resource/en/datasheet/iis2mdc.pdf. Do you have any idea if I'm missing something with the reading sequence? Any kind of help is greatly appreciated since I'm running out of ideas. And to let you know. Both the sensor and pic are running off 3.3 V. The I2C has two 10k pull-up resistors. Thanks in advance.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Apr 17, 2020 11:17 pm     Reply with quote

This is not how make16() is used:
Quote:

compass.x = make16(x_msb<<8, x_lsb);
compass.y = make16(y_msb<<8, y_lsb);
compass.z = make16(z_msb<<8, z_lsb);

make16() does the "shift" for you. That's the purpose of it.
With the '<<8' shifts in there, it sets the msb's to 0x00.
Remove all the '<<8' shifts shown in bold above.

Another thing I don't like about your code is this:
Quote:

#define MAGN_SDA PIN_B5
#define MAGN_SCL PIN_B6

I don't like to use debugger/ICSP programmer pins for other purposes.
Look at the Pickit 3 poster:
http://ww1.microchip.com/downloads/en/devicedoc/50002010b.pdf
Look on the right side. It's got 4.7K pull-downs on the programming pins.
And you're trying to use one of those pins (B6) for i2c. You'd have to
disconnect the Pickit 3 every time you tried to run the board, or it would
interfere with the i2c.

Quote:

#use rs232(baud=9600,parity=N,xmit=PIN_C6, rcv=PIN_C7,bits=8)

This line can be better written like this:
Code:
#use rs232(baud=9600, UART1, ERRORS)



Quote:
#FUSES WDT, NOMCLR, NOBROWNOUT, PROTECT, CPD, PUT, NOLVP

Also, you're using WDT but have no calls to restart_wdt(). Don't use
WDT or Code Protection during development. It's not necessary.
And without the restart calls, you will get WDT resets. You don't want this.
Remove it.

Quote:

Both the sensor and pic are running off 3.3 V. The I2C has two 10k pull-up resistors.

Pullups should be 3.3K (not 10K) in this case.
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