View previous topic :: View next topic |
Author |
Message |
autova
Joined: 24 May 2011 Posts: 14
|
hmc6352 help |
Posted: Wed Jun 08, 2011 12:23 pm |
|
|
Can anyone help me when I turn the compass 90 degrees the led will on ???
Seriously i need help here.
This is the coding from the forum. I done doing the compass part but to make led on when 90 degree still not achieved. Please.
Code: |
#include <16F877A.H>
#fuses hs, no protect, nowdt, nolvp
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#use i2c(Master, sda=PIN_C4, scl=PIN_C3, FORCE_HW)
#define use_portb_lcd TRUE
#include <lcd.c>
#byte PORTa=5
#byte PORTb=6
#byte PORTc=7
#byte PORTd=8
#byte PORTe=9
#define SLAVE2_WRT_ADDR 0x14
#define SLAVE2_READ_ADDR 0x15
#define HMC6352_GET_DATA_COMMAND 0x41
#define HMC6352_I2C_WRITE_ADDRESS 0x42
#define HMC6352_I2C_READ_ADDRESS 0x43
#define HMC6352_I2C_UPDATE_ADDRESS 0x4F
void led();
//int16 heading();
// 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((msb*256)+lsb);
}
void led
if
//===================================
void main()
{
int16 heading;
set_tris_d(0x00);
set_tris_b(0x00);
lcd_init();
while(1)
{
heading = HMC6352_read_heading();
lcd_gotoxy(1,1);
lcd_putc(" ");
delay_ms(200);
lcd_gotoxy(1,1);
printf(lcd_putc,"Degrees is %lu\n\r",heading/10);
//turn left
//loop
//dalam loop kena check
//if heading/10 >90//stop motor
delay_ms(500);
}
}
|
What I need to declare and which statement I need to use ?? Teach me |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jun 08, 2011 1:21 pm |
|
|
If you want to turn on an LED if the heading is at least 90 degrees, then
do something like this:
Code: |
while(1)
{
heading = HMC6352_read_heading();
heading = heading /10; // Get heading in degrees from 0 to 359
if(heading >= 90) // Is heading from 90 to 359 ?
output_high(PIN_B0); // If so, turn on LED
else
output_low(PIN_B0); // If not, turn off LED
}
|
|
|
|
autova
Joined: 24 May 2011 Posts: 14
|
|
Posted: Wed Jun 08, 2011 1:33 pm |
|
|
so , i don't need to declare anything else ?
one more thing . are the code effect my lcd output ? |
|
|
autova
Joined: 24 May 2011 Posts: 14
|
|
Posted: Wed Jun 08, 2011 2:14 pm |
|
|
i already test the code but what happen is the led on when i push the on switch .
what actually happen ? i really confuse here :( |
|
|
|