2012-08-23 34 views
2

初学者尝试学习ADC设置,但不幸的是,大多数在线示例都是针对其他pic18模型的,并且我没有将adc.h定义作为资源。 (否则不适用于C代码。)不想真正回答问题,但如果任何人都可以提出一些很好的步骤,在线资源等,我会非常感激,谢谢!ADC设置为pic18f1xk22 C

此外,我写的这个伪代码的任何帮助都会很棒。可能会有错误......如果我在正确的轨道上,不知道。

//configure port 
    //disable pin output driver (TRIS) - same thing as clear? 
    //configure pin as analog 
//configure adc module 
    //set ADC conversion clock 
    // configure voltage reference 
    //select adc input channel 
     //CH0-CH12 of ADCON0 
    // select result format 
     //select data format using the ADFM bit of the ADCON1 register             
      //select aquisition delay 
    // turn on ADC module 
     //enable A/D converter by setting the ADON bit of the ADCON0 register              
//start conversion by setting GODONE bit of ADCON0 register 
    //GODONE = 1; 

// read ADC result 
    //read the ADRESH and ADRESL registers 
//clear the adc interrupt flag (optional) 
+2

该微控制器数据表具有所有的SFR位置,ADC只有8个寄存器 - 您可以轻松完成自己的'adc.h'。数据表还有一个汇编程序示例,应该很容易在C中实现。为什么不尝试一些代码并查看它是否有效? –

回答

0

还不完美,但大部分都想通了。休息不应该太困难..只需要现在大部分实现中断。

TRISc = 0x01; // disable tri-state  
    ANSEL = 0xFF;//setting to analog 
    ADCON1 = 0x00;//Voltage References Set 
    ADCON2 = 0x00;// left justified ADFM, set Acquisition Time and Conversion Clock 
    ADCON0 = 0x11;// Analog Channel Select, GODONE set, Enable ADC 

    //TODO:interrupt ADIF bit in the PIR1 register 
    ADIF = 0; 
    ADIE = 1; 
    GODONE = 1; 
    while(GODONE) {}; 

    adcValue = ADRESH; 
    adcValue <<= 8; 
    adcValue |= ADRESL; 

    while(1){}