先补习下PWM :http://baike.baidu.com/view/168039.htm
void BUZZER_PWM_Test( void )
{
U16 freq =800;// lci 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 < 2000 ) //lci 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 ;
}
}
}
这个才是重点,对Timer控制器的控制
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
}
看看Datasheet OK 简单的说明,就可以让你知道了