710的PWM有两种途径去输出,一般是用输出比较来做PWM的。它可以选择Timer2或Timer3作为其时基。当定时器值与比较寄存器值匹配时,输出引脚的状态发生改变
输出比较模块有7种工作模式
低/高电平有效单事件模式
- 翻转模式
- 延时单事件模式
- 连续单事件模式
- 不带故障保护的PWM模式
- 带故障保护的PWM模式
OCM<2:0>来选择比较模式,怎么选去看芯片资料吧,178页
PWM的详细初始化代码如下(选择了OC1,一共有9个。OC1-OC8)
1 //Initialize Output Compare Module 2 OC1CONbits.OCM = 0b000; //Disable Output Compare Module 3 OC1R = 100; //Write the duty cycle for the first PWM pulse 4 OC1RS = 200; //Write the duty cycle for the second PWM pulse 5 OC1CONbits.OCTSEL = 0; //Select Timer2 as output compare time base 6 OC1R = 400; //Load the Compare Register Value (加载比较储存器值) 7 OC1CONbits.OCM = 0b110; //select the PWM mode 8 9 10 //Initialize and enable Timer2 11 T2CONbits.TON = 0; //Disable Timer 12 T2CONbits.TCS = 0; //Select internal instruction cycle clock(选择内部指令周期时钟 )) 13 T2CONbits.TGATE = 0; //Disable Gated Timer mode 14 T2CONbits.TCKPS = 0b00; //Select 1:1 Prescaler 15 16 TMR2 = 0x00; //Clear timer register 17 PR2 = 500; //Load the period value(载入周期值) 18 19 IPC1bits.T2IP = 0x01; //Set Timer2 Interrupt Priority Level 20 IFS0bits.T2IF = 0; //Clear Timer2 Interrupt Flag 21 IEC0bits.T2IE = 1; //Enable Timer2 Interrupt 22 23 T2CONbits.TON = 1; //Start Timer
只需要初始化PWM的话只要上半部分就可以了。后面使能Timer2的可以放到其它地方