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% 形式,下載到開發板可以看看效果。
評論
查看更多