C51 single-chip computer outputs multiple PWM through timer simulation

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 based on STC89C52RC and keil5 C51 development;

If there is anything I haven't done, please give me some advice;

Things you need

This is done using Timer 0 (and Timer 1 as well).

Use P1.0~P1.3 to complete 4-way PWM output (different duty cycles);

code show as below:


#include


#define ENABLE 1

#define DISENABLE 0


typedef unsigned int uint16;

typedef unsigned char uchar8;


sbit P1_0 = P1^0;

sbit P1_1 = P1^1;

sbit P1_2 = P1^2;

sbit P1_3 = P1^3;


/*

Duty cycle time calculation:

Duty Cycle = High_Level_Time_P1_x/Cycle;

*/

uchar8 Cycle = 10; //Set PWM cycle: 10*timer overflow cycle (100us) = 1000us;

uchar8 High_Level_Time_P1_0 = 0; //The four global variables here control the high level time in each cycle;

uchar8 High_Level_Time_P1_1 = 0;

uchar8 High_Level_Time_P1_2 = 0;

uchar8 High_Level_Time_P1_3 = 0;   


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: 9C (decimal 156, that is, the overflow period of the timer is 100us);

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

*/


void Time0_Init(void)

{

  TMOD = 0x02;

TH0 = 0x9C;

TL0 = 0x9C;

TR0 = ENABLE;

ET0 = ENABLE;

EA = ENABLE;

}


void Timer0_IT() interrupt 1

static uchar8 count_P1_0,count_P1_1,

  count_P1_2,count_P1_3; 

if(count_P1_0==High_Level_Time_P1_0)

{

P1_0 = 0;

}

count_P1_0++;

if(count_P1_0==Cycle)

{

P1_0= 1;

count_P1_0 = 0;

}

if(count_P1_1==High_Level_Time_P1_1)

{

P1_1 = 1;

}

count_P1_1++;

if(count_P1_1==Cycle)

{

P1_1= 0;

count_P1_1 = 1;

}

if(count_P1_2==High_Level_Time_P1_2)

{

count_P1_2=0;

}

count_P1_2++;

if(count_P1_2==Cycle)

{

P1_2 = 0;

count_P1_2=1;

}

if(count_P1_3==High_Level_Time_P1_3)

{

count_P1_3 = 0;

}

count_P1_3++;

if(count_P1_3==Cycle)

{

P1_3 = 1;

count_P1_3= 1;

}

}

Reference address:C51 single-chip computer outputs multiple PWM through timer simulation

Previous article:Notes on assembly operators based on the MCS-51 kernel
Next article:Teach you to learn 51 single chip microcomputer step by step: light up your LED

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号