C51 COMPILER V9.59.0.0 STC8A_ADC 06/10/2021 16:43:53 PAGE 1 C51 COMPILER V9.59.0.0, COMPILATION OF MODULE STC8A_ADC OBJECT MODULE PLACED IN .\list\STC8A_ADC.obj COMPILER INVOKED BY: C:\Keil_v5\C51\BIN\C51.EXE STC8A_ADC.c LARGE OMF2 OPTIMIZE(8,SPEED) BROWSE DEBUG PRINT(.\list\STC8A -_ADC.lst) TABS(2) OBJECT(.\list\STC8A_ADC.obj) line level source 1 /*---------------------------------------------------------------------*/ 2 /* --- STC MCU Limited ------------------------------------------------*/ 3 /* --- STC 1T Series MCU Demo Programme -------------------------------*/ 4 /* --- Mobile: (86)13922805190 ----------------------------------------*/ 5 /* --- Fax: 86-0513-55012956,55012947,55012969 ------------------------*/ 6 /* --- Tel: 86-0513-55012928,55012929,55012966 ------------------------*/ 7 /* --- Web: www.STCMCU.com --------------------------------------------*/ 8 /* --- Web: www.STCMCUDATA.com ---------------------------------------*/ 9 /* --- QQ: 800003751 -------------------------------------------------*/ 10 /* 如果要在程序中使用此代码,请在程序中注明使用了STC的资料及程序 */ 11 /*---------------------------------------------------------------------*/ 12 13 #include "STC8A_ADC.h" 14 15 //======================================================================== 16 // 函数: void ADC_Inilize(ADC_InitTypeDef *ADCx) 17 // 描述: ADC初始化程序. 18 // 参数: ADCx: 结构参数,请参考adc.h里的定义. 19 // 返回: none. 20 // 版本: V1.0, 2012-10-22 21 //======================================================================== 22 void ADC_Inilize(ADC_InitTypeDef *ADCx) 23 { 24 1 ADCCFG = (ADCCFG & ~ADC_SPEED_2X16T) | ADCx->ADC_Speed; //设置ADC工作时钟频率 25 1 ADC_Justify(ADCx->ADC_AdjResult); //AD转换结果对齐方式 26 1 27 1 if(ADCx->ADC_SMPduty > 31) return; //错误 28 1 if(ADCx->ADC_CsSetup > 1) return; //错误 29 1 if(ADCx->ADC_CsHold > 3) return; //错误 30 1 31 1 EAXSFR(); /* MOVX A,@DPTR/MOVX @DPTR,A指令的操作对象为扩展SFR(XSFR) */ 32 1 ADCTIM = (ADCx->ADC_CsSetup << 7) | (ADCx->ADC_CsHold << 5) | ADCx->ADC_SMPduty ; //设置 ADC 内部时序,A -DC采样时间建议设最大值 33 1 EAXRAM(); /* MOVX A,@DPTR/MOVX @DPTR,A指令的操作对象为扩展RAM(XRAM) */ 34 1 } 35 36 37 //======================================================================== 38 // 函数: void ADC_PowerControl(u8 pwr) 39 // 描述: ADC电源控制程序. 40 // 参数: pwr: 电源控制,ENABLE或DISABLE. 41 // 返回: none. 42 // 版本: V1.0, 2012-10-22 43 //======================================================================== 44 void ADC_PowerControl(u8 pwr) 45 { 46 1 if(pwr == ENABLE) ADC_CONTR |= 0x80; 47 1 else ADC_CONTR &= 0x7f; 48 1 } 49 50 //======================================================================== 51 // 函数: u16 Get_ADCResult(u8 channel) 52 // 描述: 查询法读一次ADC转换结果. 53 // 参数: channel: 选择要转换的ADC通道. C51 COMPILER V9.59.0.0 STC8A_ADC 06/10/2021 16:43:53 PAGE 2 54 // 返回: ADC转换结果. 55 // 版本: V1.0, 2012-10-22 56 //======================================================================== 57 u16 Get_ADCResult(u8 channel) //channel = 0~15 58 { 59 1 u16 adc; 60 1 u8 i; 61 1 62 1 if(channel > ADC_CH15) return 4096; //错误,返回4096,调用的程序判断 63 1 ADC_RES = 0; 64 1 ADC_RESL = 0; 65 1 66 1 ADC_CONTR = (ADC_CONTR & 0xf0) | ADC_START | channel; 67 1 NOP(4); //对ADC_CONTR操作后要4T之后才能访问 68 1 69 1 for(i=0; i<250; i++) //超时返回,正常i等于10以内就可以转换完成 70 1 { 71 2 if(ADC_CONTR & ADC_FLAG) 72 2 { 73 3 ADC_CONTR &= ~ADC_FLAG; 74 3 if(ADCCFG & (1<<5)) //转换结果右对齐。 75 3 { 76 4 adc = ((u16)ADC_RES << 8) | ADC_RESL; 77 4 } 78 3 else //转换结果左对齐。 79 3 { 80 4 adc = (u16)ADC_RES; 81 4 adc = (adc << 4) | ((ADC_RESL >> 4)&0x0f); 82 4 } 83 3 return adc; 84 3 } 85 2 } 86 1 return 4096; //错误,返回4096,调用的程序判断 87 1 } MODULE INFORMATION: STATIC OVERLAYABLE CODE SIZE = 245 ---- CONSTANT SIZE = ---- ---- XDATA SIZE = ---- 2 PDATA SIZE = ---- ---- DATA SIZE = ---- ---- IDATA SIZE = ---- ---- BIT SIZE = ---- ---- EDATA SIZE = ---- ---- HDATA SIZE = ---- ---- XDATA CONST SIZE = ---- ---- FAR CONST SIZE = ---- ---- END OF MODULE INFORMATION. C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)