📄 iic_comm.lst
字号:
C51 COMPILER V8.02 IIC_COMM 09/17/2008 10:45:02 PAGE 1
C51 COMPILER V8.02, COMPILATION OF MODULE IIC_COMM
OBJECT MODULE PLACED IN iic_comm.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\c51.exe iic_comm.c DB OE
line level source
1 //------------------------------------------------------------------------------------
2 //
3 // FILE NAME : iic_comm.c
4 // TARGET DEVICE : C8051F314
5 // CREATED ON : 07/14/06
6 // CREATED BY : Jzp
7 //
8 // Revision 1.0
9
10 // 功能描述:作为iic总线从机接受MAIN MCU的查询
11 // P0.0 -> SDA (SMBus)
12 // P0.1 -> SCL (SMBus)
13 //------------------------------------------------------------------------------------
14 #include <C8051F310.H>
15 #include "..\\inc\\define.h"
16 #include "..\\inc\\key_scan.h"
17 #include "..\\inc\\ow_comm.h"
18
19 // global constant
20 // 作为从器件的地址
21 #define SLA_ADD 0xF0 // Device addresses (7 bits, lsb is a don't care)
22 // 作为主器件时目标地址
23 #define MST_ADD 0xE0
24
25 // sfr定义
26 //sfr16 TMR3RL = 0x92; // Timer2 reload registers
27 //sfr16 TMR3 = 0x94; // Timer3 counter registers
28
29 //
30 #define WRITE 0x00 // SMBus WRITE command
31 #define READ 0x01 // SMBus READ command
32
33
34 // SMB0CN寄存器的高4位(MASTER/TXMODE/STA/STO),处理器通过该四位判断SMBUS总线当前状态。
35 // --MASTER
36 // 作为从机时状态标志
37 #define SMB_SRADD 0x20 // (SR) 接收到从地址 (也可能是总线竞争失败)
38 #define SMB_SRSTO 0x10 // (SR) 接受到停止位, or lost arbitration
39 #define SMB_SRDB 0x00 // (SR) 接受到数据, or lost arbitration
40 #define SMB_STDB 0x40 // (ST) 已发送完数据
41 #define SMB_STSTO 0x50 // (ST) 发送停止位
42 // 作为主机时状态标志
43 #define SMB_MTSTA 0xE0 // (MT) 开始传送
44 #define SMB_MTDB 0xC0 // (MT) 数据传送完
45 #define SMB_MRDB 0x80 // (MR) 接收到数据
46 // 初始化状态
47 #define SMB_MASTER 1
48 #define SMB_SLAVE 0
49
50
51 // 数据缓冲区长度
52 #define DATABUF_LEN 8
53
54 // GLOBAL VARIABLES
55 typedef struct{
C51 COMPILER V8.02 IIC_COMM 09/17/2008 10:45:02 PAGE 2
56 UCHAR info_id;
57 UCHAR info_len;
58 UCHAR checksum;
59 } FRAME_QUERY;
60
61 typedef struct{
62 UCHAR info_id;
63 UCHAR info_len;
64 UCHAR info_scancode[2];
65 UCHAR checksum;
66 } FRAME_RETURN_KEY;
67
68 typedef struct{
69 UCHAR info_id;
70 UCHAR info_len;
71 UCHAR info_batvol[2];
72 UCHAR checksum;
73 } FRAME_RETURN_BAT;
74
75 typedef union{
76 FRAME_QUERY stFQ;
77 FRAME_RETURN_KEY stFRK;
78 FRAME_RETURN_BAT stFRB;
79 } DATAUNION;
80
81 typedef union{
82 UCHAR databuf[DATABUF_LEN];
83 DATAUNION unDataUnion;
84 } DATA_BUFF;
85
86 DATA_BUFF unRcvDataBuf; // 接收缓冲区
87 DATA_BUFF unSndDataBuf; // 发送缓冲区
88 UCHAR SndDataLen; // 当前发送缓冲区中的数据长度
89 UCHAR RcvDataFlag; // iic接收数据标志 =1 有数据
90 UCHAR TARGET; // 发送目标地址
91 //UCHAR SMB_RW; // 总线读写状态 R/nW
92 UCHAR SMB_SINGLEBYTE_OUT; // Global holder for single byte writes
93 UCHAR* pSMB_DATA_OUT; // Global pointer for SMBus data.
94 // All transmit data is read from here
95 UCHAR* pSMB_DATA_IN; //
96 UCHAR SMB_DATA_LEN; // Global holder for number of bytes
97 // 全局位变量
98 bit SMBUS_BUSY = 0;
99 bit SMB_RW; // 总线读写状态 R/nW
100 bit SMB_SENDWORDADDR;
101 bit SMB_RANDOMREAD;
102 bit SMB_ACKPOLL;
103
104 #define FRK_INFO_ID 0x10
105 #define FRB_INFO_ID 0x20
106
107 // 函数原型定义
108 UCHAR ReturnScanCode(DATA_BUFF *RcvData);
109 UCHAR ReturnBatStatus(DATA_BUFF *RcvData);
110 void CalChksum(DATA_BUFF *SndData);
111 UCHAR CompChksum(DATA_BUFF *RcvData);
112 void DataProcess(DATA_BUFF *RcvData);
113 void IIC_ByteWrite( DATA_BUFF *SndData, unsigned char SndLocation);
114 void IIC_SndData(DATA_BUFF *SndData, UCHAR *SndLen);
115 void iic_init(UCHAR flag);
116 void iic_reset(void);
117 void Timer1_Init(void);
C51 COMPILER V8.02 IIC_COMM 09/17/2008 10:45:02 PAGE 3
118 void Timer3_Init(void);
119 void SMBUS_ISR(void);
120 void Timer3_ISR(void);
121
122
123
124 /************************************************************
125 -----TIMER3中断服务程序-----
126 *************************************************************/
127
128 void Timer3_ISR (void) interrupt 14
129 {
130 1 SMB0CN &= ~0x40; // Disable SMBus
131 1 SMB0CN |= 0x40; // Re-enable SMBus
132 1 // 禁止-〉使能总线,相当于总线复位
133 1 TMR3CN &= ~0x80; // Clear Timer3 interrupt-pending flag
134 1 }
135
136 /************************************************************
137 -----SMBUS中断服务程序-----
138 *************************************************************/
139
140 void SMBUS_ISR (void) interrupt 7
141 {
142 1 static UCHAR iRcvCounter; // 接收数据计数
143 1 static UCHAR iSndCounter; // 发送数据计数
144 1 static UCHAR FAIL = 0;
145 1
146 1 switch (SMB0CN & 0xF0)
147 1 {
148 2 // 从接收方式: 已收到地址
149 2 case SMB_SRADD:
150 2 if((SMB0DAT&0xFE) == SLA_ADD) // 地址匹配
151 2 {
152 3 STA = 0;
153 3 ACK = 1; // 确认
154 3 if(SMB0DAT&0x01==READ) // 本协议不支持主模式读
155 3 {
156 4 // SMB0DAT = SMB_DATA; // Prepare outgoing byte
157 4 FAIL = 1;
158 4 }
159 3 else // 准备接收数据
160 3 {
161 4 iRcvCounter = 0; // 接收数据计数清零
162 4 pSMB_DATA_IN = &unRcvDataBuf.databuf[0]; // 有新的数据到,将接收指针指向接收缓冲区首址
163 4 }
164 3 }
165 2 else // 地址不匹配
166 2 {
167 3 ACK = 0; // 不确认
168 3 }
169 2 break;
170 2 // 从接收方式: 接收数据
171 2 case SMB_SRDB:
172 2 iRcvCounter++; // 保存接收数据到缓冲区
173 2 if(iRcvCounter >= 8) // 防止接收越界
174 2 // pSMB_DATA_IN = &unRcvDataBuf.databuf[0];
175 2 *(pSMB_DATA_IN++) = SMB0DAT;
176 2 ACK = 1; // ACK received data
177 2 break;
178 2 // 从接收方式: 接收完成
179 2 case SMB_SRSTO:
C51 COMPILER V8.02 IIC_COMM 09/17/2008 10:45:02 PAGE 4
180 2 STO = 0; // STO must be cleared by software when
181 2 // a STOP is detected as a slave
182 2 RcvDataFlag = 1; // Indicate new data received
183 2 break;
184 2 // 从发送方式: 数据已发送
185 2 case SMB_STDB:
186 2 break; // 本协议不支持
187 2 // 从发送方式:检测到停止条件
188 2 case SMB_STSTO:
189 2 break; // 不需任何操作
190 2 // 主发送方式: 检测到起始条件已发出
191 2 case SMB_MTSTA:
192 2 SMB0DAT = TARGET; // 装入目标地址 7bit
193 2 SMB0DAT |= SMB_RW; // 装入读写位 1bit
194 2 STA = 0; //
195 2 iSndCounter = 0; // 发送数据计数清零
196 2 break;
197 2 // 主发送方式: 数据或地址字节已发出
198 2 case SMB_MTDB:
199 2 if (ACK) // Slave Address or Data Byte
200 2 { // Acknowledged?
201 3 if (SMB_RW==WRITE) // Is this transfer a WRITE?
202 3 {
203 4
204 4 if (iSndCounter < SMB_DATA_LEN) // Is there data to send?
205 4 {
206 5 SMB0DAT = *pSMB_DATA_OUT; // send data byte
207 5 pSMB_DATA_OUT++; // increment data out pointer
208 5 iSndCounter++; // increment number of bytes sent
209 5 }
210 4 else
211 4 {
212 5 STO = 1; // set STO to terminte transfer
213 5 SMBUS_BUSY = 0; // clear software busy flag
214 5 }
215 4 }
216 3 }
217 2 else // If slave NACK,
218 2 {
219 3 if(SMB_ACKPOLL)
220 3 {
221 4 STA = 1; // Restart transfer
222 4 }
223 3 else
224 3 {
225 4 FAIL = 1; // Indicate failed transfer
226 4 } // and handle at end of ISR
227 3 }
228 2 break;
229 2
230 2 // 主接收方式: 已收到数据字节 // 本协议不支持
231 2 case SMB_MRDB:
232 2 FAIL = 1;
233 2 break;
234 2 // Default: all other cases undefined
235 2 default:
236 2 SMB0CN &= ~0x40; // Reset communication
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -