PDA

View Full Version : I知 having a problem with getting the correct remainder using // remainder.



brianAN49
- 7th March 2016, 17:52
I知 having a problem with getting the correct remainder using // remainder.

I have a 10K res connected to the power rail using the centre pot connected to AN2 of a 16F690.

With the pot at VDD I get a reading of 1023 which I divide by 213 to get the 4.79 voltage rail.

I have a volt meter connected to the wiper of the pot to compare the displayed LCD results.

The remainder does not display correctly.

What am I doing wrong?
'************************************************* ***************
ANSEL = %00000100
ANSELH = %00000000
TRISA = %11111110 'All digital except PortA 0
TRISB = %00000000
TRISC = %00000000

DEFINE OSC 4

' Define LCD registers and bits
DEFINE LCD_DREG PORTC
DEFINE LCD_DBIT 0
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 5
DEFINE LCD_RWREG PORTB
DEFINE LCD_RWBIT 4
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 6
DEFINE LCD_BITS 8
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50
'################################################# ######################
' Set up AD
DEFINE ADC_BITS 10 ' Set number of bits in result
DEFINE ADC_CLOCK 2 ' Set clock source (rc = 3)
DEFINE ADC_SAMPLEUS 50
ANSEL = %00000100 'enable AN2, ALL THE REST DIGITAL
ANSELH = %00000000 'AN8-11 DIGITAL
adcVal var word '
adcVal1 var word
adcVal2 var word
adcVal3 var word
ADCON0 = %10001000 ' ADC Control 1
' 7=1 -1 = Right just, 0 = Left just
' 6=0 -1 = Vref pin, 0= Vdd
' 5=0 -Analog Channel Select Bits
' 4=0 -Analog Channel Select Bits
' 3=1 -Analog Channel Select Bits
' 2=1 -Analog Channel Select Bits
' 1=1 -1 = A/D in progress, 0 = Compleated
' 0=0 -1 = ADC enable, 0 = Disable
ADCON1 = %00000000 ' 7=0 -Unimplimented read as 0
' 6=0 -A/D clock conversion bots
' 5=0 -A/D clock conversion bots
' 4=0 -A/D clock conversion bots
' 3=0 -Unimplimented read as 0
' 2=0 -Unimplimented read as 0
' 1=0 -Unimplimented read as 0
' 0=0 -Unimplimented read as 0
Pause 500 ' Wait .5 second LCD Stablise
start:
Display:
ADCIN 2, adcVal
pauseus 50
adcVal1 = adcVal / 213 'Voltage
adcVal2 = adcVal // 213 'Remainder
Lcdout $fe, $80, "A/D Conversion"
lcdout $fe, $C0, dec adcval,"_",dec adcVal1, ".",dec adcVal2
Pause 500 ' Wait .5 second
Goto Display ' Do it forever

Jerson
- 8th March 2016, 08:00
Using integer math

1023 / 213 = 4 remainder 171
remainder 171*100 / 213 = 80

So, adcVal1 = adcVal / 213
adcVal2 = ((adcVal // 213)*100)/213

Then print the value
This is the quick and easy way. Others might suggest more optimal techniques using PBP

richard
- 8th March 2016, 08:30
maybe like this

100/213 *256 = 120

using the */ operator
1023 */ 120 = 479

so
adcVal1 = adcVal */120

lcdout $fe, $C0, dec adcval1/100 ".",dec1 adcVal1//100

brianAN49
- 8th March 2016, 09:39
Sorry about the delay in replying I'm the other side of the pond and only just had my breakfast.
I will try you codes out and let you know