📄 ad5258.lst
字号:
C51 COMPILER V8.02 AD5258 01/11/2007 14:49:21 PAGE 1
C51 COMPILER V8.02, COMPILATION OF MODULE AD5258
OBJECT MODULE PLACED IN Ad5258.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE Ad5258.c ROM(SMALL) BROWSE DEBUG OBJECTEXTEND
line level source
1 /*------------------------------------------------------------------------------------------------------
2 FileName: Ad5258.c
3
4 -------------------------------------------------------------------------------------------------------*/
5 #include "AT89X51.H"
6 #include "Ad5258.h"
7
8
9
10 //I2Caddress IC4, IC5, IC7, IC9
11 code uchar I2C_addr_wr[ADDR_NUM] = {0x30, 0x98, 0x34, 0x9c};
12 code uchar I2C_addr_rd[ADDR_NUM] = {0x31, 0x99, 0x35, 0x9d};
13 code uchar BIT_TEST[8]={0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
14
15 //I2C bus
16 sbit I2C_SDA = P0^1;
17 sbit I2C_SCL = P0^0;
18 bit I2C_ack;
19
20 //I2C bus
21 void I2CStart(); //I2C start
22 void I2CStop(); //I2C stop
23 void I2CSendByte(uchar str); //I2C send a byte to slave
24 uchar I2CRcvByte(); //I2C recieve a byte from slave
25
26 /*-----------------------------------------------------------------------------------------------------*/
27 //I2C bus--------------------------------------------------------
28 void ConfigAd5258(uchar uIndex, uchar uVal)
29 {
30 1 I2CStart(); //start
31 1 I2CSendByte(I2C_addr_wr[uIndex]); //address
32 1 I2CSendByte(0x00); //instruction
33 1 I2CSendByte(uVal); //data
34 1 I2CStop(); //stop
35 1 }
36
37 //Ad5258 active or deactive write protect
38 void ActiveAd5258(uchar uIndex, uchar bActive)
39 {
40 1 if(bActive == 1)
41 1 {
42 2 I2CStart(); //start
43 2 I2CSendByte(I2C_addr_wr[uIndex]); //address
44 2 I2CSendByte(0x40); //instruction
45 2 I2CSendByte(0x00); //data
46 2 I2CStop();
47 2 }
48 1 else
49 1 {
50 2 I2CStart(); //start
51 2 I2CSendByte(I2C_addr_wr[uIndex]); //address
52 2 I2CSendByte(0x40); //instruction
53 2 I2CSendByte(0x01); //data
54 2 I2CStop();
55 2 }
C51 COMPILER V8.02 AD5258 01/11/2007 14:49:21 PAGE 2
56 1 }
57
58 //write data from ram to eeprom
59 void WrData2EEprom(uchar uIndex)
60 {
61 1 I2CStart(); //start
62 1 I2CSendByte(I2C_addr_wr[uIndex]); //address
63 1 I2CSendByte(0xC0); //instruction
64 1 I2CStop();
65 1 }
66
67 //read data from eeprom to ram
68 void RdDataFromEEprom(uchar uIndex)
69 {
70 1 I2CStart(); //start
71 1 I2CSendByte(I2C_addr_wr[uIndex]); //address
72 1 I2CSendByte(0xA0); //instruction
73 1 I2CStop();
74 1 }
75
76 //read data from ram
77 uchar RdDataFromRam(uchar uIndex)
78 {
79 1 uchar val;
80 1 I2CStart(); //start
81 1 I2CSendByte(I2C_addr_wr[uIndex]); //address
82 1 I2CSendByte(0x00); //instruction
83 1 I2CStart(); //start
84 1 I2CSendByte(I2C_addr_rd[uIndex]); //address
85 1 val = I2CRcvByte();
86 1 I2CStop();
87 1 return val;
88 1 }
89
90 //I2C start
91 void I2CStart()
92 {
93 1 I2C_SDA = 1;
94 1 NOP;
95 1 I2C_SCL = 1;
96 1 NOP;
97 1 I2C_SDA = 0; //high to low
98 1 NOP; //interval time > 0.6us
99 1 I2C_SCL = 0; //make scl low to prepare to transfer data
100 1 }
101
102 //I2C stop
103 void I2CStop()
104 {
105 1 I2C_SDA = 0; //prepare
106 1 NOP;
107 1 I2C_SCL = 1; //make scl high
108 1 NOP; //wait
109 1 I2C_SDA = 1; //make SDA low to high
110 1 NOP; //wait
111 1 I2C_SCL = 0; //make scl high
112 1 }
113
114 //I2C send a byte to slave
115 void I2CSendByte(uchar sch)
116 {
117 1 uchar BitCnt;
C51 COMPILER V8.02 AD5258 01/11/2007 14:49:21 PAGE 3
118 1 for(BitCnt = 0; BitCnt < 8; BitCnt ++) //8bit to transfer
119 1 {
120 2 if(sch&BIT_TEST[BitCnt])
121 2 I2C_SDA = 1; //judge
122 2 else
123 2 I2C_SDA = 0;
124 2 NOP;
125 2 I2C_SCL = 1; //clock
126 2 NOPN; //
127 2 I2C_SCL = 0;
128 2 }
129 1 NOP;
130 1 I2C_SDA = 1; //make SDA high
131 1 NOP;
132 1 I2C_SCL = 1;
133 1 if(I2C_SDA == 1)
134 1 I2C_ack = 0;
135 1 else
136 1 I2C_ack = 1; //slave acknowledge
137 1 I2C_SCL = 0;
138 1 }
139
140 //I2C recieve a byte from slave
141 uchar I2CRcvByte()
142 {
143 1 uchar BitCnt, retc = 0;
144 1 I2C_SDA = 1; //
145 1 for(BitCnt = 0; BitCnt < 8; BitCnt ++)
146 1 {
147 2 I2C_SCL = 1; //
148 2 NOPN; //wait
149 2 I2C_SCL = 0; //high to low
150 2 NOP;
151 2 if(I2C_SDA == 1)
152 2 retc = retc + BIT_TEST[BitCnt]; //read data
153 2
154 2 }
155 1 NOP;
156 1 I2C_SDA = 1;
157 1 NOP;
158 1 return(retc);
159 1 }
160
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 239 ----
CONSTANT SIZE = 16 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = ---- 7
IDATA SIZE = ---- ----
BIT SIZE = 1 ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -