uint16_t readAdcChannel(uint8_t lowByteAddress, uint8_t highByteAddress) { received_data[0] = 0; received_data[1] = 0; i2c_start(LIGHTSENSOR_I2C, SLAVE_ADDRESS << 1, I2C_Direction_Transmitter); // start a transmission in Master transmitter mode i2c_write(LIGHTSENSOR_I2C, lowByteAddress); i2c_stop(LIGHTSENSOR_I2C); // stop the transmission i2c_start(LIGHTSENSOR_I2C, SLAVE_ADDRESS << 1, I2C_Direction_Receiver); // start a transmission in Master receiver mode received_data[0] = i2c_read_nack(LIGHTSENSOR_I2C); // read data0 low byte i2c_start(LIGHTSENSOR_I2C, SLAVE_ADDRESS << 1, I2C_Direction_Transmitter); // start a transmission in Master transmitter mode i2c_write(LIGHTSENSOR_I2C, highByteAddress); i2c_stop(LIGHTSENSOR_I2C); // stop the transmission for (uint16_t i = 0; i < 65535; i++) { } i2c_start(LIGHTSENSOR_I2C, SLAVE_ADDRESS << 1, I2C_Direction_Receiver); // start a transmission in Master receiver mode received_data[1] = i2c_read_nack(LIGHTSENSOR_I2C); // read data0 high byte data = (uint16_t) received_data[0]; data |= (uint16_t) (received_data[1] << 8); return data; }
i2c_status_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout) { i2c_status_t status = i2c_start(devaddr, timeout); if (status) return status; status = i2c_write(regaddr, timeout); if (status) return status; status = i2c_start(devaddr | 0x01, timeout); if (status) return status; for (uint16_t i = 0; i < (length-1); i++) { status = i2c_read_ack(timeout); if (status >= 0) { data[i] = status; } else { return status; } } status = i2c_read_nack(timeout); if (status >= 0 ) { data[(length-1)] = status; } else { return status; } status = i2c_stop(timeout); if (status) return status; return I2C_STATUS_SUCCESS; }
void collect_data_mpu(void) { i2c_start(MPU_6050_WRITE_ADDRESS); i2c_write(MPU_6050_REGISTER_ACCEL_XOUT_H); i2c_rep_start(MPU_6050_READ_ADDRESS); i2c_read_ack(&accelXH); i2c_read_ack(&accelXL); i2c_read_ack(&accelYH); i2c_read_ack(&accelYL); i2c_read_ack(&accelZH); i2c_read_ack(&accelZL); i2c_read_ack(&temperatureH); i2c_read_ack(&temperatureL); i2c_read_ack(&gyroXH); i2c_read_ack(&gyroXL); i2c_read_ack(&gyroYH); i2c_read_ack(&gyroYL); i2c_read_ack(&gyroZH); i2c_read_nack(&gyroZL); i2c_stop(); accelX = (accelXH << 8) | accelXL; accelY = (accelYH << 8) | accelYL; accelZ = (accelZH << 8) | accelZL; temperature = (temperatureH << 8) | temperatureL; gyroX = (gyroXH << 8) | gyroXL; gyroY = (gyroYH << 8) | gyroYL; gyroZ = (gyroZH << 8) | gyroZL; }
i2c_status_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout) { i2c_status_t status = i2c_start(address | I2C_READ, timeout); if (status) return status; for (uint16_t i = 0; i < (length-1); i++) { status = i2c_read_ack(timeout); if (status >= 0) { data[i] = status; } else { return status; } } status = i2c_read_nack(timeout); if (status >= 0 ) { data[(length-1)] = status; } else { return status; } status = i2c_stop(timeout); if (status) return status; return I2C_STATUS_SUCCESS; }
static matrix_row_t read_cols(uint8_t row) { if (row < 8) { if (mcp23018_status) { // if there was an error return 0; } else { uint8_t data = 0; mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (mcp23018_status) goto out; mcp23018_status = i2c_write(GPIOB, I2C_TIMEOUT); if (mcp23018_status) goto out; mcp23018_status = i2c_start(I2C_ADDR_READ, I2C_TIMEOUT); if (mcp23018_status) goto out; data = i2c_read_nack(I2C_TIMEOUT); if (mcp23018_status < 0) goto out; data = ~((uint8_t)mcp23018_status); mcp23018_status = I2C_STATUS_SUCCESS; out: i2c_stop(); return data; } } else { // read from teensy return (PINF&(1<<0) ? 0 : (1<<0)) | (PINF&(1<<1) ? 0 : (1<<1)) | (PINF&(1<<4) ? 0 : (1<<2)) | (PINF&(1<<5) ? 0 : (1<<3)) | (PINF&(1<<6) ? 0 : (1<<4)) | (PINF&(1<<7) ? 0 : (1<<5)) ; } }
uint8_t mux_get(void) { uint8_t retval; i2c_start(MUX_READ); retval = i2c_read_nack(); i2c_stop(); return retval - 0x08; }
/* * Init function for the light sensor. the sensor has to be activated by 0x3 (see p. 14) * As integration time 402ms will be used (p.15) */ void initLightSensorI2C(void) { /* ENABLE DEVICE to set the configuration */ i2c_start(LIGHTSENSOR_I2C, SLAVE_ADDRESS << 1, I2C_Direction_Transmitter); // start a transmission in Master transmitter mode i2c_write(LIGHTSENSOR_I2C, 0xA0); // 10010000 - // Command Register (0x00) CommandBit (0x80), Word protocol i2c_write(LIGHTSENSOR_I2C, 0x03); // Power up the device (0x3) i2c_stop(LIGHTSENSOR_I2C); // stop the transmission i2c_start(LIGHTSENSOR_I2C, SLAVE_ADDRESS << 1, I2C_Direction_Receiver); // start a transmission in Master transmitter mode received_data[0] = i2c_read_ack(LIGHTSENSOR_I2C); // 0x03 returns if the controller is active received_data[1] = i2c_read_nack(LIGHTSENSOR_I2C); // read one byte and request another byte }
/** * This function reads a byte from the slave and his sub component * @param baseAddress - The baseaddres of the slave to be adressed * @param subAddress - The address of the corresponding component you want to read from. * @return - Returns the byte read from the component */ uint8_t i2c_read8(uint8_t baseAddress, uint8_t subAddress) { i2c_start(baseAddress, TW_WRITE); i2c_write(subAddress); i2c_start(baseAddress,TW_READ); uint8_t result = i2c_read_nack(); i2c_stop(); return result; }
uint8_t i2c_receive(uint8_t address, uint8_t *data, uint16_t length) { if (i2c_start((uint8_t) (address | I2C_READ))) return 1; for (uint16_t i = 0; i < (length - 1); i++) { data[i] = i2c_read_ack(); } data[(length - 1)] = i2c_read_nack(); i2c_stop(); return 0; }
uint16_t read16(uint8_t address) { uint8_t err, pec; uint16_t ret = 0; i2c_start_wait(MLX90614_I2CADDR << 1); err = i2c_write(address); // send register address to read from if (err > 0) { return 0; } err = i2c_start((MLX90614_I2CADDR << 1) + I2C_READ); if (err > 0) { return 0; } ret = i2c_read_ack(); ret |= i2c_read_ack() << 8; pec = i2c_read_nack(); i2c_stop(); uint8_t commArray[5] = {MLX90614_I2CADDR << 1, address, (MLX90614_I2CADDR << 1) + I2C_READ, ret & 0xFF, (ret >> 8) & 0xFF }; uint8_t crc = crc8_ccitt(commArray, 5); if (pec != crc) { // PEC error return 0; } return ret; } int32_t readTemp(uint8_t reg) { uint16_t reading = read16(reg); if (reading == 0) { return -1; } int32_t temp = ((int32_t) reading) * 100; temp /= 50; temp -= 27315; return temp; }
lc_err_t read_byte(e2p_addr_t address, unsigned char *byte) { register lc_err_t err_code; do { do { i2c_start(); err_code = i2c_read_state_wait(); if (err_code != I2C_MASTER_START && err_code != I2C_MASTER_REPSTART) break; i2c_slaW(0xa0); err_code = i2c_read_state_wait(); } while (err_code == I2C_MASTER_SLAW_NACK); if (err_code != I2C_MASTER_SLAW_ACK) break; i2c_write(address >> 8); err_code = i2c_read_state_wait(); if (err_code != I2C_MASTER_TXDATA_ACK) break; i2c_write(address); err_code = i2c_read_state_wait(); if (err_code != I2C_MASTER_TXDATA_ACK) break; i2c_start(); err_code = i2c_read_state_wait(); if (err_code != I2C_MASTER_REPSTART) break; i2c_slaR(0xa0); err_code = i2c_read_state_wait(); if (err_code != I2C_MASTER_SLAR_ACK) break; i2c_read_nack(); err_code = i2c_read_state_wait(); if (err_code != I2C_MASTER_RXDATA_NACK) break; *byte = i2c_get_byte(); err_code = 0; } while (0); i2c_stop(); return err_code; }
uint8_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t *data, uint16_t length) { if (i2c_start(devaddr)) return 1; i2c_write(regaddr); if (i2c_start((uint8_t) (devaddr | 0x01))) return 1; for (uint16_t i = 0; i < (length - 1); i++) { data[i] = i2c_read_ack(); } data[(length - 1)] = i2c_read_nack(); i2c_stop(); return 0; }
void sensor_get(uint8_t channel, uint16_t* values) { uint8_t i; mux_select(channel); i2c_start(SENSOR_READ); for(i = 0; i < 4; i++) { if (i < 3) { values[i] = i2c_read_ack(); values[i] |= (i2c_read_ack() << 8); } else if (i == 3) { values[i] = i2c_read_ack(); values[i] = (i2c_read_nack() << 8); } } i2c_stop(); }
int16_t mpu_read_accel_z(){ uint16_t accel_z; int16_t az; i2c_start(address,write); i2c_write(accel_z_H); i2c_start(address,read); accel_z = ((uint8_t)i2c_read_ack())<<8; accel_z |= i2c_read_nack(); i2c_stop(); az = zweierkompliment(accel_z); return az; }
int16_t mpu_read_accel_y(){ uint16_t accel_y; int16_t ay; i2c_start(address,write); i2c_write(accel_y_H); i2c_start(address,read); accel_y = ((uint8_t)i2c_read_ack())<<8; accel_y |= i2c_read_nack(); i2c_stop(); ay = zweierkompliment(accel_y); return ay; }
int16_t mpu_read_accel_x(){ uint16_t accel_x; int16_t ax; i2c_start(address,write); i2c_write(accel_x_H); i2c_start(address,read); accel_x = ((uint8_t)i2c_read_ack())<<8; accel_x |= i2c_read_nack(); i2c_stop(); ax = zweierkompliment(accel_x); return ax; }
void cell_adcReadings(void) { uint8_t c = cellAddress_MIN; //read adc results and store them to adcReadings array for (uint8_t x = 0; x < cellNumber; x++) { //Set i2c address pointer to adc readings memory i2c_start(c+I2C_WRITE); i2c_write(cellMemory_adc); i2c_stop(); //Start reading adc information and write to array i2c_start(c+I2C_READ); adcReadings[x] = i2c_read_ack(); adcReadings[x] |= (i2c_read_nack()<<8); i2c_stop(); c+=2; } }
int16_t mpu_read_gyro_y(){ uint16_t gyro_y; int16_t gy; i2c_start(address,write); i2c_write(gyro_y_H); i2c_start(address,read); gyro_y = ((uint8_t)i2c_read_ack())<<8; gyro_y |= i2c_read_nack(); i2c_stop(); gy = zweierkompliment(gyro_y); return gy; }
int16_t mpu_read_gyro_z(){ uint16_t gyro_z; int16_t gz; i2c_start(address,write); i2c_write(gyro_z_H); i2c_start(address,read); gyro_z = ((uint8_t)i2c_read_ack())<<8; gyro_z |= i2c_read_nack(); i2c_stop(); gz = zweierkompliment(gyro_z); return gz; }
uint8_t left_read(uint8_t reg, uint8_t *data) { if (!i2c_initialized) { return 0; } uint8_t ret = 0; ret = i2c_start(I2C_ADDR_WRITE, HOTDOX_I2C_TIMEOUT); if (ret) goto out; ret = i2c_write(reg, HOTDOX_I2C_TIMEOUT); if (ret) goto out; ret = i2c_start(I2C_ADDR_READ, HOTDOX_I2C_TIMEOUT); if (ret) goto out; *data = i2c_read_nack(HOTDOX_I2C_TIMEOUT); out: i2c_stop(); return ret; }
float mpu__read_temp(){ uint16_t temp; float temp_c; i2c_start(address,write); i2c_write(temp_H); i2c_start(address,read); temp = ((uint8_t)i2c_read_ack())<<8; temp |= i2c_read_nack(); i2c_stop(); temp_c = temp / 340 + 36.53; return temp_c; }
//read the low and and the high byte of the gyro in the mcu int16_t mpu_read_gyro_x(){ uint16_t gyro_x; int16_t gx; i2c_start(address,write); i2c_write(gyro_x_H); i2c_start(address,read); gyro_x = ((uint8_t)i2c_read_ack())<<8; gyro_x |= i2c_read_nack(); i2c_stop(); gx = zweierkompliment(gyro_x); return gx; }
uint8_t ds3231_read(uint8_t addr, uint8_t* data) { uint8_t ecode; do { i2c_start(); ecode = i2c_read_state_wait(); if ((ecode != I2C_MASTER_START) && (ecode != I2C_MASTER_REPSTART)) break; i2c_slaW(_DS3231_ADDR); ecode = i2c_read_state_wait(); if (ecode != I2C_MASTER_SLAW_ACK) break; i2c_write(addr); ecode = i2c_read_state_wait(); if (ecode != I2C_MASTER_TXDATA_ACK) break; i2c_start(); ecode = i2c_read_state_wait(); if (ecode != I2C_MASTER_REPSTART) break; i2c_slaR(_DS3231_ADDR); ecode = i2c_read_state_wait(); if (ecode != I2C_MASTER_SLAR_ACK) break; i2c_read_nack(); ecode = i2c_read_state_wait(); if (ecode != I2C_MASTER_RXDATA_NACK) break; *data = i2c_get_byte(); ecode = 0; } while(0); i2c_stop(); return ecode; }
int main(void) { //I2C設定 i2c_init(); _delay_ms(15); i2c_start(HMC5883L_WRITE); i2c_write(HMC5883L_CONFIG_A); i2c_write(0x78); // 平均化8回 75Hz 通常測定 i2c_stop(); i2c_start(HMC5883L_WRITE); i2c_write(HMC5883L_CONFIG_B); i2c_write(HMC5883L_GAIN); i2c_stop(); i2c_start(HMC5883L_WRITE); i2c_write(HMC5883L_MODE); // set pointer to measurement mode i2c_write(0x00); // continous measurement i2c_stop(); //入出力設定 DDRB =0b00010000; //SPI IR入力 DDRD =0b00001100; //トリガー IR電源 IR入力 PORTD =0b00001000; //IR電源OFF //超音波関連 //割り込み許可 PCICR =0b00000010; PCMSK1 =0b00001111; //タイマー TCCR0A = 0b00000000; TCCR0B = 0b00000101; //トリガー用タイマー初期化 TCCR1A = 0b00000000; TCCR1B = 0b00000100; //SPI設定 SPCR=0b11000000; SPDR=0; sei(); _delay_ms(15); while (1) { int x_raw,y_raw; unsigned long ir[6]={0,0,0,0,0,0}; float arg; //方位データ読み出し PORTD = 0b00000000;//IR電源ON i2c_start(HMC5883L_WRITE); i2c_write(0x03); i2c_stop(); i2c_start(HMC5883L_READ); data[X_H]=i2c_read_ack(); data[X_L]=i2c_read_ack(); data[Y_H]=i2c_read_ack(); data[Y_L]=i2c_read_ack(); data[Z_H]=i2c_read_ack(); data[Z_L]=i2c_read_nack(); x_raw=(data[X_H]<<8)+data[X_L]; y_raw=(data[Z_H]<<8)+data[Z_L]; x_raw= x_raw+X_OFFSET; y_raw= y_raw+Y_OFFSET; x_raw*= HMC5883L_M; y_raw*= HMC5883L_M; arg = atan2((double)x_raw,(double)y_raw); arg = arg/M_PI; data[ARG]=(char)(arg*(128)); i2c_stop(); for(int i=0;i<IR_CYCLE;i++){ char d = PIND^0xFF; char b = PINB^0xFF; if(d & (1<<PIND4))ir[0]++; if(d & (1<<PIND5))ir[1]++; if(d & (1<<PIND6))ir[2]++; if(d & (1<<PIND7))ir[3]++; if(b & (1<<PINB0))ir[4]++; if(b & (1<<PINB1))ir[5]++; _delay_us(15); } PORTD = 0b00001000;//IR電源OFF data[IR0]=ir[0]; data[IR1]=ir[1]; data[IR2]=ir[2]; data[IR3]=ir[3]; data[IR4]=ir[4]; data[IR5]=ir[5]; int t =TCNT1L;//←よくわからないが入れると動く if(TCNT1H > 10 ){ //測距 PORTD =0b00000100; _delay_us(1000); PORTD =0b00000000; TCNT1H =0; TCNT1L =0; TCNT0 =0; } _delay_ms(15); } }