我这个精度要求不高 就是几个电阻分压。代码也基本是范例代码多的那些寄存器也都有了!
- <ol class="code_ol_Tql" style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; --tw-rotate: 0; --tw-skew-x: 0; --tw-skew-y: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-pan-x: ; --tw-pan-y: ; --tw-pinch-zoom: ; --tw-scroll-snap-strictness: proximity; --tw-gradient-from-position: ; --tw-gradient-via-position: ; --tw-gradient-to-position: ; --tw-ordinal: ; --tw-slashed-zero: ; --tw-numeric-figure: ; --tw-numeric-spacing: ; --tw-numeric-fraction: ; --tw-ring-inset: ; --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgb(59 130 246 / 0.5); --tw-ring-offset-shadow: 0 0 #0000; --tw-ring-shadow: 0 0 #0000; --tw-shadow: 0 0 #0000; --tw-shadow-colored: 0 0 #0000; --tw-blur: ; --tw-brightness: ; --tw-contrast: ; --tw-grayscale: ; --tw-hue-rotate: ; --tw-invert: ; --tw-saturate: ; --tw-sepia: ; --tw-drop-shadow: ; --tw-backdrop-blur: ; --tw-backdrop-brightness: ; --tw-backdrop-contrast: ; --tw-backdrop-grayscale: ; --tw-backdrop-hue-rotate: ; --tw-backdrop-invert: ; --tw-backdrop-opacity: ; --tw-backdrop-saturate: ; --tw-backdrop-sepia: ; --tw-contain-size: ; --tw-contain-layout: ; --tw-contain-paint: ; --tw-contain-style: ; list-style-position: initial; list-style-image: initial; background-color: rgb(240, 240, 240);"><li style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; --tw-rotate: 0; --tw-skew-x: 0; --tw-skew-y: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-pan-x: ; --tw-pan-y: ; --tw-pinch-zoom: ; --tw-scroll-snap-strictness: proximity; --tw-gradient-from-position: ; --tw-gradient-via-position: ; --tw-gradient-to-position: ; --tw-ordinal: ; --tw-slashed-zero: ; --tw-numeric-figure: ; --tw-numeric-spacing: ; --tw-numeric-fraction: ; --tw-ring-inset: ; --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgb(59 130 246 / 0.5); --tw-ring-offset-shadow: 0 0 #0000; --tw-ring-shadow: 0 0 #0000; --tw-shadow: 0 0 #0000; --tw-shadow-colored: 0 0 #0000; --tw-blur: ; --tw-brightness: ; --tw-contrast: ; --tw-grayscale: ; --tw-hue-rotate: ; --tw-invert: ; --tw-saturate: ; --tw-sepia: ; --tw-drop-shadow: ; --tw-backdrop-blur: ; --tw-backdrop-brightness: ; --tw-backdrop-contrast: ; --tw-backdrop-grayscale: ; --tw-backdrop-hue-rotate: ; --tw-backdrop-invert: ; --tw-backdrop-opacity: ; --tw-backdrop-saturate: ; --tw-backdrop-sepia: ; --tw-contain-size: ; --tw-contain-layout: ; --tw-contain-paint: ; --tw-contain-style: ; padding-left: 5px; list-style-position: initial; list-style-image: initial; border-left: 1px solid rgb(205, 205, 205) !important;"><font face="monospace"><span style="white-space: pre;">//========================================================================
- BYTE GetADCResult(BYTE ch)
- {
- ADC_CONTR = (ADC_CONTR & 0xF0)|ADC_START|ch; //ADC_CONTR = ADC_CONTR | 0x40 |ch= ADC_CONTR | 01000000|ch 代表ADC_START置1 // ADC_CONTR &= 0xf0; //清空通道
- _nop_();
- _nop_();
- _nop_();
- while (!(ADC_CONTR & ADC_FLAG)); //查询ADC完成标志 ADC_CONTR&ADC_FLAG=ADC_CONTR & 0x20=ADC_CONTR & 0010 0000 用于检查ADC_CONTR寄存器中ADC_FLAG对应于0x20(二进制00100000)的位的状态。
- ADC_CONTR &= ~ADC_FLAG; //#define ADC_FLAG 0x20 //ADC_CONTR = ADC_CONTR & (~0x20)=ADC_CONTR & (~0010 0000)=ADC_CONTR & 1101 1111 代表ADC_FLAG清零
- return (ADC_RES<< 8 )|ADC_RESL; //右对齐,所以高位左移动8位加低八位; 读取ADC结果
- }
- /*----------------------------
- 初始化ADC
- ----------------------------*/
- void InitADC()
- {
- P1M0 = 0x00; P1M1 = 0xcb; //1.2 1.4 1.5 双向 1.0 1.1 1.3 1.6 1.7 高祖
-
- // P_SW2 |= 0x80; //P_SW2 |= 0x80;相当于P_SW2 = P_SW2 | 0x80
- ADCTIM = 0x3f; //设置ADC内部时序 ADCTIM = 0x3f=0 01 11111
- // P_SW2 &= 0x7f; //P_SW2 &= 0x80;相当于P_SW2 = P_SW2 & 0x7F = P_SW2 &01111111
- ADCCFG = 0x20; //ADCCFG = 0x2f = 00 1 0 1111; RESFMT = 1 第6位 0左对齐 1右对齐; 后四位 设置ADC时钟为系统时钟/2/16
- ADC_CONTR = ADC_POWER; //使能ADC模块 ADC_CONTR = ADC_POWER=0x80=1000 0000
- }</span></font></li></ol>
复制代码
|