?? i2c_2.lst
字號:
1:
2: /*******************************************************************
3: 一、程序說明:
4: 1, 24LC02器件地址是1010000R/W. 同時也一部分為SMBUS 0x16
5: 2, 數組寫入24LC02采取頁寫方式.
6: 3, 數組code從24LC02讀出時采取自由讀方式.
7: 4, 采用4.00M晶體。
8: 5,采用軟件I2C。
9:
10: 二、硬件連接:
11: 1, SDA_DIRE------->23 pin.(當然你可以任意選擇腳位)
12: 2, SCL_DIRE------->18 Pin.(當然你可以任意選擇腳位)
13: 3, PORTD----->外接8個LED,顯示讀出的數據,在這里,讀出的剛好是一個閃動的流水燈狀態。
14:
15: *******************************************************************/
16: #include "pic.h"
17:
18: #define uchar unsigned char
19: #define nop() asm("nop")
20: //#define SCL_DIRE TRISC3 //PIC mcu專用I2C
21: //#define SDA_DIRE TRISC4
22: //#define SCL RC3
23: //#define SDA_E RC4
24:
25: //#define SCL RA0
26: //#define SDA RA1
27: //#define SCL_DIR TRISA0
28: //#define SDA_DIR TRISA1
29:
30: void start_i2c();
31: void stop_i2c();
32: void send_byte(uchar c);
33: uchar receive_byte();
34: void I_send_str(uchar sla,uchar suba,uchar *s,uchar no);
35: void I_send_word(uchar sla,uchar suba,uchar datal,uchar datah);
36: void delay_250ms();
37: void i2c_error ();
38: int ReadByte_EE(uchar addr);
39:
40:
41: uchar code[]={0x34,0x64,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff};
42: uchar no,ack,c,data;
43:
44:
45: /*******************************************************************
46: 起動總線函數
47: 函數原型: void start_i2c();
48: Function: start on the I2C bus
49: *******************************************************************/
50: void start_i2c()
51: {
52: SDA_DIR=1; //發送啟始條件的數據信號
53: nop();
54: SCL_DIR=1;
55: nop();nop();nop();nop();nop(); //24LC02要求建立時間大于4,7S
56: SDA_DIR=0; //發送起始信號
57: nop();nop();nop();nop();nop();
58: SCL_DIR=0; //鉗住I2C總線,準備發送數據或接收數據
59: nop();nop();
60: }
61:
62:
63: /*******************************************************************
64: 停止總線函數
65: 函數原型: void stop_i2c();
66: Function: stop the I2C bus
67: *******************************************************************/
68: void stop_i2c()
69: {
70:
71: SDA_DIR=0; //發送結束條件的數據信號
72: nop();
73: SCL_DIR=1;
74: nop();nop();nop();nop();nop();
75: SDA_DIR=1;
76: nop();nop();nop();nop();
77: }
78:
79: /*=================================================================
80: 字節數據傳送函數
81: 函數原型: void send_byte(uchar c);
82: Function: 將數據C發送出去,可以是地址,也可以是數據,發完后等待回應,并對此狀態
83: 位進行操作(不應答或非應答都使ack=0 ),發送數據正常,ack=1;ack=0
84: 表示被控器無應答或損壞。
85: ==================================================================*/
86: void send_byte(uchar c)
87: {
88: uchar bit_count;
89: for (bit_count=0;bit_count<8;bit_count++)
90: {
91: if ((c<<bit_count)&0x80) {SDA_DIR=1;}
92: else {SDA_DIR=0;}
93: nop();
94: SCL_DIR=1;
95: nop();nop();nop();nop();nop();
96: SCL_DIR=0;
97: }
98: nop();nop();
99: SDA_DIR=1;
100: nop();nop();
101: SCL_DIR=1;
102: nop();nop();nop();
103: if (SDA==1) ack=0;
104: else ack=1; //用ASK=1為有應答信號
105: SCL_DIR=0;
106: nop();nop();
107: }
108:
109: /*==================================================================
110: 字節數據接收函數
111: 函數原型:uchar receive_byte();
112: FUNCTION: 用來接收從器件傳來的數據,并判斷總線錯誤(不發應答信號),
113: 發完后請用應答函數。
114: ===================================================================*/
115: uchar receive_byte()
116: {
117: uchar retc,bit_count;
118: retc=0;
119: SDA_DIR=1;
120: for (bit_count=0;bit_count<8;bit_count++)
121: {
122: nop();
123: SCL_DIR=0;
124: nop();nop();nop();nop();nop();
125: SCL_DIR=1;
126: nop();nop();
127: retc=retc<<1;
128: if (SDA==1) retc=retc+1;
129: nop();nop();
130: }
131: SCL_DIR=0;
132: nop();nop();
133: return (retc);
134: }
135:
136: ////////********************************//////////////
137: int ReadByte_EE(uchar addr){
138: uchar data1;
139: start_i2c();
140: send_byte(0xA0); //發送器件地址,即DEVICE ADDRESS。
141: if (ack==0) i2c_error(); //如果24LC02無應答。則進入I2C ERROR錯誤指示。
142: send_byte(addr); //發送字地址,即WORD ADDRESS。D口顯示數組。
143: if (ack==0) i2c_error();
144: start_i2c(); //重新啟動總線。
145: send_byte(0xA1); //發送讀命令和器件地址DEVICE ADDRESS。
146: if (ack==0) i2c_error();
147: data1=receive_byte();
148: stop_i2c();
149: return(data1);
150: }
151:
152:
153: /*================================================================
154: 向有子地址器件發送多字節數據函數
155: 函數原型: bit I_send_str(uchar sla,uchar suba,uchar *s,uchar no);
156: Function: 從啟動總線到發送地址,數據,結束總線的全過程,從器件地址sla。如果
157: 返回1表示操作成功,否則操作有誤。
158: =================================================================*/
159: void I_send_str(uchar sla,uchar suba,uchar *s,uchar no)
160: {
161: uchar i;
162: start_i2c();
163: send_byte(sla);
164: if (ack==0) i2c_error();
165: send_byte(suba);
166: if (ack==0) i2c_error();
167: for (i=0;i<no;i++)
168: {
169: send_byte(*s);
170: if (ack==0) i2c_error();
171: s++;
172: }
173: stop_i2c();
174: // return(1);
175: }
176:
177: /*================================================================
178: 向有子地址器件發送兩字節數據函數
179: 函數原型: bit I_send_word(uchar sla,uchar suba,uchar datal,uchar datah);
180: Function: 從啟動總線到發送地址,數據,結束總線的全過程,從器件地址sla。如果
181: 返回1表示操作成功,否則操作有誤。
182: =================================================================*/
183: void I_send_word(uchar sla,uchar suba,uchar datal,uchar datah)
184: {
185: uchar i;
186: start_i2c();
187: send_byte(sla);
188: if (ack==0) i2c_error();
189: send_byte(suba);
190: if (ack==0) i2c_error();
191: send_byte(datal);
192: if (ack==0) i2c_error();
193: send_byte(datah);
194: if (ack==0) i2c_error();
195: stop_i2c();
196: // return(1);
197: }
198: /*****************************************************************
199: 延時函數
200: 函數原型: void delay_250ms();
201: FUNCTION: 延明250ms
202: *****************************************************************/
203: void delay_250ms()
204: {
205: unsigned int d=24999;
206: while (--d);
207: }
208:
209: /*****************************************************************
210: 總線錯誤函數
211: 函數原型: void i2c_error();
212: Function: 通過RD7閃動8次表示總線操作失敗一次報警。
213: *****************************************************************/
214: void i2c_error ()
215: {
216: uchar i;
217: for (i=0;i<8;i++)
218: {
219: RB7=0;
220: delay_250ms();
221: RB7=1;
222: delay_250ms();
223: }
224:
225: }
226: /**********END**************/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -