📄 temp_1.lst
字号:
C51 COMPILER V8.08 TEMP_1 12/24/2008 09:37:52 PAGE 1
C51 COMPILER V8.08, COMPILATION OF MODULE TEMP_1
OBJECT MODULE PLACED IN temp_1.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE temp_1.c BROWSE DEBUG OBJECTEXTEND
line level source
1 //-----------------------------------------------------------------------------
2 // F06x_ADC0_ExternalInput.c
3 //-----------------------------------------------------------------------------
4 // Copyright 2005 Silicon Laboratories, Inc.
5 // http://www.silabs.com
6 //
7 // Program Description:
8 //
9 // This program measures the voltage on an external ADC input and prints the
10 // result to a terminal window via the UART.
11 // 作为电压输入ADC然后在终端上显示。
12 // The system is clocked using the internal 24.5MHz oscillator. 系统时钟内部晶振24.5
13 // Results are printed to the UART from a loop with the rate set by a delay
14 // based on Timer 2. This loop periodically reads the ADC value from a global
15 // variable, Result.
16 //
17 // The ADC makes repeated measurements at a rate determined by SAMPLE_RATE
18 // using Timer 3. The end of each ADC conversion initiates an interrupt which
19 // calls an averaging function.
20 // <INT_DEC> samples are averaged then the Result value updated.
21 //
22 // For each power of 4 of <INT_DEC>, you gain 1 bit of effective resolution.
23 // For example, <INT_DEC> = 256 gain you 4 bits of resolution: 4^4 = 256.
24 //
25 // The ADC input multiplexer is set for a single-ended input at AIN2.1.
26 // The input amplifier is set for unity gain so a voltage range of 0 to Vref
27 // (2.43V) may be measured. Although voltages up to Vdd may be applied without
28 // damaging the device, only the range 0 to Vref may be measured by the ADC.
29 //
30 // A 100kohm potentiometer may be connected as a voltage divider between
31 // VREF and AGND as shown below: (minimum value = 12Kohms as the maximum
32 // recommended current through the Vref is of 200uA @2.4V)
33 //
34 // ---------
35 // |
36 // 8 o| AGND ----|
37 // o| VREF ----|<-|
38 // o| AIN2.1 | |
39 // o| | |
40 // o| --------
41 // o|
42 // o|
43 // 1 o|
44 // |
45 //----------
46 //
47 // How To Test:
48 //
49 // 1) Download code to a 'F06x device that is connected to a UART transceiver
50 // 2) Connect serial cable from the transceiver to a PC
51 // 3) On the PC, open HyperTerminal (or any other terminal program) and connect
52 // to the COM port at <BAUDRATE> and 8-N-1
53 // 4) Connect a variable voltage source (between 0 and Vref)
54 // to AIN 2.1, or a potentiometer voltage divider as shown above.
55 // 5) HyperTerminal will print the voltage measured by the device if
C51 COMPILER V8.08 TEMP_1 12/24/2008 09:37:52 PAGE 2
56 // everything is working properly
57 //
58 // FID: 06X000022
59 // Target: C8051F06x
60 // Tool chain: Keil C51 7.50 / Keil EVAL C51
61 // Command Line: None
62 //
63 //
64 // Release 1.0
65 // -Initial Revision (CG)
66 // -8-June-06
67 //
68 //-----------------------------------------------------------------------------
69 // Includes
70 //-----------------------------------------------------------------------------
71
72 #include <c8051F060.h> // SFR declarations
73 #include <stdio.h>
74
75 //-----------------------------------------------------------------------------
76 // 16-bit SFR Definitions for 'F06x
77 //-----------------------------------------------------------------------------
78
79 //sfr16 ADC2L = 0xbe; // ADC2 data
80 sfr16 RCAP2 = 0xca; // Timer2 capture/reload
81 sfr16 RCAP3 = 0xca; // Timer3 capture/reload
82 sfr16 TMR2 = 0xcc; // Timer2
83 sfr16 TMR3 = 0xcc; // Timer3
84 sfr16 ADC2=0xbe;
85
86 //-----------------------------------------------------------------------------
87 // Global Constants
88 //-----------------------------------------------------------------------------
89
90 #define BAUDRATE 9600 // Baud rate of UART in bps
91 #define SYSCLK 24500000 // System Clock
92 #define SAMPLE_RATE 50000 // Sample frequency in Hz
93 #define INT_DEC 256 // Integrate and decimate ratio
94 #define SAR_CLK 2500000 // Desired SAR clock speed
95
96 #define SAMPLE_DELAY 50 // Delay in ms before taking sample
97
98 sbit LED = P2^0; // LED='1' means ON
99 sbit LED1 = P2^1;
100 sbit SW1 = P3^7; // SW1='0' means switch pressed
101
102 //-----------------------------------------------------------------------------
103 // Function Prototypes
104 //-----------------------------------------------------------------------------
105
106 void OSCILLATOR_Init (void);
107 void PORT_Init (void);
108 void UART1_Init (void);
109 void ADC2_Init (void);
110 void TIMER3_Init (void);
111 void TIMER3_ISR (void);
112 void ADC2_ISR (void);
113 void Wait_MS (unsigned int ms);
114
115 //-----------------------------------------------------------------------------
116 // Global Variables
117 //-----------------------------------------------------------------------------
C51 COMPILER V8.08 TEMP_1 12/24/2008 09:37:52 PAGE 3
118
119 long Result; // ADC0 decimated value
120 int lchhxy=0;
121 int lch=0;
122
123 //-----------------------------------------------------------------------------
124 // main() Routine
125 //-----------------------------------------------------------------------------
126
127 void main (void)
128 {
129 1 long measurement=0; // Measured voltage in mV
130 1 // long Mu,Md;
131 1
132 1 WDTCN = 0xde; // Disable watchdog timer
133 1 WDTCN = 0xad;
134 1
135 1 OSCILLATOR_Init (); // Initialize oscillator
136 1 PORT_Init (); // Initialize crossbar and GPIO
137 1 UART1_Init (); // Initialize UART1
138 1 TIMER3_Init (); // Initialize Timer3 to overflow at
139 1 // sample rate 用TM3满足采样频率。
140 1
141 1 ADC2_Init (); // Init ADC
142 1
143 1 SFRPAGE = ADC2_PAGE;
144 1 AD2EN = 1; // Enable ADC
145 1
146 1 EA = 1; // Enable global interrupts
147 1
148 1 while (1)
149 1 {
150 2 EA = 0; // Disable interrupts
151 2
152 2 // The 10-bit ADC2 value is averaged across INT_DEC measurements.
153 2 // The result is then stored in Result, and is right-justified
154 2 // The measured voltage applied to AIN 2.1 is then:
155 2 //
156 2 // Vref (mV)
157 2 // measurement (mV) = --------------- * Result (bits)
158 2 // (2^10)-1 (bits)
159 2 if(lchhxy==1)
160 2 {
161 3 measurement = Result;
162 3 lch=1;
163 3 lchhxy=0;
164 3 }
165 2
166 2 EA = 1;
167 2 if(lch==1)
168 2 { // Re-enable interrupts
169 3 measurement = (measurement*2.43)/(2^11); // 将偏差校正到 0度 0V
170 3 measurement = (measurement -0.776)/0.00289; // 2.5mV/摄氏度
171 3
172 3 /* Mu=measurement/100;
173 3 Md= measurement- Mu*100; */
174 3
175 3 SFRPAGE = UART1_PAGE;
176 3
177 3 printf("Temperature: %ld C\n",measurement);
178 3
179 3 SFRPAGE = CONFIG_PAGE;
C51 COMPILER V8.08 TEMP_1 12/24/2008 09:37:52 PAGE 4
180 3 LED = ~LED; // LED reflects state of switch
181 3 lch=0;
182 3 }
183 2 Wait_MS(SAMPLE_DELAY); // Wait 50 milliseconds before taking
184 2 // another sample
185 2
186 2 }
187 1 }
188
189 //-----------------------------------------------------------------------------
190 // Initialization Subroutines
191 //-----------------------------------------------------------------------------
192
193 //-----------------------------------------------------------------------------
194 // OSCILLATOR_Init
195 //-----------------------------------------------------------------------------
196 //
197 // Return Value : None
198 // Parameters : None
199 //
200 // This function initializes the system clock to use the internal oscillator
201 // at 24.5 MHz.
202 //
203 //-----------------------------------------------------------------------------
204 void OSCILLATOR_Init (void)
205 {
206 1 char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
207 1
208 1 SFRPAGE = CONFIG_PAGE; // Set SFR page
209 1
210 1 OSCICN = 0x83; // Set internal oscillator to run
211 1 // at its maximum frequency
212 1
213 1 CLKSEL = 0x00; // Select the internal osc. as
214 1 // the SYSCLK source
215 1
216 1 SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
217 1 }
218
219 //-----------------------------------------------------------------------------
220 // PORT_Init
221 //-----------------------------------------------------------------------------
222 //
223 // Return Value : None
224 // Parameters : None
225 //
226 // This function configures the crossbar and GPIO ports.
227 //
228 // P0.4 digital push-pull UART TX
229 // P0.5 digital open-drain UART RX
230 // P1.6 digital push-pull LED
231 // AIN1.1 analog Analog input
232 //-----------------------------------------------------------------------------
233 void PORT_Init (void)
234 {
235 1 char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
236 1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -