Analog to Digital convertor ADC source code (C lang on PIC18, HiTech compiler,MPLAB)

Discussion in 'Programmer's Corner' started by JK-FlipFlop, Aug 15, 2010.

  1. JK-FlipFlop

    Thread Starter Member

    Jul 5, 2010
    111
    0
    I have made this code for those who doesn't know how to program a A2D converter on PIC18 series. (with HITECH compiler and MPLAB)

    Code ( (Unknown Language)):
    1. #include <adc.h>
    2. #include <hitech.h>
    3. #include <pic18.h>
    4. int ADC_VALUE; /* The value from the analog - digital convertor */
    5. main()
    6. {
    7. TRISA=0x3f; /* PORTA as Anlog input */
    8. TRISD=0x00; /* PORTD as output */
    9. while(1) /* loop */
    10. {
    11. ADCON0=0b1000011; /* config : [URL]http://roboticsforyou.blogspot.com/2008/02/pic16f877a-programming-analog-digital.html[/URL] - the osc F bits 7-6 | the channel bits 5-3| start bit2 | none bit 1| A2D on when "1" bit 0| */
    12. ADCON1= 0x00; /* 10 bit / 8 bit convertor when "0x10" 10 bit - "0x00" 8 bit */
    13.  
    14. ADIE  = 0; /* Masking the interrupt */
    15.  ADIF  = 0; /* Resetting the ADC interupt bit */
    16. ADRESL = 0; /* Resetting the ADRES value register */
    17. ADRESH = 0;
    18. GODONE=1;     /* Staring the ADC process */
    19. while(GODONE); /* Wait for conversion complete */
    20. ADC_VALUE = (ADRESH); /* Getting HSB of CCP1, in 8 bit mode only ADRESH in 10 bit mode need the ADRESL to perform the first 8 bits, THE ADRESH perform the 8 last bits*/
    21.  
    22. PORTD = ADC_VALUE; /* perform in PORTD the Value */
    23. }
    24. }
    for more info: http://roboticsforyou.blogspot.com/2008/02/pic16f877a-programming-analog-digital.html
    http://extremeelectronics.co.in/microchip-pic-tutorials/using-analog-to-digital-converter-%E2%80%93-pic-microcontroller-tutorial/
    http://www.expertcore.org/viewtopic.php?f=18&t=603
    http://www.ermicro.com/blog/?p=1408

    I hope it will help for anyone someday...
    (Niv the onlyone who belive...)
     
    Last edited: Sep 27, 2010
Loading...