打印

关于AD采集电压的问题

[复制链接]
1513|16
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
本帖最后由 reverse_L0111 于 2016-5-20 11:48 编辑

我的程序是这样的,AD采集了四次每次的电压值都是一样的,而且结果也是错的  ,每次编译完后ADRESH的值就是0xF0了,
然后每次读到的电压值都是0xF000,请各位帮忙看下是咋回事?新手各种烦啊
型号是PIC18F46K80
                            for(k=0;k<4;k++)
                             {
                                     PORTD=((0x01<<k)|0x30);
                                     ADCON0bits.ADON=1;  
                                     ADCON0bits.GO=1; //ADCON0=0b00000011;启动一次AD转换 GO/DONE=1
                                     while(ADCON0bits.GO=1);
                                     result=(ADRESH<<8)|ADRESL;
                                     Volt[j+k]=5.0*result/4095*1000;
                                     Delay10Ms(10);                     
                                }

                     
沙发
mcumail| | 2016-5-20 14:45 | 只看该作者
IO口的模拟功能打开了没?这个ANSELD寄存器

使用特权

评论回复
板凳
Latin_newday| | 2016-5-20 14:47 | 只看该作者
AD切换通道转换前需要加一定延时,详细可以看数据手册!
你可以参考一下我正常使用的ad转换程序!
u16 ad_convert(u8 channel)
{
        u8 time=0;
        u16 ADResult=0;
       
        for(time=0;time<8;time++)
        {
                ADCON0=(channel<<2)|0x01;                        //AD Enabled
                Delay10TCYx(5);                                                //延时10us
                ADCON0bits.GO_NOT_DONE=1;
                while(ADCON0bits.DONE);               
                ADResult+=ADRESH*256+ADRESL;
        };
        return(ADResult>>3);       
}

使用特权

评论回复
地板
reverse_L0111|  楼主 | 2016-5-20 17:26 | 只看该作者
mcumail 发表于 2016-5-20 14:45
IO口的模拟功能打开了没?这个ANSELD寄存器

设置的AN7啊
ADCON0=0X1C;

使用特权

评论回复
5
reverse_L0111|  楼主 | 2016-5-20 17:29 | 只看该作者
Latin_newday 发表于 2016-5-20 14:47
AD切换通道转换前需要加一定延时,详细可以看数据手册!
你可以参考一下我正常使用的ad转换程序!
u16 ad_c ...

我这边四个电压值都是在同一个通道采集的,只是通过一个多路复用器来切换的

使用特权

评论回复
6
mcumail| | 2016-5-20 17:48 | 只看该作者
reverse_L0111 发表于 2016-5-20 17:26
设置的AN7啊
ADCON0=0X1C;

我没用过你这个片子,我用的是PIC18F46K22,ADC的时候需要设置端口为模拟端口,而非数字端口,相关寄存器是ANSELx,x为端口(如A、B等),你再仔细看看手册吧,应该有类似说明。祝你好运

使用特权

评论回复
7
reverse_L0111|  楼主 | 2016-5-21 09:19 | 只看该作者
本帖最后由 reverse_L0111 于 2016-5-21 09:22 编辑
mcumail 发表于 2016-5-20 17:48
我没用过你这个片子,我用的是PIC18F46K22,ADC的时候需要设置端口为模拟端口,而非数字端口,相关寄存器 ...

        ANCON0bits.ANSEL7=1;刚试了加上上面的设置还是不行,那个ADRESH,ADRESL可以初始化为空吗?再帮忙想下有没有什么其他原因吧,感激

使用特权

评论回复
8
mcumail| | 2016-5-21 09:29 | 只看该作者
reverse_L0111 发表于 2016-5-21 09:19
ANCON0bits.ANSEL7=1;刚试了加上上面的设置还是不行,那个ADRESH,ADRESL可以初始化为空吗?再帮忙 ...

建议仔细研究一下手册,所有的问题都能解决。

使用特权

评论回复
9
兰天白云| | 2016-5-23 13:23 | 只看该作者
手册看足2小时,到时候你就懒得在这里问了

使用特权

评论回复
10
reverse_L0111|  楼主 | 2016-5-23 15:05 | 只看该作者
兰天白云 发表于 2016-5-23 13:23
手册看足2小时,到时候你就懒得在这里问了

我看的肯定不止2小时,是我太笨了
版主能不能给点提示啊,这个问题纠结好多天了

使用特权

评论回复
11
zhanzr21| | 2016-5-25 20:41 | 只看该作者
PIC18F46J50的參考ADC函數

#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;
}

使用特权

评论回复
12
reverse_L0111|  楼主 | 2016-5-26 09:23 | 只看该作者
谢谢上面各位的回复,我这边问题找到了,是负参考电压设置错了

使用特权

评论回复
13
reverse_L0111|  楼主 | 2016-5-26 09:24 | 只看该作者
zhanzr21 发表于 2016-5-25 20:41
PIC18F46J50的參考ADC函數

请问这是microchip官网的DEMO吗?

使用特权

评论回复
14
zhanzr21| | 2016-5-26 14:54 | 只看该作者
reverse_L0111 发表于 2016-5-26 09:24
请问这是microchip官网的DEMO吗?

是的 官方發布的 Application Library, 就是MLA裡面的函數

使用特权

评论回复
15
reverse_L0111|  楼主 | 2016-5-26 16:46 | 只看该作者
zhanzr21 发表于 2016-5-26 14:54
是的 官方發布的 Application Library, 就是MLA裡面的函數

请问Application Library在哪里找的啊,能发个链接给我吗?

使用特权

评论回复
16
zhanzr21| | 2016-5-26 23:49 | 只看该作者
reverse_L0111 发表于 2016-5-26 16:46
请问Application Library在哪里找的啊,能发个链接给我吗?

Microchip Libraries for Applications

http://www.microchip.com/mplab/microchip-libraries-for-applications

使用特权

评论回复
17
reverse_L0111|  楼主 | 2016-5-27 14:45 | 只看该作者
zhanzr21 发表于 2016-5-26 23:49
Microchip Libraries for Applications

http://www.microchip.com/mplab/microchip-libraries-for-applic ...

非常感谢。。。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

13

主题

76

帖子

0

粉丝