设置PB0输出PMW波,驱动蜂鸣器发声:
void Buzzer_Freq_Set( U32 freq ) { rGPBCON &= ~3; //set GPB0 as tout0, pwm output rGPBCON |= 2; rTCFG0 &= ~0xff; //prescaler = 15+1 timer input clock frequency=PCLK/{prescaler value+1}/{divider value} rTCFG0 |= 15; rTCFG1 &= ~0xf; //mux = 1/8 rTCFG1 |= 2; 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; }