 |
 |
| View previous topic :: View next topic |
| Author |
Message |
jaka
Joined: 04 May 2014 Posts: 45 Location: Finland
|
| ADC2 (or ADCC) operation with K83 |
Posted: Thu Feb 26, 2026 8:38 am |
|
|
I am trying to use the ADC with computation on PIC18F26K83. My compiler version is 5.116. I have used it on legacy mode for some years and working OK, but I wanted to have some averaging to get rid of small noise in results.
I have tried to set it up on burst average mode, to average 4 samples automatically
| Code: | | setup_adc(ADC_BURST_AVERAGE_MODE | ADC_CLOCK_DIV_16 | ADC_TAD_MUL_64, 2, 4); |
The conversions are started with command
| Code: | | read_adc(ADC_START_ONLY); |
Then the filtered result should be possible to read like this:
| Code: | | fresh_adc_readout = adc_read(ADC_FILTER); |
However, I am getting almost static readings. But if I try to read with the legacy command
| Code: | | fresh_adc_readout = read_adc(ADC_READ_ONLY); |
I am getting the expected result as earlier.
The list file perhaps reveals the problem. This shows the output for three different readout commands. To me it seems that the adc_read() function doesn't read the low byte correctly. The fresh_adc_readout variable is located at 1F1,1F2 accoding to symbol file.
| Code: | .................... unsigned int16 fresh_adc_readout;
....................
.................... if (adc_done()) {
0342E: MOVLB 3E
03430: BTFSC xF8.0
03432: BRA 3536
.................... fresh_adc_readout = adc_read(ADC_FILTER); // Read the previous, filtered result
03434: MOVFFL 3EE7,1F2
0343A: MOVLB 1
0343C: MOVFF EE6,1F1
.................... fresh_adc_readout = read_adc(ADC_READ_ONLY); // Read the previous ADC result with legacy command
03440: MOVLB 3E
03442: BTFSC xF8.0
03444: BRA 3442
03446: MOVFFL 3EF0,1F2
0344C: MOVFFL 3EEF,1F1
.................... fresh_adc_readout = adc_read(ADC_RESULT); // Read the previous ADC result with new command
03452: MOVFFL 3EF0,1F2
03458: MOVLB 1
0345A: MOVFF EEF,1F1 |
Has someone else used the ADC2 peripheral succesfully? Am I doing something wrong with setting up the peripheral? |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20043
|
|
Posted: Sun Mar 01, 2026 7:36 am |
|
|
Have you tried testing for ads_done, before the read?.
How small is the result?. Possibly 1/4 what you expect?. The filter mode
automatically divides by the number of samples. You are specifying to
shift the result 2 places left as well, so 1/4 the normal result. |
|
 |
jaka
Joined: 04 May 2014 Posts: 45 Location: Finland
|
|
Posted: Mon Mar 02, 2026 2:18 am |
|
|
Yes, I test for adc_done, as shown in above list file output.
The result is of approximately correct magnitude, but lacking detail. The low byte of result is static.
I tried to make a minimal example to show the problem, but it doesn't show it. The minimal example list file shows a bit similar compiled output. The old read_adc() uses MOVFFL instructions for both high and low bytes. The new adc_read() uses MOVFFL for high byte, and MOVF+MOVWF for low byte. But this time, the bank setting is correct. In the list file of my previous post it is shown that the bank is set to 1 before MOVFF command so the low byte of the result is nonsense.
In my full project showing the problem, I am able to work around it by reading ADFLTR directly, either via named register or with inline ASM using two MOVFFL commands. Using the named register method, the compiler produces two MOVFFL commands, as shown below:
| Code: | .................... unsigned int16 fresh_adc_readout;
.................... register _ADFLTR unsigned int16 filter_register;
....................
.................... if (adc_done()) {
03430: MOVLB 3E
03432: BTFSC xF8.0
03434: BRA 351A
.................... fresh_adc_readout = filter_register;
03436: MOVFFL 3EE7,1F2
0343C: MOVFFL 3EE6,1F1 |
edit: Using #word as workaroud works too.
| Code: | .................... unsigned int16 fresh_adc_readout;
.................... #word ADFLTR=getenv("SFR:ADFLTRL")
....................
....................
.................... if (adc_done()) {
03430: MOVLB 3E
03432: BTFSC xF8.0
03434: BRA 351A
.................... fresh_adc_readout = ADFLTR;
03436: MOVFFL 3EE7,1F2
0343C: MOVFFL 3EE6,1F1 |
|
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20043
|
|
Posted: Mon Mar 02, 2026 9:00 am |
|
|
OK. You need to report this to CCS. It sounds as if there is an issue with
how the read function is working. |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|