这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 企业专区 » Microchip » PIC32MZ的硬件I2C

共1条 1/1 1 跳转至

PIC32MZ的硬件I2C

菜鸟
2014-08-02 11:03:34     打赏
我现在调到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%的误差。
第二个问题是卡在了while(I2C1STATbits.TBF);这句话,就是得不到应答。
求大神解救呀,希望我的问题能得到解决。




关键词: PIC32MZ     硬件I2C    

共1条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]