void pwm1_init()//RE0,RE1
{
PTPER=3125-1;//控制周期,越大频率也越小,14位寄存器,最大取值=2^14=16384
PTCONbits.PTEN = 1; //PWM time base is on
PTCONbits.PTMOD = 0b01; // operates in a Free-Running mode
PTCONbits.PTCKPS = 0b11; // PWM time base input clock period is TCY (1:64 prescale)
PWMCON1bits.PMOD1 = 1; //PWM I/O pin pair is in the Independent PWM Output mod
PWMCON1bits.PEN1H = 1; //PWMxH pin is enabled for PWM output
PWMCON1bits.PEN1L = 1; //PWMxL pin is enabled for PWM output
PWMCON2bits.SEVOPS = 0; //PWM Special Event Trigger Output Postscale
PWMCON2bits.IUE = 1; // Updates to the active PDC registers are immediate
PDC1 = PTPER; //控制占空比,范围是(0~2)*PTPER
}
void pwmINT_init()
{
IPC14bits.PWMIP = 0b001;
IEC3bits.PWMIE = 0b1;
IFS3bits.PWMIF = 0b0;
}
void __attribute__((interrupt, no_auto_psv)) _PWMInterrupt()
{
if(IFS3bits.PWMIF == 1)
{
PTCONbits.PTEN = 1;
IFS3bits.PWMIF = 0;
}
}
int main()
{
pwmINT_init();
pwm1_init();
while(1);
} |