📄 adc_temp.lst
字号:
C51 COMPILER V7.50 ADC_TEMP 10/19/2006 16:46:02 PAGE 1
C51 COMPILER V7.50, COMPILATION OF MODULE ADC_TEMP
OBJECT MODULE PLACED IN adc_temp.OBJ
COMPILER INVOKED BY: D:\keil\C51\BIN\c51.exe adc_temp.c DB OE
line level source
1 //-----------------------------------------------------------------------------
2 // Includes
3 //-----------------------------------------------------------------------------
4
5 #include <c8051f310.h> // SFR declarations
6 #include <intrins.h>
7 #include <stdio.h>
8
9 //-----------------------------------------------------------------------------
10 // 16-bit SFR Definitions for 'F31x
11 //-----------------------------------------------------------------------------
12
13 sfr16 DP = 0x82; // data pointer
14 sfr16 TMR2RL = 0xca; // Timer2 reload value
15 sfr16 TMR2 = 0xcc; // Timer2 counter
16 sfr16 TMR3 = 0x94; // Timer3 counter
17 sfr16 TMR3RL = 0x92; // Timer3 reload value
18 sfr16 PCA0CP0 = 0xfb; // PCA0 Module 0 Capture/Compare
19 sfr16 PCA0CP1 = 0xe9; // PCA0 Module 1 Capture/Compare
20 sfr16 PCA0CP2 = 0xeb; // PCA0 Module 2 Capture/Compare
21 sfr16 PCA0CP3 = 0xed; // PCA0 Module 3 Capture/Compare
22 sfr16 PCA0CP4 = 0xfd; // PCA0 Module 4 Capture/Compare
23 sfr16 PCA0 = 0xf9; // PCA0 counter
24 sfr16 ADC0 = 0xbd; // ADC Data Word Register
25 sfr16 ADC0GT = 0xc3; // ADC0 Greater-Than
26 sfr16 ADC0LT = 0xc5; // ADC0 Less-Than
27 //-----------------------------------------------------------------------------
28 // Temperature Sensor Calibration PARAMETERS
29 //-----------------------------------------------------------------------------
30
31 //-----------------------------------------------------------------------------
32 // Global CONSTANTS
33 //-----------------------------------------------------------------------------
34
35 #define SYSCLK 24500000 // SYSCLK frequency in Hz
36 #define BAUDRATE 9600 // Baud rate of UART in bps
37 #define TIMER2_RATE 1000 // Timer 2 overflow rate in Hz
38
39 sbit LED = P3^3; // LED='1' means ON
40 //-----------------------------------------------------------------------------
41 // Temperature Sensor Calibration PARAMETERS
42 //-----------------------------------------------------------------------------
43 #define AMB_TEMP 25 // Ambient Calibration Temperature
44 // (degC)
45
46 #define TEMP_SENSOR_GAIN 3300 // Temp Sensor Gain in (uV / degC)
47
48 #define VREF 3300 // ADC Voltage Reference (mV)
49
50 #define SOAK_TIME 15 // Soak Time in Seconds
51
52
53 //-----------------------------------------------------------------------------
54 // Global VARIABLES
55 //-----------------------------------------------------------------------------
C51 COMPILER V7.50 ADC_TEMP 10/19/2006 16:46:02 PAGE 2
56
57 // TEMP_OFFSET allocates two bytes of code memory space in FLASH
58 // that will be used to store the calibrated temperature value
59 unsigned int code TEMP_OFFSET = 0xFFFF;
60
61 //-----------------------------------------------------------------------------
62 // Function PROTOTYPES
63 //-----------------------------------------------------------------------------
64
65 void SYSCLK_Init (void);
66 void ADC0_Init (void);
67 void UART0_Init (void);
68 void PORT_Init (void);
69 void Timer2_Init (int);
70 void Write_COM(unsigned yjcom );
71 void Write_CHAR(unsigned char yjchar);
72 void YJ_Init(void);
73 void calibrate(void);
74 void wait_one_second (void);
75 int get_temp (void);
76 void SPI0_Init (void);
77 unsigned int measure(void);
78
79 unsigned char xdata NCDdata[14]={0x43,0x61,0x7c,0x69,0x62,0x72,0x61,0x74,0x69,0x6e,0x67,0x2e,0x2e,0x2e};
80
81 //-----------------------------------------------------------------------------
82 // MAIN Routine
83 //-----------------------------------------------------------------------------
84 void main (void)
85 {
86 1 unsigned temperature;
87 1 int i;
88 1 int temp_int, temp_frac;
89 1 // Disable Watchdog timer
90 1 PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer
91 1 // enable)
92 1 PORT_Init(); // Initialize Port I/O
93 1 SPI0_Init ();
94 1 SYSCLK_Init (); // Initialize Oscillator
95 1
96 1 ADC0_Init (); // Init ADC0
97 1 Timer2_Init(SYSCLK/TIMER2_RATE); // Init Timer 2
98 1 UART0_Init();
99 1
100 1 YJ_Init();
101 1
102 1 AD0EN = 1; // Enable ADC0
103 1
104 1 if (TEMP_OFFSET == 0xFFFF) { // TRUE if first-time to execute
105 2 printf ("Calibrating...\n");
106 2
107 2 Write_COM(0x02);
108 2 for(i=0;i<14;i++){
109 3 Write_CHAR(NCDdata[i]);}
110 2
111 2 calibrate (); // execute calibration sequence
112 2 }
113 1 else
114 1 {
115 2 printf ("Not calibrating.\n");
116 2 }
117 1
C51 COMPILER V7.50 ADC_TEMP 10/19/2006 16:46:02 PAGE 3
118 1 while (1) {
119 2 // example of how to read the temperature
120 2 PORT_Init();
121 2 LED = 1;
122 2 temperature = get_temp ();
123 2 LED = 0;
124 2 temp_int = temperature / 100;
125 2 temp_frac = temperature - (temp_int * 100);
126 2 printf ("Temperature = %+02d hundredths degrees C\n", temperature);
127 2
128 2 NCDdata[0]=temp_int/100+0x30;NCDdata[1]=(temp_int%100)/10+0x30;NCDdata[2]=(temp_int%100)%10+0x30;NCDdat
-a[3]=0x2e;
129 2 NCDdata[4]=temp_frac/10+0x30;NCDdata[5]=temp_frac%10+0x30;NCDdata[6]=0x20;NCDdata[7]=0x20;NCDdata[8]=0x
-20;
130 2 NCDdata[9]=0x20;NCDdata[10]=0x20;NCDdata[11]=0x20;NCDdata[12]=0x20;NCDdata[13]=0x20;NCDdata[14]=0x20;
131 2 YJ_Init();
132 2 for(i=0;i<14;i++)
133 2 {
134 3 Write_CHAR(NCDdata[i]);}
135 2 }
136 1 }
137
138 //-----------------------------------------------------------------------------
139 // Initialization Subroutines
140 //-----------------------------------------------------------------------------
141
142 //-----------------------------------------------------------------------------
143 // PORT_Init
144 //-----------------------------------------------------------------------------
145 //
146 // Configure the Crossbar and GPIO ports.
147 //
148 // P0.4 - UART TX
149 // P0.5 - UART RX
150 // P3.3 - LED
151
152 void PORT_Init (void)
153 {
154 1 P0SKIP = 0xCE;
155 1 P1SKIP = 0x7F;
156 1 XBR0 = 0x03; // Enable SPI pins
157 1 XBR1 = 0x40; // Enable crossbar and weak pull-ups
158 1
159 1 P0MDOUT = 0xFF; // All P0 pins open-drain output
160 1 P2MDOUT = 0xFF;
161 1 P3MDOUT |= 0x04; // P3.3 push-pull output
162 1 }
163
164 //-----------------------------------------------------------------------------
165 // SYSCLK_Init
166 //-----------------------------------------------------------------------------
167 //
168 // This routine initializes the system clock to use the internal oscillator
169 // at its maximum frequency.
170 // Also enables the Missing Clock Detector.
171 //
172
173 void SYSCLK_Init (void)
174 {
175 1 OSCICN |= 0x03; // Configure internal oscillator for
176 1 // its maximum frequency
177 1 RSTSRC = 0x04; // Enable missing clock detector
C51 COMPILER V7.50 ADC_TEMP 10/19/2006 16:46:02 PAGE 4
178 1
179 1 }
180
181 //-----------------------------------------------------------------------------
182 // ADC0_Init ADBUSY, LP tracking, no Interrupt, ADC disabled
183 //-----------------------------------------------------------------------------
184 //
185 // Configure ADC0 to use ADBUSY as conversion source, and to sense the output
186 // of the temp sensor. Disables ADC end of conversion interrupt. Leaves ADC
187 // disabled.
188 //
189 void ADC0_Init (void)
190 {
191 1 ADC0CN = 0x40; // ADC0 disabled; LP tracking
192 1 // mode; ADC0 conversions are initiated
193 1 // on a write to ADBusy
194 1 AMX0P = 0x1E; // Temp sensor selected at + input
195 1 AMX0N = 0x1F; // Single-ended mode
196 1
197 1 ADC0CF = (SYSCLK/3000000) << 3; // ADC conversion clock <= 3MHz
198 1
199 1 ADC0CF &= ~0x04; // Make ADC0 right-justified
200 1 REF0CN = 0x0e; // enable temp sensor, VREF = VDD, bias
201 1 // generator is on.
202 1
203 1 EIE1 &= ~0x08; // Disable ADC0 EOC interrupt
204 1 }
205
206 //-----------------------------------------------------------------------------
207 // UART0_Init
208 //-----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -