4.2 Example 6 – Multiply-accumulate Operation

The example below uses data from the ADC. The ADC should be configured so that the format of the ADC result is compatible with the fractional two’s complement format. For the ATmega83/163, this means that the ADLAR bit in the ADMUX I/O register is set and a differential channel is used. (The ADC result is normalized to one.)

ldi r23,$62 ; Load highbyte of fraction 0.771484375
ldi r22,$C0 ; Load lowbyte of fraction 0.771484375
in r20,ADCL ; Get lowbyte of ADC conversion
in r21,ADCH ; Get highbyte of ADC conversion
call fmac16x16_32 ;Call routine for signed fractional multiply accumulate

The registers R19:R18:R17:R16 will be incremented with the result of the multiplication of 0.771484375 with the ADC conversion result. In this example, the ADC result is treated as a signed fraction number. We could also treat it as a signed integer and call it “mac16x16_32” instead of “fmac16x16_32”. In this case, the 0.771484375 should be replaced with an integer.