STC MCU PWM pulse width modulation function module

Publisher:genius6Latest update time:2015-11-10 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
// STC MCU PWM pulse width modulation function module

#include

// PWM initialization
// Initialize PCA to PWM mode with an initial duty cycle of 0
// Parameters: 0, 1, 2, 3, representing initialization of PWM0, PWM1, PWM2, and PWM3 respectively
void PWM_Init(int i)
{
 CMOD = 0x02; // Set the PCA timer frequency to the internal clock fosc/2
 CL = 0x00;
 CH = 0x00;

 switch(i)
 {
  case 0:
   CCAPM0 = 0x42; // PWM0 sets PCA working mode to PWM mode (0100 0010)
   CCAP0L = 0x00; // Set the initial value of PWM0 to be the same as CCAP0H
   CCAP0H = 0x00; // PWM0 is initially 0
   break;
  case 1:
   CCAPM1 = 0x42;
   CCAP1L = 0x00;
   CCAP1H = 0x00;
   break;
  case 2:
   CCAPM2 = 0x42;
   CCAP2L = 0x00;
   CCAP2H = 0x00;
   break;
  case 3:
   CCAPM3 = 0x42;
   CCAP3L = 0x00;
   CCAP3H = 0x00;
   break;
  default:
   break;
 }

 CR = 1; // Start PCA timer
}

// PWM duty cycle setting
// Parameter 1: 0, 1, 2, 3, respectively represent setting the duty cycle for PWM0, PWM1, PWM2, and PWM3
// Parameter 2: 0x00~0xFF (or 0~255), all low level when 0x00, all high level when 0xFF
void PWM_Set(int i, unsigned char c)
{
 switch(i)
 {
  case 0: 
   CCAP0L = c;
   CCAP0H = c;
   break;
  case 1:
   CCAP1L = c;
   CCAP1H = c;
   break; case
  2:
   CCAP2L = c;
   CCAP2H = c;
   break;
  case 3:
   CCAP3L = c;
   CCAP3H = c;
   break;
  default:
   break;
 }
}


Reference address:STC MCU PWM pulse width modulation function module

Previous article:Classic explanation of single chip music performance program
Next article:Teach you to learn single-chip microcomputer 1: Introduction, about single-chip microcomputer OOXX

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号