bigal_scorpio
Active Member
Hi to all,
I have written a program to display 2 different voltages and below to display the sum of the voltage. My code works ok up to a point. Both numbers are from 10 bit ADC and they are ok when displayed separately, or so I thought!
If both numbers are below xx.5 then the result is correct but if for instance one is 12.6 and the other is 12.5 the sum would show 24.1 instead of 25.1.
This is probably something stupid as that is what I seem to do best! But I just can't see it and I am not really an expert at working with numbers. Many of my projects consist of turning things on and off when conditions are met, so I don't have a lot of experience with the math part.
Anyone help?
Al
PS please ignore the 'LCDOut $fe,$c0,"portC=",DEC disp lines, they were just to test something and will be deleted eventually.
I have written a program to display 2 different voltages and below to display the sum of the voltage. My code works ok up to a point. Both numbers are from 10 bit ADC and they are ok when displayed separately, or so I thought!
If both numbers are below xx.5 then the result is correct but if for instance one is 12.6 and the other is 12.5 the sum would show 24.1 instead of 25.1.
This is probably something stupid as that is what I seem to do best! But I just can't see it and I am not really an expert at working with numbers. Many of my projects consist of turning things on and off when conditions are met, so I don't have a lot of experience with the math part.
Anyone help?
Al
PS please ignore the 'LCDOut $fe,$c0,"portC=",DEC disp lines, they were just to test something and will be deleted eventually.
Code:
'****************************************************************
'* Name : 16f872 multi ADC.BAS *
'* Author : Platform = PicBasicPro Program written by BigAl *
'* Notice : With help from many, too numerous to list *
'* : No Rights Reserved *
'* Date : 18/06/2012 *
'* Version : 3.0 *
'* Notes : Program to display both negative and positive of *
'* : PSU 15-0-15 *
'****************************************************************
DEFINE OSC 8
' Define LCD pins
Define LCD_DREG PORTB
Define LCD_DBIT 4
Define LCD_RSREG PORTB
Define LCD_RSBIT 2
Define LCD_EREG PORTB
Define LCD_EBIT 3
' Define ADCIN parameters
DEFINE ADC_BITS 10 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS
TRISC = %00001111
TRISA = %00011111
' variables
therm var word
posval var WORD
negval var WORD
static var WORD
adval Var Word ' Create variables to store results
five var word
disp var byte
temp var byte
varamp var word
ADCON1 = %10000010 ' Set PortA to A/D inputs
Pause 100 ' Wait for LCD to start
Goto mainloop ' Skip subroutines
' Subroutine to read a/d converter
getad:
PAUSEuS 50 ' Wait for A/D channel acquisition time
ADCON0.2 = 1 ' Start conversion
WHILE ADCON0.2
WEND ' Wait for it to complete
adval.highbyte = ADRESH 'Write conversion result
adval.lowbyte = ADRESL
adval = (adval */ 500)>>2 ' Equates to: (adval * 500)/1024
PAUSEUS 50
disp = PORTC & $0f
pause 2
Return
' Subroutine to get pot x value
getposval:
ADCON0 = $41 ' Set A/D to Fosc/8, Channel 0, On
Gosub getad
posval = (adval * 41)
Return
' Subroutine to get pot negval value
getnegval:
ADCON0 = $49 ' Set A/D to Fosc/8, Channel 1, On
Gosub getad
negval = (adval * 46)
Return
' Subroutine to get 5v current value
getfive:
ADCON0 = $51 ' Set A/D to Fosc/8, Channel 2, On
Gosub getad
five = adval
Return
' Subroutine to get temperature value
gettherm:
ADCON0 = $59 ' Set A/D to Fosc/8, Channel 4, On
Gosub getad
therm = adval
Return
' Subroutine to get variable amps value
getvaramp:
ADCON0 = $61 ' Set A/D to Fosc/8, Channel 4, On
Gosub getad
varamp = adval
Return
mainloop:
Gosub getposval ' Get + value
Gosub getnegval ' Get - value
Gosub getfive ' Get 5v rail amps
Gosub getvaramp ' Get variable amps
select case disp
case 1
goto volts
case 2
goto amps
case 4
goto temper
case 8
goto sumvolt
end select
volts:
LCDOut $fe,1,"POS Volts=+",DEC (posval/1000),".", DEC2 posval ' Display the decimal value
LCDOut $fe,$c0,"NEG Volts=-",DEC (negval/1000),".", DEC2 negval ' Display the decimal value
'LCDOut $fe,$c0,"portC=",DEC disp
Pause 200
Goto mainloop
amps:
if five >1023 then five = 0
if five <0 then five = 0
LCDOut $fe,1,"+5 Rail = ",DEC (five*4 - 40),"mA" ' Display the decimal value
LCDOut $fe,$c0,"Var Rail = ",DEC (varamp*4 - 40),"mA" ' Display the decimal value
'LCDOut $fe,$c0,"portC=",DEC disp
Pause 200
Goto mainloop
temper:
temp = ((((therm-131)*100)/323)-4)
LCDOut $fe,1,"Temp ",$df,"c= ",DEC temp,".", DEC1 temp ' Display the decimal value
if temp > 20 then goto fanon
LCDOut $fe,$c0,"Cooling Fan OFF" ' Display fan condition
'LCDOut $fe,$c0,"portC=",DEC disp
Pause 200
Goto mainloop
sumvolt:
LCDOut $fe,1,"Sum Volts= ",DEC (posval/1000 + negval/1000),".", DEC2 (posval + negval) ' Display the decimal value
LCDOut $fe,$c0,DEC (posval/1000),".", DEC2 posval,"v & -",DEC (negval/1000),".", DEC2 negval,"v" ' Display the decimal value
'LCDOut $fe,$c0,"portC=",DEC disp
Pause 200 ' Do it about 5 times a second
Goto mainloop ' Do it forever
fanon: LCDOut $fe,$c0,"Cooling Fan ON" ' Display fan condition
'LCDOut $fe,$c0,"portC=",DEC disp
Pause 200
Goto mainloop
End