📄 adc.lst
字号:
C51 COMPILER V8.02 ADC 07/05/2010 14:30:22 PAGE 1
C51 COMPILER V8.02, COMPILATION OF MODULE ADC
OBJECT MODULE PLACED IN ADC.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE ADC.c BROWSE DEBUG OBJECTEXTEND
line level source
1 /*------------------------------------------------------------------*/
2 /* --- STC MCU International Limited -------------------------------*/
3 /* --- STC 1T Series MCU A/D Conversion Demo -----------------------*/
4 /* --- Mobile: (86)13922805190 -------------------------------------*/
5 /* --- Fax: 86-755-82944243 ----------------------------------------*/
6 /* --- Tel: 86-755-82948412 ----------------------------------------*/
7 /* --- Web: www.STCMCU.com -----------------------------------------*/
8 /* If you want to use the program or the program referenced in the */
9 /* article, please specify in which data and procedures from STC */
10 /*------------------------------------------------------------------*/
11
12 #include "reg51.h"
13 #include "intrins.h"
14
15 #define FOSC 18432000L
16 #define BAUD 9600
17
18 typedef unsigned char BYTE;
19 typedef unsigned int WORD;
20
21 /*Declare SFR associated with the ADC */
22 sfr ADC_CONTR = 0xBC; //ADC control register
23 sfr ADC_RES = 0xBD; //ADC high 8-bit result register
24 sfr ADC_LOW2 = 0xBE; //ADC low 2-bit result register
25 sfr P1ASF = 0x9D; //P1 secondary function control register
26
27 /*Define ADC operation const for ADC_CONTR*/
28 #define ADC_POWER 0x80 //ADC power control bit
29 #define ADC_FLAG 0x10 //ADC complete flag
30 #define ADC_START 0x08 //ADC start control bit
31 #define ADC_SPEEDLL 0x00 //420 clocks
32 #define ADC_SPEEDL 0x20 //280 clocks
33 #define ADC_SPEEDH 0x40 //140 clocks
34 #define ADC_SPEEDHH 0x60 //70 clocks
35
36 void InitUart();
37 void InitADC();
38 void SendData(BYTE dat);
39 BYTE GetADCResult(BYTE ch);
40 void Delay(WORD n);
41 void ShowResult(BYTE ch);
42
43 void main()
44 {
45 1 InitUart(); //Init UART, use to show ADC result
46 1 InitADC(); //Init ADC sfr
47 1 while (1)
48 1 {
49 2 ShowResult(0); //Show Channel0
50 2 ShowResult(1); //Show Channel1
51 2 ShowResult(2); //Show Channel2
52 2 ShowResult(3); //Show Channel3
53 2 ShowResult(4); //Show Channel4
54 2 ShowResult(5); //Show Channel5
55 2 ShowResult(6); //Show Channel6
C51 COMPILER V8.02 ADC 07/05/2010 14:30:22 PAGE 2
56 2 ShowResult(7); //Show Channel7
57 2 }
58 1 }
59
60 /*----------------------------
61 Send ADC result to UART
62 ----------------------------*/
63 void ShowResult(BYTE ch)
64 {
65 1 SendData(ch); //Show Channel NO.
66 1 SendData(GetADCResult(ch)); //Show ADC high 8-bit result
67 1
68 1 //if you want show 10-bit result, uncomment next line
69 1 // SendData(ADC_LOW2); //Show ADC low 2-bit result
70 1 }
71
72 /*----------------------------
73 Get ADC result
74 ----------------------------*/
75 BYTE GetADCResult(BYTE ch)
76 {
77 1 ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ch | ADC_START;
78 1 _nop_(); //Must wait before inquiry
79 1 _nop_();
80 1 _nop_();
81 1 _nop_();
82 1 while (!(ADC_CONTR & ADC_FLAG));//Wait complete flag
83 1 ADC_CONTR &= ~ADC_FLAG; //Close ADC
84 1
85 1 return ADC_RES; //Return ADC result
86 1 }
87
88 /*----------------------------
89 Initial UART
90 ----------------------------*/
91 void InitUart()
92 {
93 1 SCON = 0x5a; //8 bit data ,no parity bit
94 1 TMOD = 0x20; //T1 as 8-bit auto reload
95 1 TH1 = TL1 = -(FOSC/12/32/BAUD); //Set Uart baudrate
96 1 TR1 = 1; //T1 start running
97 1 }
98
99 /*----------------------------
100 Initial ADC sfr
101 ----------------------------*/
102 void InitADC()
103 {
104 1 P1ASF = 0xff; //Open 8 channels ADC function
105 1 ADC_RES = 0; //Clear previous result
106 1 ADC_CONTR = ADC_POWER | ADC_SPEEDLL;
107 1 Delay(2); //ADC power-on and delay
108 1 }
109
110 /*----------------------------
111 Send one byte data to PC
112 Input: dat (UART data)
113 Output:-
114 ----------------------------*/
115 void SendData(BYTE dat)
116 {
117 1 while (!TI); //Wait for the previous data is sent
C51 COMPILER V8.02 ADC 07/05/2010 14:30:22 PAGE 3
118 1 TI = 0; //Clear TI flag
119 1 SBUF = dat; //Send current data
120 1 }
121
122 /*----------------------------
123 Software delay function
124 ----------------------------*/
125 void Delay(WORD n)
126 {
127 1 WORD x;
128 1
129 1 while (n--)
130 1 {
131 2 x = 5000;
132 2 while (x--);
133 2 }
134 1 }
135
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 146 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = ---- 1
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 + -