Author Topic: For PIC ADC Experts (2 channel sampling with isr)  (Read 1597 times)

0 Members and 1 Guest are viewing this topic.

Offline 0xFFF0Topic starter

  • Regular Contributor
  • *
  • Posts: 105
  • Country: de
For PIC ADC Experts (2 channel sampling with isr)
« on: June 07, 2018, 04:41:37 pm »
Hi!
I am a PIC greenhorn. Could someone with experience please help me here? I refer to this project from the microChip.

http://microchipdeveloper.com/8bit:adcautoconvert

It is a sample program in which an ADC is triggered periodically by a timer and makes an interrupt after the conversion. Now I wonder how to do the same procedure if you want to work with 2 analog channels (or more). How do I set the other channel and how do I get the converted value into a 2nd variable? Can this work at all with the interrupt approach?

P.S. I really want to do it with interrupt, because I do not want to wait for the conversion in the while loop.
« Last Edit: June 07, 2018, 04:46:12 pm by 0xFFF0 »
 

Offline StillTrying

  • Super Contributor
  • ***
  • Posts: 2850
  • Country: se
  • Country: Broken Britain
Re: For PIC ADC Experts (2 channel sampling with isr)
« Reply #1 on: June 07, 2018, 04:54:18 pm »
"How do I set the other channel and how do I get the converted value into a 2nd variable?"

ASM ?
Once the ADC is triggered just change to the other channel, next time around it will read the other channel.
If you have only 2 alternating values, an easy way is just to keep incrementing a variable, when its bit0=0, you do one thing when its bit0=1, you do the other. :)
.  That took much longer than I thought it would.
 

Online JPortici

  • Super Contributor
  • ***
  • Posts: 3553
  • Country: it
Re: For PIC ADC Experts (2 channel sampling with isr)
« Reply #2 on: June 07, 2018, 04:58:25 pm »
I don't know if you use any sort of libraries.

I Don't. I work with registers.

So, the curiosity board has a PIC16F1619 (which i never heard of, strange :P from that era i used PIC16F15xx,17xx,18xx and 19xx)

The most current datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/40001770D.pdf
REGISTER 17-1: ADCON0
The Analog channel to be sampled is selected by the CHS field.
Using C code, it's just a matter of writing
Code: [Select]
ADCON0bits.CHS = xxx;where xxx is the number of the channel (0 for AN0, 7 for AN7 and so on)

Since your demo is using the auto sample / convert capability of the ADC module it's just a matter of adding this line, or, in other words, changing the channel, right after
Code: [Select]
adc_result_t adval = ADC_GetConversionResult();
Yes. There is a library function that does the same thing but i have no idea on how to call it, as i tend not to use libraries, esp on 8 bit PICs.

You may notice that the sample rate -per channel- is now halved, as it's going to alternate between two channels.
Will it work? Yes, in theory. In practice, unless the sample time is too short <-> the ADC source impedance is too high, you will have the expected result.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf