• AVR Freaks

AnsweredHot!PIC18F46K42 problem reading from DIA

Author
Spacerowa
Starting Member
  • Total Posts : 43
  • Reward points : 0
  • Joined: 2011/12/29 02:27:36
  • Location: Poland
  • Status: offline
2020/01/10 03:23:13 (permalink)
0

PIC18F46K42 problem reading from DIA

Hi,
I am using the PIC18F46K42 on the MPLab Xpress development board and trying to get sensible results reading data from the DIA ( Device Information Area ). If you have succeeded with this PIC, or others in the family, I would like your feedback. I post here as this issue concerns the PIC rather than the Xpress board.
I would like to use the PIC temperature sensor, and have been trying to read the calibration data to use with it from the DIA, in particular the Fixed Voltage Reference data, FVRA1x from memory location 0x3F0030, FVRA2x from location 0x3F0032 and FVRA4x from location 0x3F0034. and also the ADCC count at 90degC for the high range setting.
But for the purpose of this post I want to focus on the FVRA*x data.
My results as displayed in my serial terminal software are: ( top to bottom FVRA1x, FVRA2x & FVRA4x )
"250 3"
"233 7"
"224 15"
The space in the middle is simply due to my formatting in the printf function, to separate high and low bytes.
I cannot see how these results can be used or manipulated to give the FVR ouptut of nominally 1024mV, 2048mV & 4096mV respectively. So I am assuming they are wrong; either original bad data or my code is wrong. More likely the latter as I am only a casual, hobbyist type of programmer, not a professional.
So what is going on ? I would appreciate your advice.
My code is attached; still work in progress obviously.
br Spacerowa. 
 
 
 
 

//Vbr
Spacerowa
#1
JPortici
Super Member
  • Total Posts : 873
  • Reward points : 0
  • Joined: 2012/11/17 06:27:45
  • Location: Grappaland
  • Status: offline
Re: PIC18F46K42 problem reading from DIA 2020/01/10 03:29:30 (permalink) ☼ Best Answerby Spacerowa 2020/01/11 04:19:54
+1 (1)
250, 3 ->  3 * 256 + 250 = 1018 = 1.018V
233, 7 ->  7 * 256 + 233 = 2025 = 2.025V
224, 15 -> 15 * 256 + 224 = 3375 = 3.375V <- sure about this one?
#2
davekw7x
Entropy++
  • Total Posts : 1828
  • Reward points : 0
  • Joined: 2012/01/16 12:01:07
  • Location: Second star on the right, straight on till morning
  • Status: offline
Re: PIC18F46K42 problem reading from DIA 2020/01/10 11:08:37 (permalink)
+2 (2)
Jack_M
224, 15 -> 15 * 256 + 224 = 3375 = 3.375V <- sure about this one?

Actually, 15*256 + 224 = 4064, which is a reasonable value in millivolts
 
 
To the OP: since you are using MCC, you can let functions from the Memory module generate the code to read the 16-bit values.
 
Also, addresses are supplied by XC8 (if you know where/how to look).
 
Part of a test program for my PIC18F47K42:

printf("\r\nCompiled on %s at %s by XC8 version %u\r\n",
__DATE__, __TIME__, __XC8_VERSION);
uint16_t fvra_1x = FLASH_ReadWord(DIA_FVRA1X);
uint16_t fvra_2x = FLASH_ReadWord(DIA_FVRA2X);
uint16_t fvra_4x = FLASH_ReadWord(DIA_FVRA4X);
printf("fvra_1x = 0x%04X = %u\r\n", fvra_1x, fvra_1x);
printf("fvra_2x = 0x%04X = %u\r\n", fvra_2x, fvra_2x);
printf("fvra_4x = 0x%04X = %u\r\n", fvra_4x, fvra_4x);

Output:
Compiled on Jan 10 2020 at 10:01:27 by XC8 version 2100
fvra_1x = 0x03FA = 1018
fvra_2x = 0x07F3 = 2035
fvra_4x = 0x0FE6 = 4070
 
Regards,
 
Dave
 
post edited by davekw7x - 2020/01/10 11:28:53

Attached Image(s)


Sometimes I just can't help myself...
#3
JPortici
Super Member
  • Total Posts : 873
  • Reward points : 0
  • Joined: 2012/11/17 06:27:45
  • Location: Grappaland
  • Status: offline
Re: PIC18F46K42 problem reading from DIA 2020/01/10 11:56:29 (permalink)
+1 (1)
davekw7x
Jack_M
224, 15 -> 15 * 256 + 224 = 3375 = 3.375V <- sure about this one?

Actually, 15*256 + 224 = 4064, which is a reasonable value in millivolts




The operator DERP'd during the process of data input to the calculator pink: pink
#4
mbrowning
USNA79
  • Total Posts : 1578
  • Reward points : 0
  • Joined: 2005/03/16 14:32:56
  • Location: Melbourne, FL
  • Status: online
Re: PIC18F46K42 problem reading from DIA 2020/01/10 14:48:40 (permalink)
+1 (1)
For LF56K42 and LF57K42 -
My DIA read function. Not restoring TBLPTRU context caused problems I recall on 57K42:
uint16_t FLASH_ReadDevice(uint32_t flashAddr) {
    uint16_t data;
    uint8_t  tu;    // temp storage for tblptru
    tu = TBLPTRU;
    NVMCON1bits.NVMREG = 1;        // point to device flash
    TBLPTRU = (uint8_t)((flashAddr & 0x00FF0000) >> 16);
    TBLPTRH = (uint8_t)((flashAddr & 0x0000FF00)>> 8);
    TBLPTRL = (uint8_t)(flashAddr & 0x000000FF);
    asm("TBLRDPOSTINC");
    data = (uint16_t)(TABLAT & 0x00ff);
    asm("TBLRD");
    data = data | ((((uint16_t)TABLAT)<<8) & 0xff00);
    NVMCON1bits.NVMREG = 2;        // back to program flash
    TBLPTRU = tu;
    return data;
}

Reading DIA:
    dia_fvra2x    = FLASH_ReadDevice(0x3f0032);
    dia_tshr    = FLASH_ReadDevice(0x3f002c);

The TS is cal'd at 90C, so at 90C
//  K = -3.8    // -3.8mV/C is nominal slope. I've seen -3.684 used also
 
//  90 = (dia_fvra2x*dia_tshr/4096)/K + Toffset  // Toffset is inherent offset of PIC TS
 
//  Toffset = 90 - (dia_fvra2x*dia_tshr/4096)/K
 
//  and thus
 
//  T = (Vdd*ADC/4096)/K + offset

I setup the ADC for burst averaging (16 samples) and Vdd as Vref+. So 1440 = 90*16, 15565 = 4096*3.8. I cycle through a lot of channels including temp sensor and FVR (with significant averaging to determine Vdd in mV). So the resulting calculations are (ad_temp.wd is from ADC accumulator):
tspic_offset = (1440L+(((int32_t)dia_tshr*(int32_t)dia_fvra2x*16L)/15565L)); // done once at startup
 
adc_temp = (int16_t)((((int32_t)vdd * ad_temp.wd) -15565L) + tspic_offset); // done every ADC reading

edit - note that adc_temp = deg C * 16. I divide by 16 before displaying.
 
A one-point calibration at 25C or so can refine the slope value (since DIA has the 90C cal) which I've found varies enough from part to part to give mediocre results at 25C. But I don't much care until temperature gets much higher where accuracy improves.
post edited by mbrowning - 2020/01/15 14:32:59
#5
Spacerowa
Starting Member
  • Total Posts : 43
  • Reward points : 0
  • Joined: 2011/12/29 02:27:36
  • Location: Poland
  • Status: offline
Re: PIC18F46K42 problem reading from DIA 2020/01/11 04:13:21 (permalink)
0
Hi,
Thanks for all the very helpful responses with the solution to my problem; quite a lot for me to go through and digest. But here are a couple of my initial thoughts, while reading through the replies.
According to the data sheet the values are stored as hex numbers but I hadn't read anywhere that the second number is multiplied by 256 and added to the first.
I had already thought about investigating the possibilities with the memory peripheral and now one of the repondents has suggested it I certainly will be doing so as my next step forward. I just couldn't believe that I wouldn't be successful with just doing a couple of 'table reads' in the main.c file as described in the NVM section of the PIC data sheet. Actually I suppose  I was successful, but just didn't know how to apply the numbers that were coming back !
Thanks again to those who replied. I will be looking at your suggestions, ideas and working code for sure. If further difficulties, or even if I am eventually successful,  I'll post here again.
br Spacerowa
 
 
 
 
post edited by Spacerowa - 2020/01/13 03:27:18

//Vbr
Spacerowa
#6
JPortici
Super Member
  • Total Posts : 873
  • Reward points : 0
  • Joined: 2012/11/17 06:27:45
  • Location: Grappaland
  • Status: offline
Re: PIC18F46K42 problem reading from DIA 2020/01/11 04:43:03 (permalink)
+1 (1)
It doesn't need to be anywhere. It's binary to decimal conversion. The data is stored in little endian format
#7
Spacerowa
Starting Member
  • Total Posts : 43
  • Reward points : 0
  • Joined: 2011/12/29 02:27:36
  • Location: Poland
  • Status: offline
Re: PIC18F46K42 problem reading from DIA 2020/01/12 02:54:53 (permalink)
0
Hi JPortici,
Thanks for pointing out that the values are in fact decimal.
Something I tried out before posting was effectively the same thing, that is to convert to binary and shift the high byte left by 8 places and then add to the low byte. It's standard, but it still wasn't giving useful values, as I had been treating them as hex, because of the statement on p79 of the PIC18F46K42 data sheet which says that " the measured data is stored in the DIA memory region as hexadecimal numbers..."
I am learning about all this rather late in life. I am sorry to say that I still need accurate and complete documentation with lots of examples and explanations; fortunately we have the forums.
I've tried Grappa, maybe you've had some Polish vodka.
Thanks again for your contributions, Ciao.
br Spacderowa.
post edited by Spacerowa - 2020/01/13 03:17:01

//Vbr
Spacerowa
#8
JPortici
Super Member
  • Total Posts : 873
  • Reward points : 0
  • Joined: 2012/11/17 06:27:45
  • Location: Grappaland
  • Status: offline
Re: PIC18F46K42 problem reading from DIA 2020/01/12 12:09:00 (permalink)
+1 (1)
Well yes, Wyborowa is my favourite commercial wodka :)
#9
Spacerowa
Starting Member
  • Total Posts : 43
  • Reward points : 0
  • Joined: 2011/12/29 02:27:36
  • Location: Poland
  • Status: offline
Re: PIC18F46K42 problem reading from DIA 2020/01/15 08:54:57 (permalink)
0
Hi,
Results from my tests to measure temperature using the PIC18F46K42 integrated temperature sensor.
 
VDD = 3V3.
Vref+ = FVR with x2 buffer gain.
Vref- = Vss.
My average ADCC 12bit = 3355.  ( full scale 4095 ).
( 16 count average, I noticed only small difference between average and burst average modes. )
FVRA2x from DIA = 2025.
TSHR2 from DIA = 2475. ( high range ).
Temperature indicator voltage sensitivity -3.684mV/C ( PIC data sheet .pdf see Table 44-26, on p769 ).
 
Tests were being conducted at normal room ambient temperature, approximately 17C.
Using my test results, what measured temperature can be calculated according to Equation 35-1, on p601 ?
Any comments ?
 
 
 
post edited by Spacerowa - 2020/01/15 10:49:53

//Vbr
Spacerowa
#10
coffee critic
Super Member
  • Total Posts : 385
  • Reward points : 0
  • Status: offline
Re: PIC18F46K42 problem reading from DIA 2020/01/15 14:03:18 (permalink)
0
See the equations in AN2798.

n_*$
#11
coffee critic
Super Member
  • Total Posts : 385
  • Reward points : 0
  • Status: offline
Re: PIC18F46K42 problem reading from DIA 2020/01/15 14:03:18 (permalink)
0
dupe.

n_*$
#12
ric
Super Member
  • Total Posts : 25101
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: PIC18F46K42 problem reading from DIA 2020/01/15 15:05:17 (permalink)
0
I've never used that chip, but just using the datasheet formula and your values, I get:
90 + [(ADCmeas - TSHR2) * FVRA2X] / [(2N-1) x Mv]
90 + [(3355 - 2475 ) * 2025] / [4095 * -3.684] = -30.8

So plainly something is not right.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#13
coffee critic
Super Member
  • Total Posts : 385
  • Reward points : 0
  • Status: offline
Re: PIC18F46K42 problem reading from DIA 2020/01/15 16:18:13 (permalink)
0
The voltage I calculate from your raw data seems high.  Try setting the ADC channel to Vss right before setting it to the temp indicator.  Make sure you have 50us delay before conversion to allow the S/H cap to settle. See if the raw reading drops to around 3000.  

n_*$
#14
Spacerowa
Starting Member
  • Total Posts : 43
  • Reward points : 0
  • Joined: 2011/12/29 02:27:36
  • Location: Poland
  • Status: offline
Re: PIC18F46K42 problem reading from DIA 2020/01/17 02:22:08 (permalink)
0
Thanks Coffee Critic,
I tried all of your suggestions but my  average conversion count is still around 3355.
Perhaps with my device I should carry out my own two point calibration not including the TSHR2 value from DIA.
Or obtain another PIC18F46K42 Xpress board for comparison.
It would be useful if other users could tell me their TSHR2 value from DIA as well.
Having the 90degC conversion value in DIA held out the prospect of temperature measurement with acceptable precision and convenience when using the integrated temperature sensor; but in the case of my project, the reality is disappointment.
 
 

//Vbr
Spacerowa
#15
coffee critic
Super Member
  • Total Posts : 385
  • Reward points : 0
  • Status: offline
Re: PIC18F46K42 problem reading from DIA 2020/01/17 09:49:16 (permalink)
0
Can you share your ADC code?

n_*$
#16
mbrowning
USNA79
  • Total Posts : 1578
  • Reward points : 0
  • Joined: 2005/03/16 14:32:56
  • Location: Melbourne, FL
  • Status: online
Re: PIC18F46K42 problem reading from DIA 2020/01/17 10:07:42 (permalink)
+1 (1)
I use the built in one point calibration and generally the output is within a couple C of other temp sensors on the board (using -3.8mv/C). I have seen a few as far off as 7-8C but most are much closer.
 
I suspect you aren't meeting the acquisition times for the sensor. I use 40uS and 16sample burst averaging for all measurements. I don't use the FVR for any direct measurements because I've found the FVR to be very noisy without a lot of averaging. I do measure the FVR every 10ms or so and keep a rolling 16 sample average for the purpose of determining Vdd precisely.
 
On one 57k42, fvra2x = 2039, tshr = 2348.
With Vdd = 2924mV (based on FVR measurement) and temp sensor ADC = 0x7D27 (2002 * 16) software reports 21.6C (thermostat is at 74F) so a couple degrees off.
#17
Jump to:
© 2020 APG vNext Commercial Version 4.5