No announcement yet.

PBP's ADCIN command

  • Filter
  • Time
  • Show
Clear All
new posts

  • PBP's ADCIN command

    Does PBP compiled code for the ADCIN command poll the GO/DONE bit, or wait for a ADC interrupt flag, or neither before setting the GO/DONE bit for the next conversion cycle?

    I have some code involving ADCIN in a loop that's working fine. It DEFINES an ADC_PAUSEUS time because I want a precise sample rate. I assume PAUSEUS is a software timer. I foresee, as I my code develops further, that I may want to sneak some code into the loop to use that dead time between ADC samples. That code will eat up some of the PAUSEUS time, so the PAUSEUS time will have to be reduced to maintain a precise sample rate. But I am curious, maybe too curious. Not withstanding the possibility that my precise sample rate may suffer if the PAUSEUS time has to be eliminated, will ADCIN start a new conversion before the preceding conversion is complete?

  • #2
    If you look at the mechanics of an ADC read, first you set up the ADCON1_2 registers (by whatever names for individual PICs) for VREF+/-, clock source, alignment, etc. and turn it on (the ON, not the GO). Next select your channel; AN3 on an older PIC or ANA3 on newer ones. Set GO = 1 and wait for GO = 0. Move ADRESH_L into your variable.

    You could use the ADINT with an ISR that captures your ADC value and sets a flag letting you know. I like to declare a variable that just supplies individual bits for flags:
    Work VAR BYTE
    Dat VAR Work.0
    You wouldn't be able to use ADCIN in conjunction with ADINT. Instead:
    PIE1.6 = 1 ;Set ADIE
    ADCON0 = 7 ;Select Channel 3 and GO = 1
    {do something else}
    IF PIR1.6 = 1 THEN
     AdVar = ADRESH ;Read the ADC value into a variable
     PIR1.6 = 0 ;Clear the ADIF flag
     PIE1.6 = 0 ;Disable the ADIE
     Dat = 1
    Then poll for Dat in your main code
    Hope this helps
    Last edited by mpgmike; 04-01-2019, 08:13 AM. Reason: Removed a single character from code
    We can crack this cotton PIC'n thang!


    • #3
      I'm fairly sure that the ADCIN command does NOT poll the bit before starting the conversion. I looked at the asm code for ADCIN on an 18F26K22 and I don't see any test before it sets the GO bit. Your concerns are valid.

      I write a lot of code that gathers ADC samples with a constant rate. My method is to run the conversion in the background without using ADCIN and pace the loop using a timer. The caveat to this is that my adc readings are behind one loop-cycle because I start a conversion immediately after reading the results of the last conversion. This could be corrected easily if you have the time to wait for the conversion to finish and gather the result immediately. The advantage is that I can do lots of stuff in code besides the conversion and I don't use an interrupt. A snippet from my code might look like this:

          GOSUB wait_for_timer
          GOSUB read_adc
          ' functional code handles new adc data
      GOTO mainloop
          IF ADCON0.1 = 1 THEN RETURN     ' not_finished, abort
          ana_timing.highbyte = ADRESH   ' get the adc result
          ana_timing.lowbyte = ADRESL
          ADCON0.1 = 1                ' start a conversion
          DO ' wait here until Timer3 overflows
              IF PIR3.5 = 1 THEN GOSUB serial_present ' handle incoming serial
          LOOP WHILE PIR4.2 = 0
          TMR3 = time_constant ' preset Timer3 for 10mS overflow  
       PIR4.2 = 0  ' clear overflow flag and continue
      Charles Leo
      ME Labs, Inc.


      • #4
        1. There is,to my knowledge, no DEFINE ADC_PAUSEUS. There is however ADC_SAMPLEUS which I suspect is what you mean. That simply sets the delay between selecting which input should be connected to the sample & hold circuit and actually starting the conversion, ie the time allowed for the capacitor in the sample & hold circuit to charge. If the sample time is too low / too short you won't get accurate results. Although I suppose it'll work I personally would not use that method as a mechanism to get precise sample rate.

        2. ADCIN does not check the GO/DONE bit before starting a conversion but since ADCIN, like most PBP commands, is blocking (ie execution does not continue to next command until current command is finished) there's really no way that I can see for ADCIN to "overrun" it self.As long as you're not trying to use ADCIN both in your main loop AND in an ISR (using DT-INTS for example) or mixing the use of ADCIN and driving the ADC "manually". But once you start driving the ADC manually there's really no reason to use ADCIN.



        • #5
          Thank you gentlemen for taking the time to respond. That's all very helpful and useful information for me.

          Yes, you are correct, Henrick, I meant SAMPLESUS, not PAUSEUS -- my mistake.