TA的每日心情 | 奋斗 2015-10-5 14:38 |
---|
签到天数: 175 天 连续签到: 1 天 [LV.7]常住居民III
|
我现在调到PIC32MZ的I2C,本来想用PIC官方的harmony库函数来写一个初始化MPU6050的程序。但试了两三天也不行,无奈之下看了PIC32MZ的I2C参考手册的那部分。写了一个寄存器版的读取MPU6050的程序。当还是有问题。
我的代码是:
void I2C1_Init()
{
uint32_t actualClock;
PLIB_I2C_BaudRateSet ( MY_I2C_ID, MY_CLOCK_FREQUENCY, MY_BAUD_RATE);
actualClock = PLIB_I2C_BaudRateGet ( MY_I2C_ID, MY_CLOCK_FREQUENCY );
if(abs(actualClock-MY_BAUD_RATE) > MY_BAUD_RATE/10);
{
while(1);
}
I2C1CONbits.A10M = 0;
I2C1CONbits.SMEN = 0;
I2C1CONbits.ON = 1;
}
void I2C1_Start()
{
I2C1CONbits.SEN = 1;
while(I2C1STATbits.P);
}
void I2C1_SendData(uint8_t Data)
{
I2C1TRNbits.I2CTRN = Data;
while(I2C1STATbits.TBF);
}
void I2C1_Stop()
{
I2C1CONbits.PEN = 1;
}
#define OAR1_ADD0_Set ((uint16_t)0x0001)
#define OAR1_ADD0_Reset ((uint16_t)0xFFFE)
void Send7bitsAddress(uint8_t Address,uint8_t I2C_Direction)
{
if(I2C_Direction == 0)
{
Address |= OAR1_ADD0_Set;
}
else
{
Address &= OAR1_ADD0_Reset;
}
I2C1TRNbits.I2CTRN = Address;
}
void I2C_Byte_Write(uint8_t SlaveAddress,uint8_t REG_Address,uint8_t REG_data)
{
I2C1_Start();
Send7bitsAddress(SlaveAddress,1);
while(I2C1STATbits.R_W);
while(I2C1STATbits.TBF);
I2C1_SendData(REG_Address);
while(I2C1STATbits.TBF);
I2C1_SendData(REG_data);
while(I2C1STATbits.TBF);
I2C1_Stop();
}
void MPU_6050_Init(void)
{
I2C_Byte_Write(MPU6050_SlaveAddress,PWR_MGMT_1,0x00);
delay(50000);
I2C_Byte_Write(MPU6050_SlaveAddress,SMPLRT_DIV,0x07);
delay(50000);
I2C_Byte_Write(MPU6050_SlaveAddress,MPU6050_CONFIG,0x06);
delay(50000);
I2C_Byte_Write(MPU6050_SlaveAddress,GYRO_CONFIG,0x08);
delay(50000);
I2C_Byte_Write(MPU6050_SlaveAddress,ACCEL_CONFIG,0x08);
}
int main ( void )
{
SYS_DEVCON_PerformanceConfig(SYS_CLK_FREQUENCY);
I2C1_Init();
while(1)
{
MPU_6050_Init();
}
/*
Initialize the application. */
/* SYS_Initialize(NULL);
while (true)
{
// Maintain the application's state machine.
SYS_Tasks();
}
*/
/* Should not come here during normal operation */
PLIB_ASSERT(false, "leaving main");
return (EXIT_FAILURE);
}
但总是卡死在了while(1)的那句话,我想也想不明白我已经设置了100K的SCL频率,但还是有超过10%的误差。
求大神解救呀,希望我的问题能得到解决。
|
|