Help with A/D conversion as an interrupt.

Author
CVRIV
Senior Member
  • Total Posts : 125
  • Reward points : 0
  • Joined: 2017/09/06 13:49:25
  • Location: 0
  • Status: offline
2017/11/30 08:49:32 (permalink)
0

Help with A/D conversion as an interrupt.

I'm using a PIC16F883.
 
Hello. So I'm having trouble with A/D conversion as an interrupt. It's such a weird problem. I thought it would be easy, but of course nothing seems to work right the first time around. 
 
I'm actually working in a TEST project, not the actual project. Once I have the code done and working I integrate it into my actually project. I attached the test project.
 
So. First, I'm only working with ADRESH. When I run the code using MPLAB SIM, Everything seems to work just fine EXCEPT, ADRESH is always 0x0000. I have a simple voltage dividing circuit, that consists of a pot and a resistor, connected to RB2. This voltage divider gives me a range ~ 13mv - 4.65v. I have RB2 setup as an analog input. When I step the code, the interrupt occurs and the ISR handles it, but ADRESH is always 0x0000. Im thinking it's because im running the code as a simulator?
 
Assuming that it's because I'm running the code as a sim, I decided to step the code with my PICKIT3. Here's the weird thing. When I step the code and it comes to the line where it's sets ADCON0, it refuses to set the GO bit when I move 0x0063 into ADCON0! If I BSF the GO bit it will work, but it won't cause an interrupt! The GO bit is cleared and ADIF goes high, but no interrupt! Lord have mercy. 
 
Im starting to think the MCU knows it's the end of the semester and I'm trying to finish up! Please help. Thanks.
#1

11 Replies Related Threads

    JorgeF
    Super Member
    • Total Posts : 3299
    • Reward points : 0
    • Joined: 2011/07/09 11:56:58
    • Location: PT/EU @ Third rock from the Sun
    • Status: offline
    Re: Help with A/D conversion as an interrupt. 2017/11/30 10:45:47 (permalink)
    0
    Hi
     
    As far as I could see you are doing only 1 convertion and its in the initialization section of the program.
    You should configure the ADC but not start it (GO) in this seection.
    In your main loop in a suitable position in your algorithm set the "GO" bit to start an actual convertion.
    The interrupt should happen when the convertion finishes.
     
    If, for testing purposes you want to do 1 single convertion, set the "GO" bit imediatly before entering the main loop.
    For things to work you must have all the interrupt machinery configured before you start an ADC convertion.
     
    Also keep in mind that an ADC convertion only takes a few microseconds, so the PIC only has time to execute an handfull of instructions before the convertion finishes.
     
     
    HIH
     
     
    Best regards
    Jorge
     
    post edited by JorgeF - 2017/11/30 10:46:49
    #2
    CVRIV
    Senior Member
    • Total Posts : 125
    • Reward points : 0
    • Joined: 2017/09/06 13:49:25
    • Location: 0
    • Status: offline
    Re: Help with A/D conversion as an interrupt. 2017/11/30 10:54:53 (permalink)
    0
    Hmmm. I was told to set the GO bit initially to get it started and once the interrupt occurs, the ISR would set it again, which is what keeps it going.
     
    I'll try it your way and see what happens. Thanks. 
    #3
    JorgeF
    Super Member
    • Total Posts : 3299
    • Reward points : 0
    • Joined: 2011/07/09 11:56:58
    • Location: PT/EU @ Third rock from the Sun
    • Status: offline
    Re: Help with A/D conversion as an interrupt. 2017/11/30 11:06:28 (permalink)
    0
    Hi
     
    If you start an ADC convertion when handling an ADC interrupt you can choke you PIC.
    You need more time to handle an interrupt than it takes for the ADC to finish a convertion, so when the ISR finally exits there will be another ADIF pending vectoring the program back to the ISR, and so on, and so on,....
     
    Starting an ADC convertion imediatly when configuring the ADC hardware its a bit weird.
    Are you sure you, or whoever told you to, did understand things correctly.
    Its common practice to turn the ADC "ON" when configuring it, but only start (GO) a convertion when needed.
    Amongst other things because the S&H circuit of the ADC needs some time to change to the voltage level at the pin before a convertion can be started. Check the "aquisition time" constraints in the datasheet.
     
     
     
    Best regards
    Jorge
     
     
     
    #4
    qɥb
    Monolothic Member
    • Total Posts : 3332
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: offline
    Re: Help with A/D conversion as an interrupt. 2017/11/30 12:22:07 (permalink)
    0
    As Jorge states, your code is trying to start a new conversion as soon as the previous one finishes, which is probably much quicker than you want it, and will prevent your PIC doing anything but servicing interrupts.
    Also note, your init code is setting the ADON bit and the GO/DONE bits at the same time.
    Section "9.2.1 STARTING A CONVERSION" specifically tells you not to do this.
     
    Also read "9.2.5 SPECIAL EVENT TRIGGER".
    This is a very useful way to take regular ADC measurements at precise intervals.
     

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #5
    PStechPaul
    Super Member
    • Total Posts : 2093
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: Help with A/D conversion as an interrupt. 2017/11/30 13:14:37 (permalink)
    0
    Also, IIRC, the simulator uses ADRESL for register injection, at least for MPLAB 8.92. I think MPLABX uses actual voltage values on the port pin for simulation. I use a file with ADC values (can be decimal or hex), and the line used increments for each conversion and loops back to the first.
     
    The ADC interrupt should trigger on the ADIF flag (at which time the GO/DONE bit is also reset), and it should reset the ADIF flag. You can do some processing on the ADC results in the interrupt, such as combining ADRESH and ADRESL to get the 10 bit result and store it in a global volatile variable (volatile for C), for additional processing in the main program. The GO/DONE bit should be set when you want to start a conversion.
    post edited by PStechPaul - 2017/11/30 15:29:07

     
    #6
    1and0
    Access is Denied
    • Total Posts : 8525
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Help with A/D conversion as an interrupt. 2017/11/30 13:59:38 (permalink)
    0
    PStechPaul
    The ADC interrupt should trigger on the GO/DONE bit and it should restore the IE flag.

    No, ADC interrupt does _not_ trigger on the GO/DONE bit. Interrupt occurs upon completion of an ADC conversion, and nothing will happen to the IE flag.
     

    You can do some processing on the ADC results in the interrupt, such as combining ADRESH and ADRESL to get the 10 bit result and store it in a global volatile variable for additional processing in the main program.

    OP uses assembly where all assembly variables are "global volatile" ;)  To get a 10-bit result into a variable requires just four moves.
     

    The GO/DONE bit should be set when you want to start a conversion.

    Yes.
    #7
    qɥb
    Monolothic Member
    • Total Posts : 3332
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: offline
    Re: Help with A/D conversion as an interrupt. 2017/11/30 14:07:51 (permalink)
    0
    1and0
    The ADC interrupt should trigger on the GO/DONE bit and it should restore the IE flag.
    ...

    Paul is stating what the code in the ISR should do, not what the hardware will do. :)
     
     

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #8
    1and0
    Access is Denied
    • Total Posts : 8525
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Help with A/D conversion as an interrupt. 2017/11/30 14:13:20 (permalink)
    0
    qɥb
    1and0
    The ADC interrupt should trigger on the GO/DONE bit and it should restore the IE flag.
    ...

    Paul is stating what the code in the ISR should do, not what the hardware will do. :)

    I'm sorry, but I'm a bit slow today. Please clarify.
     
     
    #9
    qɥb
    Monolothic Member
    • Total Posts : 3332
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: offline
    Re: Help with A/D conversion as an interrupt. 2017/11/30 14:22:30 (permalink)
    0
    and I'm too quick. I was mentally correcting what he said.
     
    "The ADC interrupt should trigger on the GO/DONE bit "
    -> "You should set the interrupt to trigger when the GO/DONE bit clears" (what actually happens is the ADIF bit is set at the same time. THAT is what triggers the interrupt if ADIE is set.
     
    "and it should restore the IE flag"
    -> "and your ISR should reset the ADIF flag".
     
    I agree, the original text did not give the correct instructions.

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #10
    PStechPaul
    Super Member
    • Total Posts : 2093
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: Help with A/D conversion as an interrupt. 2017/11/30 15:28:01 (permalink)
    0
    Good catch - I edited my post above. Lots of things on my mind...

     
    #11
    CVRIV
    Senior Member
    • Total Posts : 125
    • Reward points : 0
    • Joined: 2017/09/06 13:49:25
    • Location: 0
    • Status: offline
    Re: Help with A/D conversion as an interrupt. 2017/11/30 16:08:01 (permalink)
    0
    I got it working! I actually got it working while in class. Project complete. Thank god. Now I can sleep at night. It runs like garbage, but it works. I am going to revise the hell out of it another time. I have one more project I have to do for him. He was impressed with this project.

    Thanks for everyones help.
    #12
    Jump to:
    © 2018 APG vNext Commercial Version 4.5