Download filezillafor windows
'----[MAIN PROGRAM CODE]-------------------------------------------------- OVER_INTERRUPT: OutputPORTB.0 ' Make PORTB.0 and output ClearNOTE_COUNTER ' Reset the note counter T1CON= %00000001 ' Turn on TIMER1, with a 1:1 prescaler ClearPIR1.0 ' Clear TMR1 interrupt flag
SetPIE1.0 ' Enable TMR1 as peripheral interrupt source INTCON= %11000000 ' Enable global interrupts, peripheral interrupts NOTE = 0 ' Reset the note to play Repeat ' Create a loop IncNOTE ' Lower the pitch of the note Delayms50 ' Wait 50ms between notes UntilNOTE = 1000 ' Stop when we reach a count of 1000 ClearGIE ' Disable interrupts to stop the note Stop The program above is very simple in principle. A TIMER1 interrupt is imple-
mented by setting the appropriate registers and pointing the compilers inter-
rupt handler to the interrupt subroutine. Whenever TIMER1 overflows. i.e.
reaches a value of 65536, an interrupt will be triggered and the NOTE_INT
subroutine will be ran. Within the NOTE_INT subroutine, TIMER1 is preloaded with a value to e ensure
that another interrupt will occur quickly. A counter variable is then incre-
mented (NOTE_COUNTER) and tested against the required note frequency (NOTE). If both variables are the same then PORTB.0 is toggled by XORing it with itself, and the counter is reset ready for the next occurrence of the inter-
rupt. However if the variables are not equal to each other then the TIMER1 n-
terrupt flag is cleared and the interrupt is exited without doing anything. This happens in the background of the program so placing a value in the variable
NOTE will alter the rate of the toggle on PORTB.0 thus increasing
or decreasing
the frequency of the square wave produced. The circuit shown below can be used for the program above. C1
10uF 10V RB0 18F452
PICmicro Speaker