Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

18F2550 ADC troubleshooting

Status
Not open for further replies.

theo92

New Member
Hi, I'm here again for ADC troubleshoot.

How to do some simple arithmetic calculations on sampled AD value.
I'm using a MPXH6115 pressure sensor as a ADC input RA0 of 18F2550 for measuring atmospheric pressure. Is it a bad idea?

It has the following transfer function:

Vout = Vs*(0.009*P-0.095)±Error

I'm ignoring the error as the operating temp is 10-50 deg C.

simplifying it we get:

P= (Vo+0.475)÷0.045 KPa

or

P=(166.7*Vo + 79.1825) mm

Now how to calculate it in digital world, i mean in the pic?
the code:
Code:
//Cloud Number Nine
//For Watching The Weather

Device = 18F2550
Clock = 20


// some LCD options...
#option LCD_DATA = PORTB.4
#option LCD_RS = PORTB.3
#option LCD_EN = PORTC.1

//DS1307 software I2C config
#option DS1307_SI2C = true
#option I2C_SDA = PORTB.0
#option I2C_SCL = PORTB.1
// uses LCD and AD libraries...
Include "LCD.bas"
Include "ADC.bas"
Include "convert.bas"
Include "USART.bas"
Include "DS1307.bas"
Include "sensirion"
       
  

// read the AD port and scale for 0 - 5 volts...
Function ADInAsVolt() As Word
   result = (ADC.Read(0) + 1) * 500 / 1024
End Function

// sampled AD value...
Dim ADVal As Word

Dim NoError As Boolean
Dim temperature, humidity, dew_point As Float
Dim TempA, TempB As Byte
Dim buf As Integer

// initialise and clear LCD...
ADCON1 = $07       // PORTE as digital (LCD)
TRISA.0 = 1        // configure AN0 as an input 
ADCON1.7 = 1       // set analogue input on PORTA.0 
DelayMS (500)
LCD.Cls

//Sensirion.Sensirion_Init(PORTC.1, PORTC.2)  // Sensirion.Sensirion_Init(Port.(Data Pin), Port.(Clock pin))
                                            // Data an Clock Pin must be at the same Port
                                            // This is a wrong configuration : Sensirion.Sensirion_Init(PORTB.1, PORTD.2) 
Sensirion.Sensirion_Init_(PORTC, 2, 3)      // Sensirion.Sensirion_Init(Port, Data Pin, Clock Pin)
//

// if the DS1307 device has not been enabled, we need to give
// it some default values - here we set for 2 o'clock  
// on the 19th April 2008...
If Not Enabled Then
   Time.Hour = 14
   Time.Minute = 0
   Time.Second = 00
   Date.Day = 19
   Date.Month = 11
   Date.Year = 8
   DS1307.Write(Time,Date)
EndIf



// main program loop...

   While true
   NoError = Sensirion.Sensirion_Get_Temp_Hum(temperature, humidity)
    If NoError Then
        buf = temperature * 100
        TempA = buf / 100
        TempB = buf Mod 100
        LCD.WriteAt(1,6, "T=",DecToStr(TempA,2),".",DecToStr(TempB,2)," ", 223, "C  ")
        buf = humidity * 100
        TempA = buf / 100
        TempB = buf Mod 100
        LCD.WriteAt(1,12, "Rh=",DecToStr(TempA,2),".",DecToStr(TempB,2)," "," %  ")
        dew_point = Sensirion.Sensirion_Get_Dew_Point(temperature, humidity)       
        
    Else 
        LCD.WriteAt(1,8, "ERROR")
    End If
  
   ADVal = ADInAsVolt
   LCD.MoveCursor (1,1)
   LCD.Write("P=", DecToStr(ADVal / 100), ".", DecToStr(ADVal, 2), " ")
   DS1307.Read(Time, Date)
   LCD.WriteAt(2,1,DecToStr(Time.Hour, 2),":",DecToStr(Time.Minute,2),":",DecToStr(Time.Second,2))
   LCD.WriteAt(2,10,DecToStr(Date.Day,2),".",DecToStr(Date.Month,2),".",DecToStr(Date.Year,1))
 
   Wend
   
   While true
   SetBaudrate(br19200)
   NoError = Sensirion.Sensirion_Get_Temp_Hum(temperature, humidity)
    If NoError Then
        buf = temperature * 100
        TempA = buf / 100
        TempB = buf Mod 100
        USART.Write("*********START************",13,10)
        USART.Write("T=",DecToStr(TempA,2),".",DecToStr(TempB,2)," ", 223, "C  ")
        buf = humidity * 100
        TempA = buf / 100
        TempB = buf Mod 100
        USART.Write("R=",DecToStr(TempA,2),".",DecToStr(TempB,2)," "," %  ",13,10)
        dew_point = Sensirion.Sensirion_Get_Dew_Point(temperature, humidity)       
        
    Else 
        USART.Write("ERROR",13,10)
    End If
  
   ADVal = ADInAsVolt
   
   USART.Write("P=", DecToStr(ADVal / 100), ".", DecToStr(ADVal, 2), " ",13,10)
   DS1307.Read(Time, Date)
   USART.Write(DecToStr(Time.Hour, 2),":",DecToStr(Time.Minute,2),":",DecToStr(Time.Second,2),13,10)
   USART.Write(DecToStr(Date.Day,2),".",DecToStr(Date.Month,2),".",DecToStr(Date.Year,1),13,10)
   USART.Write("***************END***************",13,10)
   DelayMS(18000)
Wend

I know there is a wrong in the last USART out portion.
But the else is 100% correct.
It includes a DS1307 RTC and SHT11.

the code where the modificatuions should be done is:
Code:
 ADVal = ADInAsVolt
   LCD.MoveCursor (1,1)
   LCD.Write("P=", DecToStr(ADVal / 100), ".", DecToStr(ADVal, 2), " ")

how to edit it....I can't understand it.
does the ADVal outputs in Volt or not?
will i need to declare a variable?
like:

dim Pout as integer

Pout=(ADVal+0.475)÷0.045

is it right?

And how to make a ADC as accurate as possible? adding a VREF+?
any other tips?
no need to be accurate like a Cs atomic clock though.

Waiting.....
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top