示例#1
0
/*
 * Read the current sensor reading and store to our current color bin.
 * An integration time is assumed (not included in this routine) between 
 * reading.
 */
void readRGB()
{
  uint8_t data[8];

  if (!i2c_master_select(COLOR_SENSOR_ADDR, TW_WRITE))
    goto end;
  TWDR = REG_BLOCK_READ;
  if (i2c_master_transmit_with_ack() != TW_MT_DATA_ACK)
    goto end;

  _delay_ms(10);

  if (i2c_master_start() != TW_REP_START)
    goto end;
  TWDR = (COLOR_SENSOR_ADDR << 1) | TW_READ;
  if (i2c_master_transmit() != TW_MR_SLA_ACK)
    goto end;
  if (i2c_master_transmit_with_ack() != TW_MR_DATA_ACK)
    goto end;
  data[0] = TWDR; //DATA1LOW
  if (i2c_master_transmit_with_ack() != TW_MR_DATA_ACK)
    goto end;
  data[1] = TWDR; //DATA1HIGH
  if (i2c_master_transmit_with_ack() != TW_MR_DATA_ACK)
    goto end;
  data[2] = TWDR; //DATA2LOW
  if (i2c_master_transmit_with_ack() != TW_MR_DATA_ACK)
    goto end;
  data[3] = TWDR; //DATA2HIGH
  if (i2c_master_transmit_with_ack() != TW_MR_DATA_ACK)
    goto end;
  data[4] = TWDR; //DATA3LOW
  if (i2c_master_transmit_with_ack() != TW_MR_DATA_ACK)
    goto end;
  data[5] = TWDR; //DATA3HIGH
 if (i2c_master_transmit_with_ack() != TW_MR_DATA_ACK)
    goto end;
  data[6] = TWDR; //DATA4LOW
  if (i2c_master_transmit() != TW_MR_DATA_NACK)
    goto end;
  data[7] = TWDR; //DATA4HIGH

	green=data[1]*256+data[0];
	red=data[3]*256+data[2];
	blue=data[5]*256+data[4];
	clr=data[7]*256+data[6];

    #ifdef DEBUG_I2C
        //debug_printf("I2C: i2c_tcs3414cs: 0x%X%X 0x%X%X 0x%X%X 0x%X%X\n",data[7],data[6],data[5],data[4],data[3],data[2],data[1],data[0]);
        debug_printf("I2C: i2c_tcs3414cs: red %.0d green %.0d blue %.0d clear %.0d\n",red,green,blue,clr);
    #endif	
end:
  i2c_master_stop();

#ifdef I2C_TCS3414CS_CALC_SUPPORT
  TSCalc(); // Berechnen
#endif

}
示例#2
0
int16_t
i2c_bh1750_read(const uint8_t chipaddress)
{
  uint8_t data[2];
  uint16_t ret = 0xffff;

  /*select slave in write mode */
  if (!i2c_master_select(chipaddress, TW_WRITE))
    goto end;
  /*send the dataaddress */
  TWDR = chipmode;
  if (i2c_master_transmit_with_ack() != TW_MT_DATA_ACK)
    goto end;

  _delay_ms(10);		// for slow devices

  /* Do an repeated start condition */
  if (i2c_master_start() != TW_REP_START)
    goto end;
  /*select the slave in read mode */
  TWDR = (chipaddress << 1) | TW_READ;
  if (i2c_master_transmit() != TW_MR_SLA_ACK)
    goto end;


  _delay_ms(250);                // wait for conversion


  /*get the first byte from the slave */
  if (i2c_master_transmit_with_ack() != TW_MR_DATA_ACK)
    goto end;
  data[0] = TWDR;

  DEBUGGI2C("read_word_data", "data0: 0x%X (%d)\n", data[0], data[0]);


  /*get the second byte from the slave */
  if (i2c_master_transmit() != TW_MR_DATA_NACK)
    goto end;
  data[1] = TWDR;

  DEBUGGI2C("read_word_data", "data1: 0x%X (%d)\n", data[1], data[1]);

  ret = (data[0] << 8 | data[1]) / 1.2;

end:
  i2c_master_stop();

  DEBUGGI2C("read_word_data", "ret: 0x%X (%d)\n", ret, ret);

  return ret;
}
示例#3
0
uint8_t i2c_24aaXX_read_block(uint8_t addr, uint8_t *ptr, uint8_t len) {
     
     uint8_t ret = 0;
     
     if (!i2c_master_select( I2C_SLA_24AAXX, TW_WRITE)) { ret=1; goto end; }
     
     TWDR = addr;
     if (i2c_master_transmit_with_ack() != TW_MT_DATA_ACK) { ret=2; goto end; }

     /* Do an repeated start condition */
     if (i2c_master_start() != TW_REP_START) {ret = 3; goto end; }

     /* Send the address again */
     TWDR = (I2C_SLA_24AAXX << 1) | TW_READ;
     if(i2c_master_transmit() != TW_MR_SLA_ACK) {ret = 4; goto end; }

     for (uint8_t i=0;i<len;i++) {
	  if (i2c_master_transmit_with_ack() != TW_MR_DATA_ACK) { ret = 5; goto end; }
	  *(ptr+i) = TWDR;
     }
     
 end:
     i2c_master_stop();
     return ret;

}
示例#4
0
uint8_t i2c_ds13x7_get_block(uint8_t addr, char *data, uint8_t len) {
     uint8_t ret = 0;

     uint8_t sreg = SREG; cli();
     
     if (!i2c_master_select( I2C_SLA_DS13X7, TW_WRITE)) { ret=1; goto end; }
     
     TWDR = addr;
     if (i2c_master_transmit_with_ack() != TW_MT_DATA_ACK) { ret=2; goto end; }

     /* Do an repeated start condition */
     if (i2c_master_start() != TW_REP_START) {ret = 3; goto end; }

     /* Send the address again */
     TWDR = (I2C_SLA_DS13X7 << 1) | TW_READ;
     if(i2c_master_transmit() != TW_MR_SLA_ACK) {ret = 4; goto end; }

     for (uint8_t i=0;i<len;i++) {
	  if (i2c_master_transmit_with_ack() != TW_MR_DATA_ACK) { ret = 5; goto end; }
	  *(data+i) = TWDR;
     }
     
     
 end:
     i2c_master_stop();
     SREG = sreg; 
     return ret;
}
示例#5
0
/*
LED4to7	7	6	6	4
LED0to3	3	2	1	0
bits	7,6	5,4	3,2	1,0

bits
00 = Output is set Hi-Z (LED off - default)
01 = Output is set low (LED on)
10 = Output blinks at PWM0 rate
11 = Output blinks at PWM1 rate
*/
uint8_t
i2c_pca9531_set(uint8_t address, 
	uint8_t prescaler0, uint8_t pwm0duty,
	uint8_t prescaler1, uint8_t pwm1duty,
	uint8_t led0to3, uint8_t led4to7)
{

  uint8_t ret;
  uint8_t tmp;

  i2c_master_select(address, TW_WRITE);

  TWDR = 0x11;
  tmp = i2c_master_transmit_with_ack();

  if (tmp != TW_MT_DATA_ACK) { ret = 0; goto end; }
  TWDR = prescaler0;
  tmp = i2c_master_transmit_with_ack();
  if (tmp != TW_MT_DATA_ACK) { ret = 0; goto end; }

  TWDR = pwm0duty;
  tmp = i2c_master_transmit_with_ack();
  if (tmp != TW_MT_DATA_ACK) { ret = 0; goto end; }

  TWDR = prescaler1;
  tmp = i2c_master_transmit_with_ack();
  if (tmp != TW_MT_DATA_ACK) { ret = 0; goto end; }

  TWDR = pwm1duty;
  tmp = i2c_master_transmit_with_ack();
  if (tmp != TW_MT_DATA_ACK) { ret = 0; goto end; }

  TWDR = led0to3;
  tmp = i2c_master_transmit_with_ack();
  if (tmp != TW_MT_DATA_ACK) { ret = 0; goto end; }

  TWDR = led4to7;
  tmp = i2c_master_transmit();
  if (tmp != TW_MT_DATA_ACK) { ret = 0; goto end; }

  ret = 0;
end:
  i2c_master_stop();
  return ret;
}
示例#6
0
void i2c_udp_newdata(void)
{
	
  struct i2c_t *REQ = uip_appdata;
		/*
  * ueberschreiben der connection info. 
  * port und adresse auf den remotehost begrenzen
  * und antwort paket senden mit der maximalen pufferlaenge (i2c open)
                */
  uint8_t resetconnection = 0;
  uint8_t error = 0;

  if(STATSI2C.last_seqnum == 0){
    uip_ipaddr_copy(uip_udp_conn->ripaddr, BUF->srcipaddr);
    uip_udp_conn->rport = BUF->srcport;
  }

  if(uip_datalen() == GETMAXDATA && REQ->seqnum == 0)
  {
    uint16_t mdpl = MAXDATAPAKETLEN;
    REQ->maxdatalen = mdpl;
    uip_slen = 2;
    resetconnection = 1;
  }
  else if(uip_datalen() >= READFROMI2C) //  && 
  {
    if(STATSI2C.last_seqnum == 0)
    {
      /* sende startcondition und adresse wenn kein paket vorher da war (last_seqnum = 0) */
      uint8_t mode = REQ->i2c_addr_rw & 0x01;
      uint8_t addr = (REQ->i2c_addr_rw & 0xfe) >> 1;
      if (! i2c_master_select(addr, mode)) 
        error = 1;
    }
    if(!resetconnection){
      if((REQ->i2c_addr_rw & 0x01) == TW_READ)
      {
        uint8_t tmp_datalen = REQ->datalen;
        uint8_t tmp_datapos = 0;
        uint16_t mdpl = MAXDATAPAKETLEN;
        if (tmp_datalen > mdpl)
          tmp_datalen = mdpl;
        
        while (tmp_datapos < tmp_datalen)
        {
          if (tmp_datapos == (tmp_datalen - 1) && REQ->seqnum == 0) {
            /* letztest Byte, wir erwarten ein NACK */
            if (i2c_master_transmit() != TW_MR_DATA_NACK)  {
              error = 1;
              break;
            }
          } else {
            if (i2c_master_transmit_with_ack() != TW_MR_DATA_ACK) {
              error = 1;
              break;
            }
          }
          REQ->readdata[tmp_datapos++] = TWDR;
        }
        if(REQ->seqnum == 0) {
          i2c_master_stop();
          resetconnection = 1;
        }
        uip_slen = tmp_datalen + 1;
      } else { /* TW_WRITE */
        if(STATSI2C.last_seqnum == 0 || REQ->seqnum != STATSI2C.last_seqnum)
        {
          uint8_t tmp_datapos = 0;
  
          while (tmp_datapos < uip_datalen() - 2) {
            TWDR = REQ->writedata[tmp_datapos++];
            /* fehler protokollieren */
            if (i2c_master_transmit() != TW_MT_DATA_ACK) {
              break;
            }
          }
          if(REQ->seqnum == 0){
            i2c_master_stop();
            resetconnection = 1;
          }
          REQ->write_datalen_ack = tmp_datapos;
        }
        else
          REQ->write_datalen_ack = uip_datalen() - 2;
        uip_slen = 2;
      }
    }
  }