2016-10-30 7 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

わかりました。私はアドレスを書いてから2つのステップでデータを読む必要があります。私は以下のようにmain関数を修正しました。

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. 
} 
関連する問題