51模擬IIC-頁讀寫操作
51代碼頁讀寫IIC--模擬IIC
#include <reg52.h> #include <intrins.h> sbit SDA = P0^0; sbit SCL = P0^1; sbit LED = P2^0; unsigned char code table[] = {0x1c,0X3B,0X2C,0X2D,0X5A,0X5C,0XC5,0X5b}; void delayms(unsigned int t) { unsigned int i,j; for(i = t; i > 0; i --) for(j = 110; j > 0; j --); } void delay() { _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); } void start() { SCL = 1; SDA = 1; delay(); SDA = 0; delay(); SCL = 0; } void stop() { SDA = 0; delay(); SCL = 1; delay(); SDA = 1; delay(); } void ack() { SCL = 0; delay(); while(SDA); SCL = 1; delay(); SCL = 0; } void noack() { SCL = 0; SDA = 1; SCL = 1; delay(); SCL = 0; } void writebyte(unsigned char byte) { //MSB first unsigned char i = 0; SCL = 0; for(i = 0; i < 8; i ++) { byte <<= 1; SDA = CY; delay(); SCL = 1; delay(); SCL = 0; } // SDA = 1;//waiting for ack // delay(); } unsigned char readbyte() { unsigned char i = 0,val = 0; SCL = 0; for(i = 0; i < 8 ; i ++) { val <<= 1; delay(); SCL = 1; delay(); if(SDA) { val |= 0x01; } else { val |= 0x00; } SCL = 0; } SCL = 0;//release IIC bus return val; } void write_data(unsigned char add, unsigned char byte) { start(); writebyte(0xae); ack(); writebyte(add); ack(); writebyte(byte); ack(); stop(); } unsigned char read_data(unsigned char add) { unsigned char val = 0; start(); writebyte(0xae); ack(); writebyte(add); ack(); start(); writebyte(0xaf); ack(); val = readbyte(); noack(); stop(); return val; } void pagewrite(unsigned char add) { unsigned char i; start(); writebyte(0xae); ack(); writebyte(add); ack(); for(i = 0; i < 8; i ++) { writebyte(table[i]); ack(); } stop(); } unsigned char *pageread(unsigned char add) { unsigned char value[8],i; start(); writebyte(0xae); ack(); writebyte(add); ack(); start(); writebyte(0xaf); ack(); for(i = 0; i < 8; i ++) { value[i] = readbyte(); if(i == 7) { noack(); } else { SCL = 0; //主機來應答 delay(); SDA = 0; delay(); SCL = 1; delay(); SCL = 0; delay(); SDA = 1; delay(); } delay(); } stop(); return value; } void main() { unsigned char *TMP_VAL, Tmp = 0; /** write_data(0x01,0xAC); //TEST 1 delayms(500); Tmp = read_data(0x01); delayms(5); if(Tmp == 0xac) { P2 = Tmp; } **/ /** pagewrite(0X00); //TEST2 delayms(500); Tmp = read_data(0x07); delayms(5); if(Tmp == table[7]) { P2 = Tmp; } **/ pagewrite(0X00); delayms(600); TMP_VAL = pageread(0x00); delayms(500); if(*(TMP_VAL+1) == table[1]) { P2 = *(TMP_VAL+1); } while(1); }
注意以下要點
過程中出現只能寫入偶數情況,是Stop()函數的問題,在SCL上升沿時發生了SDA改變導致的,調整一下先后順序即可;
頁讀操作注意ack是主機拉低SDA發出的應答,和單字節讀寫不同;
如何接收函數返回的數組,以及如何寫這個返回數組的函數。
調試過程中有EE不拉低SDA應答的情況,后來排查是EE芯片壞了。
寫完數據不能立刻讀,要有稍長的延時才行。

浙公網安備 33010602011771號