?? bq_smbus.lst
字號:
1: /* I2C test program that writes & reads data to an I2C EEPROM device. */
2: /*
3: 此程序用于SMBUS通訊測試,配置可參考I2C.H
4: 軟件I2C通訊,使用RA1==>SDA,RA0==>SCL
5: 單片機使用PIC16F716
6: 晶振4M
7:
8: */
9:
10: #include <pic.h>
11: #include "delay.h"
12: #include "i2c.h"
13: #include "delay.c"
14: #include "i2c.c"
15: #include "i2c_2.c"
16:
17: #define ROM1 0x16 /* smbus */
18: #define ROM 0xA0 /* I2C EEPROM */
19: #define XTAL_FREQ 4MHZ
20: #define I2C_MODULE 0
21:
22:
23:
24:
25: uchar code1[]={0x06,0x06,0x5a,0xff,0x80,0x00,0x00,0x00,0xff};
26: uchar code2[]={0x5a,0xff,0x5a,0xff};
27: uchar code3[]={0x80,0x00,0x5a,0xff};
28: uchar code4[]={0x00,0x00,0x5a,0xff};
29:
30: void flashled(void) { /* flash a led on RB0 */
31: while(1) {
32: RB0 = 1;
33: DelayMs(200);
34: RB0 = 0;
35: DelayMs(200);
36: }
37: }
38:
39:
40: void WriteByte(unsigned char addr, unsigned char byte) {
41: i2c_WriteTo(ROM);
42: if (i2c_PutByte(addr)==I2C_ERROR)
43: flashled();
44: if (i2c_PutByte(byte)==I2C_ERROR)
45: flashled();
46: }
47:
48:
49: /*================================================================
50: 讀取BQ寄存器里面數據函數-word
51: 函數原型: unsigned int ReadByte_smbus(unsigned char addr),讀取指定地址BQ
52: 器件里數據。器件的ROM1碼已經設定
53: 返回讀取的數據表示操作成功,否則操作有誤。
54: =================================================================*/
55: unsigned int ReadByte_smbus(unsigned char addr) {
56: unsigned int data=0;
57: unsigned char data1,data2;
58: i2c_WriteTo(ROM1); //i2c_Open((address), I2C_read); i2c_read=0;
59: if (i2c_PutByte(addr)==I2C_ERROR) //send adress and sendack
60: flashled();
61: i2c_ReadFrom(ROM1); //i2c_Open((address), I2C_WRITE) i2c_write=1;
62: data1=i2c_GetByte(I2C_MORE); //i2c_more==>true sendack: more byte to send
63: data2=i2c_GetByte(I2C_LAST); //i2c_last==>false nosendack:no more byte to send
64: data|=data1;
65: data<<=8;
66: data|=data2;
67: return(data);
68: }
69:
70: /*================================================================
71: 寫BQ寄存器里面數據函數-
72: 函數原型
73: =================================================================*/
74: void write_word_smbus(unsigned char addr,unsigned char datal,unsigned char datah) {
75: unsigned int data=0;
76: unsigned char data1,data2;
77: i2c_WriteTo(ROM1); //i2c_Open((address), I2C_WRITE) i2c_write=1;
78: if (i2c_PutByte(addr)==I2C_ERROR) //send adress and sendack
79: flashled();
80: if (i2c_PutByte(datal)==I2C_ERROR) //send adress and sendack
81: flashled();
82: if (i2c_PutByte(datah)==I2C_ERROR) //send adress and sendack
83: flashled();
84: i2c_Stop();
85:
86: }
87: /*================================================================
88: 寫i2c寄存器里面數據函數-
89: 函數原型
90: =================================================================*/
91: void write_word_i2c(unsigned char addr,unsigned char datal,unsigned char datah) {
92: unsigned int data=0;
93: unsigned char data1,data2;
94: i2c_WriteTo(ROM); //i2c_Open((address), I2C_WRITE) i2c_write=1;
95: if (i2c_PutByte(addr)==I2C_ERROR) //send adress and sendack
96: flashled();
97: if (i2c_PutByte(datal)==I2C_ERROR) //send adress and sendack
98: flashled();
99: if (i2c_PutByte(datah)==I2C_ERROR) //send adress and sendack
100: flashled();
101: i2c_Stop();
102:
103: }
104: ////////********************************//////////////
105: int ReadWord(uchar addr){
106: uchar data1,data2;
107: int data;
108:
109: start_i2c();
110: send_byte(0x16); //發送器件地址,即DEVICE ADDRESS。
111: if (ack==0) i2c_error(); //如果24LC02無應答。則進入I2C ERROR錯誤指示。
112: send_byte(addr); //發送字地址,即WORD ADDRESS。D口顯示數組。
113: if (ack==0) i2c_error();
114: start_i2c(); //重新啟動總線。
115: send_byte(0x17); //發送讀命令和器件地址DEVICE ADDRESS。
116: if (ack==0) i2c_error();
117: data2=receive_byte();
118: i2c_SendAcknowledge(1);
119: data1=receive_byte();
120: stop_i2c();
121: //data<<=8;
122: //data|=data1;
123: return(data1);
124: }
125:
126: //////////////////////////////////////////////
127: static int remain,remain1;
128:
129: void main(void) {
130: unsigned char count,val;
131: //const unsigned char *add;
132: //add=0x20;
133: //TRISB=0; /* use a led on RB0 - set as output */
134: //PORTB=0;
135: //RB0=0;
136: PORTA=0;
137: ADCON1=0X06;
138: TRISA=0x0; /* use a led on RB0 - set as output */
139: RA2=0;
140: remain=0x0;
141:
142: /* initialize i2c */
143: /*
144: #ifdef I2C_MODULE
145: //SSPMode(MASTER_MODE);
146: //SSPEN = 1;
147: CKP = 1;
148: #else
149: SCL_DIR = I2C_OUTPUT;
150: SDA_DIR = I2C_OUTPUT;
151: SDA = 0;
152: SCL = 0;
153: #endif
154: */
155: SCL_DIR = I2C_OUTPUT;
156: SDA_DIR = I2C_OUTPUT;
157: SDA = 0;
158: SCL = 0;
159:
160: while(1) {
161:
162: RA2=0;
163: remain=0;
164: remain1=0;
165: count=0x1;
166:
167: DelayMs(200);
168:
169: remain1=ReadByte_smbus(0x02); //測試OK,能正確讀取BQ器件的數據
170:
171: DelayMs(20);
172:
173: // I_send_word(0x16,0x4f,0x5a,0xff);
174: // I_send_word(0x16,0x4f,0x80,0x00);
175: // I_send_word(0x16,0x4f,0x00,0x00);
176: // I_send_word(0x16,0x00,0x06,0x06);
177: // I_send_str(0x16,0x0,code1,2);
178:
179: write_word_smbus(0x0,0x06,0x06);
180: DelayMs(10);
181: DelayMs(200);
182: DelayMs(200);
183: DelayMs(200);
184: DelayMs(200);
185: DelayMs(200);
186: DelayMs(200);
187: write_word_i2c(0x02,0x6d,0x29);
188: write_word_i2c(0x03,0x75,0xaf);
189: DelayMs(200);
190: DelayMs(200);
191: DelayMs(200);
192: DelayMs(200);
193: DelayMs(200);
194: DelayMs(200);
195: DelayMs(20);
196: remain1=ReadByte_smbus(0x02); //測試OK,能正確讀取BQ器件的數據
197:
198: remain1=ReadByte_EE(0x4); //測試OK,能正確讀取BQ器件的數據
199: remain1=ReadByte_EE(0x5);
200: remain1=ReadByte_EE(0x6);
201: remain1=ReadByte_EE(0x7);
202: remain1=ReadByte_EE(0x8);
203:
204: I_send_str(0xa0,0x0,code,5); //測試OK,能正確發送數據給BQ器件
205: DelayMs(20);
206: I_send_word(0x16,0x4f,0x5a,0xff);
207: I_send_word(0x16,0x4f,0x80,0x00);
208: I_send_word(0x16,0x4f,0x00,0x00);
209: DelayMs(20);
210: remain1=ReadByte_smbus(0x15); //OK
211: remain1=ReadByte_smbus(0x15); //OK
212: DelayMs(200);
213: /*===================以下直接寫EEPROM
214: RA2=1;
215: I_send_str(0xa0,0x00,code,5); //OK
216: remain=0;
217: remain1=0;
218: count=0x1;
219: DelayMs(200);
220: remain1=ReadByte_EE(0x4); //OK
221: DelayMs(200);
222: ==================================*/
223:
224:
225: }
226: }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -