[开发工具] MPLAB X IDE v2.05 +CX8 Pro PIC12F1501的AD问题

[复制链接]
4109|4
 楼主| eleczj 发表于 2014-4-11 00:44 | 显示全部楼层 |阅读模式
RT:
长话短说,PIC新手,高手请轻拍。
代码如下,运行的时候发现AD没有数据出来,也就是AD值一直是0,请帮忙分析下!多谢!
  1. #include <xc.h> // include standard header file
  2. // set Config bits
  3. #pragma config CP = OFF, FOSC = INTOSC     // Code Protection bit (Code protection off).
  4. #pragma config MCLRE = OFF                          
  5. #pragma config WDTE = OFF                           //Watch dog timer off
  6. // Definitions
  7. #define _XTAL_FREQ  4000000       //this is used by the __delay_ms(xx) and __delay_us(xx) functions
  8. //**********************************************************************************
  9. void init( )
  10. {
  11.     // PORT A Assignments
  12.     TRISAbits.TRISA0 = 1;                                        //RA0 = Analog Voltage In
  13.     TRISAbits.TRISA1 = 1;                                        //RA1 = input
  14.     IOCANbits.IOCAN1 = 1;                                        //RA1 NEGATIVE EDGE REGISTER IOC
  15.     TRISAbits.TRISA2 = 0;                                        //RA2 = output
  16.     TRISAbits.TRISA3 = 0;                                        //RA3 = nc (MCLR)
  17.     TRISAbits.TRISA4 = 0;                                        //RA4 = output
  18.     TRISAbits.TRISA5 = 0;                                        //RA5 = output
  19.     // set up oscillator control register.
  20.     OSCCONbits.IRCF = 0x0D;             //set OSCCON IRCF bits 1101 frequency=4Mhz
  21.     OSCCONbits.SCS = 0x02;              //set the SCS bits internal oscillator block
  22.     // set up INTCON register.
  23.     INTCONbits.IOCIE = 1;                      //set INTCON IOCIE bits Enabled
  24.     // set up FVRCON register.
  25.     //FVRCONbits.FVREN = 1;       //Fixed Voltage Reference Enabled 1.024V
  26.     //FVRCONbits.ADFVR = 0x02;    //ADC FVR Buffer Gain is 2x, 2 x VFVR = 2.048V
  27.     // set up ADC control register.
  28.     ANSELAbits.ANSA0 = 1;                                        //Select A0 as analog input pin
  29.     ADCON0bits.CHS = 0x00;              //Set ADC conversion Channel AN0.
  30.     ADCON0bits.ADON = 1;                                        //ADC is on
  31.     ADCON1bits.ADCS = 0x04;             //Set ADC clock FRC x111. 0x04 = Fosc/4
  32.     ADCON1bits.ADFM = 0x01;             //Set Data Right justified.
  33.     ADCON1bits.ADPREF = 0x00;           //Set Vref connected to VDD.

  34. }
  35. //**********************************************************************************
  36. unsigned int Read_ADC_Value(void)
  37. {
  38.     unsigned int ADCValue;
  39.     __delay_ms(10);
  40.     ADCON0bits.GO = 1;              //start conversion
  41.     while (ADCON0bits.GO)  continue;//wait for conversion to finish
  42.     ADCValue = ADRESH << 8;         //get the 2 msbs of the result and rotate 8 bits to the left
  43.     ADCValue = ADCValue + ADRESL;   //now add the low 8 bits of the resut into our return variable
  44.     return (ADCValue);              //return the 10bit result in a single variable
  45. }
  46. //***********************************************************************************
  47. //***********************************************************************************
  48. void main( )
  49. {
  50.     unsigned int AnalogValue = 0;
  51.     unsigned char i = 0;
  52.     init();
  53.     while(1)
  54.     {
  55.         AnalogValue = Read_ADC_Value();
  56.         __delay_ms(400);
  57.         if(AnalogValue <= 200)
  58.             LATAbits.LATA5 ^= 1;
  59.         else
  60.             LATAbits.LATA5 = 0;
  61.     }
  62. }
 楼主| eleczj 发表于 2014-4-11 00:51 | 显示全部楼层
。。。XC8
打错了~
 楼主| eleczj 发表于 2014-4-13 15:40 | 显示全部楼层
:lol...自己搞定了。。。


将ADCON0bits.GO更改为ADCON0bits.GO_nDONE即可。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
d-jason 发表于 2016-12-30 22:29 | 显示全部楼层
如何找到pic12f1501.h???
d-jason 发表于 2017-1-3 21:25 | 显示全部楼层
OK啦!!!!!!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

3

主题

20

帖子

1

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