控制/MCU
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< < span="" >1023)
{ duty = ((float)duty/1023)*(_XTAL_FREQ/(PWM_freq*TIMER2PRE));
// CCPRXL和CCPCON< 5:4 >的转空比
CCP1CONbits.DC1B=0b11&duty;//0b00; //把pwm10位的低两位赋值为< 5:4 >
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 (PORTB< 4:0 >pins 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.h"
void 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% 形式,下载到开发板可以看看效果。
全部0条评论
快来发表一下你的评论吧 !