Hi guys,
can you have a look at this code and telle me if there is something wrong please?
The readings frm the ADC are a little unstable.
I first connected a temperature sensor to AN0 and found the readings were unstable.
Then, thinking the sensor was noisy, a connected a variable resistor insted. But still the problem persists.
Thanks.
Francesco
--------------------------------------------------------------------
Device = 18F242
Clock = 4
Include "convert.bas"
Include "ADC.bas"
Include "USART.bas"
Dim water_temp As Word
'=============== Read Water_Temp =========================
Sub Read_Temp()
GO_DONE =1 //start convertion
While GO_DONE=1 //wait until finished
Wend
water_temp=(ADRESULT*0.0048876)*1000
// 5V full_scale binary value.
// = 5/1023 =0.0048875V = 4.8875 mV per bit.
// Multiply by ADResult and get the voltage.
End Sub
Input(PORTA.0) // Analogue input AN0
ADCON0 = %11000001 // ADC chanel 0, RC clock
ADCON1 = %11001110 // Set Right Justyfied.+Ref VDD
USART.SetBaudrate(br9600)
While true
Read_Temp
USART.Write(DecToStr(water_temp),10,13)
DelayMS(500) //So i can see whilst it is scrolling
Wend
ADC readings are unstable!
Moderators: David Barker, Jerry Messina
- Senacharim
- Posts: 139
- Joined: Tue Aug 10, 2010 5:19 pm
- Location: Ventura, CA
Not sure what's going on with your setup, but here's an excerpt of my ADC code. This code has been serving me in good stead for a few years now.
Note: the voltage flex number will vary depending upon certain conditions... 'Actual_V / ADC_Reading = _voltage_flex
Note: the voltage flex number will vary depending upon certain conditions... 'Actual_V / ADC_Reading = _voltage_flex
Code: Select all
Dim
VoltsBuffer(6) As Word
#option _voltage_flex = 1.2565445026178010471204188481675 'Good for 3.3V VCC
Public Dim
#if _device in (18F25K22, 18LF25K22, 18F26K22, 18LF26K22)
CHS4 As ADCON0.6,
CHS3 As ADCON0.5,
CHS2 As ADCON0.4,
CHS1 As ADCON0.3,
CHS0 As ADCON0.2,
ADON As ADCON0.0,
PVCFG1 As ADCON1.3,
PVCFG0 As ADCON1.2,
NVCFG1 As ADCON1.1,
NVCFG0 As ADCON1.0,
#else
CHS3 As ADCON0.6,
CHS2 As ADCON0.5,
CHS1 As ADCON0.4,
CHS0 As ADCON0.3,
_GO As ADCON0.2,
ADON As ADCON0.0,
VCFG1 As ADCON1.5,
VCFG0 As ADCON1.4,
PCFG3 As ADCON1.3,
PCFG2 As ADCON1.2,
PCFG1 As ADCON1.1,
PCFG0 As ADCON1.0,
#endif
ACQT2 As ADCON2.5,
ACQT1 As ADCON2.4,
ACQT0 As ADCON2.3,
ADCS2 As ADCON2.2,
ADCS1 As ADCON2.1,
ADCS0 As ADCON2.0
'----------------------------------=-=-----------------------------------------
Function Volt_Buff() As Word
'----------------------------------=-=-----------------------------------------
Dim
VoltReading As Word,
Lop,
Voltcount As Byte
'---------------=-=---------------
VoltReading = 0
VoltCount = 0
For Lop = 0 To Bound(VoltsBuffer)
If (VoltsBuffer(Lop) > 0) Then
VoltReading = VoltReading + VoltsBuffer(Lop)
Inc(VoltCount)
EndIf
Next
If (VoltCount > 0) Then
Result = VoltReading / VoltCount
Else
Result = 0
EndIf
End Function
'----------------------------------=-=-----------------------------------------
Public Function ReadVolts() As Word
'----------------------------------=-=-----------------------------------------
Dim
VoltReading As Word,
Lop As Byte
'---------------=-=---------------
'I Shift buffer by 1.
For Lop = 1 To Bound(VoltsBuffer)
VoltsBuffer(Lop -1) = VoltsBuffer(Lop)
Next
'II Get new reading.
VoltReading = ADC.Read(Volt_Pin)
VoltReading = (VoltReading * FlexVoltage) / 10
' VoltReading = ((VoltReading * 1000) / Volt_Calibration) / 10
VoltsBuffer(Bound(VoltsBuffer)) = VoltReading
'III Return average result (sans results of 0)
Result = Volt_Buff()
End Function
'Initialization sequence
SetConvTime(FOSC_32)
Clear(VoltsBuffer)
'Volt_Pin is PORTA.1
#if _device in (18F25K22, 18LF25K22, 18F26K22, 18LF26K22)
ANSELA.0 = 1
TRISA.0 = 1
ANSELA.1 = 1
TRISA.1 = 1
'Channel Select
CHS4 = 0
CHS3 = 0
CHS2 = 0
CHS1 = 1
'Positive Voltage Config
PVCFG1 = 0
PVCFG0 = 0
'Negative Voltage Config
NVCFG1 = 0
NVCFG0 = 0
ACQT2 = 0
ACQT1 = 0
ACQT0 = 0
ADCS2 = 0
ADCS1 = 1
ADCS0 = 1
#else
TRISA.0 = 1
TRISA.1 = 1
CHS3 = 0
CHS2 = 0
CHS1 = 0
CHS0 = 0
PCFG3 = 1
PCFG2 = 1
PCFG1 = 0
PCFG0 = 1
ADCS2 = 0
ADCS1 = 1
ADCS0 = 1
#endif
Surviving Member
Bermuda Triangle Battalion
from 2026 to 1992
Voted "Most likely to time travel"--Class of 2024.
Bermuda Triangle Battalion
from 2026 to 1992
Voted "Most likely to time travel"--Class of 2024.