Ejemplo n.º 1
0
unsigned char lib_i2c_readreg(unsigned char address,unsigned char reg) {
    lib_i2c_start_wait((address<<1)+I2C_WRITE);
    lib_i2c_write(reg);
    lib_i2c_rep_start((address<<1)+I2C_READ);

    unsigned char returnvalue = lib_i2c_readnak();

    lib_i2c_stop();
   
    return(returnvalue);
}
Ejemplo n.º 2
0
void lib_i2c_readdata(unsigned char address,unsigned char reg,unsigned char *data,unsigned char length) {
    lib_i2c_start_wait((address<<1)+I2C_WRITE);
    lib_i2c_write(reg);
    lib_i2c_rep_start((address<<1)+I2C_READ);

    while (--length) {
        *data++ = lib_i2c_readack();
    }
    *data = lib_i2c_readnak();

    lib_i2c_stop();
}
Ejemplo n.º 3
0
char readgps()
   { // returns 1 if new data was read
   if (lib_timers_gettimermicroseconds(gpstimer)<50000) return(0); // 20 hz

   gpstimer=lib_timers_starttimer();
   
   unsigned char shiftedaddress=I2C_GPS_ADDRESS<<1;
   lib_i2c_start_wait(shiftedaddress+I2C_WRITE);
   if (lib_i2c_write(I2C_GPS_STATUS_00))
      return(0); 

   lib_i2c_rep_start(shiftedaddress+I2C_READ);

   unsigned char gps_status= lib_i2c_readnak();
 
   global.gps_num_satelites = (gps_status & 0xf0) >> 4;
   if (gps_status & I2C_GPS_STATUS_3DFIX) 
      { //Check is we have a good 3d fix (numsats>5)
      long longvalue;
      unsigned char *ptr=(unsigned char *)&longvalue;
      
      lib_i2c_rep_start(shiftedaddress+I2C_WRITE);
      lib_i2c_write(I2C_GPS_LOCATION); 
      lib_i2c_rep_start(shiftedaddress+I2C_READ);

      *ptr++ = lib_i2c_readack();
      *ptr++ = lib_i2c_readack();
      *ptr++ = lib_i2c_readack();
      *ptr = lib_i2c_readack();

      global.gps_current_latitude=lib_fp_multiply(longvalue,27488L); // convert from 10,000,000 m to fixedpointnum
      
      ptr=(unsigned char *)&longvalue;
      
      *ptr++ = lib_i2c_readack();
      *ptr++ = lib_i2c_readack();
      *ptr++ = lib_i2c_readack();
      *ptr = lib_i2c_readnak();
      
      global.gps_current_longitude=lib_fp_multiply(longvalue,27488L); // convert from 10,000,000 m to fixedpointnum

      int intvalue;
      ptr= (unsigned char *)&intvalue;
      
      lib_i2c_rep_start(shiftedaddress+I2C_WRITE);
      lib_i2c_write(I2C_GPS_GROUND_SPEED); 
      lib_i2c_rep_start(shiftedaddress+I2C_READ);

      *ptr++ = lib_i2c_readack();
      *ptr++ = lib_i2c_readack();
      
      global.gps_current_speed=intvalue*665L; // convert fro cm/s to fixedpointnum to m/s
      
      ptr= (unsigned char *)&intvalue;
      *ptr++ = lib_i2c_readack();
      *ptr = lib_i2c_readnak();

      global.gps_current_altitude=((fixedpointnum)intvalue)<<FIXEDPOINTSHIFT;

      lib_i2c_stop();
      return(1);
      }
   lib_i2c_stop();

   return(0);
   }