uint8_t MPU6050_Setup(void) { I2C_Setup(); uint8_t data[2] = {0,0}; data[0] = MPU6050_CLOCK_PLL_ZGYRO; if(I2C_WriteData(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_PWR_MGMT_1, data, 1))//电源管理,典型值:0x00(正常启用) return 1; delay_ms(2); data[0] = 0x07; if(I2C_WriteData(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_SMPLRT_DIV, data, 1))//陀螺仪采样率,典型值:0x04,五分频 return 2; delay_ms(2); data[0] = MPU6050_CONFIG_DLPF_CFG_95HZ; if(I2C_WriteData(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_CONFIG, data, 1))//低通滤波频率,典型值:0x02(截止频率100Hz) return 3; delay_ms(2); data[0] = MPU6050_GYRO_CONFIG_FS_SEL_1000; data[1] = MPU6050_ACCEL_CONFIG_HPF_5HZ|MPU6050_ACCEL_CONFIG_FS_SEL_8G; I2C_WriteData(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_GYRO_CONFIG, data, 2);//加速计自检、测量范围及高通滤波频率,典型值:0x10(不自检,8G,5Hz) delay_ms(10); __loop_cnt_acc__ = 0; __loop_cnt_gyro__ = 0; return 0; }
uint8_t EEPROM24AA512WriteNByte(uint16_t Register, uint8_t *Buffer, uint8_t Length) { uint8_t result = 1; if(I2C_WriteData(g_I2cHandleMaster, EEPROM24AA512_SLADDRESS, (uint16_t)Register, I2C_MEMADD_SIZE_16BIT, Buffer, Length) != HAL_OK) result = 0; return result; }
/* Write to RTC if OK Return Result */ bool setTimeAD5242BR(const uint8 contr, const uint8 position) { TI2C_Result res; TINFO_AD5242 infoAD5242={0}; validAD5242 = false; //I2C_SetBaudRate(DS1338_BAUDRATE); infoAD5242.control = contr; infoAD5242.data = position; //__disable_interrupt(); res = I2C_WriteData(AD5242_DEVICE_ADD, 1, 0x00, sizeof(TINFO_AD5242), (uint8*)&infoAD5242); //__enable_interrupt(); if (res == I2C_RES_OK) validAD5242 = true; return validAD5242; }
/*主函数*/ void main() { INT8U Temp_Key; //存放按键处理值 BOOL I2C_OK; //I2C读写是否成功(1成功,0失败) LCD1602_Init(); //LCD1602初始化 IR_TX_RX_Init(); //IR发送、接收初始化 I2C_Init(); //I2C初始化 Delay_Ms(50); LCD1602_Write_String(0, 0, "Welcome to use!"); LCD1602_Write_String(0, 1, "IR Remote Module"); //欢迎文字 Delay_Ms(5000); //5秒延时 LCD1602_Clear(); //LCD1602清屏 LCD1602_Write_String(0, 0, "Switch Mode!"); LCD1602_Write_String(0, 1, "Default CopyMode"); //功能选择提示文字 while (1) { if (IR_RX_Finish) //IR是否接收完成 { IR_RX_Finish = 0; LCD1602_Clear(); /*LCD1602以16进制显示IR接收到的用户名与键值码*/ LCD1602_Write_String(0, 1, INT8UtostrHEX(IR_RX_CODE[0])); LCD1602_Write_String(3, 1, INT8UtostrHEX(IR_RX_CODE[1])); LCD1602_Write_String(7, 1, INT8UtostrHEX(IR_RX_CODE[2])); LCD1602_Write_String(10, 1, INT8UtostrHEX(IR_RX_CODE[3])); } Temp_Key = KeyPro(); //提取按键处理值 if (KeyPro_OK) //按键处理是否成功 { KeyPro_OK = 0; if (TorR_Mod) //选择为发送模式 { if (Temp_Key != 0xfd) //非选择模式按键按下(S3,发送模式) { do { I2C_OK = I2C_ReadData(Temp_Key); } while (!I2C_OK); //等待I2C读取发送码成功 Transmit_TR_CODE(); //发送红外编码 LCD1602_Clear(); LCD1602_Write_String(0, 1, INT8UtostrHEX(Temp_Key)); //显示学习按键值 LCD1602_Write_String(5, 1, INT8UtostrHEX(I2C_OK)); //显示成功读取IR_TX_CODE /*LCD1602以16进制显示从I2C取出的IR_TX_CODE*/ LCD1602_Write_String(0, 0, INT8UtostrHEX(IR_TX_CODE[0])); LCD1602_Write_String(3, 0, INT8UtostrHEX(IR_TX_CODE[1])); LCD1602_Write_String(7, 0, INT8UtostrHEX(IR_TX_CODE[2])); LCD1602_Write_String(10, 0, INT8UtostrHEX(IR_TX_CODE[3])); } else //选择模式按键按下(S3) { LCD1602_Clear(); LCD1602_Write_String(0, 0, "IR Transmit Mode"); LCD1602_Write_String(3, 1, "Please Key!"); } } else //学习模式 { if (Temp_Key != 0xfe) //非选择模式按键按下(S2,学习模式) { LCD1602_Clear(); LCD1602_Write_String(14, 1, INT8UtostrHEX(Temp_Key)); //显示按键值 do { I2C_OK = I2C_WriteData(Temp_Key); } while (!I2C_OK); //等待将接收红外码存入I2C成功 LCD1602_Write_String(14, 0, INT8UtostrHEX(I2C_OK)); //显示存入成功 Delay_Ms(100); /*从I2C读取刚存入的IR学习码*/ I2C_ReadData(Temp_Key); LCD1602_Write_String(0, 0, INT8UtostrHEX(IR_TX_CODE[0])); LCD1602_Write_String(3, 0, INT8UtostrHEX(IR_TX_CODE[1])); LCD1602_Write_String(7, 0, INT8UtostrHEX(IR_TX_CODE[2])); LCD1602_Write_String(10, 0, INT8UtostrHEX(IR_TX_CODE[3])); } else //选择模式按键按下(S2,学习模式) { LCD1602_Clear(); LCD1602_Write_String(0, 0, "IRcode Copy Mode"); LCD1602_Write_String(0, 1, "Please In IRcode"); } } } } }
static bool I2C_SendForGeneral(I2C_CHANNEL eCh, u8 ucSlvAddr, u8 aAddr[], unsigned int nNumOfAddr, u8 aData[], unsigned int nNumOfData) { bool bResult = TRUE; unsigned int i; if (g_aI2c[eCh].eError == I2C_INVALID_TX_CLOCK) { bResult = FALSE; goto I2C_Stop; } // // I2C Phase - Check BUS Status // if (I2C_WaitForBusReady(eCh, g_aI2c[eCh].nTimeOut) == FALSE) { g_aI2c[eCh].eError = I2C_TIMEOUT_BUS_READY_START; goto I2C_Stop; } I2C_InitMaster(eCh, g_aI2c[eCh].ePrescaler, g_aI2c[eCh].nPrescaler); // Init I2C // // I2C Phase - START // I2C_SetMode(eCh, MASTER_TX_MODE); // Set Mode I2C_WriteAddress(eCh, ucSlvAddr); // Write Slave Address I2C_GenerateSignal(eCh, START_CONDITION); // Send START Signal udelay(10); if (I2C_WaitForXferAck(eCh, g_aI2c[eCh].nTimeOut) == FALSE) { g_aI2c[eCh].eError = I2C_TIMEOUT_SLAVE_ADDRESS; goto I2C_Stop; } // // I2C Phase - Write Address // for (i = 0; i < nNumOfAddr; i++) { I2C_WriteData(eCh, aAddr[i]); I2C_ClearIntStatus(eCh); if (I2C_WaitForXferAck(eCh, g_aI2c[eCh].nTimeOut) == FALSE) { g_aI2c[eCh].eError = I2C_TIMEOUT_WRITE_ADDRESS; goto I2C_Stop; } } // // I2C Phase - Write Data // for (i = 0; i < nNumOfData; i++) { I2C_WriteData(eCh, aData[i]); I2C_ClearIntStatus(eCh); udelay(10); if (I2C_WaitForXferAck(eCh, g_aI2c[eCh].nTimeOut) == FALSE) { g_aI2c[eCh].eError = I2C_TIMEOUT_SLAVE_ADDRESS; goto I2C_Stop; } } g_aI2c[eCh].eError = I2C_SUCCESS; I2C_Stop: // // I2C Phase - STOP // I2C_GenerateSignal(eCh, STOP_CONDITION); // Send STOP Signal I2C_ClearIntStatus(eCh); I2C_SetMode(eCh, DISABLE_I2C); // Deactivate I2C // // I2C Phase - Check BUS Status // if (I2C_WaitForBusReady(eCh, g_aI2c[eCh].nTimeOut) == FALSE) { g_aI2c[eCh].eError = I2C_TIMEOUT_BUS_READY_STOP; bResult = FALSE; } I2C_PrintErrorCase(g_aI2c[eCh].eError); return bResult; }