Microchip PIC系列8位单片机入门教程(7)PWM

01第一节 知识点
(1)捕捉/ 比较/pwm (ccp)模块
pic18f2420/2520/4420/4520 器件配有两个ccp (捕捉/ 比较/pwm)模块。每个模块包含一个16 位寄存器,它可以用作16 位捕捉寄存器、16 位比较寄存器或pwm 主/ 从占空比寄存器。
(2)pwm模式
在脉宽调制(pulse width modulation,pwm)模式下,ccpx 引脚可输出分辨率高达10 位的pwm输出。由于ccp2 引脚与portb 或portc 数据锁存器复用,因此必须清零相应的tris 位才能使ccp2 引脚成为输出引脚。
(3) pwm周期
pwm 周期可以通过写pr2 寄存器来指定。使用以下公式计算pwm 周期:
(4)pwm占空比
pwm 占空比可通过写入ccprxl 寄存器和ccpxcon5:4 位来指定。最高分辨率可达10 位。ccprxl 包含占空比的高8 位,ccpxcon5:4 包含低2 位。由ccprxl:ccpxcon5:4 表示这个10 位值。计算占空比的公式如下:
(5) pwm 模式设置
在为ccp 模块配置pwm 工作模式时应该遵循以下步骤:
1). 通过写pr2 寄存器设置pwm 周期。
2). 通过写ccprxl 寄存器和ccpxcon5:4 位来设置pwm 占空比。
3). 通过清零相应的tris 位将ccpx 引脚配置为输出。
4). 通过写t2con 来设置tmr2 预分频值并使能timer2。
5). 配置ccpx 模块使之工作于pwm 模式。
(6)pwm相关的寄存器
(7)pwm的系统框图
02程序设计
(1)pwm_config.h
/** file: * author:* comments:* revision history:*/// this is a guard condition so that contents of this file are not included// more than once. #ifndef _pwm_config_h_#define _pwm_config_h_#include // include processor files - each processor file is guarded. void pwm_config_init(void);unsignedint duty_percent(unsignedint per);void pwm_config_duty(unsignedint duty);#endif /* _pwm_config_h_ */(2)pwm_config.c
/** file: pwm_config.c* author: greg gong** created on july 4, 2018, 9:25 pm*/#include#include pwm_config.h#include main.h#define timer2pre 16#define pwm_freq 5000/**pwm ccp1的设置,采用基本的pwm 模式,rc2 为pwm的输出端口* pwm的周期是2khz, 占领比为 50%, 4khz.*/void pwm_config_init(void){ ccp1conbits.ccp1m=0b1111;// pwm mpdel. pr2=(_xtal_freq/(pwm_freq*4*timer2pre))-1;; // pwm 的周期为1/5khz tmr2 预分频为16 triscbits.rc2=0; //output port t2conbits.t2ckps=0b11; //16分频 tmr2on=1; //enable timer2}/** 占空比 10位 0~1023 对应 0%~100%***/void pwm_config_duty(unsignedint duty){ if(duty< 1023) { duty = ((float)duty/1023)*(_xtal_freq/(pwm_freq*timer2pre)); // ccprxl和ccpcon的转空比 ccp1conbits.dc1b=0b11&duty;//0b00; //把pwm10位的低两位赋值为 ccpr1l=duty >>2;//0b1001110; // 高八位赋值为 ccpr1l }}unsignedint duty_percent(unsignedint per){ return (unsignedint)(1023*per/100);}(3)main.c
/** file: pwm_main.c* author: greg gong** created on 2021年2月28日, 下午11:07*/// pic18f4520 configuration bit settings#pragma config osc = hs // oscillator selection bits (hs oscillator)#pragma config fcmen = off // fail-safe clock monitor enable bit (fail-safe clock monitor disabled)#pragma config ieso = off // internal/external oscillator switchover bit (oscillator switchover mode disabled)// config2l#pragma config pwrt = off // power-up timer enable bit (pwrt disabled)#pragma config boren = sbordis // brown-out reset enable bits (brown-out reset enabled in hardware only (sboren is disabled))#pragma config borv = 3 // brown out reset voltage bits (minimum setting)// config2h#pragma config wdt = off // watchdog timer enable bit (wdt disabled (control is placed on the swdten bit))#pragma config wdtps = 32768 // watchdog timer postscale select bits (1:32768)// config3h#pragma config ccp2mx = portc // ccp2 mux bit (ccp2 input/output is multiplexed with rc1)#pragma config pbaden = off // portb a/d enable bit (portbpins are configured as analog input channels on reset)#pragma config lpt1osc = off // low-power timer1 oscillator enable bit (timer1 configured for higher power operation)#pragma config mclre = off // mclr pin enable bit (re3 input pin enabled; mclr disabled)// config4l#pragma config stvren = off // stack full/underflow reset enable bit (stack full/underflow will not cause reset)#pragma config lvp = off // single-supply icsp enable bit (single-supply icsp disabled)#pragma config xinst = off // extended instruction set enable bit (instruction set extension and indexed addressing mode disabled (legacy mode))// config5l#pragma config cp0 = off // code protection bit (block 0 (000800-001fffh) not code-protected)#pragma config cp1 = off // code protection bit (block 1 (002000-003fffh) not code-protected)#pragma config cp2 = off // code protection bit (block 2 (004000-005fffh) not code-protected)#pragma config cp3 = off // code protection bit (block 3 (006000-007fffh) not code-protected)// config5h#pragma config cpb = off // boot block code protection bit (boot block (000000-0007ffh) not code-protected)#pragma config cpd = off // data eeprom code protection bit (data eeprom not code-protected)// config6l#pragma config wrt0 = off // write protection bit (block 0 (000800-001fffh) not write-protected)#pragma config wrt1 = off // write protection bit (block 1 (002000-003fffh) not write-protected)#pragma config wrt2 = off // write protection bit (block 2 (004000-005fffh) not write-protected)#pragma config wrt3 = off // write protection bit (block 3 (006000-007fffh) not write-protected)// config6h#pragma config wrtc = off // configuration register write protection bit (configuration registers (300000-3000ffh) not write-protected)#pragma config wrtb = off // boot block write protection bit (boot block (000000-0007ffh) not write-protected)#pragma config wrtd = off // data eeprom write protection bit (data eeprom not write-protected)// config7l#pragma config ebtr0 = off // table read protection bit (block 0 (000800-001fffh) not protected from table reads executed in other blocks)#pragma config ebtr1 = off // table read protection bit (block 1 (002000-003fffh) not protected from table reads executed in other blocks)#pragma config ebtr2 = off // table read protection bit (block 2 (004000-005fffh) not protected from table reads executed in other blocks)#pragma config ebtr3 = off // table read protection bit (block 3 (006000-007fffh) not protected from table reads executed in other blocks)// config7h#pragma config ebtrb = off // boot block table read protection bit (boot block (000000-0007ffh) not protected from table reads executed in other blocks)// #pragma config statements should precede project file includes.// use project enums instead of #define for on and off.#include#include pwm_config.h#include main.hvoid main(void) { pwm_config_init(); pwm_config_duty(duty_percent(50)); return;}(4)main.h
/** file: * author: greg* comments:* revision history:*/// this is a guard condition so that contents of this file are not included// more than once. #ifndef _main_h_#define _main_h_#include // include processor files - each processor file is guarded. #define _xtal_freq 40000000 //定义时钟为40mhz#endif /* xc_header_template_h */我们将代码的占空比设置为100% 形式,下载到开发板可以看看效果。

企业须抓住IoT应用新契机 速度提升公司发展进程
博尔捷数字科技集团董事长侯正宇博士荣登“2022中国人力资源服务100人”榜单
索尼Xperia 1 II详细规格配置一览
谷歌Pixel 2系列更新Android 10后竟无法上网,还尚未解决
如何对医疗设备进行安全测试
Microchip PIC系列8位单片机入门教程(7)PWM
嵌入式Linux市场增长迅速
各领域都在争夺低功耗 看MCU如何成为低功耗争夺之战的焦点
我国集成电路产业快速发展,但却仍被光刻机“掐脖子”?
以嵌入式系统作为移动终端的软件平台浅析
芯片板块回调到位了吗
现代汽车拟9.21亿美元收购波士顿动力
墙壁开关什么牌子好
华为麒麟5G芯片量很大,华为Mate60上架
iPhone 12和华为Mate40已经发布,骁龙875的安卓机还远吗?
ARRAY of DB_ANY中参数实例的使用示例
两会车企代表谈汽车芯片短缺
怎么设计一个基于单片机的交通灯控制系统?
以价格为导向,高性价比Mini LED背光技术介绍
固定频率工作的降压稳压器设计