[STC15 library function hands-on notes] 7. PCA and PWM

Publisher:MengyunLatest update time:2022-08-01 Source: csdnKeywords:STC15 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

STC Experiment Box 4

IAP15W4K58S4

Keil uVision V5.29.0.0

PK51 Prof.Developers Kit Version:9.60.0.0


Hard Knowledge

Hardware knowledge

       Excerpted from "STC15 Series MCU Device Manual"

       Some STC15 series MCUs integrate 3-channel programmable counter array (CCP/PCA) modules (STC15W4K32S4 series MCUs have only 2-channel CCP/PCA), which can be used for software timer, external pulse capture, high-speed pulse output and pulse width modulation (PWM) output.

       The following table summarizes the STC15 series MCU models that integrate CCP/PCA/PM functions:

insert image description here

       In the above table, √ indicates that the corresponding series has the corresponding function.

insert image description here

Structure of CCP/PWM/PCA module

       Some MCUs in the STC15 series have 3-channel programmable counter arrays CCP/PCA/PWM (CCP/PCA/PWM can be set to switch from port P1 to port P2 to port P3 through the AUXR1/P_SW1 register).

       The PCA contains a special 16-bit timer with three 16-bit capture/compare modules connected to it, as shown in the figure below.

insert image description here

       Each module can be programmed to work in 4 modes: rising/falling edge capture, software timer, high-speed pulse output or modulated pulse output.

       The 16-bit PCA timer/counter is the common time base for the three modules, and its structure is shown in the figure below.

insert image description here

Software knowledge

       Excerpted from "STC Library Function Usage Reference"


PCA initialization function

PCA_Init

insert image description here

PCA_id: Select the PCA channel to initialize:

insert image description here

The definition of PCA_InitTypeDef is found in the file "PCA.H".


typedef struct

{

u8 PCA_IoUse;

u8 PCA_Clock; 

u8 PCA_Mode; 

u8 PCA_PWM_Wide;

u8 PCA_Interrupt_Mode; 

u8 PCA_Polity;

u16 PCA_Value;

} PCA_InitTypeDef;


PCA_IoUse: Select the IO used by PCA: the value when initializing PCA_Counter, ignored when initializing PCA0~PCA2

insert image description here

PCA_Clock: Select the clock used by PCA: the value used when initializing PCA_Counter, ignored when initializing PCA0~PCA2

insert image description here

PCA_Mode: Set the working mode of the PCA channel: the value when initializing PCA0~PCA2, ignored when initializing PCA_Counter

insert image description here

PCA_PWM_Wide: Set the PWM width when the PCA channel works in PWM mode: Initialize the value of PCA0~PCA2 when working in PWM mode, ignore when initializing PCA_Counter or other modes of PCA channel

insert image description here

PCA_Interrupt_Mode: interrupt enable or disable: value when initializing PCA0~PCA2, ignored when initializing PCA_Counter

insert image description here

Note: The above parameters can be combined as follows:


PCA_InitStructure.PCA_Interrupt_Mode = PCA_Fall_Active | ENABLE; //Falling edge interrupt, enable interrupt.

PCA_InitStructure.PCA_Interrupt_Mode = PCA_Rise_Active | ENABLE; //Rising edge interrupt, enable interrupt.

PCA_InitStructure.PCA_Interrupt_Mode = PCA_Rise_Active | PCA_Fall_Active | ENABLE; //Rising edge and falling edge interrupts, enable interrupts.


If | DISABLE is used afterwards, the interrupt is disabled.


PCA_Polity: Interrupt priority: value taken when initializing PCA_Counter, ignored when initializing PCA0~PCA2

PCA_Value: Set the initial value of the PCA channel. The value used when initializing PCA0~PCA2, ignored when initializing PCA_Counter

insert image description here

PWM update duty cycle function

UpdatePwm

insert image description here


test program

PWM mode generates PWM signal

The UpdatePwm function in PCA.c is commented out by default and needs to be uncommented:

insert image description here

main.c

#include "./Drivers/config.h"

#include "./Drivers/delay.h"


#include "./Drivers/GPIO.h"

#include "./Drivers/PCA.h"


void GPIO_config(void)

{

GPIO_InitTypeDef GPIO_InitStructure; //Structure definition

GPIO_InitStructure.Mode = GPIO_OUT_PP; //Specify IO input or output mode, GPIO_PullUp, GPIO_HighZ, GPIO_OUT_OD, GPIO_OUT_PP

GPIO_InitStructure.Pin = GPIO_Pin_5 | GPIO_Pin_6; //Specify the IO to be initialized, or operation

GPIO_Inilize(GPIO_P2, &GPIO_InitStructure); //P2.5 and P2.6 are initialized as push-pull output

}


void PCA_config(void)

{

PCA_InitTypeDef PCA_InitStructure;


PCA_InitStructure.PCA_Mode     = PCA_Mode_PWM; //PCA_Mode_PWM, PCA_Mode_Capture, PCA_Mode_SoftTimer, PCA_Mode_HighPulseOutput

PCA_InitStructure.PCA_PWM_Wide = PCA_PWM_8bit; //PCA_PWM_8bit, PCA_PWM_7bit, PCA_PWM_6bit

PCA_InitStructure.PCA_Interrupt_Mode = DISABLE; //PCA_Rise_Active, PCA_Fall_Active, ENABLE, DISABLE

PCA_InitStructure.PCA_Value = (u16)((1 << 8) * (1 - 0.5)); //For software timing, it is the match comparison value

PCA_Init(PCA0,&PCA_InitStructure);


PCA_InitStructure.PCA_Mode     = PCA_Mode_PWM; //PCA_Mode_PWM, PCA_Mode_Capture, PCA_Mode_SoftTimer, PCA_Mode_HighPulseOutput

PCA_InitStructure.PCA_PWM_Wide = PCA_PWM_7bit; //PCA_PWM_8bit, PCA_PWM_7bit, PCA_PWM_6bit

PCA_InitStructure.PCA_Interrupt_Mode = DISABLE; //PCA_Rise_Active, PCA_Fall_Active, ENABLE, DISABLE

PCA_InitStructure.PCA_Value = (u16)((1 << 7) * (1 - 0.5)); //For software timing, it is the match comparison value

PCA_Init(PCA1,&PCA_InitStructure);


PCA_InitStructure.PCA_Clock    = PCA_Clock_1T; //PCA_Clock_1T, PCA_Clock_2T, PCA_Clock_4T, PCA_Clock_6T, PCA_Clock_8T, PCA_Clock_12T, PCA_Clock_Timer0_OF, PCA_Clock_ECI

PCA_InitStructure.PCA_IoUse    = PCA_P24_P25_P26_P27;//PCA_P12_P11_P10_P37, PCA_P34_P35_P36_P37, PCA_P24_P25_P26_P27

PCA_InitStructure.PCA_Interrupt_Mode = DISABLE; //ENABLE, DISABLE

PCA_InitStructure.PCA_Polity = PolityHigh; //Priority setting PolityHigh, PolityLow

PCA_Init(PCA_Counter,&PCA_InitStructure);

}


void main(void)

{

GPIO_config();

PCA_config();


UpdatePwm(PCA0, (1 << 8) * (1 - 0.75));

UpdatePwm(PCA1, (1 << 7) * (1 - 0.25));


while(1)

{


}

}


Experimental phenomena

image.png

insert image description here

image.png

 insert image description here

image.png

insert image description here

16-bit software timer mode to generate PWM signal

PWM frequency = PCA clock frequency / PWMx_DUTY


Modify PWMx_DUTY in PCA.h

insert image description here

Modify the corresponding pin in PCA_Handler

insert image description here

//========================================================================

// 函数: void PCA_Handler (void) interrupt PCA_VECTOR

// Description: PCA interrupt handler.

// Parameters: None

// Returns: none.

// Version: V1.0, 2012-11-22

//========================================================================

void PCA_Handler (void) interrupt PCA_VECTOR

{

if(CCF0) //PCA module 0 interrupt

{

CCF0 = 0; // Clear PCA module 0 interrupt flag

if(P25) CCAP0_tmp += PCA_Timer0; //output is high level, then load the high level time length to the mapping register

else CCAP0_tmp += PWM0_low; //If the output is low level, load the low level time length into the mapping register

CCAP0L = (u8)CCAP0_tmp; //Write the mapping register into the capture register, write CCAP0L first

CCAP0H = (u8)(CCAP0_tmp >> 8); // write CCAP0H later

}


if(CCF1) //PCA module 1 interrupt

{

CCF1 = 0; // Clear PCA module 1 interrupt flag

if(P26) CCAP1_tmp += PCA_Timer1; //output is high level, then load the high level time length to the mapping register

else CCAP1_tmp += PWM1_low; //If the output is low level, load the low level time length into the mapping register

CCAP1L = (u8)CCAP1_tmp; //Write the mapping register into the capture register, write CCAP0L first

CCAP1H = (u8)(CCAP1_tmp >> 8); //后写CCAP0H

}


if(CCF2) //PCA module 2 interrupt

{

CCF2 = 0; // Clear PCA module 1 interrupt flag

if(P27) CCAP2_tmp += PCA_Timer2; //output is high level, then load the high level time length into the mapping register

else CCAP2_tmp += PWM2_low; //If the output is low level, load the low level time length into the mapping register

CCAP2L = (u8)CCAP2_tmp; //Write the mapping register into the capture register, write CCAP0L first

CCAP2H = (u8)(CCAP2_tmp >> 8); // write CCAP0H later

}


if(CF) //PCA overflow interrupt

{

CF = 0; // Clear PCA overflow interrupt flag

}


}


main.c

#include "./Drivers/config.h"

#include "./Drivers/delay.h"


#include "./Drivers/GPIO.h"

#include "./Drivers/PCA.h"


void GPIO_config(void)

{

GPIO_InitTypeDef GPIO_InitStructure; //Structure definition

GPIO_InitStructure.Mode = GPIO_OUT_PP; //Specify IO input or output mode, GPIO_PullUp, GPIO_HighZ, GPIO_OUT_OD, GPIO_OUT_PP

GPIO_InitStructure.Pin = GPIO_Pin_5 | GPIO_Pin_6; //Specify the IO to be initialized, or operation

GPIO_Inilize(GPIO_P2, &GPIO_InitStructure); //P2.5 and P2.6 are initialized as push-pull output

}


void PCA_config(void)

{

PCA_InitTypeDef PCA_InitStructure;


PCA_InitStructure.PCA_Mode     = PCA_Mode_HighPulseOutput; //PCA_Mode_PWM, PCA_Mode_Capture, PCA_Mode_SoftTimer, PCA_Mode_HighPulseOutput

PCA_InitStructure.PCA_PWM_Wide = 0; //PCA_PWM_8bit, PCA_PWM_7bit, PCA_PWM_6bit

PCA_InitStructure.PCA_Interrupt_Mode = ENABLE; //PCA_Rise_Active, PCA_Fall_Active, ENABLE, DISABLE

PCA_InitStructure.PCA_Value = 65535; //For software timing, it is the match comparison value

PCA_Init(PCA0,&PCA_InitStructure);


PCA_InitStructure.PCA_Mode     = PCA_Mode_HighPulseOutput; //PCA_Mode_PWM, PCA_Mode_Capture, PCA_Mode_SoftTimer, PCA_Mode_HighPulseOutput

PCA_InitStructure.PCA_PWM_Wide = 0; //PCA_PWM_8bit, PCA_PWM_7bit, PCA_PWM_6bit

PCA_InitStructure.PCA_Interrupt_Mode = ENABLE; //PCA_Rise_Active, PCA_Fall_Active, ENABLE, DISABLE

PCA_InitStructure.PCA_Value = 65535; //For software timing, it is the match comparison value

PCA_Init(PCA1,&PCA_InitStructure);


PCA_InitStructure.PCA_Clock    = PCA_Clock_12T; //PCA_Clock_1T, PCA_Clock_2T, PCA_Clock_4T, PCA_Clock_6T, PCA_Clock_8T, PCA_Clock_12T, PCA_Clock_Timer0_OF, PCA_Clock_ECI

PCA_InitStructure.PCA_IoUse    = PCA_P24_P25_P26_P27; //PCA_P12_P11_P10_P37, PCA_P34_P35_P36_P37, PCA_P24_P25_P26_P27

PCA_InitStructure.PCA_Interrupt_Mode = DISABLE; //ENABLE, DISABLE

PCA_InitStructure.PCA_Polity = PolityHigh; //Priority setting PolityHigh, PolityLow

PCA_Init(PCA_Counter, &PCA_InitStructure);

}


void main(void)

{

GPIO_config();

PCA_config();


PWMn_Update(PCA0, (u16)(PWM0_DUTY * 0.75));

PWMn_Update(PCA1, (u16)(PWM1_DUTY * 0.25));

EA = 1;


while(1)

{


}

}


Experimental phenomena

image.png

insert image description here
insert image description here

Capture mode measures cycle length

Modify PCA_Handler in PCA.c

insert image description here

//========================================================================

// 函数: void PCA_Handler (void) interrupt PCA_VECTOR

// Description: PCA interrupt handler.

// Parameters: None

// Returns: none.

// Version: V1.0, 2012-11-22

//========================================================================

void PCA_Handler (void) interrupt PCA_VECTOR

[1] [2]
Keywords:STC15 Reference address:[STC15 library function hands-on notes] 7. PCA and PWM

Previous article:[STC15 library function hands-on notes] 8. Comparator
Next article:[STC15 library function hands-on notes] 6. ADC

Recommended ReadingLatest update time:2024-11-15 05:25

Sharing of DC motor PWM speed control program based on AT89C51 single chip microcomputer
This is a PWM speed control program for DC motors with AT89C51 MCU. The program can be directly used in AT89C52, AT89S51, AT89S51, STC89C51, STC89C52 MCUs. The MCU crystal uses 11.0592M. The DC motor is controlled by L298 integrated circuit. The frequency of the generated PWM is about 91Hz. The pins of L298 have been
[Microcontroller]
Sharing of DC motor PWM speed control program based on AT89C51 single chip microcomputer
Noise is annoying. PWM regulated power supply can help you suppress noise.
PWM type switching power supply has many advantages, such as high efficiency, small size, etc. Therefore , PWM is widely used as a power supply device in many fields. However, the short duration of the working state conversion of the switching transistor and the wide spectrum of the spike interference are its fat
[Power Management]
Noise is annoying. PWM regulated power supply can help you suppress noise.
TIM1 PWM settings for STM8S (register version)
1 Overview The pulse width modulation (PWM) mode can generate a signal with a frequency determined by the TIM1_ARR register and a duty cycle determined by the TIM1_CCRi register. 2. Chip information Refer to the STM8S datasheet for the following information: Writing '110' (PWM mode 1) or '111' (PWM mode 2) to the
[Microcontroller]
TIM1 PWM settings for STM8S (register version)
STM32 one timer generates PWM of different frequencies
I usually have a bad memory. I forget the program I debugged two days later. It often takes a while to remember it. Sometimes I can't find the previous information, which is even more annoying. I have to search the Internet again. I just debugged a type of program successfully and wrote it down immediately, haha, don't
[Microcontroller]
AVR MCU Tutorial - PWM Dimming
PWM The driving mode of the two-digit digital tube is dynamic scanning, and each bit is only lit for 50% of the time. We call this value its duty cycle. Let the pin output high level to light up the LED, and the duty cycle is 100%. When driving the digital tube, we have to make the duty cycle 50% because we cannot l
[Microcontroller]
Analysis of the use of STC single chip microcomputer hardware PWM
How to achieve variable frequency PWM output for STC microcontroller (with PCA function)? Many friends are still using timers to do variable frequency PWM. Here I will introduce how to use hardware PWM and design PWM under the frequency division base of timer 0.  First, let's look at the CMOD register. Here we m
[Microcontroller]
Analysis of the use of STC single chip microcomputer hardware PWM
AVR M16 Experiment 3 PWM Experiment
/********************************************************************** * File name: main.c * Program author: kidcao1987 * Program version: V1.0 * Function description: There is an LED light in the lower left corner, the brightness will change from dark to bright, and then dark again. * Compiler: WinAVR-20090313
[Microcontroller]
51 single chip microcomputer generates pwm duty cycle 10khz frequency Proteus simulation program
  The simulation schematic is as follows   The microcontroller source program is as follows: #include reg51.h #define uchar unsigned char #define uint unsigned int  sbit pwm=P1^1; fly num=0; main() {    TMOD=0x02;          TH0=TL0=206;          ET0=1;          TR0=1;    EA=1;          while(1); } void init(
[Microcontroller]
51 single chip microcomputer generates pwm duty cycle 10khz frequency Proteus simulation program
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号