• 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: I2C 玩不出來 請大仔解惑一下 Orz
#5
高級會員
高級會員


查看用戶資訊
目前是還停留在 eeprom i2c 這邊,玩不出來 冏rz
我有去 microchip 看過應用筆記,有點問題想請問一下

24LC32A的 Datasheet 提到
Q1 ~~~~~~~~~~~~~~~~~~~~~~~
byte write :

start > control byte > address high byte >
address low byte > data > stop

page write :

start > control byte > address high byte >
address low byte > data byte0 > data byte31 > stop

這兩種是哪裡不一樣 ? 高低位址 ?? 不是只要送 slave 的位址就好了嗎 ? ex : 送 0xa0 就是位址 幹麻還要分高低 ?

Q2 :

我程式再寫的時候我需要去寫等待 ack 的這個回應嗎 ?

我是用CCS在寫的,他範例我是看過套用過了,玩不出來
他寫的很簡單,我也想的很簡單 ~

Master
i2c_start(); // Start condition
i2c_write(0xa0); // write command
i2c_write(0xa0); // Device address
i2c_write('A'); // 寫一個 A 過去
i2c_stop(); // Stop condition

Slave
i2c_start(); // Start condition
i2c_write(0xa1); // read command
i2c_write(0xa0); // Device address
data = i2c_read(); //讀回來放在 data
i2c_stop(); // Stop condition

我想的很簡單...可就是玩不出來,我有用邏輯分析儀去看
SDA SCL ~ 都是出現 start & stop 訊號而已
就算有跑出 address 也是偶爾,而且位置也不對
請問我還需要怎麼玩 .... 冏

發表於: 2009/2/24 15:29
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: I2C 玩不出來 請大仔解惑一下 Orz
#4
資深會員
資深會員


查看用戶資訊
在CCS中你可以先試用寫EEPROM來確認i2c功能

下列是CCS檔案中2416.c檔,這是當Master來讀取EEPROM

在/PICC/drivers/中可找到


#ifndef EEPROM_SDA

#define EEPROM_SDA PIN_E0
#define EEPROM_SCL PIN_E1

#endif


#use i2c(master, sda=EEPROM_SDA, scl=EEPROM_SCL)

#define EEPROM_ADDRESS unsigned int16
#define EEPROM_SIZE 1024

void init_ext_eeprom() {
output_float(EEPROM_SCL);
output_float(EEPROM_SDA);
}

int1 ext_eeprom_ready() {
int1 ack;
i2c_start(); // If the write command is acknowledged,
ack = i2c_write(0xa0); // then the device is ready.
i2c_stop();
return !ack;
}

void write_ext_eeprom(EEPROM_ADDRESS address, BYTE data) {
while(!ext_eeprom_ready());
i2c_start();
i2c_write((0xa0|(BYTE)(address>>7))&0xfe);
i2c_write(address);
i2c_write(data);
i2c_stop();
}


BYTE read_ext_eeprom(EEPROM_ADDRESS address) {
BYTE data;

while(!ext_eeprom_ready());
i2c_start();
i2c_write((0xa0|(BYTE)(address>>7))&0xfe);
i2c_write(address);
i2c_start();
i2c_write((0xa0|(BYTE)(address>>7))|1);
data=i2c_read(0);
i2c_stop();
return(data);
}


至於當slave可在

/PICC/Examples/ 中的 EX_SLAVE.C

也有當slave時的範例

發表於: 2009/2/18 16:57
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: I2C 玩不出來 請大仔解惑一下 Orz
#3
高級會員
高級會員


查看用戶資訊
我有CCS,我提供一下我的意見:
在CCS環境中使用I2C是需要宣告的,
#use i2c(master, sda=Device_SDA, scl=Device_SCL) // Configure Device as Master
請參考下列CCS線上說明.

I2C: These options lets the user configure the hardware to communicate with other devices over the I2C interface. All devices do not have hardware I2C. Software I2C can be used for these devices.



Relevant Functions:

i2c_start() Issues a start command when in the I2C master mode.

i2c_write(data) Sends a single byte over the I2C interface.

i2c_read() Reads a byte over the I2C interface.

i2c_stop() Issues a stop command when in the I2C master mode.

i2c_poll() Returns a TRUE if the hardware has received a byte in the buffer.



Relevant Preprocessor:

#use i2c Configures the device as a Master or a Slave and assigns the SDA and

SCL pins used for the interface.



Relevant Interrupts:

#INT_SSP I2C or SPI activity

#INT_BUSCOL Bus Collision

#INT_I2C I2C Interrupt (Only on 14000)

#INT_BUSCOL2 Bus Collision (Only supported on some PIC18's)

#INT_SSP2 I2C or SPI activity (Only supported on some PIC18's)



Relevant Include Files:

None, all functions built-in



Relevant getenv() Parameters:

I2C_SLAVE Returns a 1 if the device has I2C slave H/W

I2C_MASTER Returns a 1 if the device has a I2C master H/W



Example Code:

#define Device_SDA PIN_C3 // Pin defines

#define Device_SLC PIN_C4

#use i2c(master, sda=Device_SDA, scl=Device_SCL) // Configure Device as Master

..

..

BYTE data; // Data to be transmitted



i2c_start(); // Issues a start command when in the I2C master mode.

i2c_write(data); // Sends a single byte over the I2C interface.

i2c_stop(); //Issues a stop command when in the I2C master mode.

發表於: 2009/2/18 11:48
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: I2C 玩不出來 請大仔解惑一下 Orz
#2
版主
版主


查看用戶資訊
I2C Specification 在這裡,請下載研讀 :
http://www.nxp.com/acrobat_download/literature/9398/39340011.pdf

還有一點在完I2C 很重要的一點,如果可以的話建議先玩 Master 的部份,Slave 端就先找一個 24LC04 之類的 EEPROM 來做測試,等到 Master 好了以後再寫 Slave 端,這樣比較好界定問題是出在哪裡。

所以也要看一下 24LC04 的 I2C 通訊規範 :
http://www.microchip.com/wwwproducts/ ... es.aspx?dDocName=en010812

CCS 的 compiler 並不熟析,這裡有 C18 的範例可供參考。W401 C18 教育訓練裡的第六章有寫 I2C Master Mode access the 24LC04 的程式。

http://www.microchip.com.tw/modules/w ... glefile.php?cid=4&lid=236

這裡也有用 PIC18F452 寫的 I2C Master/Slave 的通訊程式:
http://www.microchip.com.tw/modules/w ... glefile.php?cid=12&lid=72

發表於: 2009/2/18 11:07
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


I2C 玩不出來 請大仔解惑一下 Orz
#1
高級會員
高級會員


查看用戶資訊
請教 I2C 的傳輸寫法
我參考了許多I2C的資料,它是寫說 ~
1. 先發出一個Start開始訊號
2. Master 發出一個8bit的訊息,其中前7bit定義一個元件的位址,第8個bit宣告Master是進行讀取或輸出
3. 受控端接收到這個位址的訊息發出一個Ack確認信號。
4. 接收資料或傳送資料
5. Master發出Stop做結束

所以我就如法泡製了一番 ( 底下是CCS所提供的範例 )
ivoid i2c_send_data()
{
i2c_start(); // Start condition
i2c_寫或獨的指令 //這裡應該插要讀要寫的指令吧 ?

接著才是以下的Device address
i2c_write(0xa0); // Device address 這裡就直接寫 Slave 的 Address了
0xa0 = 1010 0000  Lsb 是0所以是寫 ?

i2c_write(data); //接著才是寫資料出去
i2c_stop(); // Stop condition
}

我的疑問是對CCS的範例有所疑問 ~~ 試不出來…不知道哪裡出錯
有用過的大仔能替我解答嗎 ?
我是把 4620 透過 I2C 傳資料給 452 由452的UART傳出來這樣 ~
4620 作Master 452作Slave這樣 ~ 在CCS精靈設定裏面我已經將452的address
設定為 0xa0 因此Slave的address 就應該是 0xa0,只是Master這邊的i2c指令下
法有點模糊了,就像我上面code說的一樣,Master 寫或讀 Slave 的指令應該是下
在address 之前吧 ? 但是範例好像不是 ~
I2c_write的範例如下
{
i2c_start(); // Start condition
i2c_write(0xa0);// Device address  先寫slave的位址,那之前的讀或寫LSB呢 ?
i2c_write(cmd);// Low byte of command  這幹麻的 ?
i2c_write(cmd>>8);// High byte of command  這幹麻的 ?
i2c_stop(); // Stop condition
}


I2c_read的範例如下
{
i2c_start();
i2c_write(0xa1); //這裡就直接0xa1 = 1010 0001  LSB是1是讀這我可以接受
data1 = i2c_read();
i2c_stop();
}

Attach file:



jpg  (0.00 KB)


發表於: 2009/2/18 10:48
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... ]

教育訓練中心

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