; ConvertFtoC ; Version 1.0 ; Date: November 11, 2014 ; Written By : Yoseph Yegezu .INCLUDE .DSEG C: .BYTE 1 F: .BYTE 1 .CSEG .DEF Denominator=R19 .DEF Quotient=R22 .ORG 0x0000 // Input a Fahrenheit value into r17 TestConvertFtoC: ldi r17,124 sts F, R17 // Call subroutine ConvertFtoC rcall ConvertFtoC rjmp TestConvertFtoC /**************************** * Subroutine converts a temperature reading in Fahrenheit (variable F) to Celsius (variable C). * (F - 32) x 5/9 = C * Range of F (32 to 255) therefore Cmax is 123.8 ****************************/ ConvertFtoC: //Load the F value into r18 lds r18, F //Input the constant value 18 in reg. 16 ldi r16, 32 //This part calculates (°F-32) sub r18, r16 ldi r20, 5 //This part calculates (°F-32)*5 mul r18, r20 //Move the products into r25H and r24L movw r25:r24, r1:r0 //Input the denominator into r19 ldi Denominator, 9 //Call the 16 bit by 8 bit division rcall Div16_8 sts C, r22 ret /************************************ * Quotient = Numerator/Denominator * * r23:r22 = r25:r24 / r19 * ************************************/ Div16_8: clr r2 clr r23 clr Quotient // r22 // Quotient is going to increment by 1 everytime L1 loops // Loop L1 stops when the numerator-denominator = less than 10(the demoninator) // This part calculates (°F-32)*5/9 L1: inc Quotient adc r23,r2 sub r24,Denominator // r19 sbc r25,r2 brcc L1 // Since the quotient is incremented by 1 when the loop began, after the loop quotient is dec dec Quotient sbc r23,r2 // Notice L1 is going to branch off when the numerator is no lnger divisiable by the denominator. // Which means L1 is branching off when r24-denominator results in a negative value. // Therefore, the denominator is going to be added to the r24 after the loop. add r24,Denominator adc r25,r2 ret