📄 temp_3.lst
字号:
C51 COMPILER V7.00 TEMP_3 12/23/2003 14:31:55 PAGE 1
C51 COMPILER V7.00, COMPILATION OF MODULE TEMP_3
OBJECT MODULE PLACED IN Temp_3.OBJ
COMPILER INVOKED BY: C:\KEIL\C51\BIN\C51.EXE Temp_3.c DB OE
stmt level source
1 //-----------------------------------------------------------------------------
2 // Temp_3.c
3 //-----------------------------------------------------------------------------
4 // Copyright 2001 Cygnal Integrated Products, Inc.
5 //
6 // AUTH: BW
7 // DATE: 19 JUL 01
8 //
9 // This program prints the C8051F020 die temperature out the hardware
10 // UART at 9600bps. Assumes an 22.1184MHz crystal is attached between
11 // XTAL1 and XTAL2.
12 //
13 // The ADC is configured to look at the on-chip temp sensor. The sampling
14 // rate of the ADC is determined by the constant <SAMPLE_RATE>, which is given
15 // in Hz.
16 //
17 // The ADC0 End of Conversion Interrupt Handler retrieves the sample
18 // from the ADC and adds it to a running accumulator. Every <INT_DEC>
19 // samples, the ADC updates and stores its result in the global variable
20 // <temperature>, which holds the current temperature in hundredths of a
21 // degree. The sampling technique of adding a set of values and
22 // decimating them (posting results every (n)th sample) is called 'integrate
23 // and dump.' It is easy to implement and requires very few resources.
24 //
25 // For each power of 4 of <INT_DEC>, you gain 1 bit of effective resolution.
26 // For example, <INT_DEC> = 256 gain you 4 bits of resolution: 4^4 = 256.
27 //
28 // Also note that the ADC0 is configured for 'LEFT' justified mode. In this
29 // mode, the MSB of the ADC word is located in the MSB position of the ADC0
30 // high byte. Using the data in this way makes the magnitude of the resulting
31 // code independent of the number of bits in the ADC (12- and 10-bits behave
32 // the same).
33 //
34 // Target: C8051F02x
35 // Tool chain: KEIL C51 6.03 / KEIL EVAL C51
36 //
37
38 //-----------------------------------------------------------------------------
39 // Includes
40 //-----------------------------------------------------------------------------
41
42 #include <c8051f020.h> // SFR declarations
43 #include <stdio.h>
44
45 //-----------------------------------------------------------------------------
46 // 16-bit SFR Definitions for 'F02x
47 //-----------------------------------------------------------------------------
48
49 sfr16 DP = 0x82; // data pointer
50 sfr16 TMR3RL = 0x92; // Timer3 reload value
51 sfr16 TMR3 = 0x94; // Timer3 counter
52 sfr16 ADC0 = 0xbe; // ADC0 data
53 sfr16 ADC0GT = 0xc4; // ADC0 greater than window
54 sfr16 ADC0LT = 0xc6; // ADC0 less than window
55 sfr16 RCAP2 = 0xca; // Timer2 capture/reload
C51 COMPILER V7.00 TEMP_3 12/23/2003 14:31:55 PAGE 2
56 sfr16 T2 = 0xcc; // Timer2
57 sfr16 RCAP4 = 0xe4; // Timer4 capture/reload
58 sfr16 T4 = 0xf4; // Timer4
59 sfr16 DAC0 = 0xd2; // DAC0 data
60 sfr16 DAC1 = 0xd5; // DAC1 data
61
62 //-----------------------------------------------------------------------------
63 // Global CONSTANTS
64 //-----------------------------------------------------------------------------
65
66 #define BAUDRATE 115200 // Baud rate of UART in bps
67 #define SYSCLK 22118400 // SYSCLK frequency in Hz
68 #define SAMPLE_RATE 50000 // Sample frequency in Hz
69 #define INT_DEC 256 // integrate and decimate ratio
70
71 sbit LED = P1^6; // LED='1' means ON
72 sbit SW1 = P3^7; // SW1='0' means switch pressed
73
74 //-----------------------------------------------------------------------------
75 // Function PROTOTYPES
76 //-----------------------------------------------------------------------------
77
78 void SYSCLK_Init (void);
79 void PORT_Init (void);
80 void UART0_Init (void);
81 void ADC0_Init (void);
82 void Timer3_Init (int counts);
83 void ADC0_ISR (void);
84
85 //-----------------------------------------------------------------------------
86 // Global VARIABLES
87 //-----------------------------------------------------------------------------
88
89 long result; // ADC0 decimated value
90
91 //-----------------------------------------------------------------------------
92 // MAIN Routine
93 //-----------------------------------------------------------------------------
94
95 void main (void) {
96 1 long temperature; // temperature in hundredths of a
97 1 // degree C
98 1 int temp_int, temp_frac; // integer and fractional portions of
99 1 // temperature
100 1
101 1 WDTCN = 0xde; // disable watchdog timer
102 1 WDTCN = 0xad;
103 1
104 1 SYSCLK_Init (); // initialize oscillator
105 1 PORT_Init (); // initialize crossbar and GPIO
106 1 UART0_Init (); // initialize UART0
107 1 Timer3_Init (SYSCLK/SAMPLE_RATE); // initialize Timer3 to overflow at
108 1 // sample rate
109 1
110 1 ADC0_Init (); // init ADC
111 1
112 1 AD0EN = 1; // enable ADC
113 1
114 1 EA = 1; // Enable global interrupts
115 1
116 1 while (1) {
117 2 EA = 0; // disable interrupts
C51 COMPILER V7.00 TEMP_3 12/23/2003 14:31:55 PAGE 3
118 2 temperature = result;
119 2 EA = 1; // re-enable interrupts
120 2
121 2 // calculate temperature in hundredths of a degree
122 2 temperature = temperature - 42380;
123 2 temperature = (temperature * 100L) / 156;
124 2 temp_int = temperature / 100;
125 2 temp_frac = temperature - (temp_int * 100);
126 2 printf ("Temperature is %+02d.%02d\n", temp_int, temp_frac);
127 2
128 2 LED = ~SW1; // LED reflects state of switch
129 2 }
130 1 }
131
132 //-----------------------------------------------------------------------------
133 // Initialization Subroutines
134 //-----------------------------------------------------------------------------
135
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -