The microcontroller generates a square wave with adjustable duty cycle (PWM)

Publisher:zonhenyLatest update time:2015-04-30 Source: 51heiKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
/****************************************************** ***************                                                       

            Program name: Brightness control of light-emitting diode D1
            Version: VER1.0
            Applicable board: 51hei learning board      
Use timer control to generate PWM wave with variable duty cycle to control the brightness of light-emitting diode D1
Press S2, the PWM value increases, the duty cycle decreases, and the D1 light becomes brighter.
Press S3, the PWM value decreases, the duty cycle increases, and the D1 light becomes darker.
When the PWM value increases to the maximum value or decreases to the minimum value, the buzzer will alarm.
******************************************************************/

#include
#include
sbit S2 =P3^4 ; //PWM value decrease key
sbit S3 =P3^5 ; //PWM value increase key
sbit BEEP =P2^3 ; //Buzzer
unsigned char PWM=0x03 ; //Assign initial value

/////////////////////////////////////////
void Beep();
void delayms(unsigned char ms );
void delay(unsigned char t);

/************************************************************/
void main()
{   
    P1=0xff;
    TMOD=0x00 ;
  TH0=0x00 ; //Delay constant
    TL0=0x00 ; //Frequency adjustment
    TH1=PWM ; //Pulse width adjustment
    TL1=0xff ;
EA=1;
ET0=1;
ET1=1;
    TR0=1 ;


   while(1)
   {
 do{
     if(PWM!=0xff)
    {PWM++ ;delayms(10);}
        else Beep() ; 
   }
    while(S3==0);

 do{
      if(PWM!=0x02)
     {PWM-- ;delayms(10);}
      else Beep() ; 
   }
    while(S2==0);
  }
}

/************************************************************/
// Timer 0 interrupt service routine (frequency)
/************************************************************/
void timer0() interrupt 1 
{  
    TR1=0 ;
    TH0=0x00;
    TL0=0x00 ;
    TH1=PWM ;
    TR1=1 ;
    P1=0xfe ; // Start output
}

/************************************************************/
// Timer 1 interrupt service routine (pulse width)
/************************************************************/
void timer1() interrupt 3 

    TR1=0 ;
    P1=0xff ; //End output
}

/************************************************************/
//Buzzer subroutine
/****************************************************************/

void Beep()     
  {
    unsigned char i ;
    for (i=0 ;i<100 ;i++)
      {
        delay(100) ;
        BEEP=!BEEP ; //Invert Beep
      } 
    BEEP=1 ; //Turn off the buzzer
 delayms(100);
  }

/************************************************************/
// Delay subroutine
/************************************************************/  
void delay(unsigned char t)
 { 
   while(t--) ;
 }

/************************************************************/
// Delay subroutine
/************************************************************/
void delayms(unsigned char ms)

{
   unsigned char i ;
   while(ms--)
    {
      for(i = 0 ; i < 120 ; i++) ;
    }
}

/****************************************************** **********/

Keywords:MCU Reference address:The microcontroller generates a square wave with adjustable duty cycle (PWM)

Previous article:51 MCU servo control program
Next article:MCU timer to achieve real-time clock program - lcd1602 display

Recommended ReadingLatest update time:2024-11-23 07:44

PIC16F676 microcontroller internal EEPROM program
BSF STATUS,RP0          CLRF EEADR ;Address 0     MOVLW .9     MOVWF EEDAT ;The data to be written is 9     BSF EECON1,WREN ;Enable writing     MOVLW 0X55     MOVWF EECON2     MOVLW 0XAA     MOVWF EECON2     BSF EECON1,WR ;Start writing     NOP     NOP ;Several NOPs are added for fear of affecting the following operat
[Microcontroller]
51 single chip microcomputer gy906 temperature measurement, voice broadcast, eepromc storage
This is a small design I made last year, mainly used to measure body temperature. Due to the special situation this year due to the epidemic, I wanted to share this design, but I found a problem when I was looking for materials on Taobao. The prices have all gone up, which is a bit uncomfortable. I also downloaded the
[Microcontroller]
51 single chip microcomputer gy906 temperature measurement, voice broadcast, eepromc storage
Introduction to microcontroller serial port, communication method of 8051 microcontroller
The serial port is a tool for the microcontroller to exchange information with the outside world. There are two communication methods for the 8051 microcontroller: Parallel communication: Each bit of data is sent or received at the same time. Serial communication: Data is sent or received bit by bit. Serial commun
[Microcontroller]
Introduction to microcontroller serial port, communication method of 8051 microcontroller
Design of solar street light controller based on Atmega48 microcontroller
    The solar controller is the core part of the solar street light system. A solar street light controller based on Atmega48 single chip microcomputer is designed. The controller uses PWM pulse modulation control technology to detect battery voltage, charging current and other parameters, and controls the switch off
[Microcontroller]
Design of solar street light controller based on Atmega48 microcontroller
Design of electronic scale based on Sinowin SH79F085 microcontroller
At present, electronic scale systems on the market mainly use two solutions to sample sensor analog signals: dual integration circuit and high-precision analog-to-digital converter (ADC). The double-integral circuit uses an indirect A/D converter. Its basic principle is to convert the analog voltage to be
[Microcontroller]
Design of electronic scale based on Sinowin SH79F085 microcontroller
Wind-solar inverter grid-connected system based on MCU-FPGA architecture
This article focuses on solar energy and wind energy, and designs a simulation device for wind-solar grid-connected power generation. It can convert the DC voltage of solar or wind generators into AC power, detect the frequency and phase of external AC power, and dynamically adjust the waveform of its own AC power to m
[Microcontroller]
Wind-solar inverter grid-connected system based on MCU-FPGA architecture
Design of DC speed regulation system based on single chip microcomputer
1 Introduction Based on the DKSZ-1 motor control experimental device, a digital controller with a single-chip microcomputer as the control core is added to realize the control of the DC digital dual closed-loop speed regulation system. 2. System composition Two regulators are set in the speed cont
[Microcontroller]
Design of DC speed regulation system based on single chip microcomputer
51 MCU timer and counter
1. Principles of timers and counters They automatically increase by 1 with the input pulse of the counter. For each pulse, the counter automatically increases by 1. When the counter is all 1, another pulse is input and the counter returns to 0. The overflow of the counter sets the corresponding interrupt flag to 1,
[Microcontroller]
51 MCU timer and counter
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号