明確這樣幾條,就能正確理解IIC總線的原理:
1. scl信號永遠是由主機產生的。
2. 主機和從機都能作為發送端,也都能作為接收端
3. 主機在寫數據的時候主機是發送端,從機是接收端,主機在讀數據的時候從機是發送端,主機是接收端。
4. 在頁寫數據的時候會以8或16作為頁寫數據的個數,如果寫入的數據超過這個數值那么先前寫入的數據會被覆蓋掉。
5. 在連續讀的模式下數據的地址會一直增加下去,沒有頁讀的8或16的限制。
6. 在SCL在高電平的時候DAT是不能變化的,否則這個變化會作為開始或結束的信號。根據這一條就可以斷定,DAT數據的變化是在SCL為低電平的時候,也就能說明在向24C02寫數據的時候寫完8個數據之后的 那個下降沿會觸發第9個應答位。這個應答位必須在SCL變為高電平之前就穩定下來,否則會被視為起始位或是結束位。同位的道理在讀數據的時候,當24C02發完一個8位的數據之后,在之后的那個SCL的下降沿,單片機必須先將DAT變為高電然后才能將SCL拉為高電平,否則也會出現上面的問題也就是會被24C02視為起始位或是結束位。
7. 24C02芯片有一個TWR參數,就是兩次寫操作的最小時間間隔,這個時間因不同的芯片生產廠商有不同,一般兩次寫的時間間隔設定為10ms是足夠的。
8. 關于24C02的頁寫入模式,我察看了一個24C02的數據手冊對頁寫模式的摘寫下來:Page Write. For the Page Writemode, the MODEpin must be at VIL. The Page Write mode allows upto 8 bytes to bewritten in a single write cycle,providedthat they are all located in the same ’row’in the memory: that is the 5 mostsignificant mem-ory address bits (A7-A3) are the same. The mastersends from oneup to 8 bytes of data, which areeach acknowledged by the memory. After eachbyteis transfered, the internal byte address counter(3 least significant bits only)is incremented. Thetransfer is terminated by the master generating aSTOPcondition. Care must be taken to avoid ad-dress counter ’roll-over’ which couldresult in databeing overwritten. Note that, for any write mode,the generationby the master of the STOP conditionstarts the internal memory program cycle.All inputsare disabled until the completion of this cycle andthe memory willnot respond to any request.
這里說明頁寫模式下最多只能寫入8個字節,如果寫得超過8個字節,前面的數據就會被覆蓋掉,這里就要注意寫入24C02的基地址了,只能是8的整數倍了,而且也只能最多寫入8個數據,這個在編寫頁寫函數時要特注意。
當然對于不是24C02的芯片,比如24C04,或是容量更大的芯片,這個數據就不是8個了,有可能是16個,或是32個。
9. 關于24C02的順序讀的操作,是沒有頁的概念的,下面是摘于24C02的數據手冊:
Sequential Read. This mode can be initiated witheither a Current Address Read or a Random Ad-dress Read. However, in this case the masterDOES acknowledge the data byte output and thememory continues to output the next byte in se-quence. To terminate the stream of bytes, themaster must NOT acknowledge the last byte out-put, but MUST generate a STOP condition. Theoutput data is from consecutive byte addresses,with the internal byte address counter automat-ically incremented after each byte output. After acount of the last memory address, the addresscounter will ’roll- over’ and the memory will continueto output data.
10. 應答是在SCL在低電平的時候將DAT拉為低電平,非應答是在SCL在低電平的時候將DAT拉為高電平,這是沒有任何問題的,只是要區分,應答是主機發出的還是從機發出的,因為應答的一定是接收方,主機和從機都可能是接收方,但是這里注意非應答數據一定是主機發出的,從這一點可以看出主機和從機在邏輯上是不對稱的。
|