/*
Find the Period register value for a desired PWM frequency of 2 kHz,
where Fosc = 32 MHz (32 MHz device clock rate) and
a Timer3 prescaler setting of 1:1.
Tcy = 2/Fosc = 62.5 ns
PWM Period = 1/PWM Frequency = 1/2 kHz = 500 μs
PWM Period = (PR2 + 1) * Tcy * (Timer 3 Prescale Value) *PWM周期计算公式
500 μs = (PR2 + 1) * 62.5 ns * 1
PR2 = 800-1
*/
void BZOUT(int Period,int rate)
{
OC1CON1 = 0;//It is a good practice to clear off the control bits initially
OC1CON2 = 0;
OC1CON2.SYNCSEL = 13;
OC1CON2.OCTRIG = 1;
OC1CON1BITS.OCSIDL = 0;// Output capture will continue to operate in CPU Idle mode
OC1CON1BITS.OCTSEL = 1; //This selects the peripheral clock as the clock input to the OC1
//Select Timer3 as the clock input to the OC1
//111 = 系统时钟
//110 = 保留
//101 = 保留
//100 = Timer1
//011 = Timer5
//010 = Timer4
//001 = Timer3
//000 = Timer2
OC1CON1BITS.OCM = 6 ; //110 = PWM模式:OCFA/B 禁用,当OCxTMR = 0 时输出设置为高电平,当OCxTMR = OCxR 时输出设
//置为低电平
T3CON = 0x00; //Stops any 16-bit Timer3 operation
TMR3 = 0x00; //Clear contents of the timer3 register
T3CONBITS.TCKPS = 0; //11 = 1:256
//10 = 1:64
//01 = 1:8
//00 = 1:1
IFS0BITS.T3IF = 0; //Clear the Timer3 interrupt status flag
IEC0BITS.T3IE = 1; //Enable Timer3 interrupts
IEC0BITS.OC1IE = 0; //Disable Compare 1 interrupts
PR3 = Period; //Determine the period
OC1R = rate; //Initial Compare Register1 duty cycle
OC1RS = rate; //Initial Secondary Compare Register1 duty cycle
T3CONBITS.TON = 1; //Start Timer3
}
文章评论(0条评论)
登录后参与讨论