c - 在 C 中为 pic18f1xk22 设置 ADC

标签 c embedded pic18

尝试学习 ADC 设置的初学者,但不幸的是,大多数在线示例都是针对其他 pic18 模型的,而且我没有 adc.h 定义作为资源。 (否则不适用于 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)

最佳答案

还不完美,但大部分都弄清楚了。休息应该不会太困难..现在主要是需要实现中断。

    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){}

关于c - 在 C 中为 pic18f1xk22 设置 ADC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12094869/

相关文章:

使用 USART2 连接两个 PIC18F 入门套件

assembly - 将 TABLAT 值发送到端口给我错误的值

有人可以帮我找出这个程序有什么问题吗? (C)

c - C的xUnit测试框架

c - 通过访问禁止的内存位置注入(inject)内存异常

c - 如果在待发送期间有传入数据包,packetbuf 如何在 ContikiOS 中工作?

c - 跨端口拆分时 PIC18 读/写数据

c - 如何修复选择排序交换访问冲突

c - 试图了解缓冲区溢出和 setuid。我没有特权

embedded - 在 PC 上使用示波器进行精确计时