Adc in pic16f877a...

Discussion in 'Programmer's Corner' started by RRITESH KAKKAR, Mar 23, 2013.

  1. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Hi,

    Testing ADC code in MP lab is this possible??
    Don't know it will work but it is according to data sheet ....

    Code ( (Unknown Language)):
    1. #include <htc.h>
    2. __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
    3. #define _XTAL_FREQ 20000000
    4.  
    5.  
    6. #define DAD PORTB
    7.  
    8.  
    9. main(){
    10.     TRISB=0X00;
    11. TRISA = 0xff ;
    12. ADCON1=0b00000000;
    13. ADCON0=0b10000001;//000 = channel 0, (RA0/AN0)
    14. while(1){
    15.     ADIF=0;
    16.     ADIE=1;
    17.     PEIE=1;
    18.     GIE=1;
    19.    
    20.     __delay_ms(1);
    21. GO_DONE=1;
    22.     __delay_ms(1);
    23.  
    24.     DAD=ADRESH;
    25.     }
    26. }
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    That's my story and I'm sticking to it.
     
  3. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,016

    ls what possible? Do you mean simulator? Just programing for ADC? What? Put some effort into your questions.. You have been told this many times.
     
  4. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
  5. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    OK, do you find any error in this code will this code will work fine??
     
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    You have it backwards. We are not a testing nor inspection unit. YOU test the code. If you have a problem you report such to us, then we may look to see why it happens.

    Your general scheme of copying the left justified contents of the result into a port is sound.
     
  7. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,016
    Im. not going to even bother answering this member anymore. Op has been around long enough to know better. Rarely does the member put any effort into the questions,half the time you have to guess what he wants. I will still monitor the ops threads but until things improve I am done trying to help this member.
     
  8. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Hi again,

    I have tested the code i found that PORTB is giving output to 8 LED's according to analog input by voltage divider ...
    but when i change the voltage via R of voltage divider there is no update in led's output but when the uc is restarted there is change in output.
    so, i think there is problem of updating the output.
     
  9. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    Do you have an in circuit debugger? That will tell you right away what the problem is.
     
  10. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    No, anyway how it will tell me??
     
  11. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    Is it really so hard to connect a 4 digit LED display to a PIC, and observe the A/D result? It is just 12 wires, after all.

    I never use a incircuit debugger, and only rarely I use the simulator. I used it more often with assembler.
     
  12. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    ??????????????????????????
     
  13. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    The Op do not have a programmer that support this. But by using MPLAB SIM you can do a lot of code checking using single stepping,watches and breakpoints. I have pointed the OP countless times to use this tool. And also given links to tutorials. But sad to say. I do not think any of this has caught his attention at all.
    It looks like the OP has looked at the datasheet. But not with much attention to details. This code will enable ADC interrupt
    Code ( (Unknown Language)):
    1.  
    2. ADIF=0;
    3.     ADIE=1;
    4.     PEIE=1;
    5.     GIE=1;
    6.  
    7.  
    But with no ISR to handle the interrupt. The code will not work. And it will be stuck in limbo due to no return from interrupt code segment. The code might work without the latter code segment.
     
    ErnieM likes this.
  14. sharkz

    New Member

    Mar 26, 2013
    1
    0
    ADC output is 10 bit so changes in most significant 2-bit might not show. This might be the reason.
     
  15. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    You are talking about this....i have done this in MPlab in AAC it was by key error.
    DAD=ADRESL;
     
  16. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Not quite. the ADC data is left justified, the 2 least significant bits are not shown, with a Vref of 5v, that is a bit weight of ~20mV, not likely to be the case that Ritesh can reliably alter his ADC input sub 20mV.
     
  17. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Since your data is left justified, ADRESH contains your 8 MSBs, reading just ADRESL only gives you the two least significant bits...
     
  18. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    You have turned on your ADC interrupt, yet there is no code at the interrupt vector (0x0004). Your code does not return from that. I would suggest you take out the interrupt do that you can figure out how to use the ADC first and add the interrupt back once you understand them better.
     
  19. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    I missed the interrupt getting turned on. DO NOT TURN THER INTERRUPOTS ON, unless of course you have an interrupt routine to handle such. Otherwise your code will have a major corruption and may get stuck somewhere.

    At least comment out the GIE=1; line.
     
  20. RG23

    Active Member

    Dec 6, 2010
    301
    2
    Use a debugger like Pickit2

    Put a breakpoint in your program where you are storing the digital value

    and run the program

    Then check the file registers where the value is saved

    Doing this you will get an idea whether you are going in the right direction and then you can manipulate accordingly
     
Loading...