Hi guys, my PIC is connected to a buzzer, and I want to make my code in way that when I press a button the frequency of the buzzer changes.
To change that frequency, I *MUST* use interrupts.
I use cc5x compiler.
I was thinking of something like:
Code:
void interrupt isr() {
static volatile bit saved_dtr;
saved_dtr = dtr;
; // save the current status of dtr
; // disable reception of serial chars
dtr = B_HIGH;
if(TMR1IF) {
++tmr_aux;
TMR1IF = false;
PORTB.1=~PORTB.1;
}
dtr = saved_dtr;
; // restore the status of dtr
}
}
I´m at school now and I can´t test the code.
I need some directions on how to make interrupts with variable timers, and some documentation on TIMERS in the cc5x compiler.
Thanks in advance, I really need to understand that.
----
My finished code looks like this now
Code:
void interrupt isr(void)
{
if ( T0IF) /* TMR0 overflow interrupt */
{
CLK3=~CLK3; //The buzzer port
TMR0 = 0; // For reseting Timmer 0 value
T0IF = 0; /* reset Timmer 0 flag INTCON<2>*/
}
}
(...)
main()
(...)
T0CON = 0b.1100.0100;
TMR0 = 0; /* 256 @ 64 = 1.64ms/2 delay */
INTCON = 0xF0; // Enable interupt
(...)
/*the function I call if the button is pressed (Every time the button is pressed, the frequency should increase to a maximum then return to the first value)*/
void muda_p(void)
{
switch(T0CON)
{
case 0b.1100.0111: T0CON=0b.1100.0100; break;
case 0b.1100.0110: T0CON=0b.1100.0111; break;
case 0b.1100.0101: T0CON=0b.1100.0110; break;
case 0b.1100.0100: T0CON=0b.1100.0101; break;
}
}
void interrupt isr(void)
{
if ( T0IF) /* TMR0 overflow interrupt */
{
CLK3=~CLK3;
TMR0 = PreSet; // For reseting Timmer 0 value
T0IF = 0; /* reset Timmer 0 flag INTCON<2>*/
}
}
Then changing the value of Preset will alter the frequency. However, Timer0 is very inflexible, timer2 is much more useful for this and you can just change the value of PR2.
You need to set the prescaler to something sensible - see the OPTION register for details. You then need to put a value in a variable as I posted earlier.
You need to setup T2CON. If you set it to 0x05 it will make PR2=200 give a frequency of 1250Hz which should be a nicer sound. You also need to set PEIE or your interrupts won't work.