di2561.txt ;*************************************************************************************************** ; LISTING 1 - ASSEMBLY CODE FOR SOUND GENERATION ; ; "Add harmony to your system," EDN, July 20, 2000, pg 150 ; ; http://www.ednmag.com/ednmag/reg/2000/072000/designideas.htm#15di6 ;*************************************************************************************************** ;*************************************** ; Setup ;*************************************** ldi r_cnt1,first_note ; load counter1 register with first_note value ldi r_cnt2,second_note ; load counter1 register with second_note value ldi r_vol,volume ; load volume register with a value (0-80hex) ldi r_lout,1 ; load output register with low value ldi r_hout,2 ; load output register with high value ;*************************************** ; Square Wave Generator ;*************************************** line1: dec r_cnt1 ; decrement counter1 register breq line5 ; branch if result of decrement equals zero nop ; no operation (waste one instruction time) jmp line7 ; jump to line7 line5: ldi r_cnt1,first_note ; load counter1 register with first_note value eor r_vol1,r_vol ; flip state of first_note intensity (0 or volume) line7: dec r_cnt2 ; decrement counter2 register breq line11 ; branch to line 11 if result of decrement equals zero nop ; no operation (waste one instruction time) jmp line13 ; jump to line13 line11: ldi r_cnt2,second_note ; load counter1 register with second_note value eor r_vol2,r_vol ; flip state of second_note intensity (0 or volume) ;*************************************** ; NCO Summing ;*************************************** line13: add r_acc,r_vol1 ; add first_note intensity to accumulator register brcs line20 ; branch if result of add overflows add r_acc,r_vol2 ; add second_note intensity to accumulator register brcs line22 ; branch if result of add overflows nop ; no operation (waste one instruction time) out portx,r_lout ; output a low level to enunciator jmp line1 ; jump to line1 line20: nop ; no operation (waste one instruction time) adc r_acc,r_vol2 ; add second_note int. and overflow bit to accumulator line22: out portx,r_hout ; output a high level to enunciator jmp line1 ; jump to line1