AD Scalling

Suraj143

Active Member
I get 0 to 720 steps from an analog sensor.

I need to scale this 0-720 steps & must show 0.0 to 25.0 on the display.

Ex: If I get 320 AD result then it must show 12.5

What math do I need to follow up?
 
Last edited:
hi,
Do you mean Ex: If I get 360 AD result then it must show 12.5

Multiply the 720 * 100 = 72000 and then divide by 288 , ie: 72000/288 = 250, that means the 72000 product must be a LONG word.

E.
 
16Bit Divide by Constant

I need to divide 16bit number by a constant "144".

The code works but the remainder is just a 8bit number.I need to round up that remainder to the nearest BCD digit so I can directly show on display.

ex: 3200 / 144 = 22.2
2810 / 144 = 19.5 etc.....

Code:
;==========================================================
;16bit division by a constant
;Number_L,Number_H = Number Input 16bit
;Result_H,Result_L = Result
;Remainder = 
;==========================================================
Div16_by_144	clrf	Result_L
		clrf	Result_H
		;
Divide_Loop	movlw	.144			; 10
		subwf	Number_L,W		; subtract 144 from value
		btfss	STATUS,C
		goto	Adjust_Res_H
		movwf	Number_L
Adjust_Res_L	incfsz	Result_L,F
		goto	Divide_Loop
		incf	Result_H,F
		goto	Divide_Loop
	
Adjust_Res_H	movwf	Temp_L
		movf	Number_H,W
		btfsc	STATUS,Z
		goto	Adjust_Remainder
		decf	Number_H,F
		movf	Temp_L,W
		movwf	Number_L
		goto	Adjust_Res_L
Adjust_Remaindermovf	Number_L,W		; remainder
		movwf	Remainder
		return
 
Last edited:
Multiply the remainder by 10 using the 16 bit variable - to do this shift left twice, add the initial value and shift left again. The result will be in the high byte.

Code:
	bcf	STATUS,C	;do Temp = Number * 2
	rlf	Number_L,w
	movwf	Temp_L
	rlf	Number_H,w
	movwf	Temp_H		;assume no carry due to only 8 bit
	rlf	Temp_L,w	;do Temp = Number * 4
	rlf	Temp_H,F
	addwf	Number_L,f	;add Number to make it * 5
	btfsc	STATUS,Z
	incf	Temp_H,f
	movfw	Temp_H
	addwf	Number_H,f
	rlf	Number_L,f	;finally * 2 to get Number * 10
	rlf	Number_H,f
This is untested but I think it should work. Note, it assumes Number_H = 0.

Mike.
 
Hi Mike

No.My dividing constant is 144.So the remainder can vary between 0 - 143.

To scale it I did such a thing.
Remainder area = 0-144
Decimal range = 0.1 - 0.9

144/9 = 16

Code:
Get_Fraction	bcf	STATUS,C	; Fraction round upto BCD
		rrf	Remainder,F	; divide by 16
		bcf	STATUS,C	;
		rrf	Remainder,F	;
		bcf	STATUS,C	;
		rrf	Remainder,F	;
		bcf	STATUS,C	;
		rrf	Remainder,F
		movf	Remainder,W
		return

Am I correct?
 
Last edited:
Hi suraj,

Sorry, I was thinking the remainder was the fractional part (0-255). What you are doing should work fine.

Mike.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…