关于AD采集电压的问题

[复制链接]
 楼主| reverse_L0111 发表于 2016-5-20 11:47 | 显示全部楼层 |阅读模式
本帖最后由 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;
 楼主| reverse_L0111 发表于 2016-5-20 17:29 | 显示全部楼层
Latin_newday 发表于 2016-5-20 14:47
AD切换通道转换前需要加一定延时,详细可以看数据手册!
你可以参考一下我正常使用的ad转换程序!
u16 ad_c ...

我这边四个电压值都是在同一个通道采集的,只是通过一个多路复用器来切换的
mcumail 发表于 2016-5-20 17:48 | 显示全部楼层
reverse_L0111 发表于 2016-5-20 17:26
设置的AN7啊
ADCON0=0X1C;

我没用过你这个片子,我用的是PIC18F46K22,ADC的时候需要设置端口为模拟端口,而非数字端口,相关寄存器是ANSELx,x为端口(如A、B等),你再仔细看看手册吧,应该有类似说明。祝你好运
 楼主| 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可以初始化为空吗?再帮忙想下有没有什么其他原因吧,感激
mcumail 发表于 2016-5-21 09:29 | 显示全部楼层
reverse_L0111 发表于 2016-5-21 09:19
ANCON0bits.ANSEL7=1;刚试了加上上面的设置还是不行,那个ADRESH,ADRESL可以初始化为空吗?再帮忙 ...

建议仔细研究一下手册,所有的问题都能解决。
兰天白云 发表于 2016-5-23 13:23 | 显示全部楼层
手册看足2小时,到时候你就懒得在这里问了
 楼主| reverse_L0111 发表于 2016-5-23 15:05 | 显示全部楼层
兰天白云 发表于 2016-5-23 13:23
手册看足2小时,到时候你就懒得在这里问了

我看的肯定不止2小时,是我太笨了
版主能不能给点提示啊,这个问题纠结好多天了
zhanzr21 发表于 2016-5-25 20:41 | 显示全部楼层
PIC18F46J50的參考ADC函數

  1. #include <adc.h>
  2. #include <stdint.h>
  3. #include <stdbool.h>
  4. #include <xc.h>

  5. #include <adc.h>

  6. #define PIN_ANALOG    1
  7. #define PIN_DIGITAL   0

  8. #define PIN_INPUT     1
  9. #define PIN_OUTPUT    0

  10. /*********************************************************************
  11. * Function: ADC_ReadPercentage(ADC_CHANNEL channel);
  12. *
  13. * Overview: Reads the requested ADC channel and returns the percentage
  14. *            of that conversions result (0-100%).
  15. *
  16. * PreCondition: channel is configured via the ADCConfigure() function
  17. *
  18. * Input: ADC_CHANNEL channel - enumeration of the ADC channels
  19. *        available in this demo.  They should be meaningful names and
  20. *        not the names of the ADC pins on the device (as the demo code
  21. *        may be ported to other boards).
  22. *         i.e. ADC_ReadPercentage(ADC_CHANNEL_POTENTIOMETER);
  23. *
  24. * Output: uint8_t indicating the percentage of the result 0-100% or
  25. *         0xFF for an error
  26. *
  27. ********************************************************************/
  28. uint8_t ADC_ReadPercentage
  29.     (ADC_CHANNEL channel) {
  30.     uint8_t percent;

  31.     switch(channel)
  32.     {
  33.         case ADC_CHANNEL_0:
  34.             break;
  35.         default:
  36.             return 0xFF;
  37.     }
  38.    
  39.     //A very crude percentage calculation
  40.     percent = (ADC_Read10bit(channel) / 10);

  41.     if(percent > 100)
  42.     {
  43.         percent = 100;
  44.     }
  45.     return percent;
  46. }

  47. /*********************************************************************
  48. * Function: ADC_Read10bit(ADC_CHANNEL channel);
  49. *
  50. * Overview: Reads the requested ADC channel and returns the 10-bit
  51. *           representation of this data.
  52. *
  53. * PreCondition: channel is configured via the ADCConfigure() function
  54. *
  55. * Input: ADC_CHANNEL channel - enumeration of the ADC channels
  56. *        available in this demo.  They should be meaningful names and
  57. *        not the names of the ADC pins on the device (as the demo code
  58. *        may be ported to other boards).
  59. *         i.e. - ADCReadPercentage(ADC_CHANNEL_POTENTIOMETER);
  60. *
  61. * Output: uint16_t the right adjusted 10-bit representation of the ADC
  62. *         channel conversion or 0xFFFF for an error.
  63. *
  64. ********************************************************************/
  65. uint16_t ADC_Read10bit(ADC_CHANNEL channel)
  66. {
  67.     uint16_t result;

  68.     switch(channel)
  69.     {
  70.         case ADC_CHANNEL_0:
  71.             break;
  72.         default:
  73.             return 0xFFFF;
  74.     }

  75.     ADCON0bits.CHS = channel;

  76.     ADCON0bits.GO = 1;              // Start AD conversion
  77.     while(ADCON0bits.NOT_DONE);     // Wait for conversion

  78.     result = ADRESH;
  79.     result <<=8;
  80.     result |= ADRESL;

  81.     return result;
  82. }

  83. /*********************************************************************
  84. * Function: bool ADC_Enable(ADC_CHANNEL channel, ADC_CONFIGURATION configuration);
  85. *
  86. * Overview: Configures the ADC module to specified setting
  87. *
  88. * PreCondition: none
  89. *
  90. * Input: ADC_CHANNEL channel - the channel to enable
  91. *        ADC_CONFIGURATION configuration - the mode in which to run the ADC
  92. *
  93. * Output: bool - true if successfully configured.  false otherwise.
  94. *
  95. ********************************************************************/
  96. bool ADC_Enable(ADC_CHANNEL channel)
  97. {
  98.     switch(channel)
  99.     {
  100.         case ADC_CHANNEL_0:
  101.             TRISAbits.TRISA0 = PIN_INPUT;
  102.             ANCON0bits.PCFG0 = PIN_ANALOG;
  103.             return true;

  104.         default:
  105.             return false;
  106.     }
  107. }

  108. /*********************************************************************
  109. * Function: bool ADC_SetConfiguration(ADC_CONFIGURATION configuration)
  110. *
  111. * Overview: Configures the ADC module to specified setting
  112. *
  113. * PreCondition: none
  114. *
  115. * Input: ADC_CONFIGURATION configuration - the mode in which to run the ADC
  116. *
  117. * Output: bool - true if successfully configured.  false otherwise.
  118. *
  119. ********************************************************************/
  120. bool ADC_SetConfiguration(ADC_CONFIGURATION configuration)
  121. {
  122.     if(configuration == ADC_CONFIGURATION_DEFAULT)
  123.     {
  124.         ADCON0=0x01;
  125.         ADCON1=0x9E;
  126.         
  127.         return true;
  128.     }

  129.     return false;
  130. }
 楼主| reverse_L0111 发表于 2016-5-26 09:23 | 显示全部楼层
谢谢上面各位的回复,我这边问题找到了,是负参考电压设置错了
 楼主| reverse_L0111 发表于 2016-5-26 09:24 | 显示全部楼层
zhanzr21 发表于 2016-5-25 20:41
PIC18F46J50的參考ADC函數

请问这是microchip官网的DEMO吗?
zhanzr21 发表于 2016-5-26 14:54 | 显示全部楼层
reverse_L0111 发表于 2016-5-26 09:24
请问这是microchip官网的DEMO吗?

是的 官方發布的 Application Library, 就是MLA裡面的函數
 楼主| reverse_L0111 发表于 2016-5-26 16:46 | 显示全部楼层
zhanzr21 发表于 2016-5-26 14:54
是的 官方發布的 Application Library, 就是MLA裡面的函數

请问Application Library在哪里找的啊,能发个链接给我吗?
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
 楼主| 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

粉丝
快速回复 在线客服 返回列表 返回顶部