WEll , another day, another bug
This one stems from what i think is a result of sillion issue with the ADC
Chip is a 16F1947 Rev 2 device ID is shown as 2522
The erratica from microchip tells of a ADC problem under some conditions , from what i am seeing is under most conditions and it hangs the cpu
http://ww1.microchip.com/downloads/e...Doc/80497F.pdf
The code has worked fine on other chips ( 16f1825, 16F1829 ,) and does work ok on this one when it does not hang the chip
Typically the chip hangs ranging form 1 min to to 20mins , but the code functions as it should during that time
i confirmed this part of the code by not doing the ADcin command and setting the "Value" variable = to a set number with no other changes and no cpu occured - tested for over 1 hour
Unfortunately i dont have another silicon revision to confirm my thoughts , but a workable get around would confirm the issue
Microchip tells of 2 workarounds , ...
1. is change to dedicated RC oscillator - not an option - when using the internal osc
2. Provide a fixed delay in software to stop the A-to-D conversion manually, after all ten bits are
converted, but before the conversion would complete automatically
Option 2 means that the command of ADCIN cant be used ?? and need to be a set register , counter and flag approch , which i not done for
ADC, so some input on this would be helpful.
snippets of code as it relate to the ADC as follows
regards
Sheldon
my internal is 32Mhz
Code:
DEFINE ADC_BITS 10 ' Number of bits in ADCIN result - Required for adcin command
DEFINE ADC_SAMPLEUS 10 ' ADC sampling time in microseconds - Required for adcin command
' ------------ADC Settings , routines and Variables ----------------
ADCON0 = %00010001 ' bit 7 -N/A , Bit 6-2 = ADC chan Sel /Temperature output /FVR output = AN4 sel = 00100 ,
' Bit 1 - Go/_done status flag - 1 = Go do ADC / 0 = Done ADC , Bit 0 = Enable ADC = 1 / 0 = Disable ADC
ADCON1 = %11110000 ' bit 7 = 1 ADFM ( ADRESH, ADRESL - right justify ).Bit 6-4 ADCS = 111 - using FRC internal ( 1.6us per sample)
' Bit3 - N/A , Bit2 = 0 (Vref- is connected to Vss) , Bit1-0 = 00 - Vref+ connected to VDD
FVRCON = %00000010 ' Bit 7 =0 disable Fixed Voltage Ref Modual, Bit 6 = VRef ready bit =1 always ok on 16F1825, Bit 5 - Temp indicator 1= enable/0 = disable
' Bit 4 = Temp Indicator Range Sel 0=Vout -Low range / 1= Vout -High range , Bit 3-2 = 00( DAC Fixed volt sel )
' Bit 1-0 = 10 - ADC Fixed Vref of 2.048v
AvgCount CON 10 ' Number of samples to average in Average routine
Fastspread CON 200 ' Fast Average threshold +/- in Average routine
ADavg VAR WORD ' Accumulated Varable of Readings in Average routine
Value VAR WORD ' Temp varable assigned in Average routine
' ----------------------------------------------------------------------------------------
y = 0 ' ensure 0
While Y < 10 ' do 10 voltage reads
ADCIN 4 ,Value ' Read PortA.5 value into variable , this applies the fixed voltage ref (via ADCON0,ADCON1,FVRCON)
' to comparitor and ADC pin selected and gives result.
gosub Average ' Go do running average of ADC reading
y = y+1 ' Increment loop counter
wend
' -=-=-=-=-=-= Average Analog values - a DT routine -=-=-=-=-=-=-=-=-=-=
Average:
IF Value = ADavg Then NoChange
IF ABS (Value - ADavg) > Fastspread OR Value < AvgCount Then FastAvg
IF ABS (Value - ADavg) < AvgCount Then RealClose
ADavg = ADavg - (ADavg/AvgCount)
ADavg = ADavg + (Value/AvgCount)
GoTo AVGok
FastAvg:
ADavg = Value
GoTo AVGok
RealClose:
ADavg = ADavg - (ADavg/(AvgCount/4))
ADavg = ADavg + (Value/(AvgCount/4))
AVGok:
Value = ADavg ' Put Average back into Value
NoChange:
Return
Bookmarks