【单片机】【710】PWM

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的可以放到其它地方

转载于:https://www.cnblogs.com/iteou/p/6129216.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值