C51 MCU timer simulates single-channel output PWM

Publisher:支持中文Latest update time:2021-11-30 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Preface

This blog post is developed based on STC89C52RC and Keil5 C51 environment;

If there are any deficiencies, please give me more advice;

Things Needed

Timer 0;

Pin P1.0;

code show as below:


#include


#define ENABLE 1

#define DISENABLE 0


typedef unsigned char uchar8;


sbit P1_0 = P1^0;

bit Flag = 0;


/*

Duty cycle time calculation:

Duty cycle = high level time / (high level time + low level time);

*/


//High level time (initial value: 0x02EE, i.e. decimal 750)

uchar8 Timer0_TH0_High_Level_Time = 0x02; //Timer 0 high eight-bit high level time;

uchar8 Timer0_TL0_High_Level_Time = 0xEE; //Timer 0 low eight bits high level time;

//Low level time (initial value: 0x00FA)

uchar8 Timer0_TH0_Low_Level_Time = 0x00; //Timer 0 high eight bits low level time;

uchar8 Timer0_TL0_Low_Level_Time = 0cFA; //Timer 0 low eight-bit low level time;


void Time0_Init(void);


void main()

{

  Time0_Heat();

  while(1);

}


/*

Timer configuration:

1. Assume that the crystal frequency of the microcontroller is 12MHz; then the timer accumulation period is 1us;

2. The initial value of the timer is: high level overflow time 0x02EE, that is, decimal 750; low level overflow time 0x00FA, that is, decimal 250;

3. Timer 0 is set to 16-bit auto-reload mode;

*/


void Time0_Init(void)

{

  TMOD = 0x01;

TH0 = Timer0_TH0_High_Level_Time; //The initial value is set to high level;

TL0 = Timer0_TL0_High_Level_Time;

TR0 = ENABLE;

ET0 = ENABLE;

EA = ENABLE;

Flag = 0;

P1_0 = 1;

}


void Timer0_IT() interrupt 1

if(Flag)

{

TH0 = Timer0_TH0_High_Level_Time;

TL0 = Timer0_TL0_High_Level_Time;

Flag = 0;

P1_0 = 1;

}

else

{

TH0 = Timer0_TH0_Low_Level_Time;

TL0 = Timer0_TL0_Low_Level_Time;

Flag = 1;

P1_0 = 0;

}

}

Reference address:C51 MCU timer simulates single-channel output PWM

Previous article:Teach you to learn 51 single chip microcomputer step by step: C language basics and the realization of running lights
Next article:51 MCU timer realizes PWM wave

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号