ADC collecting the ADC value from the AN channel.

Discussion in 'Embedded Systems and Microcontrollers' started by mrasheed, Apr 15, 2014.

  1. mrasheed

    Thread Starter New Member

    Apr 15, 2014
    5
    0
    hey guys i need to collect the ADC value from the AN channel and if that value is grater than some other constant than an LED should be turned ON... here is my code plx i heed your help urgent....
    Code ( (Unknown Language)):
    1.  
    2. #include<htc.h>
    3. #include<pic.h>
    4. #define _XTAL_FREQ 20000000
    5.  
    6. void ADC_Init()
    7. {
    8. ADCON0 = 0x41; //ADC Module Turned ON and Clock is selected
    9. ADCON1 = 0xC0; //All pins as Analog Input
    10. //With reference voltages VDD and VSS
    11. }
    12.  
    13. unsigned int ADC_Read(unsigned char channel)
    14. {
    15. if(channel > 7) //If Invalid channel selected
    16. return 0; //Return 0
    17. ADCON0 &= 0xC5; //Clearing the Channel Selection Bits
    18. ADCON0 |= channel<<3; //Setting the required Bits
    19. __delay_ms(2); //Acquisition time to charge hold capacitor
    20. GO_nDONE = 1; //Initializes A/D Conversion
    21. while(GO_nDONE); //Wait for A/D Conversion to complete
    22. return ((ADRESH<<8)+ADRESL); //Returns Result
    23. }
    24.  
    25. void main()
    26. {
    27. unsigned int a;
    28. PORTB=0x00;
    29. TRISB = 0x00; //PORTB as output
    30. TRISC = 0x00; //PORTC as output
    31. TRISA = 0xFF; //PORTA as input
    32. ADC_Init(); //Initializes ADC Module
    33. do
    34. {
    35. a = ADC_Read(0); //Reading Analog Channel 0
    36.  
    37. if (a>0100){
    38. RB0=1;
    39. }
    40.  
    41. __delay_ms(30); //Delay
    42. }while(1); //Infinite Loop
    43. }
    44.  
     
    Last edited by a moderator: Apr 15, 2014
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    You need to add something to turn the LED off when that is desirable.

    But first you need to put the ADC_Read() insire a loop that repeats. "do..." only runs once, then smacks into an infinite while(1) loop.

    So delete the "do" and put the while there instead.

    And for all that is holy never ever post code without code tags.
     
  3. mrasheed

    Thread Starter New Member

    Apr 15, 2014
    5
    0
    thanx for the reply..
    did that but still does not work... im very new to this can anyone plx help me out

    Code ( (Unknown Language)):
    1.  
    while(1){
    ADC_value = GetADCValue(0); //Reading Analog Channel 0


    if (ADC_value>Temp35){
    RB0=1;
    }
    else
    RB0=0;
    __delay_ms(30); //Delay
    }
     
  4. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,812
    834
    Unless it's a typo, you are missing an open brace after "else". And the corresponding close brace.
     
  5. mrasheed

    Thread Starter New Member

    Apr 15, 2014
    5
    0
    is the code correct except for that. cox its still not working
     
  6. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,365
    When posting code please use code tags:

    [code=rich]
    Your code goes here
    [/code]
     
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    That's not going to matter... the else sets the output low, then either way a delay is encountered. That seems (perhaps accidentally) correct as the two paths from the if can both be 1 statement long.

    You need an explanation with much more detail than "does not work"

    Does it compile? Does it load? Does the LED seem to work? Do you have a schematic to post?

    Currently we even have no idea which PIC you use. The more detail you share the easier it is to see the problem(s).
     
    djsfantasi likes this.
  8. mrasheed

    Thread Starter New Member

    Apr 15, 2014
    5
    0
    it compiles bit the LED will not light up... i have been testing this with proteus.
     
  9. Brownout

    Well-Known Member

    Jan 10, 2012
    2,375
    998
    Post your schematic.
     
  10. fernan82

    New Member

    Apr 19, 2014
    26
    9
    you can try changing
    Code ( (Unknown Language)):
    1.  
    2.  return ((ADRESH<<8)+ADRESL;
    3.  
    to:
    Code ( (Unknown Language)):
    1.  
    2.  return ((((unsigned int)ADRESH)<<8)|ADRESL;
    3.  
    and:
    Code ( (Unknown Language)):
    1.  
    2.  RB0 = 1;
    3.  
    to

    Code ( (Unknown Language)):
    1.  
    2.  LATB |= 1;
    3.  
    and see what happens. BTW, your do loop was correct, the only different that makes is when the condition gets checked and since it's always true it makes no difference.
     
Loading...