2016-10-30 111 views
0

我正在使用MPLABX 3.10,並使用MSSPI2C主中斷功能生成I2C主接口。我能夠產生I2C寫入事件,在振盪器上看起來沒問題。然而,閱讀失敗。當我查看範圍輸出時,我可以清楚地看到生成了啓動條件,並且設置了讀取位的設備ID被生成並確認。接下來,我期待看到寄存器地址出去,但是我看到所有的零。我是否錯誤地使用生成的代碼?我需要做設備寫入,然後讀取設備嗎?我試圖減少代碼以下:PIC微控制器I2C讀取失敗,MPLABX生成代碼

void I2C_Initialize(void) { 
    i2c_object.pTrHead = i2c_tr_queue; 
    i2c_object.pTrTail = i2c_tr_queue; 
    i2c_object.trStatus.s.empty = true; 
    i2c_object.trStatus.s.full = false; 

    i2c_object.i2cErrors = 0; 

    // BF RCinprocess_TXcomplete; UA dontupdate; SMP Sample At Middle; P stopbit_notdetected; S startbit_notdetected; R_nW write_noTX; CKE Idle to Active; D_nA lastbyte_address; 
    SSP1STAT = 0x00; 
    // SSPEN enabled; WCOL no_collision; SSPOV no_overflow; CKP Idle:Low, Active:High; SSPM FOSC/4_SSPxADD; 
    SSP1CON1 = 0x28; 
    // BOEN disabled; AHEN disabled; SBCDE disabled; SDAHT 100ns; DHEN disabled; ACKTIM ackseq; PCIE disabled; SCIE disabled; 
    SSP1CON3 = 0x00; 
    // Baud Rate Generator Value: SSPADD 3; 
    SSP1ADD = 0x03; 

    /* Byte sent or received */ 
    // clear the master interrupt flag 
    PIR1bits.SSP1IF = 0; 
    // enable the master interrupt 
    PIE1bits.SSP1IE = 1; 

} 

void I2C_MasterRead(
     uint8_t *pdata, 
     uint8_t length, 
     uint16_t address, 
     I2C_MESSAGE_STATUS *pflag) { 
    static I2C_TRANSACTION_REQUEST_BLOCK trBlock; 


    // check if there is space in the queue 
    if (i2c_object.trStatus.s.full != true) { 
     I2C_MasterReadTRBBuild(&trBlock, pdata, length, address); 
     I2C_MasterTRBInsert(1, &trBlock, pflag); 
    } else { 
     *pflag = I2C_MESSAGE_FAIL; 
    } 

} 

void I2C_MasterReadTRBBuild(
     I2C_TRANSACTION_REQUEST_BLOCK *ptrb, 
     uint8_t *pdata, 
     uint8_t length, 
     uint16_t address) { 
    ptrb->address = address << 1; 
    // make this a read 
    ptrb->address |= 0x01; 
    ptrb->length = length; 
    ptrb->pbuffer = pdata; 
} 

void main(void) { 
    #define BMA222E_BASE_ADDRESS_DEV0 (0x18) // <BMA222E base address 
    uint8_t dummy[2]; 
    I2C_MESSAGE_STATUS pflag; 
    /* Configure the oscillator for the device */ 

    ConfigureOscillator(); 
    I2C_Initialize(); 


    dummy[0] = 0x0F; // I expect to see 0x0F go out as the register value 
    dummy[1] = 0x00; 
    I2C_MasterRead (&dummy, 2, BMA222E_BASE_ADDRESS_DEV0, &pflag); 
} 

回答

0

好吧,所以計算出來。我需要寫地址,然後分兩步讀取數據。我修改了主要功能如下。

void main(void) { 
    #define BMA222E_BASE_ADDRESS_DEV0 (0x18) // <BMA222E base address 
    uint8_t dummy[2]; 
    I2C_MESSAGE_STATUS pflag; 
    /* Configure the oscillator for the device */ 
    ConfigureOscillator(); 
    I2C_Initialize(); 


    dummy[0] = 0x0F; // I expect to see 0x0F go out as the register value 
    dummy[1] = 0x00; 
    I2C_MasterWrite (&dummy, 1, BMA222E_BASE_ADDRESS_DEV0, &pflag); 
    I2C_MasterRead (&dummy, 1, BMA222E_BASE_ADDRESS_DEV0, &pflag); 

    // dummy[0] now contains the read data. 
} 
相關問題