2蜂鸣器(BEEP)
void Buzzer_Freq_Set( U32 freq )
{
rGPBCON &= ~3; //set GPB0 as tout0, pwm output
rGPBCON |= 2;
rTCFG0 &= ~0xff;
rTCFG0 |= 15; //prescaler = 15+1
rTCFG1 &= ~0xf;
rTCFG1 |= 2; //mux = 1/8
rTCNTB0 = (PCLK>>7)/freq;
rTCMPB0 = rTCNTB0>>1; // 50%
rTCON &= ~0x1f;
rTCON |= 0xb; //disable deadzone, auto-reload, inv-off, update TCNTB0&TCMPB0, start timer 0
rTCON &= ~2; //clear manual update bit
}
void Buzzer_Stop( void )
{
rGPBCON &= ~3; //set GPB0 as output
rGPBCON |= 1;
rGPBDAT &= ~1;
}
//***************************[ BOARD BEEP ]*******************************
void Beep(U32 freq, U32 ms)
{
Buzzer_Freq_Set( freq ) ;
Delay( ms ) ;
Buzzer_Stop() ;
}
/****************************************************************************
【功能说明】蜂鸣器PWM测试
****************************************************************************/
void BUZZER_PWM_Test( void )
{
U16 freq = 1000 ;
Uart_Printf( "\nBUZZER TEST ( PWM Control )\n" );
Uart_Printf( "Press +/- to increase/reduce the frequency of BUZZER !\n" ) ;
Uart_Printf( "Press 'ESC' key to Exit this program !\n\n" );
Buzzer_Freq_Set( freq ) ;
while( 1 )
{
U8 key = Uart_Getch();
if( key == '+' )
{
if( freq < 20000 )
freq += 10 ;
Buzzer_Freq_Set( freq ) ;
}
if( key == '-' )
{
if( freq > 11 )
freq -= 10 ;
Buzzer_Freq_Set( freq ) ;
}
Uart_Printf( "\tFreq = %d\n", freq ) ;
if( key == ESC_KEY )
{
Buzzer_Stop() ;
return ;
}
}
}
有四个函数
蜂鸣器频率的设定:void Buzzer_Freq_Set( U32 freq )
蜂鸣器停止:void Buzzer_Stop( void )
板上蜂鸣器测试:void Beep(U32 freq, U32 ms)
蜂鸣器PWM测试:void BUZZER_PWM_Test( void )
1) 蜂鸣器频率的设定:
void Buzzer_Freq_Set( U32 freq )
{
rGPBCON &= ~3; //set GPB0 as tout0, pwm output
rGPBCON |= 2;
rTCFG0 &= ~0xff;
rTCFG0 |= 15; //prescaler = 15+1
rTCFG1 &= ~0xf;
rTCFG1 |= 2; //mux = 1/8
rTCNTB0 = (PCLK>>7)/freq;
rTCMPB0 = rTCNTB0>>1; // 50%
rTCON &= ~0x1f;
rTCON |= 0xb; //disable deadzone, auto-reload, inv-off, update TCNTB0&TCMPB0, start timer 0
rTCON &= ~2; //clear manual update bit
}
首先设置端口GPB0为TOUT0功能
rGPBCON &= ~3; //set GPB0 as tout0, pwm output
rGPBCON |= 2;
然后设置预分频和分割值,计数器的输入时钟(CCLK)频率:PCLK/(预分频+1)/分割值 。预分频值和分割值分别由TCFG0,TCFG1来确定。
rTCFG0 &= ~0xff;
rTCFG0 |= 15; //prescaler = 15+1
rTCFG1 &= ~0xf;
rTCFG1 |= 2; //mux = 1/8
接着设定初始值TCMPB3和TCNTB3
rTCNTB0 = (PCLK>>7)/freq;
rTCMPB0 = rTCNTB0>>1; // 50%
由于计数器的输入时钟为PCLK/16/8;而rTCNTB0 = (PCLK>>7)/freq;并且计数计到最大值的一半时发生翻转。因为计数的基值是 由rTCMPB0 = rTCNTB0>>1; // 50%
设定为最大计数值的50%。
最后是定时控制器的设定 rTCON
rTCON &= ~0x1f;
rTCON |= 0xb; //disable deadzone, auto-reload, inv-off, update TCNTB0&TCMPB0, start timer 0
rTCON &= ~2; //clear manual update bit
这样TOUT0就产生了频率是rTCNTB0 ,占空比是50%的方波。
2)蜂鸣器停止:void Buzzer_Stop( void )
void Buzzer_Stop( void )
{
rGPBCON &= ~3; //set GPB0 as output
rGPBCON |= 1;
rGPBDAT &= ~1;
}
通过设定GPB0为输出功能,并输出低电平,使蜂鸣器停止。
2) 板上蜂鸣器测试:void Beep(U32 freq, U32 ms)
void Beep(U32 freq, U32 ms)
{
Buzzer_Freq_Set( freq ) ;
Delay( ms ) ;
Buzzer_Stop() ;
}
输入的是蜂鸣器的频率和蜂鸣器响的时间,通过
Buzzer_Freq_Set( freq ) ;
Delay( ms ) ;
来实现蜂鸣器按照feq的频率响ms的时间。(ms=1,时间为0.32ms)
3) 蜂鸣器PWM测试:void BUZZER_PWM_Test( void )
void BUZZER_PWM_Test( void )
{
U16 freq = 1000 ;
Uart_Printf( "\nBUZZER TEST ( PWM Control )\n" );
Uart_Printf( "Press +/- to increase/reduce the frequency of BUZZER !\n" ) ;
Uart_Printf( "Press 'ESC' key to Exit this program !\n\n" );
Buzzer_Freq_Set( freq ) ;
while( 1 )
{
U8 key = Uart_Getch();
if( key == '+' )
{
if( freq < 20000 )
freq += 10 ;
Buzzer_Freq_Set( freq ) ;
}
if( key == '-' )
{
if( freq > 11 )
freq -= 10 ;
Buzzer_Freq_Set( freq ) ;
}
Uart_Printf( "\tFreq = %d\n", freq ) ;
if( key == ESC_KEY )
{
Buzzer_Stop() ;
return ;
}
}
}
首先接收按键的值U8 key = Uart_Getch();
如果是‘+’,并小于频率的上限,则增加频率10,如果是‘-’,并且大于频率的下限,则减少频率10。如果按下‘esc’则蜂鸣器停止,程序返回。