您需要 登录 才可以下载或查看,没有账号?注册
举报
mcumail 发表于 2016-5-20 14:45 IO口的模拟功能打开了没?这个ANSELD寄存器
Latin_newday 发表于 2016-5-20 14:47 AD切换通道转换前需要加一定延时,详细可以看数据手册! 你可以参考一下我正常使用的ad转换程序! u16 ad_c ...
reverse_L0111 发表于 2016-5-20 17:26 设置的AN7啊 ADCON0=0X1C;
mcumail 发表于 2016-5-20 17:48 我没用过你这个片子,我用的是PIC18F46K22,ADC的时候需要设置端口为模拟端口,而非数字端口,相关寄存器 ...
reverse_L0111 发表于 2016-5-21 09:19 ANCON0bits.ANSEL7=1;刚试了加上上面的设置还是不行,那个ADRESH,ADRESL可以初始化为空吗?再帮忙 ...
兰天白云 发表于 2016-5-23 13:23 手册看足2小时,到时候你就懒得在这里问了
#include <adc.h> #include <stdint.h> #include <stdbool.h> #include <xc.h> #include <adc.h> #define PIN_ANALOG 1 #define PIN_DIGITAL 0 #define PIN_INPUT 1 #define PIN_OUTPUT 0 /********************************************************************* * Function: ADC_ReadPercentage(ADC_CHANNEL channel); * * Overview: Reads the requested ADC channel and returns the percentage * of that conversions result (0-100%). * * PreCondition: channel is configured via the ADCConfigure() function * * Input: ADC_CHANNEL channel - enumeration of the ADC channels * available in this demo. They should be meaningful names and * not the names of the ADC pins on the device (as the demo code * may be ported to other boards). * i.e. ADC_ReadPercentage(ADC_CHANNEL_POTENTIOMETER); * * Output: uint8_t indicating the percentage of the result 0-100% or * 0xFF for an error * ********************************************************************/ uint8_t ADC_ReadPercentage (ADC_CHANNEL channel) { uint8_t percent; switch(channel) { case ADC_CHANNEL_0: break; default: return 0xFF; } //A very crude percentage calculation percent = (ADC_Read10bit(channel) / 10); if(percent > 100) { percent = 100; } return percent; } /********************************************************************* * Function: ADC_Read10bit(ADC_CHANNEL channel); * * Overview: Reads the requested ADC channel and returns the 10-bit * representation of this data. * * PreCondition: channel is configured via the ADCConfigure() function * * Input: ADC_CHANNEL channel - enumeration of the ADC channels * available in this demo. They should be meaningful names and * not the names of the ADC pins on the device (as the demo code * may be ported to other boards). * i.e. - ADCReadPercentage(ADC_CHANNEL_POTENTIOMETER); * * Output: uint16_t the right adjusted 10-bit representation of the ADC * channel conversion or 0xFFFF for an error. * ********************************************************************/ uint16_t ADC_Read10bit(ADC_CHANNEL channel) { uint16_t result; switch(channel) { case ADC_CHANNEL_0: break; default: return 0xFFFF; } ADCON0bits.CHS = channel; ADCON0bits.GO = 1; // Start AD conversion while(ADCON0bits.NOT_DONE); // Wait for conversion result = ADRESH; result <<=8; result |= ADRESL; return result; } /********************************************************************* * Function: bool ADC_Enable(ADC_CHANNEL channel, ADC_CONFIGURATION configuration); * * Overview: Configures the ADC module to specified setting * * PreCondition: none * * Input: ADC_CHANNEL channel - the channel to enable * ADC_CONFIGURATION configuration - the mode in which to run the ADC * * Output: bool - true if successfully configured. false otherwise. * ********************************************************************/ bool ADC_Enable(ADC_CHANNEL channel) { switch(channel) { case ADC_CHANNEL_0: TRISAbits.TRISA0 = PIN_INPUT; ANCON0bits.PCFG0 = PIN_ANALOG; return true; default: return false; } } /********************************************************************* * Function: bool ADC_SetConfiguration(ADC_CONFIGURATION configuration) * * Overview: Configures the ADC module to specified setting * * PreCondition: none * * Input: ADC_CONFIGURATION configuration - the mode in which to run the ADC * * Output: bool - true if successfully configured. false otherwise. * ********************************************************************/ bool ADC_SetConfiguration(ADC_CONFIGURATION configuration) { if(configuration == ADC_CONFIGURATION_DEFAULT) { ADCON0=0x01; ADCON1=0x9E; return true; } return false; }
zhanzr21 发表于 2016-5-25 20:41 PIC18F46J50的參考ADC函數
reverse_L0111 发表于 2016-5-26 09:24 请问这是microchip官网的DEMO吗?
zhanzr21 发表于 2016-5-26 14:54 是的 官方發布的 Application Library, 就是MLA裡面的函數
reverse_L0111 发表于 2016-5-26 16:46 请问Application Library在哪里找的啊,能发个链接给我吗?
zhanzr21 发表于 2016-5-26 23:49 Microchip Libraries for Applications http://www.microchip.com/mplab/microchip-libraries-for-applic ...
本版积分规则 发表回复 回帖并转播 回帖后跳转到最后一页
人才类勋章
等级类勋章
发帖类勋章
时间类勋章
13
76
0
扫码关注 21ic 官方微信
扫码关注嵌入式微处理器
扫码关注电源系统设计
扫码关注21ic项目外包
扫码浏览21ic手机版
本站介绍 | 申请友情链接 | 欢迎投稿 | 隐私声明 | 广告业务 | 网站地图 | 联系我们 | 诚聘英才
京公网安备 11010802024343号