📄 ad.lst
字号:
C51 COMPILER V8.16 AD 11/12/2008 21:50:52 PAGE 1
C51 COMPILER V8.16, COMPILATION OF MODULE AD
OBJECT MODULE PLACED IN ad.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.exe ad.c DB OE BR
line level source
1 //-----------------------------------------------------------------------------
2 // F32x_ADC0_ExternalInput.c
3 //-----------------------------------------------------------------------------
4 // Copyright 2006 Silicon Laboratories, Inc.
5 // http://www.silabs.com
6 //
7 // Program Description:
8 // --------------------
9 //
10 // This example code takes and averages 2048 analog measurements from input
11 // P2.4 using ADC0, then prints the results to a terminal window via the UART.
12 //
13 // The system is clocked by the internal 12MHz oscillator. Timer 2 triggers
14 // a conversion on ADC0 on each overflow. The completion of this conversion
15 // in turn triggers an interrupt service routine (ISR). The ISR averages
16 // 2048 measurements, then prints the value to the terminal via printf before
17 // starting another average cycle.
18 //
19 // The analog multiplexer selects P2.4 as the positive ADC0 input. This
20 // port is configured as an analog input in the port initialization routine.
21 // The negative ADC0 input is connected via mux to ground, which provides
22 // for a single-ended ADC input.
23 //
24 // A 100kohm potentiometer may be connected as a voltage divider between
25 // VREF and AGND on the terminal strip as shown below:
26 //
27 // ---------
28 // |
29 // |
30 // |
31 // o| VREF ----|
32 // o| GND ---|<-|
33 // o| P2.4-| | |
34 // o| | |
35 // | |------|
36 //----------
37 // C8051F320-TB
38 //
39 // Terminal output is done via printf, which directs the characters to
40 // UART0. A UART initialization routine is therefore necessary.
41 //
42 // ADC Settling Time Requirements, Sampling Rate:
43 // ----------------------------------------------
44 //
45 // The total sample time per input is comprised of an input setting time
46 // (Tsettle), followed by a conversion time (Tconvert):
47 //
48 // Tsample = Tsettle + Tconvert
49 //
50 // |--------Settling-------|==Conversion==|----Settling--- . . .
51 // Timer 2 overflow ^
52 // ADC0 ISR ^
53 //
54 // The ADC input voltage must be allowed adequate time to settle before the
55 // conversion is made. This settling depends on the external source
C51 COMPILER V8.16 AD 11/12/2008 21:50:52 PAGE 2
56 // impedance, internal mux impedance, and internal capacitance.
57 // Settling time is given by:
58 //
59 // | 2^n |
60 // Tsettle = ln | --- | * Rtotal * Csample
61 // | SA |
62 //
63 // In this application, assume a 100kohm potentiometer as the voltage divider.
64 // The expression evaluates to:
65 //
66 // | 2^10 |
67 // Tsettle = ln | ---- | * 105e3 * 5e-12 = 4.4uS
68 // | 0.25 |
69 //
70 // In addition, one must allow at least 1.5uS after changing analog mux
71 // inputs or PGA settings. The settling time in this example, then, is
72 // dictated by the large external source resistance.
73 //
74 // The conversion is 10 periods of the SAR clock <SAR_CLK>. At 3 MHz,
75 // this time is 10 * 400nS = 3.3 uS.
76 //
77 //
78 // Tsample, minimum = Tsettle + Tconvert
79 // = 4.4uS + 3.3uS
80 // = 7.7 uS
81 //
82 // Timer 2 is set to start a conversion every 100uS, which is far
83 // longer than the minimum required.
84 //
85 // F320 Resources:
86 // ---------------
87 // Timer1: clocks UART
88 // Timer2: overflow initiates ADC conversion
89 //
90 // How To Test:
91 // ------------
92 // 1) Download code to a 'F320 device on a C8051F320-TB development board
93 // 2) Connect serial cable from the transceiver to a PC
94 // 3) On the PC, open HyperTerminal (or any other terminal program) and connect
95 // to the COM port at <BAUDRATE> and 8-N-1
96 // 4) Connect a variable voltage source (between 0 and Vref)
97 // to P2.4, or a potentiometer voltage divider as shown above.
98 // 5) HyperTerminal will print the voltage measured by the device if
99 // everything is working properly
100 //
101 // FID: 32X000081
102 // Target: C8051F320
103 // Tool chain: Keil C51 7.50 / Keil EVAL C51
104 // Command Line: None
105 //
106 //
107 // Release 1.0
108 // -Initial Revision (clm)
109 // -24-Jul-06
110
111
112 //-----------------------------------------------------------------------------
113 // Includes
114 //-----------------------------------------------------------------------------
115
116 #include "c8051F320.h" // SFR declarations
117 #include <stdio.h>
C51 COMPILER V8.16 AD 11/12/2008 21:50:52 PAGE 3
118
119 //-----------------------------------------------------------------------------
120 // 16-bit SFR Definitions for 'F34x
121 //-----------------------------------------------------------------------------
122
123 sfr16 TMR2RL = 0xca; // Timer2 reload value
124 sfr16 TMR2 = 0xcc; // Timer2 counter
125 sfr16 ADC0 = 0xbd; // ADC0 result
126
127 //-----------------------------------------------------------------------------
128 // Global CONSTANTS
129 //-----------------------------------------------------------------------------
130
131 #define SYSCLK 12000000 // SYSCLK frequency in Hz
132 #define BAUDRATE 115200 // Baud rate of UART in bps
133
134 sbit LED = P2^2; // LED='1' means ON
135
136 //-----------------------------------------------------------------------------
137 // Function PROTOTYPES
138 //-----------------------------------------------------------------------------
139
140 void SYSCLK_Init (void);
141 void PORT_Init (void);
142 void Timer2_Init(void);
143 void ADC0_Init(void);
144 void UART0_Init (void);
145 void ADC0_Conver(void);
146 //-----------------------------------------------------------------------------
147 // MAIN Routine
148 //-----------------------------------------------------------------------------
149
150 void main (void)
151 {
152 1 PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer
153 1 // enable)
154 1
155 1 SYSCLK_Init (); // Initialize system clock to
156 1 // 12MHz
157 1 PORT_Init (); // Initialize crossbar and GPIO
158 1 //Timer2_Init(); // Init Timer2 to generate
159 1 // overflows to trigger ADC
160 1 //UART0_Init(); // Initialize UART0 for printf's
161 1 ADC0_Init(); // Initialize ADC0
162 1
163 1 //EA = 1; // enable global interrupts
164 1 while (1)
165 1 {
166 2 ADC0_Conver(); // spin forever
167 2 }
168 1 }
169
170 //-----------------------------------------------------------------------------
171 // Initialization Subroutines
172 //-----------------------------------------------------------------------------
173 //
174 //-----------------------------------------------------------------------------
175 // SYSCLK_Init
176 //-----------------------------------------------------------------------------
177 //
178 // Return Value: None
179 // Parameters: None
C51 COMPILER V8.16 AD 11/12/2008 21:50:52 PAGE 4
180 //
181 // This routine initializes the system clock to use the internal 12MHz
182 // oscillator as its clock source. Also enables missing clock detector reset.
183 //
184 //-----------------------------------------------------------------------------
185 void SYSCLK_Init (void)
186 {
187 1 OSCICN = 0x83; // configure internal oscillator for
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -