; ConvertCtoF ; 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 celsius value into r17 TestConvertCtoF: ldi r17,74 sts C, R17 //call subroutine ConvertCtoF rcall ConvertCtoF rjmp TestConvertCtoF /**************************** * subroutine converts a temperature reading in Celsius (variable C) to Fahrenheit (variable F). * F=(C × 9/5) + 32 ==(C × 18/10) + 32 * Range for C input is from (0 to 124), since F max is 255 ****************************/ ConvertCtoF: //Load the C value into r18 lds r18, C //Input the constant value 18 in reg. 16 ldi r16,18 //This part calculates (C*18) mul r18, r16 //Move the products into r25H and r24L movw r25:r24, r1:r0 //Input the denominator into r19 ldi Denominator, 10 //r19,10 //Call the 16 bit by 8 bit division rcall Div16_8 // add 32 to the quotient (18*C)/10 + 32 ldi r26,32 add Quotient,r26 //r22,r26 adc r23,r2 //Store the answer into F sts F, r22 ret /************************************ * Quotient = Numerator/Denominator * * r23:r22 = r25:r24 / r19 * r24 = remainder * ************************************/ Div16_8: clr r2 clr r23 clr Quotient // r22 // quotient is going to increment by 1 every time L1 loops // loop L1 stops when the numerator-denominator is less than the demoninator(10) //(18*C)/10 L1: inc Quotient adc r23,r2 sub r24,Denominator // r24,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