Browsing this Thread:
1 Anonymous Users
|
Re: 想請問I2C應用範例
|
|
新會員
Joined
: 2005/8/3 23:09 最後登入時間
: 2005/8/4 18:12
Group:
註冊會員
Level : 1 HP : 0 / 5 MP : 1 / 299 EXP : 21
|
真是太厲害了.... 小妹我看到程式真是嚇到 感激不盡阿.... 對了!!請問這是c16還是c18的程式?? 目前我這邊有18F452的現成晶片可以用 可以直接用ㄇ
發表於: 2005/8/4 17:08
|
|
|
Re: 想請問I2C應用範例
|
|
高級會員
Joined
: 2004/6/7 22:42 最後登入時間
: 2018/10/18 9:39
Group:
註冊會員
Level : 10 HP : 0 / 228 MP : 41 / 13294 EXP : 12
|
void IIC_INIT(void) { output_high(P_SDA); output_high(P_SCL); } void IIC_WAIT(void) { delay_us(10); }
void IIC_START(void) { output_high(P_SDA); //P_SDA = 1; output_high(P_SCL); //P_SCL = 1; IIC_WAIT(); output_low(P_SDA); //P_SDA = 0; IIC_WAIT(); output_low(P_SCL); //P_SCL = 0;
} //***** VERSION : 1.0 ***** // /------------ // SCL -----/ // // /---------- // SDA-------/ // // SCL先拉HI SDA 再跟著拉HI // //***********************************
void IIC_STOP(void) { output_low(P_SDA); //P_SDA = 0; IIC_WAIT(); output_high(P_SCL); //P_SCL = 1; IIC_WAIT(); output_high(P_SDA); //P_SDA = 1; } int8 IIC_SEND_BYTE(int8 databyte) { int8 i; int8 ack; for(i = 0;i<8;i++) { if ( databyte & 0x80) output_high(P_SDA); else output_low(P_SDA); databyte <<=1; // IIC_WAIT(); output_high(P_SCL); IIC_WAIT(); output_low(P_SCL); // IIC_WAIT();
} //in 8051 mcu is setting input mode output_high(P_SDA); input(P_SDA); IIC_WAIT(); output_high(P_SCL); IIC_WAIT(); IIC_WAIT(); IIC_WAIT(); ack = input(P_SDA); output_low(P_SCL); IIC_WAIT();
return ack; } int8 IIC_RECEIVE_BYTE(void) { int8 i; int8 databyte =0; input(P_SDA); for (i=0;i<8;i++) { output_high(P_SCL); IIC_WAIT(); databyte <<=1; if (input(P_SDA)) databyte |= 0x01; output_low(P_SCL); IIC_WAIT();
} return databyte; }
void SEND_ACK(int8 ack) { if (ack==0x01) output_high(P_SDA); else output_low(P_SDA); output_high(P_SCL); IIC_WAIT(); output_low(P_SCL);
} void IIC_BYTEWRITE(int8 device,int8 address,int8 databyte1,int8 databyte2) { int8 i; int8 ack; for (i=0;i<10;i++) { IIC_START(); ack = IIC_SEND_BYTE(device); if (ack==1) { IIC_STOP(); continue; } ack = IIC_SEND_BYTE(address); if (ack==1) { IIC_STOP(); continue; } ack = IIC_SEND_BYTE(databyte1); if (ack==1) { IIC_STOP(); continue; } ack = IIC_SEND_BYTE(databyte2); if (ack==1) { IIC_STOP(); continue; }
IIC_STOP(); if (ack==0) break;
} delay_us(2); } void IIC_READ_IOPORT(int8 device,int8 command) { int8 i; int8 ack; // F_SENDTIMEOUT = 1; for (i=0;i<10;i++) { IIC_START(); ack = IIC_SEND_BYTE(device); if (ack==1) { IIC_STOP(); continue; } ack = IIC_SEND_BYTE(command); if (ack==1) { IIC_STOP(); continue; } IIC_STOP(); if (ack==0) break;
}
delay_us(2); PORTDATA = IIC_RECEIVE_BYTE();
} void READ_IO(int8 address,int8 command,int8 address1) { int8 i; int8 ack; /* i2c_start(); // Generate START condition i2c_write(AddressW); // Transmit ADDRESS with WRITE command i2c_write(Command); // Transmit COMMAND byte i2c_repStart(); // Generate a REPEATED-START condition i2c_write(AddressR); // Transmit ADDRESS with READ command MCP_LSB=i2c_read(1); // Receive first DATA byte (LSB) and acknowledge MCP_MSB=i2c_read(0); // Receive second DATA byte (MSB) and don't acknowledge i2c_stop(); // Generate a STOP condition */ // F_SENDTIMEOUT = 1; for (i=0;i<10;i++) { IIC_START(); ack = IIC_SEND_BYTE(address); if (ack==1) { IIC_STOP(); continue; } ack = IIC_SEND_BYTE(command); if (ack==1) { IIC_STOP(); continue; } if (ack==0) break; } delay_us(14); for (i=0;i<10;i++) { IIC_START(); ack = IIC_SEND_BYTE(address1); if (ack==1) { IIC_STOP(); continue; } if (ack==0) break;
} BUFFER1 = IIC_RECEIVE_BYTE(); delay_us(20); input(P_SDA); IIC_WAIT(); output_high(P_SCL); IIC_WAIT(); IIC_WAIT(); IIC_WAIT(); if (input(P_SDA)) { IIC_STOP(); } output_low(P_SCL); IIC_WAIT(); BUFFER2 = IIC_RECEIVE_BYTE(); input(P_SDA); IIC_WAIT(); output_high(P_SCL); IIC_WAIT(); IIC_WAIT(); IIC_WAIT(); if (input(P_SDA)) { IIC_STOP(); } output_low(P_SCL); IIC_WAIT(); IIC_STOP(); }
void main() { delay_ms(100); EXPANDER_IO_INITIAL();
while (TRUE) { if (!input(P_INTERRUPT)) { debug_pin(); READ_IO(WRITECOMMAND_TO_IC,ACCES_GP1,READCOMMAND_FROM_IC);
while(!input(P_INTERRUPT)); }else { IIC_BYTEWRITE(WRITECOMMAND_TO_IC,ACCES_OLAT0,0X7F,0xff); //寫初始直 delay_ms(250); IIC_BYTEWRITE(WRITECOMMAND_TO_IC,ACCES_OLAT0,0XBF,0xff); //寫初始直 delay_ms(250); }
} }
發表於: 2005/8/4 15:18
|
|
|
Re: 想請問I2C應用範例
|
|
新會員
Joined
: 2005/8/3 23:09 最後登入時間
: 2005/8/4 18:12
Group:
註冊會員
Level : 1 HP : 0 / 5 MP : 1 / 299 EXP : 21
|
不好意思補充一下我的問題 我有APPOO1工作版以及ICD2
我是想用PIC當master做I2C串列通訊格式 發出及接收SDA以及SCL的命令 目前看到16F873有I2C的功能 請問18F系列有出I2C功能的晶片嗎
還有是否有可能拿到C18或C16的I2C控制範例程式
還有 請問24LCXX與25LCXX,,93c86 這些東西是用I2C格式做溝通的外接記憶體ㄇ 因為我是新手 看不太懂 希望可以得到解答 感激不盡阿
發表於: 2005/8/3 23:46
|
|
|
想請問I2C應用範例
|
|
新會員
Joined
: 2005/8/3 23:09 最後登入時間
: 2005/8/4 18:12
Group:
註冊會員
Level : 1 HP : 0 / 5 MP : 1 / 299 EXP : 21
|
我有在範例程式那邊看到I2C的應用範例 可是因為是組語寫成的 請問有可能拿到c code 控制I2C的範例程式ㄇ 還有那是針對哪一個晶片所寫的
有I2C電路介面的PIC晶片目前台灣買的到ㄇ
發表於: 2005/8/3 23:23
|
|
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.