• slider image 442
  • slider image 483
  • slider image 484
  • slider image 485
  • slider image 486
  • slider image 487
:::


Browsing this Thread:   1 Anonymous Users






Re: 關於PIC24F 使用I2C 存取EEPROM
#4
新會員
新會員


查看用戶資訊
目前測試結果為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
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 關於PIC24F 使用I2C 存取EEPROM
#3
中級會員
中級會員


查看用戶資訊
你的 I2C_BRG 算出來值是多少? 你要知道, 你這裡的 C 是 integer, 除完小數後面會被截掉.

建議你用 while 一直讓 I2C 寫出, 讓示波器的 SCL 出現方波, 算一下它是幾 KHZ
EEPROM datasheet 大概會寫可以承受 400 KHz, 但建議不要超過 390.

你也可以暫時不要用公式算, 先用故定值填入試試, 先避開其它不必要的變因, 但也要量一下示波器, 例如
OpenI2C1( I2C_ON, 0x22 /*I2C_BRG*/ );
OpenI2C1( I2C_ON, 0x11 /*I2C_BRG*/ );



發表於: 2009/2/2 14:41
曾經 狂奔 舞蹈 貪婪的說話, 隨著冷的 濕的 心腐化

個人的休閒小站
歡迎來參觀
http://www.elevior.com
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 關於PIC24F 使用I2C 存取EEPROM
#2
新會員
新會員


查看用戶資訊
經過修正目前有訊號產生,不過還是寫不進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
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


關於PIC24F 使用I2C 存取EEPROM
#1
新會員
新會員


查看用戶資訊
使用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
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部







You can view topic.
不可以 發起新主題
You cannot reply to posts.
You cannot edit your posts.
You cannot delete your posts.
You cannot add new polls.
You cannot vote in polls.
You cannot attach files to posts.
You cannot post without approval.
You cannot use topic type.
You cannot use HTML syntax.
You cannot use signature.
You cannot create PDF files.
You cannot get print page.

[進階搜尋]


:::

Microchip連結

https://www.facebook.com/microchiptechnologytaiwan/
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=13
https://mu.microchip.com/page/tmu
http://elearning.microchip.com.tw/modules/tad_link/index.php?cate_sn=1
https://page.microchip.com/APAC-PrefCenters-TW.html
http://www.microchip.com/
http://www.microchip.com/treelink
http://www.microchipdirect.com/
http://www.microchip.com.cn/newcommunity/index.php?m=Video&a=index&id=103
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=2
http://www.microchip.com.tw/Data_CD/eLearning/index.html
http://www.microchip.com.tw/RTC/RTC_DVD/
https://www.microchip.com/development-tools/
https://www.youtube.com/user/MicrochipTechnology
[ more... ]

教育訓練中心

!開發工具購買
辦法說明 [業界客戶] [教育單位]
----------------------------------
!校園樣品申請
辦法說明 [教師資格] [學生資格]
----------------------------------