Program for Sound

0380;   demo_beep; 0000; NOP;
0381;            ; 1D0B; BTFSS 0x0B, 2; INTCON_TMROIF
0382;            ; 0008; RETURN;
0383;            ; 110B; BCF 0x0B, 2; INTCON_TMROIF
0384;            ; 3099; MOVLW 0x99; W=99h or 153d
0385;     setPW/2; 0020; MOVLB 0x00;
0386;            ; 0095; MOVWF 0x15; TMR0
0387;            ; 1CFD; BTFSS 0x7D, 1; FLAG_beepTog
0388;            ; 3204; BRA $+0x004; pulseHi
0389;     pulseLo; 10FD; BCF 0x7D, 1; FLAG_beepTog
038A;            ; 0022; MOVLB 02;
038B;            ; 128C; BCF 0x0C, 5; LATA_LATA5
038C;            ; 0008; RETURN;
038D;     pulseHi; 14FD; BSF 0x7D, 1; FLAG_beepTog
038E;            ; 0022; MOVLB 02;
038F;            ; 168C; BSF 0x0C, 5; LATA_LATA5
0390;            ; 0008; RETURN;

Using a piezo-electric buzzer, the code sets the pulse width of Timer0 to half the pulse width of the desired frequency. A flag variable is needed to force a toggle between the high and low portion of the waveform. Once the timer has been set, the code above will only perform an action if the timers interrupt flag has been tripped. The timer does not interrupt normal processor flow. When the program loops back around to perform this function it will only toggle the output low or high if the timer0 interrupt flag is high. It then clears the flag and loads the pulse width variable into the timer causing it to start again.