//(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%.
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
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
- Intelligent Control Technology of Permanent Magnet Synchronous Motor (Written by Wang Jun)
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- How to select ultra-low power MCU
- Wireless receiving decoding program based on STM32 and hs1527, ev1527, rt1527, fp1527
- [Qinheng RISC-V core CH582] ADC internal temperature conversion
- Predictive Control Algorithm Program
- GigaDevice GD32450I-EVAL is available for free evaluation and trial. It has rich onboard functions and covers multiple fields. Are you interested?
- I am looking for an adjustable negative voltage chip. The negative voltage end of -0.5V to -5V used for OP37 must be adjustable.
- PCI Bus Standard Protocol (Chinese Version)
- [Fudan Micro FM33LC046N Review] + GPTIM breathing light
- [Evaluation of domestic FPGA Gaoyun GW1N series development board]——(4) Serial port experiment
- How can UWB take industry and sports to the next level?