Re: 關於PIC24F 使用I2C 存取EEPROM
|
||||
---|---|---|---|---|
新會員
|
目前測試結果為PIC24FJ16GA002的問題
使用4.7K Pull High電阻拉I2C Chip本身會發生I2C衝突問題 使用10K Pull High電阻拉I2C 目前不會發生I2C衝突問題 依據國外論壇說明的SDA腳位再INIT I2C後 需在拉Low SCL才會正常動作 所以建議有使用PIC24FJ16GA002的朋友犧牲一支腳位 在INIT I2C後將拉SDA拉LOW確保I2C會作用正常 http://forum.microchip.com/tm.aspx?m= ... ey=PIC24FJ16GA002
發表於: 2009/2/4 14:12
|
|||
|
Re: 關於PIC24F 使用I2C 存取EEPROM
|
||||
---|---|---|---|---|
新會員
|
經過修正目前有訊號產生,不過還是寫不進EEPROM
原因: 原來是Sample code 沒將I2C腳位定義為輸出腳位 這樣因該不能算是Sample code吧 第一次用MicroChip 的人因該都不知道怎樣修改吧 以為SampleCode就會動作下載下來卻不能動作 // calculate baud rate of I2C #define Fosc (20000000) #define Fcy (Fosc/2) // no PLL #define Fsck 100000 #define I2C_BRG ((Fcy/Fsck)-2)//依據SPEC修正 ((Fcy/2/Fsck)-1)//Sample Code int main (void) { ... PADCFG1=0xffff; TRISBbits.TRISB6 = 0;//定義為輸出腳位 LATBbits.LATB6 = 0; TRISBbits.TRISB8 = 0;//定義為輸出腳位 SDA1 // LATBbits.LATB8 = 0; TRISBbits.TRISB9 = 0;//定義為輸出腳位SCL1 // LATBbits.LATB9 = 0; }
發表於: 2009/1/21 12:13
|
|||
|
關於PIC24F 使用I2C 存取EEPROM
|
||||
---|---|---|---|---|
新會員
|
使用Sample code 利用I2C對PIC24F系列讀寫EEPROM
可是腳位都不會動作,請問一下使用PIC24F有遇到相同問題嗎? #include "p24fxxxx.h" #define USE_AND_OR // To enable AND_OR mask setting for I2C. #include <i2c.h> // JTAG/Code Protect/Write Protect/Clip-on Emulation mode // Watchdog Timer/ICD pins select _CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx1) // Disable CLK switch and CLK monitor, OSCO or Fosc/2, HS oscillator, // Primary oscillator _CONFIG2(FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMOD_HS & FNOSC_PRI) #define lenArray(arr) (sizeof(arr)/sizeof(arr[0])) // calculate baud rate of I2C #define Fosc (20000000) //8000000 #define Fcy (Fosc/2) // no PLL #define Fsck 100000 #define I2C_BRG ((Fcy/Fsck)-1)//((Fcy/2/Fsck)-1) #define I2C_EEPROM_2402 // use 24XX02X, Low Address only //#define I2C_EEPROM_24256 // use 24XX256, HiLo Address void i2c_wait(unsigned int cnt) { while(--cnt) { asm( "nop" ); asm( "nop" ); } } int main (void) { unsigned char SlaveAddress; char i2cData[10]; int DataSz; int Index = 0; unsigned char *pWrite, *pRead; unsigned int qqq; // The data to be written should be end with '\0' if using // char MasterputsI2C1(unsigned char *) library function. unsigned char tx_data[] = {'P', 'I', 'C', '2', '4', 'F', '\0'}; unsigned char rx_data[lenArray(tx_data)]; char status; unsigned char i2cbyte; PADCFG1=0xffff; TRISBbits.TRISB6 = 0; LATBbits.LATB6 = 0; pWrite = tx_data; pRead = rx_data; //Enable channel OpenI2C1( I2C_ON, I2C_BRG ); I2C1STATbits.ACKSTAT = 1; qqq = ((I2C1STAT&I2C1STATbits.ACKSTAT)==I2C1STATbits.ACKSTAT); SlaveAddress = 0x50; //0b1010000 Serial EEPROM address // Send Data to eeprom to program one location #ifdef I2C_EEPROM_24256 // one page is 64 bytes i2cData[0] = (SlaveAddress << 1) | 0; //Device Address & WR i2cData[1] = 0x05; //eeprom high address byte i2cData[2] = 0x40; //eeprom low address byte i2cData[3] = 0xAC; //data to write DataSz = 4; #endif #ifdef I2C_EEPROM_2402 // one page is 8 bytes i2cData[0] = (SlaveAddress << 1) | 0; //Device Address & WR i2cData[1] = 0x10; //eeprom low address byte i2cData[2] = 0xAC; //data to write (one byte) DataSz = 3; #endif /* * Write one byte to designated address. [1] */ LATBbits.LATB6 = 1; StartI2C1(); //Send the Start Bit IdleI2C1(); //Wait to complete while( DataSz ) { MasterWriteI2C1( i2cData[Index++] ); IdleI2C1(); //Wait to complete DataSz--; //ACKSTAT is 0 when slave acknowledge, //if 1 then slave has not acknowledge the data. qqq = I2C1STATbits.ACKSTAT; if( I2C1STATbits.ACKSTAT ) break; } // one byte has been written // write some bytes continuously // The data to be written should be end with '\0' if using // char MasterputsI2C1(unsigned char *) library function. status = MasterputsI2C1(pWrite); Nop(); if (status == -3) while (1); StopI2C1(); //Send the Stop condition IdleI2C1(); //Wait to complete LATBbits.LATB6 = 0; /* * End writing. [1] */ #if 0 while(1) { DDCCI_Handler } #endif // wait for eeprom to complete write process. poll the ack status while(1) { i2c_wait(10); LATBbits.LATB6 = 1; StartI2C1(); //Send the Start Bit IdleI2C1(); //Wait to complete MasterWriteI2C1( i2cData[0] ); IdleI2C1(); //Wait to complete if( I2C1STATbits.ACKSTAT == 0 ) //eeprom has acknowledged { StopI2C1(); //Send the Stop condition IdleI2C1(); //Wait to complete LATBbits.LATB6 = 0; break; } StopI2C1(); //Send the Stop condition IdleI2C1(); //Wait to complete LATBbits.LATB6 = 0; } /********************************************************************/ /* * Now Readback one data from the serial eeprom. [2] */ #ifdef I2C_EEPROM_24256 i2cData[0] = (SlaveAddress << 1) | 0; //Device Address & WR i2cData[1] = 0x05; //eeprom high address byte i2cData[2] = 0x40; //eeprom low address byte DataSz = 3; #endif #ifdef I2C_EEPROM_2402 i2cData[0] = (SlaveAddress << 1) | 0; //Device Address & WR i2cData[1] = 0x10; //eeprom low address byte DataSz = 2; #endif LATBbits.LATB6 = 1; StartI2C1(); //Send the Start Bit IdleI2C1(); //Wait to complete //send the address to read from the serial eeprom Index = 0; while( DataSz ) { MasterWriteI2C1( i2cData[Index++] ); IdleI2C1(); //Wait to complete DataSz--; // ACKSTAT is 0 when slave acknowledge, // if 1 then slave has not acknowledge the data. if( I2C1STATbits.ACKSTAT) break; } //now send a start sequence again RestartI2C1(); //Send the Restart condition i2c_wait(10); //wait for this bit to go back to zero IdleI2C1(); //Wait to complete MasterWriteI2C1( (SlaveAddress << 1) | 1 ); //transmit read command IdleI2C1(); //Wait to complete i2cbyte = MasterReadI2C1(); // read one byte StopI2C1(); //Send the Stop condition IdleI2C1(); //Wait to complete LATBbits.LATB6 = 0; /* * End reading one byte. [2] */ // verify write and read I2C EEPROM (single byte) if( i2cbyte != 0xAC ) while(1); //error: verify failed /********************************************************************/ /* * Now Readback several data from the serial eeprom. [3] */ #ifdef I2C_EEPROM_24256 i2cData[0] = (SlaveAddress << 1) | 0; //Device Address & WR i2cData[1] = 0x05; //eeprom high address byte i2cData[2] = 0x41; //eeprom low address byte DataSz = 3; #endif #ifdef I2C_EEPROM_2402 i2cData[0] = (SlaveAddress << 1) | 0; //Device Address & WR i2cData[1] = 0x11; //eeprom low address byte DataSz = 2; #endif LATBbits.LATB6 = 1; StartI2C1(); //Send the Start Bit IdleI2C1(); //Wait to complete //send the address to read from the serial eeprom Index = 0; while( DataSz ) { MasterWriteI2C1( i2cData[Index++] ); IdleI2C1(); //Wait to complete DataSz--; // ACKSTAT is 0 when slave acknowledge, // if 1 then slave has not acknowledge the data. if( I2C1STATbits.ACKSTAT ) break; } //now send a start sequence again RestartI2C1(); //Send the Restart condition i2c_wait(10); //wait for this bit to go back to zero IdleI2C1(); //Wait to complete MasterWriteI2C1( (SlaveAddress << 1) | 1 ); //transmit read command IdleI2C1(); //Wait to complete // read some bytes back status = MastergetsI2C1(lenArray(tx_data), pRead, 20); if (status!=0) while(1); StopI2C1(); //Send the Stop condition IdleI2C1(); //Wait to complete LATBbits.LATB6 = 0; /* * End reading several bytes. [3] */ // verify write and read I2C EEPROM (multiple bytes) Index = 0; do { if(*pRead++ != *pWrite++) while(1); //error: verify failed Index++; } while(Index<lenArray(tx_data)-1); while(1); // Success return 0; }
發表於: 2009/1/20 15:14
|
|||
|