📄 sht7i.lst
字号:
C51 COMPILER V3.96, SN-83203013 SHT7I 06/22/05 10:35:56 PAGE 1
DOS C51 COMPILER V3.96, COMPILATION OF MODULE SHT7I
OBJECT MODULE PLACED IN .\OUT\SHT7I.OBJ
COMPILER INVOKED BY: C:\C51\BIN\C51.EXE SHT7I.C-CODE-DEBUG-SMALL-OBJECTEXTEND-OJ(.\OUT\SHT7I.OBJ)
stmt level source
1 #include <reg931.h> //Microcontroller specific library, e.g. port definitions
2 #include <intrins.h> //Keil library (is used for _nop()_ operation)
3 #include <math.h> //Keil library
4 #include <stdio.h> //Keil library
5
6 //********************************************************************************
7 #define uchar unsigned char //数据类型定义
8 #define uint unsigned int
9
10 sbit DATA= P0^1;
11 sbit SCK= P0^0;
12
13 #define _Nop() _nop_(),_nop_(),_nop_() //定义空指令
14
15 #define noACK 0
16 #define ACK 1
17 //adr command r/w
18 #define STATUS_REG_W 0x06 //000 0011 0 //读状态寄存器
19 #define STATUS_REG_R 0x07 //000 0011 1 //写状态寄存器
20 #define MEASURE_TEMP 0x03 //000 0001 1 //测温命令
21 #define MEASURE_HUMI 0x05 //000 0010 1 //测湿度命令
22 #define RESET 0x1e //000 1111 0 //软复位命令
23
24
25 typedef union
26 { unsigned int i;
27 float f;
28 } value;
29
30
31
32 //********************************************************************************
33 //tools(串口输出)
34 //********************************************************************************
35 void Prints(uchar sMessage[26]);
36 void Putchar(uchar byte);
37 void Puthexbyte(uchar ch);
38
39 //char s_write_byte(unsigned char value);
40 //char s_write_byte(unsigned char tvalue);
41 /**********************************************************************
42 * 名称 : Puthexbyte
43 * 功能描述: 将数据用16进制的格式表示
44 * 输入参量: unsigned char ch
45 * 输出参量: 无
46 * 调用子程: Putchar
47 * 使用方法: 例如数据puthexbyte(0x16)将向串口送出0x31 0x36,如果用串口工具的文本模式将看到"16"
48 --------------------------------*/
49 void Puthexbyte(unsigned char ch)
50 {
51 1 unsigned char i;
52 1 i=(ch>>4);
53 1 if (i<=9) Putchar(0x30+i);
54 1 else Putchar (0x37+i);
55 1 i=(ch&0x0F);
C51 COMPILER V3.96, SN-83203013 SHT7I 06/22/05 10:35:56 PAGE 2
56 1 if (i<=9) Putchar(0x30+i);
57 1 else Putchar (0x37+i);
58 1 }
59
60 /**********************************************************************
61 * 名称 : prints
62 * 功能描述: 打印一串字符,不超过50bytes
63 * 输入参量: unsigned char sMessage[50]
64 * 输出参量: 无
65 * 调用子程: Putchar
66 * 使用方法: prints("hello");
67 -------------------------------------*/
68 void Prints(unsigned char sMessage[26])
69 {
70 1 uchar k=0;
71 1 while(sMessage[k] != 0)
72 1 {
73 2 Putchar(sMessage[k]);
74 2 k++;
75 2 }
76 1 }
77 /**********************************************************************
78 * 名称 : Putchar
79 * 功能描述: 输出一个字节函数
80 * 输入参量: unsigned char byte
81 * 输出参量: 无
82 * 调用子程: 无
83 * 使用方法: 直接调用,例如如果要向串口发送方波即0x55,则调用 Putchar(0x55);
84 -------------------------------*/
85 void Putchar(unsigned char byte)
86 {
87 1 SBUF=byte;
88 1 while(TI==0);
89 1 TI=0;
90 1 }
91
92
93 /****************************************************
94 初始化串口
95 ****************************************************/
96 void InitialUart()
97 {
98 1 SCON=0x50; //SM0=0,SM1=1,模式1
99 1 //REN=1,RI=0,允许接收
100 1 SSTAT=0x20; //使用独立中断
101 1 BRGCON=0; //先设置成0后,才可以置位下两位
102 1 BRGR0=0xF0; //7.373MHz内部RC振荡,波特率9600
103 1 BRGR1=0x02;
104 1 BRGCON=0x03; // enable BRG
105 1 IEN0=0X10; //开串口接收中断
106 1 IEN1=0x40; //开串口发送中断
107 1 EA=1;
108 1 }
109
110 /****************************************************
111 初始化IO口
112 ****************************************************/
113 void InitialIO()
114 {
115 1 // P2M1=0xd3; //SPI总线IO口
116 1 // P2M2=0xd3;
117 1 P1M1=0x40; //串口和P1.7为准双向口
C51 COMPILER V3.96, SN-83203013 SHT7I 06/22/05 10:35:56 PAGE 3
118 1 P1M2=0x40;
119 1 P0M1=0x00; //置P0.0,P1.1为准双向口
120 1 P0M2=0x00;
121 1 }
122
123
124 //----------------------------------------------------------------------------------
125 char s_write_byte(unsigned char tvalue)
126 //----------------------------------------------------------------------------------
127 // writes a byte on the Sensibus and checks the acknowledge
128 {
129 1
130 1 unsigned char i,error=0;
131 1 for (i=0x80;i>0;i/=2) //shift bit for masking
132 1 {
133 2 if (i & tvalue)
134 2 DATA=1; //masking value with i , write to SENSI-BUS
135 2 else DATA=0;
136 2 SCK=1; //clk for SENSI-BUS
137 2 _Nop();_Nop();_Nop(); //pulswith approx. 5 us
138 2 SCK=0;
139 2 _Nop();
140 2 }
141 1 DATA=1; //release DATA-line
142 1 SCK=1; //clk #9 for ack
143 1 error=DATA; //check ack (DATA will be pulled down by SHT11)
144 1 // Prints("DATA=");
145 1 // Puthexbyte(DATA);
146 1 // Prints("\r\n");
147 1 SCK=0;
148 1 return error; //error=1 in case of no acknowledge
149 1
150 1 }
151
152
153 //----------------------------------------------------------------------------------
154 char s_read_byte(unsigned char ack)
155 //----------------------------------------------------------------------------------
156 // reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
157 {
158 1 uchar i,val=0;
159 1 DATA=1; //release DATA-line
160 1 for (i=0x80;i>0;i/=2) //shift bit for masking
161 1 {
162 2 SCK=1; //clk for SENSI-BUS
163 2 _Nop(); //后加
164 2 if (DATA) val=(val | i); //read bit
165 2 SCK=0; //后加
166 2 _Nop();
167 2 }
168 1 DATA=!ack; //in case of "ack==1" pull down DATA-Line
169 1 SCK=1; //clk #9 for ack
170 1 _Nop();//_Nop();_Nop(); //pulswith approx. 5 us
171 1 SCK=0;
172 1 _Nop();//_Nop(); //后加
173 1 DATA=1; //release DATA-line
174 1 return val;
175 1 }
176
177
178
179 //----------------------------------------------------------------------------------
C51 COMPILER V3.96, SN-83203013 SHT7I 06/22/05 10:35:56 PAGE 4
180 void s_transstart(void)
181 //----------------------------------------------------------------------------------
182 // generates a transmission start
183 // _____ ________
184 // DATA: |_______|
185 // ___ ___
186 // SCK : ___| |___| |______
187 {
188 1
189 1 DATA=1; SCK=0; //Initial state
190 1 _Nop();
191 1 SCK=1;
192 1 _Nop();
193 1 DATA=0;
194 1 _Nop();
195 1 SCK=0;
196 1 _Nop();//_Nop();_Nop(); //修改
197 1 SCK=1;
198 1 _Nop();
199 1 DATA=1;
200 1 _Nop();
201 1 SCK=0;
202 1 }
203
204 //----------------------------------------------------------------------------------
205 void s_connectionreset(void)
206 //----------------------------------------------------------------------------------
207 // communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
208 // _____________________________________________________ ________
209 // DATA: |_______|
210 // _ _ _ _ _ _ _ _ _ ___ ___
211 // SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
212 {
213 1 unsigned char i;
214 1 DATA=1; SCK=0; //Initial state
215 1 for(i=0;i<9;i++) //9 SCK cycles
216 1 {
217 2 SCK=1;
218 2 _Nop();//_Nop();
219 2 SCK=0;
220 2 _Nop();//_Nop();
221 2 }
222 1 s_transstart(); //transmission start
223 1 }
224
225 /*
226 //----------------------------------------------------------------------------------
227 char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
228 //----------------------------------------------------------------------------------
229 // reads the status register with checksum (8-bit)
230 {
231 unsigned char error=0;
232 s_transstart(); //transmission start
233 error=s_write_byte(STATUS_REG_R); //send command to sensor
234 *p_value=s_read_byte(ACK); //read status register (8-bit)
235 *p_checksum=s_read_byte(noACK); //read checksum (8-bit)
236 Prints("status=");
237 Puthexbyte(*p_value);
238 Prints("\r\n");
239 return error; //error=1 in case of no response form the sensor
240 }
241
C51 COMPILER V3.96, SN-83203013 SHT7I 06/22/05 10:35:56 PAGE 5
242 //----------------------------------------------------------------------------------
243 char s_write_statusreg(unsigned char *p_value)
244 //----------------------------------------------------------------------------------
245 // writes the status register with checksum (8-bit)
246 {
247 unsigned char error=0;
248 //s_transstart(); //transmission start
249 s_connectionreset(); //后加
250 error+=s_write_byte(STATUS_REG_W);//send command to sensor
251 error+=s_write_byte(*p_value); //send value of status register
252 return error; //error>=1 in case of no response form the sensor
253 }
254
255 //----------------------------------------------------------------------------------
256 char s_softreset(void)
257 //----------------------------------------------------------------------------------
258 // resets the sensor by a softreset
259 {
260
261 unsigned char error=0;
262 s_connectionreset(); //reset communication
263 error+=s_write_byte(RESET); //send RESET-command to sensor
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -