📄 f32x_adc0_externalinput_mux.lst
字号:
C51 COMPILER V8.16 F32X_ADC0_EXTERNALINPUT_MUX 11/11/2008 21:00:18 PAGE 1
C51 COMPILER V8.16, COMPILATION OF MODULE F32X_ADC0_EXTERNALINPUT_MUX
NO OBJECT MODULE REQUESTED
COMPILER INVOKED BY: C:\SiLabs\MCU\IDEfiles\C51\BIN\C51.exe F32x_ADC0_ExternalInput_Mux.c DB OE BR
line level source
*** WARNING C500 IN LINE 1 OF F32X_ADC0_EXTERNALINPUT_MUX.C: MISSING LICENSE ID CODE (LIC) IN 'TOOLS.INI'
1 //-----------------------------------------------------------------------------
2 // F32x_ADC0_ExternalInput_Mux.c
3 //-----------------------------------------------------------------------------
4 // Copyright 2005 Silicon Laboratories, Inc.
5 // http://www.silabs.com
6 //
7 // This code example illustrates using the internal analog multiplexer to
8 // measure analog voltages on up to 8 different analog inputs. Results are
9 // printed to a PC terminal program via the UART.
10 //
11 // The inputs are sequentially scanned, beginning with input 0 (P1.0), up
12 // to input number <ANALOG_INPUTS>-1 based on the values in <PIN_TABLE>.
13 //
14 //
15 // ADC Settling Time Requirements, Sampling Rate:
16 // ----------------------------------------------
17 //
18 // The total sample time per input is comprised of an input setting time
19 // (Tsettle), followed by a conversion time (Tconvert):
20 //
21 // Tsample = Tsettle + Tconvert
22 //
23 // Settling and conversion times may overlap, as the ADC holds the value once
24 // conversion begins. This program takes advantage of this to increase the
25 // settling time above the minimum required. In other words, when
26 // converting the value from analog input Ain(n), the input mux is switched
27 // over to the next input Ain(n+1) to begin settling.
28 //
29 // |---Settling Ain(n)---|=Conversion Ain(n)=|
30 // |---Settling Ain(n+1)---|=Conversion Ain(n+1)=|
31 // |---Settling Ain(n+2)---|
32 // ISR: Timer 2 ^ ^ ^
33 // ISR: ADC0 ^ ^
34 //
35 // The ADC input voltage must be allowed adequate time to settle before the
36 // conversion is made. This settling depends on the external source
37 // impedance, internal mux impedance, and internal capacitance.
38 // Settling time is given by:
39 //
40 // | 2^n |
41 // Tsettle = ln | --- | * Rtotal * Csample
42 // | SA |
43 //
44 // In this application, assume a 100kohm potentiometer as the voltage divider.
45 // The expression evaluates to:
46 //
47 // | 2^12 |
48 // Tsettle = ln | ---- | * 105e3 * 10e-12 = 10.2uS
49 // | 0.25 |
50 //
51 // In addition, one must allow at least 1.5 us after changing analog MUX
52 // inputs or PGA settings. The settling time in this example, then, is
53 // dictated by the large external source resistance.
54 //
C51 COMPILER V8.16 F32X_ADC0_EXTERNALINPUT_MUX 11/11/2008 21:00:18 PAGE 2
55 // The conversion is 16 periods of the SAR clock. At 2.5 MHz,
56 // this time is 16 * 400nS = 6.4 uS.
57 //
58 //
59 // Tsample, minimum = Tsettle + Tconvert
60 // = 10.2uS + 6.4uS
61 // = 16.6 uS
62 //
63 // Timer2 is set to change the MUX input and start a conversion every 20 us.
64 //
65 // General:
66 // --------
67 //
68 // The system is clocked using the internal 12MHz oscillator. Results are
69 // printed to the UART from a loop with the rate set by a delay based on
70 // Timer0. This loop periodically reads the ADC value from a global array,
71 // <RESULT>.
72 //
73 // The ADC makes repeated measurements at 20 us intervals based on Timer2.
74 // The end of each ADC conversion initiates an interrupt which calls an
75 // averaging function. <INT_DEC> samples are averaged, then the Result
76 // values updated.
77 //
78 // For each power of 4 of <INT_DEC>, you gain 1 bit of effective resolution.
79 // For example, <INT_DEC> = 256 gain you 4 bits of resolution: 4^4 = 256.
80 //
81 // The ADC input multiplexer is set for a single-ended input. The example
82 // sequentially scans through the inputs, starting at P1.0. <ANALOG_INPUTS>
83 // inputs are read. The amplifier is set for unity gain so a voltage range of
84 // 0 to Vref (2.43V) may be measured. Although voltages up to Vdd may be
85 // applied without damaging the device, only the range 0 to Vref may be
86 // measured by the ADC.
87 //
88 // A 100 kohm potentiometer may be connected as a voltage divider between
89 // VREF and AGND as shown below:
90 //
91 // ---------
92 // |
93 // o| AGND ----|
94 // o| VREF ----|<-|
95 // o| P1.x | |
96 // o| | |
97 // o| --------
98 // o|
99 // o|
100 // o|
101 // |
102 // ---------
103 //
104 // How To Test:
105 //
106 // 1) Download code to a 'F32x device that is connected to a UART transceiver
107 // 2) Verify the TX and RX jumpers are populated on J3.
108 // 3) Connect a serial cable from the DB9 connector to a PC
109 // 4) On the PC, open HyperTerminal (or any other terminal program) and connect
110 // to the COM port at <BAUDRATE> and 8-N-1
111 // 5) Connect a variable voltage source (between 0 and Vref) to the Port1 pins,
112 // or a potentiometer voltage divider as shown above.
113 // 6) HyperTerminal will print the voltages measured by the device if
114 // everything is working properly. Note that some of the analog inputs are
115 // floating and will return nonzero values.
116 //
C51 COMPILER V8.16 F32X_ADC0_EXTERNALINPUT_MUX 11/11/2008 21:00:18 PAGE 3
117 // FID: 32X000091
118 // Target: C8051F32x
119 // Tool chain: Keil C51 7.50 / Keil EVAL C51
120 // Command Line: None
121 //
122 // Release 1.0
123 // -Initial Revision (SM / TP)
124 // -19 OCT 2006
125 //
126
127
128 //-----------------------------------------------------------------------------
129 // Includes
130 //-----------------------------------------------------------------------------
131
132 #include <C8051F320.h> // SFR declarations
133 #include <stdio.h>
134
135 //-----------------------------------------------------------------------------
136 // 16-bit SFR Definitions for 'F32x
137 //-----------------------------------------------------------------------------
138
139 sfr16 TMR2RL = 0xCA; // Timer2 reload value
140 sfr16 TMR2 = 0xCC; // Timer2 counter
141 sfr16 ADC0 = 0xBD; // 10-bit ADC0 result
142
143 //-----------------------------------------------------------------------------
144 // Global CONSTANTS
145 //-----------------------------------------------------------------------------
146
147 #define SYSCLK 12000000 // SYSCLK frequency in Hz
148 #define BAUDRATE 115200 // Baud rate of UART in bps
149 #define ANALOG_INPUTS 6 // Number of AIN pins to measure,
150 // skipping the UART0 pins
151 #define INT_DEC 256 // Integrate and decimate ratio
152
153 #define TIMER0_RELOAD_HIGH 0 // Timer0 High register
154 #define TIMER0_RELOAD_LOW 255 // Timer0 Low register
155
156 //-----------------------------------------------------------------------------
157 // Function PROTOTYPES
158 //-----------------------------------------------------------------------------
159
160 void Oscillator_Init (void);
161 void Port_Init (void);
162 void Timer2_Init(void);
163 void ADC0_Init(void);
164 void UART0_Init (void);
165
166 void Timer0_wait(int ms);
167
168 //-----------------------------------------------------------------------------
169 // Global Variables
170 //-----------------------------------------------------------------------------
171
172 long RESULT[ANALOG_INPUTS]; // ADC0 decimated value, one for each
173 // analog input
174
175 // Port1 ADC Inputs
176 unsigned char idata PIN_TABLE[ANALOG_INPUTS] = {0,1,2,3,4,5};
177
178 unsigned char AMUX_INPUT = 0; // Index of analog MUX inputs
C51 COMPILER V8.16 F32X_ADC0_EXTERNALINPUT_MUX 11/11/2008 21:00:18 PAGE 4
179
180 //-----------------------------------------------------------------------------
181 // MAIN Routine
182 //-----------------------------------------------------------------------------
183 void main (void)
184 {
185 1 unsigned char i;
186 1 unsigned long measurement;
187 1
188 1 PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer
189 1 // enable)
190 1
191 1 Oscillator_Init (); // Initialize system clock to
192 1 // 12MHz
193 1 Port_Init (); // Initialize crossbar and GPIO
194 1 Timer2_Init(); // Init Timer2 to generate
195 1 // overflows to trigger ADC
196 1 UART0_Init(); // Initialize UART0 for printf's
197 1 ADC0_Init(); // Initialize ADC0
198 1
199 1 EA = 1; // Enable global interrupts
200 1 while (1)
201 1 {
202 2 EA = 0; // Disable interrupts
203 2 printf("\f");
204 2 for(i = 0; i < ANALOG_INPUTS; i++)
205 2 {
206 3 // The 10-bit ADC value is averaged across INT_DEC measurements.
207 3 // The result is then stored in RESULT, and is right-justified
208 3 // The measured voltage applied to the port pins is then:
209 3 //
210 3 // Vref (mV)
211 3 // measurement (mV) = --------------- * Result (bits)
212 3 // (2^10)-1 (bits)
213 3
214 3 measurement = RESULT[i] * 2430 / 1023;
215 3 printf("P1.%bu voltage: %4ld mV\n",PIN_TABLE[i],measurement);
216 3 }
217 2 EA = 1; // Re-enable interrupts
218 2
219 2 Timer0_wait(25); // Wait before displaying new values
220 2 }
221 1 }
222
223 //-----------------------------------------------------------------------------
224 // Initialization Subroutines
225 //-----------------------------------------------------------------------------
226
227 //-----------------------------------------------------------------------------
228 // SYSCLK_Init
229 //-----------------------------------------------------------------------------
230 //
231 // Return Value : None
232 // Parameters : None
233 //
234 // This routine initializes the system clock to use the internal 12MHz
235 // oscillator as its clock source. Also enables missing clock detector reset.
236 //
237 //-----------------------------------------------------------------------------
238 void Oscillator_Init (void)
239 {
240 1 OSCICN = 0x83; // Configure internal oscillator for
C51 COMPILER V8.16 F32X_ADC0_EXTERNALINPUT_MUX 11/11/2008 21:00:18 PAGE 5
241 1 // its highest frequency
242 1 RSTSRC = 0x04; // Enable missing clock detector
243 1 }
244
245 //-----------------------------------------------------------------------------
246 // Port_Init
247 //-----------------------------------------------------------------------------
248 //
249 // Return Value : None
250 // Parameters : None
251 //
252 // Configure the Crossbar and GPIO ports.
253 //
254 // P0.4 - UART TX (push-pull)
255 // P0.5 - UART RX
256 //
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -