/************************************************************* * @fn Write_Data * * @brief This func for read data form slave device * * @param uint8 - Address of slave device * * @return uint8 - The byte read form the Addr */ uint8 Read_Data(uint8 Addr) { uint8 Value; HAL_DISABLE_INTERRUPTS(); I2c_Start(); //make the I2C bus begin //if((Addr != Last_Addr + 1) || (Addr & 0xFF)) //{ I2c_Write_Byte(0xC8 ); // write address 1100100 and make the R/w with 0 I2c_Ack(); I2c_Write_Byte(Addr);// write register to slaver I2c_Ack(); I2c_Start(); //repeat make the I2C bus begin I2c_Write_Byte(0xC9);// write address 1100100 and make the R/w with 1 I2c_Ack(); Value = I2c_Read_Byte(); // we recive the content from slaver I2c_Stop(); HAL_ENABLE_INTERRUPTS(); return Value; }
int main(void) { _U08 u8Config; ANCON0 = 0XFF; /*Desactivamos las entradas analógicas*/ ANCON1 = 0XFF; /*Desactivamos las entradas analógicas*/ Gpios_PinDirection(GPIOS_PORTD, 0, GPIOS_INPUT); /*SCL2*/ Gpios_PinDirection(GPIOS_PORTD, 1, GPIOS_INPUT); /*SDA2*/ Gpios_PinDirection(GPIOS_PORTC, 6, GPIOS_OUTPUT); /*puerto de tx uart como salida*/ (void)Uart_Init(UART_PORT1, 115200); /*velocidad a 115200 bauds*/ xdev_out(putChar); /*funcion Uart_PutChar como salida estandar*/ I2c_Init(I2C_PORT2, 100000); /*puerto I2C 2 a 100KHz de velocidad*/ I2c_Start(I2C_PORT2); /*generamos condicion start*/ (void)I2c_bTxByte(I2C_PORT2, ADDR_WRITE(0b1001101)); /*madamos direccion del sensor en modo escritura*/ (void)I2c_bTxByte(I2C_PORT2, 0x01); /*mandamos direccion a leer*/ I2c_RepeatedStart(I2C_PORT2); /*repetimos señal start*/ (void)I2c_bTxByte(I2C_PORT2, ADDR_READ(0b1001101)); /*madmaos direccion del sensor en modo lectura*/ u8Config = I2c_u8RxByte(I2C_PORT2, I2C_NACK); /*leemos dato leido y contestamos NACK*/ I2c_Stop(I2C_PORT2); /*indicamos fin de comunicacion*/ /*mostramos por serial el byte leido el cual tendra el valor de 0x40, indica sensor listo*/ xprintf("Registro config: 0x%X\r\n", (_U16)u8Config); while (1) { /*Escribe aqui tu aplicacion*/ } }
/************************************************************* * @fn Write_Data * * @brief This func for write words * We can write 2 byte into register * * @param uint8 - Address of slave device * uint8 - The byte want to be writen * * @return none */ void Write_Data(uint8 Addr,uint8 Value) { I2c_Start(); //make the I2C bus begin I2c_Write_Byte(0xC8);//master send slaver address I2c_Ack(); //we can get Ackknowledge from slaver if equal to 0,means right I2c_Write_Byte(Addr);//master send register to slaver I2c_Ack(); I2c_Write_Byte(Value);//master send value to slaver if( I2c_Ack() ) { I2c_Stop(); // return False; } //MicroWait(I2C_DELAY_TIME); delay_us(3); I2c_Stop(); }