📄 f06x_adc2_externalinput.lst
字号:
216 1 char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
217 1
218 1 SFRPAGE = CONFIG_PAGE; // set SFR page
219 1
220 1 XBR0 = 0x00;
221 1 XBR1 = 0x00;
222 1 XBR2 = 0x44; // Enable crossbar and weak pull-up
223 1 // Enable UART1
224 1
225 1 P0MDOUT |= 0x01; // Set TX1 pin to push-pull
226 1 P1MDOUT |= 0x40; // Set P1.6(LED) to push-pull
227 1
228 1 P1MDIN = 0xFD; // P1.1 Analog Input, Open Drain
229 1
230 1 SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
231 1 }
232
233 //-----------------------------------------------------------------------------
234 // UART1_Init
235 //-----------------------------------------------------------------------------
236 //
237 // Return Value : None
238 // Parameters : None
239 //
240 // Configure the UART1 using Timer1, for <baudrate> and 8-N-1.
241 //
C51 COMPILER V8.08 F06X_ADC2_EXTERNALINPUT 02/18/2008 14:59:47 PAGE 5
242 //-----------------------------------------------------------------------------
243 void UART1_Init (void)
244 {
245 1 char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
246 1
247 1 SFRPAGE = UART1_PAGE;
248 1 SCON1 = 0x10; // SCON1: mode 0, 8-bit UART, enable RX
249 1
250 1 SFRPAGE = TIMER01_PAGE;
251 1 TMOD &= ~0xF0;
252 1 TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit reload
253 1
254 1
255 1 if (SYSCLK/BAUDRATE/2/256 < 1) {
256 2 TH1 = -(SYSCLK/BAUDRATE/2);
257 2 CKCON |= 0x10; // T1M = 1; SCA1:0 = xx
258 2 } else if (SYSCLK/BAUDRATE/2/256 < 4) {
259 2 TH1 = -(SYSCLK/BAUDRATE/2/4);
260 2 CKCON &= ~0x13; // Clear all T1 related bits
261 2 CKCON |= 0x01; // T1M = 0; SCA1:0 = 01
262 2 } else if (SYSCLK/BAUDRATE/2/256 < 12) {
263 2 TH1 = -(SYSCLK/BAUDRATE/2/12);
264 2 CKCON &= ~0x13; // T1M = 0; SCA1:0 = 00
265 2 } else {
266 2 TH1 = -(SYSCLK/BAUDRATE/2/48);
267 2 CKCON &= ~0x13; // Clear all T1 related bits
268 2 CKCON |= 0x02; // T1M = 0; SCA1:0 = 10
269 2 }
270 1
271 1 TL1 = TH1; // Initialize Timer1
272 1 TR1 = 1; // Start Timer1
273 1
274 1 SFRPAGE = UART1_PAGE;
275 1 TI1 = 1; // Indicate TX1 ready
276 1
277 1 SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
278 1
279 1 }
280
281 //-----------------------------------------------------------------------------
282 // ADC2_Init
283 //-----------------------------------------------------------------------------
284 //
285 // Return Value : None
286 // Parameters : None
287 //
288 // Configure ADC0 to use Timer3 overflows as conversion source, to
289 // generate an interrupt on conversion complete, and to use right-justified
290 // output mode. Enables ADC end of conversion interrupt. Leaves ADC disabled.
291 //
292 //-----------------------------------------------------------------------------
293 void ADC2_Init (void)
294 {
295 1 char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
296 1
297 1 SFRPAGE = ADC2_PAGE;
298 1
299 1 ADC2CN = 0x04; // ADC2 disabled; normal tracking
300 1 // mode; ADC2 conversions are initiated
301 1 // on overflow of Timer3; ADC2 data is
302 1 // right-justified
303 1
C51 COMPILER V8.08 F06X_ADC2_EXTERNALINPUT 02/18/2008 14:59:47 PAGE 6
304 1 REF2CN = 0x03; // Enable on-chip VREF,
305 1 // and VREF output buffer
306 1 AMX2CF = 0x00; // AIN inputs are single-ended (default)
307 1 AMX2SL = 0x01; // Select AIN2.1 pin as ADC mux input
308 1 ADC2CF = (SYSCLK/SAR_CLK) << 3; // ADC conversion clock = 2.5MHz
309 1 EIE2 |= 0x10; // enable ADC interrupts
310 1 SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
311 1 }
312
313 //-----------------------------------------------------------------------------
314 // TIMER3_Init
315 //-----------------------------------------------------------------------------
316 //
317 // Return Value : None
318 // Parameters :
319 // 1) int counts - calculated Timer overflow rate
320 // range is postive range of integer: 0 to 32767
321 //
322 // Configure Timer3 to auto-reload at interval specified by <counts> (no
323 // interrupt generated) using SYSCLK as its time base.
324 //
325 //-----------------------------------------------------------------------------
326 void TIMER3_Init (int counts)
327 {
328 1 char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
329 1
330 1 SFRPAGE = TMR3_PAGE;
331 1
332 1 TMR3CN = 0x00; // Stop Timer3; Clear TF3;
333 1 TMR3CF = 0x08; // use SYSCLK as timebase
334 1
335 1 RCAP3 = -counts; // Init reload values
336 1 TMR3 = RCAP3; // Set to reload immediately
337 1 EIE2 &= ~0x01; // Disable Timer3 interrupts
338 1 TR3 = 1; // start Timer3
339 1
340 1 SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
341 1 }
342
343 //-----------------------------------------------------------------------------
344 // Interrupt Service Routines
345 //-----------------------------------------------------------------------------
346
347 //-----------------------------------------------------------------------------
348 // ADC2_ISR
349 //-----------------------------------------------------------------------------
350 //
351 // Here we take the ADC2 sample, add it to a running total <accumulator>, and
352 // decrement our local decimation counter <int_dec>. When <int_dec> reaches
353 // zero, we post the decimated result in the global variable <Result>.
354 //
355 //-----------------------------------------------------------------------------
356 void ADC2_ISR (void) interrupt 18
357 {
358 1 static unsigned int_dec=INT_DEC; // Integrate/decimate counter
359 1 // we post a new result when
360 1 // int_dec = 0
361 1 static long accumulator=0L; // Here's where we integrate the
362 1 // ADC samples
363 1 AD2INT = 0; // Clear ADC conversion complete
364 1 // indicator
365 1 accumulator += ADC2; // Read ADC value and add to running
C51 COMPILER V8.08 F06X_ADC2_EXTERNALINPUT 02/18/2008 14:59:47 PAGE 7
366 1 // total
367 1 int_dec--; // Update decimation counter
368 1 if (int_dec == 0) // If zero, then post result
369 1 {
370 2 int_dec = INT_DEC; // Reset counter
371 2 Result = accumulator >> 8;
372 2 accumulator = 0L; // Reset accumulator
373 2 }
374 1 }
375
376 //-----------------------------------------------------------------------------
377 // Support Subroutines
378 //-----------------------------------------------------------------------------
379
380 //-----------------------------------------------------------------------------
381 // Wait_MS
382 //-----------------------------------------------------------------------------
383 //
384 // Return Value : None
385 // Parameters:
386 // 1) unsigned int ms - number of milliseconds of delay
387 // range is full range of integer: 0 to 65335
388 //
389 // This routine inserts a delay of <ms> milliseconds.
390 //
391 //-----------------------------------------------------------------------------
392 void Wait_MS(unsigned int ms)
393 {
394 1 char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
395 1
396 1 SFRPAGE = TMR2_PAGE;
397 1
398 1 TMR2CN = 0x00; // Stop Timer3; Clear TF3;
399 1 TMR2CF = 0x00; // use SYSCLK/12 as timebase
400 1
401 1 RCAP2 = -(SYSCLK/1000/12); // Timer 2 overflows at 1 kHz
402 1 TMR2 = RCAP2;
403 1
404 1 ET2 = 0; // Disable Timer 2 interrupts
405 1
406 1 TR2 = 1; // Start Timer 2
407 1
408 1 while(ms)
409 1 {
410 2 TF2 = 0; // Clear flag to initialize
411 2 while(!TF2); // Wait until timer overflows
412 2 ms--; // Decrement ms
413 2 }
414 1
415 1 TR2 = 0; // Stop Timer 2
416 1
417 1 SFRPAGE = SFRPAGE_SAVE; // Restore SFRPAGE
418 1 }
419
420 //-----------------------------------------------------------------------------
421 // End Of File
422 //-----------------------------------------------------------------------------
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 407 ----
CONSTANT SIZE = 24 ----
C51 COMPILER V8.08 F06X_ADC2_EXTERNALINPUT 02/18/2008 14:59:47 PAGE 8
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 10 4
IDATA SIZE = ---- ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -