改写一下更易看懂
- #include <STC15F2K60S2.H>
- #include <intrins.h>
- #include "LCD1602.h"
- //宏定义ADC的操作命令
- #define ADC_POWER 0x80 //ADC电源控制位 1000 0000
- #define ADC_START 0x08 //ADC启动控制位 0000 1000
- #define ADC_FLAG 0x10 //ADC完成标志位 0001 0000
- #define ADC_SPEEDLL 0x00 //420时钟 0000 0000
- unsigned char str[5]="00.00";
- unsigned char aa[]={'L','I','G','H','T',':'};
- unsigned int result;
- void Init_ADC();
- void delay(unsigned int ms);
- unsigned int GetADCResult(unsigned char CHA);
- void main()
- {
- P0M0 = 0x00;
- P0M1 = 0x00;
- P1M1 = 0x04; //设置P1.2高阻
- P1M0 = 0x00;
- P2M0 = 0x00;
- P2M1 = 0x00;
- P3M0 = 0x00;
- P3M1 = 0x00;
- P4M0 = 0x00;
- P4M1 = 0x00;
- P5M0 = 0x00;
- P5M1 = 0x00;
- P6M0 = 0x00;
- P6M1 = 0x00;
- P7M0 = 0x00;
- P7M1 = 0x00;
- Init_ADC();
- while (1)
- {
- result=GetADCResult(2);//P1.2
- // result=result*0.0475;
- result=result*4.75;
- //显示数据范围00.00~48.64
- str[0]=result/1000%10+'0';
- str[1]=result/100%10+'0';
- // str[2]='.';
- str[3]=result/10%10+'0';
- str[4]=result%10+'0';
- LCD_Write_String(6,1,str);
- LCD_Write_String(0,1,aa);
- delay(100);//控制刷新周期约100ms
- }
- }
- /*-------------------------------
- ms延时子程序(11.0592MHz 1T)
- -------------------------------*/
- void delay(unsigned int ms)
- {
- unsigned int i,j;
- for(i=ms;i>0;i--)
- for(j=1150;j>0;j--);
- }
- /*-----------------------------------------------------------------------------
- InitADC 初始化ADC
- -----------------------------------------------------------------------------*/
- void Init_ADC()
- {
- P1ASF = 0x04; //将P1.2口设为ADC转换模拟输入
- ADC_RES =0; //清除ADC_RES存储器以前的结果
- ADC_RESL=0;
- ADC_CONTR=ADC_POWER|ADC_SPEEDLL;//ADC 开启电源、转换速度 1000 0000
- delay(1); //上电延时1ms
- }
- /*----------------------------------------------------------------------------
- Get ADC Result(获取ADC结果)
- ----------------------------------------------------------------------------*/
- unsigned int GetADCResult(unsigned char CHA) //获取ADC结果
- {
- ADC_CONTR=ADC_POWER|ADC_SPEEDLL|CHA|ADC_START;//启动转换1000 1xxx
- _nop_();_nop_();_nop_();_nop_();//小延时
- while(!(ADC_CONTR & ADC_FLAG)); //等待ADC转换完成标志置位 1001 1000
- ADC_CONTR &=~ADC_FLAG; //关闭ADC
- return ADC_RES<<2 | ADC_RESL; //返回十位ADC结果
- }
复制代码 |