Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

HMC 5843 interfacing with PIC 16F877A

Status
Not open for further replies.

shanishp

Newbie level 6
Joined
Sep 11, 2007
Messages
13
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,422
Hi.
I have interfaced Honeywells HMC 5843 compass with PIC 16F877A in single supply mode,The problem is that LCD display connected to the same is showing heading angle as 360 and 180 degree, no further angle is obtaining,please help me any one having more experience with this IC.
I am attaching my c program with this
/****************************************/
#include<pic.h>
#include<math.h>
#include"877_i2c.h"
#include"delay.h"
#include"lcd.h"

__CONFIG(HS & WDTDIS & LVPDIS & PWRTEN & PROTECT);

#define one_eighty_by_pi 57.29577
#define PORTBIT(adr, bit) ((unsigned)(&adr)*8+(bit))
//static bit @ PORTBIT(PORTB, 1);
void write_HMC5843(unsigned char,unsigned char);
void read_HMC5843(unsigned char);
void write_HMC5843_new(void);

unsigned int heading;
signed int mag_x,mag_y,mag_z;
double angle;
unsigned char lcd_val,stat,vl;
void main()
{
TRISA=0;
TRISB=0;
TRISC=0;
TRISD=0;
TRISE=0;
TRISC3=0;
TRISC4=1;
ADCON1=0X07;
PORTA=0;
PORTB=0;
PORTC=0;
PORTD=0;
PORTE=0;
heading =0;
i2c_init();
lcd_init();
lcd_clear();
lcd_goto(0);
lcd_puts(" HMC 5843 ");
lcd_goto(0x40);
lcd_puts("Heading : "); //4B
DelayMs(150);
//write_HMC5843_new();
write_HMC5843(0,0x10); // Configuration register A : set default value
write_HMC5843(1,0x20); // Configuration register B : set default value
write_HMC5843(2,0x00); // Mode Selection : continous mode selected
DelayMs(950);


while(1)
{
read_HMC5843(0x03);
angle=(double)(mag_x/mag_y);
if((mag_y<0) && (mag_x>0))
heading=(unsigned int)(atan(angle));
else if((mag_y<0) && (mag_x<0))
heading=(unsigned int)(360-atan(angle));
else if((mag_y>0) && (mag_x<0))
heading=(unsigned int)(180+atan(angle));
else if((mag_y>0) && (mag_x>0))
heading=(unsigned int)(180-atan(angle));

lcd_goto(0x4B);
lcd_val=(heading/100)+48; // 128 1
lcd_putch(lcd_val);

lcd_val=heading%100; // 28
lcd_val=(lcd_val/10)+48; // 2
lcd_putch(lcd_val);

lcd_val=heading%100; //28
lcd_val=(lcd_val%10)+48; // 8
lcd_putch(lcd_val);
// lcd_puts(" ");
// i2c_start();
// i2c_write(0x3C);
// i2c_write(0x09);
// i2c_repStart();
// i2c_write(0x3D);
//
// stat=i2c_read(0);
// i2c_stop();
// if(stat & 0x01)
// lcd_puts("1");
// else
// lcd_puts("0");
// lcd_puts(" ");
// if(stat & 0x02)
// lcd_puts("1");
// else
// lcd_puts("0");
//
DelayMs(950);

}


}

//-------------------------------------------------------------
void write_HMC5843(unsigned char address,unsigned char data)
{
i2c_start();
i2c_write(0x3C);
i2c_write(address);
i2c_write(data);
i2c_stop();
DelayMs(15);
}
//-------------------------------------------------------------
void read_HMC5843(unsigned char addr)
{
i2c_start();
i2c_write(0x3C);
i2c_write(addr);
i2c_repStart();
i2c_write(0x3D);

mag_x=i2c_read(1);
mag_x=(mag_x<<8)+i2c_read(1);
//heading=mag_x;
//lcd_goto(0x10);
//lcd_val=(heading/100)+48;
//lcd_putch(lcd_val);
//
//lcd_val=heading%100;
//lcd_val=(lcd_val/10)+48;
//lcd_putch(lcd_val);
//lcd_val=heading%100;
//lcd_val=(lcd_val%10)+48;
//lcd_putch(lcd_val);
//lcd_puts(" ");

mag_y=i2c_read(1);
mag_y=(mag_x<<8)+i2c_read(1);
//heading=mag_y;
//lcd_val=(heading/100)+48;
//lcd_putch(lcd_val);
//
//lcd_val=heading%100;
//lcd_val=(lcd_val/10)+48;
//lcd_putch(lcd_val);
//lcd_val=heading%100;
//lcd_val=(lcd_val%10)+48;
//lcd_putch(lcd_val);
//lcd_puts(" ");

mag_z=i2c_read(1);
mag_z=(mag_x<<8)+i2c_read(0);
//heading=mag_z;
//lcd_val=(heading/100)+48;
//lcd_putch(lcd_val);
//
//lcd_val=heading%100;
//lcd_val=(lcd_val/10)+48;
//lcd_putch(lcd_val);
//lcd_val=heading%100;
//lcd_val=(lcd_val%10)+48;
//lcd_putch(lcd_val);
//lcd_puts(" ");

i2c_stop();

}
//--------------------------
void write_HMC5843_new(void)
{
i2c_start();
i2c_write(0x3C);
i2c_write(0x02);
i2c_write(0x00);
i2c_stop();
DelayMs(15);
}
 

Hi.
My name is Mustafa;
firstly I'm sorry for my english, it's bad :)
so, I have problem
I have interfaced Honeywells HMC 5843 compass with PIC 16F877A in single supply mode,bt not works. I think you have made.I don't made this project so can you help me.Can you give works the source code.

my mail mstfozkara@gmail.com
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top