51 Study Notes: Using Timer 0 to generate a PWM wave with a maximum period of 16s (STC12C5A60S2)

Publisher:Yuexiang666Latest update time:2024-07-24 Source: cnblogs Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Hardware: STC12C5A60S2


Function: To realize PWM wave generation. The program cycle is 256ms and the output port is P1.3.


Code:


#include "reg51.h"

#include "intrins.h"

#define DARK 11059260L

#define T1MS (65536-FOSC/12/1000)


//The pulse generated by the timer is about 1MS, and the time after PWM is divided by 256 is 256ms

//By adjusting the value of T1MS, the maximum cycle time of PWM is 16s.


typedef unsigned char BYTE;

typedef unsigned int WORD;


/*Declare SFR associated with the PCA */

sfr CCON = 0xD8; //PCA control register

sbit CCF0 = CCON^0; //PCA module-0 interrupt flag

sbit CCF1 = CCON^1; //PCA module-1 interrupt flag

sbit CR = CCON^6; //PCA timer run control bit

sbit CF = CCON^7; //PCA timer overflow flag

sfr CMOD = 0xD9; //PCA mode register

sfr CL = 0xE9; //PCA base timer LOW

sfr CH = 0xF9; //PCA base timer HIGH

sfr CCAPM0 = 0xDA; //PCA module-0 mode register

sfr CCAP0L = 0xEA; //PCA module-0 capture register LOW

sfr CCAP0H = 0xFA; //PCA module-0 capture register HIGH

sfr CCAPM1 = 0xDB; //PCA module-1 mode register

sfr CCAP1L = 0xEB; //PCA module-1 capture register LOW

sfr CCAP1H = 0xFB; //PCA module-1 capture register HIGH

sfr PCAPWM0 = 0xf2;

sfr PCAPWM1 = 0xf3;


void Delay_xms(unsigned int z){

unsigned char i, j;

unsigned int k;

for(k=0;k

_nop_();

_nop_();

i = 12;

j = 84;

do{while (--j);}

while (--i);

}

}


// Initialize timer 0


void Time0Init()

{

TMOD = 0x01;

TL0=T1MS;

TH0=T1MS>>8;

TR0=1;

ET0=1;

EA=1;

}


void PWMInit()

{

CCON = 0; //Initial PCA control register

//PCA timer stop running

//Clear CF flag

//Clear all module interrupt flag

CL = 0; //Reset PCA base timer

CH = 0;

//    CMOD = 0x00; //Set PCA timer clock source as Fosc/2

CMOD = 0x04;

//Disable PCA timer overflow interrupt

CCAP0H = CCAP0L = 0x80; //PWM0 port output 50% duty cycle square wave

CCAPM0 = 0x42; //The device is timer 0 overflow pulse

CCAP1H = CCAP1L = 0xff; //PWM1 port output 0% duty cycle square wave

PCAPWM1 = 0x03;

CCAPM1 = 0x42; //PCA module-1 work in 8-bit PWM mode

//and no PCA interrupt

CR = 1; //PCA timer start run

}




void PWMSet(float a) //Input a represents the duty cycle

{

CCAP0H =CCAP0L =(BYTE)((1-a)*256);

}


void main()

{


Time0Init();

PWMInit();

PWMSet(0.1);

while(1)

{


PWMSet(0.5); //duty cycle is 50%

Delay_xms(2000);

}

}



//Timer 0 interrupt

void tm0_isr() interrupt 1 using 1

{

TL0=T1MS;

TH0=T1MS>>8;

}


Reference address:51 Study Notes: Using Timer 0 to generate a PWM wave with a maximum period of 16s (STC12C5A60S2)

Previous article:51 Study Notes: Using hardware spi and max6675 communication to realize K-type thermocouple temperature measurement
Next article:51 Study Notes: The meaning of the keywords data, bdata, idata, pdata, xdata, and code

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号