unsigned char Write24C64_Byte(unsigned char devAddr, unsigned int regAddr, unsigned char value) { TWI_Start(); TWI_Write(_WRITE_MODE(devAddr)); TWI_Write(regAddr>>8); TWI_Write(regAddr); TWI_Write(value); TWI_Stop(); }
void TWI_WRITEBYTE(unsigned char address, unsigned char data) { TWI_Start(); TWI_Write(MPU6050_ADR); TWI_Write(address); TWI_Write(data); TWI_Stop(); }
void imu_init(){ TWI_Write(MPU6050_RA_PWR_MGMT_1, MPU6050_CLOCK_PLL_XGYRO); // Clock selection PLL X_GYRO temp disables TWI_Write(MPU6050_RA_GYRO_CONFIG, MPU6050_GYRO_FS_500); // Gyro Scale +/- 500 º/s TWI_Write(MPU6050_RA_ACCEL_CONFIG, MPU6050_ACCEL_FS_16); // Accel Scale at +/- 16 G TWI_Write(MPU6050_RA_CONFIG, MPU6050_DLPF_BW_256); // Config DLPF Low Pass Filter IMU Off TWI_Write(MPU6050_RA_SMPLRT_DIV, MPU6050_CLOCK_DIV_308); // Sample rate divider at 2 KHz if DLPF OFF else 250 Hz TWI_WriteBit(MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_TEMP_DIS_BIT, True); // Disable sleep mode for wake up IMU TWI_WriteBit(MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_SLEEP_BIT, False); // Disable sleep mode for wake up IMU }
int TWI_READBYTE(unsigned char address) { signed int data = 0x00; TWI_Start(); TWI_Write(MPU6050_ADR); TWI_Write(address); TWI_Start(); TWI_Write(MPU6050_ADR | 0x01); data = TWI_Read_NACK(); TWI_Stop(); return data; }
unsigned char Read24C64_Byte(unsigned char devAddr, unsigned int regAddr) { unsigned char val; TWI_Start(); TWI_Write(0b10100000);//_WRITE_MODE(devAddr)); TWI_Write(regAddr>>8); // MSB Byte first TWI_Write(regAddr); // LSB Byte afterward TWI_Start(); // Repeated start TWI_Write(0b10100001);//_READ_MODE(devAddr)); val = TWI_Read(0u); TWI_Stop(); return val; }
tStatus TWI_RegisterWrite(uint8_t u8addr, uint8_t u8data, uint8_t slaveAddress) { TWI_Start(); // send a start code to begin the write uint8_t status = TWI_GetStatus(); if (status != START && status != REP_START) // start not sent/acknowledged { //Serial.print("TWI Write failed, start not sent."); //Serial.print(" Status code is: 0x"); Serial.println(status,HEX); return ERROR; } TWI_Write( (slaveAddress<<1) ); // write the address shifted so that last bit is 0, meaning write request status = TWI_GetStatus(); if (status != MT_SLA_ACK) // SLA+W was not acknowledged { TWI_Stop(); // send a stop to end failed transmission //Serial.print("TWI Write failed, sla address not sent."); //Serial.print(" Status code is: 0x"); Serial.println(status,HEX); return ERROR; } TWI_Write(u8addr); // send the address to write to status = TWI_GetStatus(); if (status != MT_DATA_ACK) // the address was not sent { // Serial.print("TWI Write failed, register address not sent"); // Serial.print(" Status code is: 0x"); Serial.println(status,HEX); return ERROR; } TWI_Write(u8data); // send the data to write status = TWI_GetStatus(); if (status != MT_DATA_ACK) { // Serial.print("TWI Write failed, data not sent"); // Serial.print(" Status code is: 0x"); Serial.println(status,HEX); return ERROR; } TWI_Stop(); return SUCCESS; }
/* -----------------------------------------------------------------------------------------------------------*/ void HD44780_TWI_enable( void ) { if ( TWI_SendAddress( HD44780_TWI_ADDR , TWI_WRITE ) == TRUE ) { HD44780_TWI_Byte &= ~( 1 << HD44780_TWI_E_PIN ); TWI_Write( HD44780_TWI_Byte ); _delay_us( 100 ); HD44780_TWI_Byte |= ( 1 << HD44780_TWI_E_PIN ); TWI_Write( HD44780_TWI_Byte ); _delay_us( 90 ); HD44780_TWI_Byte &= ~( 1 << HD44780_TWI_E_PIN ); TWI_Write( HD44780_TWI_Byte ); TWI_SendStop(); _delay_us( 90 ); } }
/* -----------------------------------------------------------------------------------------------------------*/ void KS0073_TWI_enable( void ) { if ( TWI_SendAddress( KS0073_TWI_ADDR , TWI_WRITE ) == TRUE ) { KS0073_TWI_Byte &= ~( 1 << KS0073_TWI_E_PIN ); TWI_Write( KS0073_TWI_Byte ); _delay_us( 100 ); KS0073_TWI_Byte |= ( 1 << KS0073_TWI_E_PIN ); TWI_Write( KS0073_TWI_Byte ); _delay_us( 90 ); KS0073_TWI_Byte &= ~( 1 << KS0073_TWI_E_PIN ); TWI_Write( KS0073_TWI_Byte ); TWI_SendStop(); _delay_us( 90 ); } }
void PCF_Write(uint8_t addr, uint8_t *data, uint8_t count) { TWI_Start(); TWI_Write(PCF8563_WRITE_ADDR); TWI_Write(addr); while (count) { count--; TWI_Write(*data); data++; } TWI_Stop(); }
// Write bit to IMU void TWI_WriteBit(unsigned char reg, unsigned char bit_num, unsigned char data ){ int read_aux[1]; unsigned char data_add = 0; TWI_Read(reg, read_aux, 1); // Read byte of a registry of IMU if (data == True) data_add |= (1 << bit_num); // Select false or true for bit to write else data_add &= ~(1 << bit_num); read_aux[0] |= data_add; // Compound new data with bit for write TWI_Write(reg, read_aux[0]); // Write byte actualized }
/*! Writes (through a \a twi interface) the content of the provided \a buffer in the registers of an AK4641 component, starting at the specified \a registerAddress. This function returns true if the write operation was successful; otherwise it returns false. */ inline unsigned char AK4641_Write(AT91S_TWI *twi, void *buffer, unsigned int bufferSize, unsigned char registerAddress) { return TWI_Write(twi, buffer, bufferSize, AK4641_SLAVEADDRESS, registerAddress, 1); }
void PCF_Read(uint8_t addr, uint8_t *data, uint8_t count) { TWI_Start(); TWI_Write(PCF8563_WRITE_ADDR); TWI_Write(addr); TWI_Stop(); TWI_Start(); TWI_Write(PCF8563_READ_ADDR); while (count) { count--; *data = TWI_Read(count); data++; } TWI_Stop(); }
/* -----------------------------------------------------------------------------------------------------------*/ void HD44780_TWI_Backlight( unsigned char value ) { if ( TWI_SendAddress( HD44780_TWI_ADDR , TWI_WRITE ) == TRUE ) { if ( value != 0 ) { HD44780_TWI_Byte |= ( 1 << HD44780_TWI_BL_PIN ); // Backlight off } else { HD44780_TWI_Byte &= ~( 1 << HD44780_TWI_BL_PIN ); // Backlight off } TWI_Write( HD44780_TWI_Byte ); TWI_SendStop(); } }
/* -----------------------------------------------------------------------------------------------------------*/ void KS0073_TWI_Backlight( unsigned char value ) { if ( TWI_SendAddress( KS0073_TWI_ADDR , TWI_WRITE ) == TRUE ) { if ( LCD_CMD != LCD_BL_OFF ) { KS0073_TWI_Byte |= ( 1 << KS0073_TWI_BL_PIN ); // Backlight off } else { KS0073_TWI_Byte &= ~( 1 << KS0073_TWI_BL_PIN ); // Backlight off } TWI_Write( KS0073_TWI_Byte ); TWI_SendStop(); } }
void PCA9634_Init() { // Enable internal oscillator. TWI_Start(); TWI_Write(PCA9634_ADDRESS); TWI_Write(PCA9634_REG_MODE1); TWI_Write(0b00001); TWI_Stop(); TWI_Start(); TWI_Write(PCA9634_ADDRESS); TWI_Write(PCA9634_REG_LEDOUT0 | PCA9634_AI_ALL); TWI_Write(0b10101010); // Enable PWM mode for PWM0 to PWM3. TWI_Write(0b10101010); // Enable PWM mode for PWM4 to PWM7. TWI_Stop(); }
uint8_t print_sequence(const uint8_t *buf, uint8_t i2c_addr) { uint8_t value; uint8_t size; uint8_t addr_msb = SEQ_END; uint8_t addr_lsb; uint8_t stat; size = 2; //printf(PSTR("INIT SIZE: %u\n"), init_size); while(1) { addr_msb = pgm_read_byte(buf++); //read address or command, then set buf to point to the data if(debug_output) printf("%02X ",addr_msb); if(addr_msb == SEQ_SIZE) { size = pgm_read_byte(buf++); //read the size from buf, then set buf to next register address or command if(debug_output) printf_P(PSTR("SEQ_SIZE %u\n"), size); continue; } else if(addr_msb == SEQ_END) { break; } else if(addr_msb == SEQ_DELAY) { //value = pgm_read_byte(buf++); _delay_ms(100); if(debug_output) printf_P(PSTR("SEQ_DELAY\n")); continue; } else if(addr_msb == SEQ_RESET) { if(debug_output) printf_P(PSTR("SEQ_RESET\n"), value); //Eval SharpLCD Reset Pin PORTB |= RESET_PIN; //Set High DDRB |= RESET_PIN; //Set Output //Drude SharpLCD Reset Pin PORTB |= RESETN_PIN; //Set High DDRB |= RESETN_PIN; //Set Output _delay_us(10); //Wait 10 microseconds PORTB &= ~(RESETN_PIN); //Drude Reset Pin Set Low PORTB &= ~(RESET_PIN); //Eval Reset Pin Set Low _delay_us(10); //Wait 10 microseconds PORTB |= RESET_PIN; //Eval Reset Pin Set High PORTB |= RESETN_PIN; //Drude Toshina Reset Pin Set High _delay_ms(10); //DDRB &= ~(RESET_PIN); //Set Input continue; } else if(addr_msb == SEQ_READ) { value = pgm_read_byte(buf++); addr_lsb = pgm_read_byte(buf++); if(debug_output) printf_P(PSTR("SEQ_READ %02X %02X\n"), value,addr_lsb); continue; }; TWI_Start(); // First start condition stat = TWI_GetStatus(); if (stat != 0x08) { if(debug_output) printf_P(PSTR("\nTWI_Start()\n")); return stat; } TWI_Write((i2c_addr<<1)); // Chip address + write stat = TWI_GetStatus(); if (stat != 0x18) { if(debug_output) printf_P(PSTR("\nTWI_Write(HDMICONV_ADDR)\n")); return stat; } /* if( ! i2c_start(HDMICONV_ADDR) ) { printf_P(PSTR("i2c_start failed.\n")); } */ TWI_Write(addr_msb); // Address high byte stat = TWI_GetStatus(); if (stat != 0x28) { if(debug_output) printf_P(PSTR("\nTWI_Write(addr_msb)\n")); return stat; } for(uint8_t i=0; i<=size && i<10 ; i++) { value = pgm_read_byte(buf++); if(debug_output) printf("%02X ",value); TWI_Write(value); stat = TWI_GetStatus(); if (stat != 0x28) return stat; } TWI_Stop(); // Send stop condition _delay_us(5); if(debug_output) printf("\n"); } }
/* -----------------------------------------------------------------------------------------------------------*/ void KS0073_TWI_init( void ) { unsigned char i; int y; KS0073_TWI_Byte = 0; // erster Reset in den 8-Bit Modus KS0073_TWI_out(HD44780_CMD_SOFT_RESET); CLOCK_delay( HD44780_DELAY_SOFT_RESET1 ); // zweiter Reset in den 8-Bit Modus KS0073_TWI_enable(); CLOCK_delay( HD44780_DELAY_SOFT_RESET2 ); // dritter Reset in den 8-Bit Modus KS0073_TWI_enable(); CLOCK_delay( HD44780_DELAY_SOFT_RESET3 ); // und jetzt endlich in den 4-Bit Modus und 'RE-Bit ein b00101100 KS0073_TWI_out(HD44780_CMD_SET_FUNCTION | HD44780_SF_4BIT); // RE:1 CLOCK_delay( HD44780_DELAY_SET_4BITMODE ); KS0073_TWI_sendCMD(HD44780_CMD_SET_FUNCTION | HD44780_SF_4BIT | HD44780_SF_2LINES | KS0073_RE_ON); // Display für KS0073 einstellen b00001001 KS0073_TWI_sendCMD(KS0073_CMD_EXT_FUNCTION | HD44780_SF_5x7 | // 5-dot-Font KS0073_4LINES); // 4-line display // RE:Bit wieder auf 0 setzen b00101000 KS0073_TWI_sendCMD(HD44780_CMD_SET_FUNCTION | HD44780_SF_4BIT | HD44780_SF_2LINES| KS0073_RE_OFF); // clear screen KS0073_TWI_sendCMD( HD44780_CMD_CLEAR ); //Display ausschalten KS0073_TWI_sendCMD(HD44780_CMD_SET_DISPLAY | HD44780_SD_DISPLAY_OFF); // Entry Mode KS0073_TWI_sendCMD(HD44780_CMD_SET_ENTRY | HD44780_SE_INCREASE); // Display clear cursor home KS0073_TWI_sendCMD( HD44780_CMD_CLEAR ); // Cursor auf Homeposition setzen KS0073_TWI_sendCMD( HD44780_CMD_CURSOR_HOME ); // Display einschalten KS0073_TWI_sendCMD( HD44780_CMD_SET_DISPLAY | HD44780_SD_DISPLAY_ON /* | // display on HD44780_SD_CURSOR_ON | // show cursor HD44780_SD_BLINKING_ON */ ); // cursor blinking KS0073_TWI_row = 0; KS0073_TWI_column = 0; // fade in LCD-Backlight if ( TWI_SendAddress( KS0073_TWI_ADDR , TWI_WRITE ) == TRUE ) { for( y = 0 ; y < 128 ; y++ )// Backlight auf 100% dimmen { for ( i = 0 ; i < 128 ; i++ ) { if( i <= y) KS0073_TWI_Byte &= ~( 1 << KS0073_TWI_BL_PIN ); else KS0073_TWI_Byte |= ( 1 << KS0073_TWI_BL_PIN ); TWI_Write( KS0073_TWI_Byte ); } } TWI_SendStop(); } KS0073_TWI_Byte &= ~( 1 << KS0073_TWI_BL_PIN ); }
/* -----------------------------------------------------------------------------------------------------------*/ void HD44780_TWI_init( void ) { unsigned char i; int y; HD44780_TWI_Byte = 0; // erster Reset in den 8-Bit Modus HD44780_TWI_out(HD44780_CMD_SOFT_RESET); CLOCK_delay( HD44780_DELAY_SOFT_RESET1 ); // zweiter Reset in den 8-Bit Modus HD44780_TWI_enable(); CLOCK_delay( HD44780_DELAY_SOFT_RESET2 ); // dritter Reset in den 8-Bit Modus HD44780_TWI_enable(); CLOCK_delay( HD44780_DELAY_SOFT_RESET3 ); // und jetzt endlich in den 4-Bit Modus HD44780_TWI_out( HD44780_CMD_SET_FUNCTION | HD44780_SF_4BIT ); CLOCK_delay( HD44780_DELAY_SET_4BITMODE ); // Display einstellen, 4Bit, 2 Lines und Zeichensatz 5x8 Pixel HD44780_TWI_sendCMD( HD44780_CMD_SET_FUNCTION | HD44780_SF_4BIT | // 4 Bit mode HD44780_SF_2LINES | // 2 lines HD44780_SF_5x10 ); // 5x10 // clear screen HD44780_TWI_sendCMD( HD44780_CMD_CLEAR ); // Cursor auf Homeposition setzen HD44780_TWI_sendCMD( HD44780_CMD_CURSOR_HOME ); // Display einschalten HD44780_TWI_sendCMD( HD44780_CMD_SET_DISPLAY | HD44780_SD_DISPLAY_ON /* | // display on HD44780_SD_CURSOR_ON | // show cursor HD44780_SD_BLINKING_ON */ ); // cursor blinking HD44780_TWI_row = 0; HD44780_TWI_column = 0; // fade in LCD-Backlight if ( TWI_SendAddress( HD44780_TWI_ADDR , TWI_WRITE ) == TRUE ) { for( y = 0 ; y < 128 ; y++ )// Backlight auf 100% dimmen { for ( i = 0 ; i < 128 ; i++ ) { if( i <= y) HD44780_TWI_Byte &= ~( 1 << HD44780_TWI_BL_PIN ); else HD44780_TWI_Byte |= ( 1 << HD44780_TWI_BL_PIN ); TWI_Write( HD44780_TWI_Byte ); } } TWI_SendStop(); } HD44780_TWI_Byte &= ~( 1 << HD44780_TWI_BL_PIN ); }
tStatus TWI_RegisterRead_Multiple(uint8_t u8addr, uint8_t* u8data, uint8_t numBytes, uint8_t slaveAddress) { TWI_Start(); // send a start code to begin the write uint8_t status = TWI_GetStatus(); if (status != START && status != REP_START) // start not sent/acknowledged { // Serial.print("TWI Read failed"); // Serial.print(" Status code is: 0x"); Serial.println(status,HEX); return ERROR; } TWI_Write( (slaveAddress<<1) ); // write the address shifted so that last bit is 0, meaning write request status = TWI_GetStatus(); if (status != MT_SLA_ACK) // SLA+W was not acknowledged { // Serial.print("TWI Read failed"); // Serial.print(" Status code is: 0x"); Serial.println(status,HEX); return ERROR; } TWI_Write(u8addr); // send the address to read from status = TWI_GetStatus(); if (status != MT_DATA_ACK) // the address was not sent { // Serial.print("TWI Read failed"); // Serial.print(" Status code is: 0x"); Serial.println(status,HEX); return ERROR; } TWI_Start(); // send a repeated start status = TWI_GetStatus(); if (status != REP_START) // start not sent { // Serial.print("TWI Read failed"); // Serial.print(" Status code is: 0x"); Serial.println(status,HEX); return ERROR; } TWI_Write( ( (slaveAddress<<1) | 1) ); // write the address shifted so that last bit is 1, meaning read request status = TWI_GetStatus(); if (status != MR_SLA_ACK) // SLA+R was not acknowledged { // Serial.print("TWI Read failed"); // Serial.print(" Status code is: 0x"); Serial.println(status,HEX); return ERROR; } for (int i=0; i<numBytes-1; i++) { u8data[i] = TWI_ReadACK(); // read the data status = TWI_GetStatus(); if (status != MR_DATA_ACK) { // Serial.print("TWI Read failed. Byte #"); Serial.print(i); // Serial.print(" Status code is: 0x"); Serial.println(status,HEX); return ERROR; } } u8data[numBytes-1] = TWI_ReadNACK(); // read the last byte, and respond with a NACK to let the slave know its the last one status = TWI_GetStatus(); if (status != MR_DATA_NACK) { // Serial.print("TWI Read failed"); // Serial.print(" Status code is: 0x"); Serial.println(status,HEX); return ERROR; } TWI_Stop(); return SUCCESS; }
tStatus TWI_RegisterRead(uint8_t u8addr, uint8_t* u8data, uint8_t slaveAddress) { TWI_Start(); // send a start code to begin the write uint8_t status = TWI_GetStatus(); if (status != START && status != REP_START) // start not sent/acknowledged { // Serial.print("TWI Read failed. Start not sent"); // Serial.print(" Status code is: 0x"); Serial.println(status,HEX); return ERROR; } TWI_Write( (slaveAddress<<1) ); // write the address shifted so that last bit is 0, meaning write request status = TWI_GetStatus(); if (status != MT_SLA_ACK) // SLA+W was not acknowledged { // Serial.print("TWI Read failed. address not sent"); // Serial.print(" Status code is: 0x"); Serial.println(status,HEX); return ERROR; } TWI_Write(u8addr); // send the address to read from status = TWI_GetStatus(); if (status != MT_DATA_ACK) // the address was not sent { // Serial.print("TWI Read failed. address of register not sent"); // Serial.print(" Status code is: 0x"); Serial.println(status,HEX); return ERROR; } TWI_Start(); // send a repeated start status = TWI_GetStatus(); if (status != REP_START) // start not sent { // Serial.print("TWI Read failed. Repeated Start not sent"); // Serial.print(" Status code is: 0x"); Serial.println(status,HEX); return ERROR; } TWI_Write( ( (slaveAddress<<1) | 1) ); // write the address shifted so that last bit is 1, meaning read request status = TWI_GetStatus(); if (status != MR_SLA_ACK) // SLA+R was not acknowledged { // Serial.print("TWI Read failed"); // Serial.print(" Status code is: 0x"); Serial.println(status,HEX); return ERROR; } *u8data = TWI_ReadNACK(); // read the data status = TWI_GetStatus(); if (status != MR_DATA_NACK) { // Serial.print("TWI Read failed"); // Serial.print(" Status code is: 0x"); Serial.println(status,HEX); return ERROR; } TWI_Stop(); return SUCCESS; }