51 MCU PWM duty cycle adjustment

Publisher:collectorsLatest update time:2016-09-12 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
//51 MCU PWM duty cycle adjustment demonstration program
//(Tested, note: AT89S51 MCU, some MCU P3_7 needs to be changed to P3^7)

/****************************************************** ******************************
* Program name: PWM duty cycle adjustment demonstration program *
*Procedure description: *
* Only 4 buttons are needed. *
* Regarding the determination of frequency and duty cycle, for a 12M crystal oscillator, the output frequency is 1KHZ, so the number of timer interrupts is set to *
*10, that is, 0.01MS interruption, then TH0=FF, TL0=F6; since the interruption time is set to 0.01ms, you can set *
*The duty cycle can be changed from 1-99%. That is, 0.01ms*100=1ms *
*************************************************** ****************************/

#include
#define uchar unsigned char
#define uint unsigned int
uchar timer0_tick,ZKB=1; //timer0_tick count, ZKB duty cycle
uchar i=0,n=0,temp=0;
code seven_seg[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//1,2,3, 4, 5, 6, 7, 8, 9
code scan[2]={0xfd,0xfe};
uchar counter[2]={0,0};
sbit AN1=P3^2; //Adjust the unit digit
sbit AN2=P3^3; //Adjust the tens digit
sbit AN3=P3^4; //Start button
sbit AN4=P3^5; //Confirm button
void delay(uint z) //software delay function
{
   uint x,y;
   for(x=z;x>0;x--)
   for(y=110;y>0;y--);
}
static void timer0_isr(void) interrupt 1 using 0 //interrupt function
{
   TR0=0;
   TL0=0xf6;
   TH0=0xff;
   TR0=1;
   if (ZKB>99) ZKB=1;
    if (ZKB<1) ZKB=99;
   counter[0]=ZKB%10;
   counter[1]=ZKB/10;
   n++;
   if(n==100)
    {
     n=0;
     i++;
     if(i==2) i=0;
     P0=seven_seg[counter[i]];
     P2=scan[i];
    }
   timer0_tick++;
   if(timer0_tick++==100)
    {
     timer0_tick=0;
  
    }
     if(AN2==0)
    {
     delay(100);
     if(AN2==0)
      {
       temp=1;
       counter[0]++;
       if(counter[0]==10)
        {
         counter[0]=0;
        }

      }
    }
   if(AN1==0)
    {
     delay(100);
     if(AN1==0)
      {
       temp=1;
       counter[1]++;
       if(counter[1]==10)
        {
         counter[1]=0;
        }

      }
      }
ZKB=counter[0]+counter[1]*10;
if(AN4==0)
   {
    delay(5);
    if(AN4==0)
    temp=0;
   }
if(temp==1)
P3_7=0; // P3_7 is the pulse output pin
else
   {
    if (timer0_tick<=ZKB) /*When it is less than the duty cycle value, it outputs a low level, and when it is higher, it outputs a high level, thereby adjusting the duty cycle*/
        {
      P3_7=1;
     }
       else
        {
     P3_7=0;
     }

   }
}
static void timer0_initialize(void) //Interrupt initialization
{
   EA=0;
   timer0_tick=0;
   TR0=0;
   TMOD=0x01;
   TL0=0xf6;
   TH0=0xff;
   PT0=0;
   ET0=1;
   TR0=1;
   EA=1;
}
void main(void)
{
   STAR: delay(100);
     if(AN3!=0) goto STAR; //Press key 3 to start pulse
     timer0_initialize();
     while(1);
}
// Press button 3 to start the pulse generator. The initial duty cycle is set to 1%. Press button 1 to adjust the tens digit.
// Press key 2 to adjust the unit digit. Once adjusted, the output is 0. You must press the confirmation key to confirm.
//Output correct pulse. The duty cycle can be adjusted arbitrarily from 1 to 99%.
Reference address:51 MCU PWM duty cycle adjustment

Previous article:Taking 51 single chip microcomputer as an example, a simple serial communication protocol C program
Next article:Reference program for microcontroller-controlled servo

Recommended ReadingLatest update time:2024-11-16 21:32

Research on PID and PWM liquid flow control system based on AVR microcontroller atmega32
1. Introduction Liquid flow control is usually achieved by solenoid valves. In recent years, the structure and control mode of solenoid valves have changed . With the entry of computers into the control field and the continuous emergence of new power electronic power components, the pulse width modulation (PWM) control
[Microcontroller]
Research on PID and PWM liquid flow control system based on AVR microcontroller atmega32
Yuanxiang FP6115: PWM controlled 2A synchronous rectifier step-down IC
The FP6115 is a step-down switching regulator for a wide range of operating voltage applications. The FP611 has a built-in high-current P-MOSFET, a high-precision reference (0.8V) for comparing the output voltage with the feedback amplifier, an internal soft-start timer, and a fixed-frequency oscillator. The oscilla
[Embedded]
Yuanxiang FP6115: PWM controlled 2A synchronous rectifier step-down IC
STM8 PWM routines
In single-chip microcomputer application systems, PWM signal output is often used, such as motor speed control. They are also integrated with PWM function modules to facilitate user applications.       For PWM signals, there are two main concepts involved, one is the period or frequency of the PWM signal, and the oth
[Microcontroller]
stm8s105k4 microcontroller PWM wave configuration
//ccr = 0~499 void setPWM1(unsigned short ccr){//PC1   TIM1_CCR1H = (unsigned char)(ccr 8);   TIM1_CCR1L = (unsigned char)(ccr & 0xff); } void setPWM2(unsigned short ccr){//PC2   TIM1_CCR2H = (unsigned char)(ccr 8);   TIM1_CCR2L = (unsigned char)(ccr & 0xff); } void setPWM3(unsigned short ccr){//PC3   TIM1_CCR3H = (u
[Microcontroller]
A design scheme of switching power supply based on PWM soft switching mode
  Main circuit analysis   This soft switching power supply adopts a full-bridge converter structure and uses MOSFET as a switch tube with a parameter of 1000V/24A. It adopts phase-shifted ZVZCS PWM control, that is, the leading arm switch tube realizes ZVS and the lagging arm switch tube realizes ZCS. The circuit
[Power Management]
A design scheme of switching power supply based on PWM soft switching mode
PWM asynchronous drive buzzer based on stm32 processor
In the past two days, I have studied the PWM (pulse width modulation) of the M3 processor to achieve asynchronous control of the buzzer due to work requirements. In view of the fact that the blocking control of the buzzer is time-consuming and affects the user experience, the original blocking control scheme has been
[Microcontroller]
PWM asynchronous drive buzzer based on stm32 processor
MSP430 PWM wave program with variable duty cycle
//******************************************************************************* // Description: Set a variable to select the initial duty cycle, then use Timer_B to set the timer for 1s, // change the duty cycle every second to generate a PWM wave with a variable duty cycle. // ACLK= TACLK = 32768Hz, MCLK= SMCLK=
[Microcontroller]
Newest integrated PFC/PWM combination solution for efficient PC power supply
Power supply engineers are always looking for simple design methods that can implement a series of circuit protection functions while making the power supply meet increasingly stringent efficiency specifications. This article will explore a highly integrated semiconductor solution that comb
[Analog Electronics]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号