![]() | ![]() | ![]() |
| |||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
![]() |
| | Tools |
| | #1 |
|
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
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), " ")
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.....
__________________ The Loosers Now Might Be Later To win For The Time's They're a Changing | |
| |
|
| Tags |
| adc, troubleshooting |
| Thread Tools | |
| Display Modes | |
| |
Similar | ||||
| Title | Starter | Forum | Replies | Latest |
| 18f2550 programming (HW) | lez | Micro Controllers | 4 | 1st November 2009 10:44 AM |
| 18F2550 USB on VB Express 2008 | serkanc | Micro Controllers | 4 | 18th May 2008 02:04 PM |
| Junebug: programming a 18F2550 | UTMonkey | Micro Controllers | 2 | 19th March 2008 09:04 PM |
| 18f2550 and cypress FX2 programmer?? | patroclus | Micro Controllers | 15 | 17th July 2006 09:23 AM |
| 18f2550 programming (SW) | lez | Micro Controllers | 4 | 6th June 2006 12:45 PM |