51 microcontroller PWM control LED light fading experiment

51 microcontroller PWM control LED light fading experiment

1 Overview

This article introduces the PWM of the microcontroller to control the fading effect of LED lights through the duty cycle. Through this experiment, we can master the principle of PWM and apply it to do something.

2. Operation steps

2.1.Hardware circuit

1.Hardware preparation
name model quantity
Microcontroller STC12C2052 1
LED lanterns none 2
crystal oscillator 12MHZ 1
capacitance 30pf 2
resistance 100 ohms 2
Breadboard none 1
Connecting line none 5
2. Circuit diagram schematic diagram

This experiment used two LED lights to flash alternately, so just connect another LED light to pin 9 in the same way that pin 11 is connected to the LED light.
Insert image description here

2.2.Program code

/*************************************************************
* 程序名: PWM控制两个LED灯亮度渐亮渐暗
* 编写人: bruce
* 硬件支持:TC12C2052 外部12MHZ晶振
* 日  期: 2023-11-14
* 修改日志: 通过控制PWM占空比调控LED灯亮和灭实现亮度渐变效果
/*************************************************************/

#include<STC12C2052AD.H>


/*********************************************************************************************
函数名:PWM初始化函数
调  用:PWM_init();
参  数:无
返回值:无
结  果:将PCA初始化为PWM模式,初始占空比为0
备  注:需要更多路PWM输出直接插入CCAPnH和CCAPnL即可
/**********************************************************************************************/
void PWM_init (void){
    
    
	CMOD=0x02; //设置PCA定时器
 	CL=0x00; 
 	CH=0x00;
	CCAPM0=0x42; //PWM0设置PCA工作方式为PWM方式(0100 0010)
 	CCAP0L=0x00; //设置PWM0初始值与CCAP0H相同
 	CCAP0H=0x00; // PWM0初始时为0

	CCAPM1=0x42; //PWM1设置PCA工作方式为PWM方式(使用时删除//)
	CCAP1L=0x00; //设置PWM1初始值与CCAP0H相同
 	CCAP1H=0x00; // PWM1初始时为0

	//CCAPM2=0x42; //PWM2设置PCA工作方式为PWM方式
	//CCAP2L=0x00; //设置PWM2初始值与CCAP0H相同
 	//CCAP2H=0x00; // PWM2初始时为0

	//CCAPM3=0x42; //PWM3设置PCA工作方式为PWM方式
	//CCAP3L=0x00; //设置PWM3初始值与CCAP0H相同
 	//CCAP3H=0x00; // PWM3初始时为0

 	CR=1; //启动PCA定时器
}


/*********************************************************************************************
函数名:PWM0占空比设置函数
调  用:PWM0_set();
参  数:0x00~0xFF(亦可用0~255)
返回值:无
结  果:设置PWM模式占空比,为0时全部高电平,为1时全部低电平
备  注:如果需要PWM1的设置函数,只要把CCAP0L和CCAP0H中的0改为1即可
/**********************************************************************************************/
void PWM0_set(unsigned char a){
    
    
	CCAP0L=a;
	CCAP0H=a;
}

/*********************************************************************************************
函数名:PWM1占空比设置函数
调  用:PWM1_set();
参  数:0x00~0xFF(亦可用0~255)
返回值:无
结  果:设置PWM模式占空比,为0时全部高电平,为1时全部低电平
/**********************************************************************************************/
void PWM1_set(unsigned char a){
    
    
	CCAP1L=a;
	CCAP1H=a;
}

/*********************************************************************************************
函数名:毫秒级CPU延时函数
调  用:DELAY_MS (?);
参  数:1~65535(参数不可为0)
返回值:无
结  果:占用CPU方式延时与参数数值相同的毫秒时间
备  注:应用于1T单片机时i<600,应用于12T单片机时i<125
/*********************************************************************************************/
void DELAY_MS (unsigned int a){
    
    
	unsigned int i;
	while( --a != 0){
    
    
		for(i = 0; i < 600; i++);
	}
}

void main(void){
    
    
	//PWM初始化
	PWM_init();
	
	while(1){
    
    
		unsigned char a;
		for(a=0x00;a<0xff;a++){
    
    
			//设置PWM0占空比
			PWM0_set(a);
			//设置PWM1占空比
			PWM1_set(~a);
			DELAY_MS(8);
		}
		for(a=0xff;a>0x00;a--){
    
    
			//设置PWM0占空比
			PWM0_set(a);
			//设置PWM1占空比
			PWM1_set(~a);
			DELAY_MS(8);
		}
	}

}

2.3. Burning program

Use the Keil tool to compile the program, open the STC-ISP software, select the compiled hex file, set the external crystal oscillator, and download the program. After burning the program into the microcontroller, cold-start the microcontroller (cut off the 5V power supply and then power on), switch the microcontroller to an external crystal oscillator, and observe the alternating light and dark changes of the two LED lights.

Guess you like

Origin blog.csdn.net/m0_38039437/article/details/134549831
Recommended