cmps03.h File Reference

#include <i2c.h>

Go to the source code of this file.

Defines

#define CMPS03_CALIBRATION_PIN   PIN_A1
 SmartITs standard pin used for calibrating the cmps03.
#define CMPS03_OVERFLOW   20000
 overflow at value computation
#define CMPS03_PWM_PIN   PIN_A0
 SmartITs standard pin used for reading the cmps03 pulse width modulation (pwm) values.

Functions

long get_angle ()
float get_angle_float ()
long get_angle_pwm ()
BYTE get_angle_short ()
BYTE get_sw_version ()
void init_i2c_compass ()
void pin_calibration ()
long read_bearing ()
BYTE read_reg1 (BYTE register_number)
long read_reg2 (BYTE register_number)
void write_calibration_register ()


Define Documentation

#define CMPS03_CALIBRATION_PIN   PIN_A1
 

SmartITs standard pin used for calibrating the cmps03.

Definition at line 42 of file cmps03.h.

Referenced by pin_calibration().

#define CMPS03_OVERFLOW   20000
 

overflow at value computation

Definition at line 46 of file cmps03.h.

#define CMPS03_PWM_PIN   PIN_A0
 

SmartITs standard pin used for reading the cmps03 pulse width modulation (pwm) values.

Definition at line 37 of file cmps03.h.

Referenced by get_angle_pwm().


Function Documentation

long get_angle  ) 
 

Returns compass angle (compass module cmps03 register 1) as long.

Returns:
compass module cmps03 register 2 and 3 as one long.

Definition at line 246 of file cmps03.h.

References init_i2c_compass(), and read_reg2().

Referenced by get_angle_float(), and read_bearing().

00246                  {
00247         long ret;
00248         init_i2c_compass();
00249         ret = read_reg2(2);
00250         return (ret);
00251 }

float get_angle_float  ) 
 

Returns compass angle (compass module cmps03 register 1) as float. This method internally uses get_angle.

Returns:
compass module cmps03 register 2 and 3 as one float.

Definition at line 258 of file cmps03.h.

References get_angle().

00258                         {
00259         float ret;
00260         long temp_angle;
00261         temp_angle=get_angle();
00262         ret = ((float)(temp_angle))*0.1;
00263         return (ret);
00264 }

long get_angle_pwm  ) 
 

Measures the direction using pulse width modulation (pwm).

Returns:
a counter, a large value means a large angle between north and current heading, 0 means north.

Definition at line 86 of file cmps03.h.

References CMPS03_PWM_PIN.

00086                      {
00087         long counter;
00088         counter=0;
00089         // if it's high, wait for a low 
00090         while(input(CMPS03_PWM_PIN) && (counter<CMPS03_OVERFLOW)) {++counter;}
00091         // account for fall time
00092         delay_us(1);              
00093         counter=0;
00094         // wait for signal go high
00095         while(!input(CMPS03_PWM_PIN) && (counter<CMPS03_OVERFLOW)) {++counter;}
00096         counter=0;
00097         // wait for signal to go low 
00098         while(input(CMPS03_PWM_PIN) && (counter<CMPS03_OVERFLOW)) {counter++;}
00099         return counter;
00100 }

BYTE get_angle_short  ) 
 

Returns compass angle (compass module cmps03 register 1) as BYTE.

Returns:
compass module cmps03 register 1 as BYTE.

Definition at line 235 of file cmps03.h.

References init_i2c_compass(), and read_reg1().

00235                        {
00236         BYTE ret;
00237         init_i2c_compass();
00238         ret = read_reg1(1);
00239         return (ret);
00240 }

BYTE get_sw_version  ) 
 

Reads software version of copass module cmps03.

Returns:
software version of compass module cmps03.

Definition at line 224 of file cmps03.h.

References init_i2c_compass(), and read_reg1().

00224                       {
00225         BYTE ret;
00226         init_i2c_compass();
00227         ret = read_reg1(0);
00228         return (ret);
00229 }

void init_i2c_compass  ) 
 

Initializes the i2c compass module cmps03.

Definition at line 105 of file cmps03.h.

References I2C_SCL, and I2C_SDA.

Referenced by get_angle(), get_angle_short(), and get_sw_version().

00106 {
00107         output_float(I2C_SCL);
00108   output_float(I2C_SDA);
00109  // output_high(CMPS03_CALIBRATION_PIN);
00110 }

void pin_calibration  ) 
 

Method used for calibration. Align compass to {north|south|east|west} and call the pin_calibration method. Do this for each direction.

make sure the pin state is well defined.

to calbrate, drive pin low and release it immediately.

Definition at line 137 of file cmps03.h.

References CMPS03_CALIBRATION_PIN.

00138 {
00140         output_high(CMPS03_CALIBRATION_PIN);
00141         delay_ms(10);
00143         output_low(CMPS03_CALIBRATION_PIN);
00144         delay_ms(10);
00145         output_high(CMPS03_CALIBRATION_PIN);
00146 }

long read_bearing  ) 
 

Function to read bearing.

Returns:
bearing

Definition at line 72 of file cmps03.h.

References get_angle().

00073 {
00074         long angle;
00075         angle = get_angle(); // a value between 0...3599 -> 0 ... 359.9
00076         angle = angle / 10; // divide and lose the 1/10
00077         //bearing = angle;
00078         return angle;
00079 }

unsigned int read_reg1 BYTE  register_number  ) 
 

Reads one byte from register regno.

Parameters:
register_number register to be read.
Returns:
byte in register register_number

standard i2c bus start sequence.

make a read request to address 0xC0 to signal a read from the following register. see i2c protocoll specification (cmps03 handbook).

write the register of interest (which is to be read).

standard i2c bus stop sequence.

standard i2c bus start sequence.

make a write request to address 0xC1 to signal a write to the following register. see i2c protocoll specification (cmps03 handbook).

read the register of interest (which was written before). a read with param 1 means "ack", a read with param 0 means no ack.

standard i2c bus stop sequence.

Definition at line 153 of file cmps03.h.

Referenced by get_angle_short(), and get_sw_version().

00154 {        
00155         unsigned int angle;
00156   BYTE datal;
00158   i2c_start();
00162         i2c_write(0xC0);
00164         i2c_write(register_number); // register 2 and 3
00166         i2c_stop();
00167         delay_ms(10);
00169         i2c_start();
00173         i2c_write(0xC1);
00176         datal=i2c_read(0); //no ack
00178         i2c_stop();
00179         angle = (unsigned int) datal;
00180         return(angle);
00181 }

long read_reg2 BYTE  register_number  ) 
 

Reads two byte from register regno.

Parameters:
register_number register to be read.
Returns:
long (2 bytes), starting in register register_number

standard i2c bus start sequence.

make a read request to address 0xC0 to signal a read from the following register. see i2c protocoll specification (cmps03 handbook).

write the register of interest (which is to be read).

standard i2c bus stop sequence.

standard i2c bus start sequence.

make a write request to address 0xC1 to signal a write to the following register. see i2c protocoll specification (cmps03 handbook).

read the register of interest (which was written before). a read with param 1 means "ack", a read with param 0 means no ack.

standard i2c bus stop sequence.

Definition at line 188 of file cmps03.h.

Referenced by get_angle().

00189 {        
00190         long angle;
00191         BYTE datah,datal;
00192 
00194         i2c_start();
00198         i2c_write(0xC0);
00200         i2c_write(register_number); // register 2 and 3
00202         i2c_stop();
00203         delay_ms(10);
00205         i2c_start();
00209         i2c_write(0xC1);
00212         datah=i2c_read(1); // ack
00213         datal=i2c_read(0); // no ack
00215         i2c_stop();
00216         angle = make16(datah, datal);
00217         return(angle);
00218 }

void write_calibration_register  ) 
 

Used to do calibration, once for north, east, west and south. The sequence is arbitrary.

standard i2c bus start sequence.

make a write request to address 0xC1 to signal a write to the following register. see i2c protocoll specification (cmps03 handbook).

write the register of interest, this time register 15.

write the calibration value (255)

standard i2c bus stop sequence.

Definition at line 116 of file cmps03.h.

00117 {
00119         i2c_start();
00123         i2c_write(0xC1);
00125         i2c_write(0x0F);
00127         i2c_write(0xFF);
00129         i2c_stop();
00130         delay_ms(11);
00131 }


Generated on Tue Nov 30 09:16:48 2004 for Smart-Its Headers by  doxygen 1.3.9.1