How to write a 51 single chip microcomputer with PWM speed regulation C language program

Updated on technology 2024-03-16
9 answers
  1. Anonymous users2024-02-06

    The method of using two timers is to use the timer t0 to control the frequency and the timer t1 to control the duty cycle. The general programming idea is as follows: the t0 timer interrupt makes an i0 port output high, and starts the timer t1 in the interrupt of the timer t0, and this t1 makes the io port output low, so that changing the initial value of timer t0 can change the frequency, and changing the initial value of timer t1 can change the duty cycle.

    When using a timer (such as timer t0), first you need to determine the period t and duty cycle d of PWM, after determining these, you can use the timer to generate a time reference T, for example, the time when the timer overflows n n times is the time when PWM is high, then d*t=n*t, and similarly you can find out how many time benchmarks n are needed for PWM's low level time'。

    Since we are generating PWM with a period of 1 ms (1000 Hz), we can set the interrupt time benchmark to 1 ms, and then interrupt 100 times to 1 ms. In the interrupt subroutine, you can set a variable such as time, in the interrupt subroutine, there are three important statements: 1. When time>=100, time is cleared to zero (this statement guarantees that the frequency is 1000Hz), 2. When time is > n (n should be changed between 0 and 100), let the corresponding I O port of the monolithic output be high, when.

  2. Anonymous users2024-02-05

    Provide ideas: define a static variable, the program will judge whether the duty cycle meets the requirements every time it enters the interrupt function of the timer, and continue to accumulate the variable values until the conditions are met, so that the pin level of the output PWM wave is reversed.

    It's a bit verbose, I hope it helps.

  3. Anonymous users2024-02-04

    1. Use timer + programming.

    The series of single-chip microcomputers do not have PWM output function, and can be output by using timer and software. It is very practical for occasions where precision is not required.

    3. Idea: Define a static variable, the program will judge whether the duty cycle meets the requirements every time it enters the interrupt function of the timer, and continue to accumulate the variable value until the condition is met, so that the pin level of the output PWM wave is reversed.

    4. For details, please refer to it.

  4. Anonymous users2024-02-03

    Use the timer interrupt to control the output of the IO port.

    Let PWM be a fixed period T, where the high level period T1 and the low level period T0, T=T1+T0.

    A cycle timer is interrupted twice, t1 and t0 respectively, increasing or decreasing t1 so that t0=t-t1, i.e., changing the output duty cycle.

  5. Anonymous users2024-02-02

    Set a timer for 20ms periods, and then let yourself set those variables change in turn when the timer reaches those points. Then according to your own variables, you can pay the output port. Looks like a servo.

    After reading what you asked, it turned out that each of the four roads produces a changing waveform. That's easy to do. These waveforms can be unified into a cycle through calculation, such as the beginning of the arrival, let the fixed value be negated, then added and then negated, and then added and then negated.

    And then I kept writing a few of them all the way like this.

    There is also a way to change the setting value of the first PWM. For example, there are a total of 5 set values in the array, which represent the high level time you set, and replace the mid-range time in the first timer in turn.

    Of course, the first two are pretty much the same, but there's another way for you to expand: use two timers and match them to the time. Of course,51Not to mention the accuracy of the single-chip microcomputer,I'm not sure that it can be up to standard.,One timer is interrupted.,The other has to stop.。。

    btw,Personally, I feel that the 51 microcontroller does not have a good PWM module,So write the PWM program,Nothing else can be done。 It is recommended to use an MCU with an EPWM module to directly call EPWM and EPWM interrupts to save CPU resources and write a lot of programs.

  6. Anonymous users2024-02-01

    You expand with 82c54! The accuracy and frequency range are suitable for your requirements, and some three-axis control systems have been implemented with this +51 single-chip microcomputer! The program is in the question I answered, this program has actually been debugged, and you can use it directly!

  7. Anonymous users2024-01-31

    A lot of 51s come with PWM, and as long as yours is accurate, you can definitely achieve it.

    Standard 51 can also be implemented with software with a timer, and it is no problem if your indicator is not high.

  8. Anonymous users2024-01-30

    You will definitely be able to understand every statement of this program, there is no need to add comments to you, tell you the principle of PWM, and you will understand it by comparing it with the program.

    You want to use 51 to generate PWM to control LED, first you need to determine the period T and duty cycle D of PWM, after determining these, you can use the timer to generate a time reference T, for example, the time when the timer overflows n times is the time of the high level of PWM, then D*T = N*T, and similarly you can find out how many time benchmarks N are needed for PWM low level time'

    Then you can write the program according to the time baseline you set.

    t to initialize the assigned value of the timer, then turn on the timer, define a flag bit, decide whether the output is high or low according to the state of the flag, assume that the output is high when flag=1, use a variable to record the number of timer interruptions, and let the variable that records the number of interruptions +1 for each interruption, and judge whether the value of this variable has reached in the interrupt program.

    n If the time to indicate the high level is enough, then change the flag to 0, output the low level, and record the value of the interrupt variable to zero, and still +1 each time it interrupts, and jump to judge whether the value of the recorded variable has reached according to the situation of flag=0.

    n'If it arrives, it means that the low level of the PWM is enough, then change flag=1, change the output to the high level, and record the number of times variable to zero, start again, and so on to get the PWM waveform you want.

  9. Anonymous users2024-01-29

    1, "If the single-chip microcomputer constant temperature can make the temperature reach the predetermined value to stop heating, low heating, with a temperature sensor feedback, so is it an automatic control" you this is a control system, but the effect will be very poor, especially for the temperature control of this large inertia system, to reach the predetermined value to stop heating, but due to inertia, the temperature will definitely continue to rise, when the electric furnace boils water, the water is boiled, and the water will boil for a certain period of time after the power is off (boiling is very energy-consuming, It can be seen that if the temperature rises more seriously if the closure is heated, you can also try it yourself with a thermometer); "If it's low, heat it up" is the same principle as the loser of the eggplant. If the system has requirements for control accuracy, you will definitely not meet the requirements. PID is a control algorithm that is the simplest compared to other control algorithms.

    The PID can reduce the heating power when the temperature is about to reach the set value, so that the temperature rise rate is slower, and finally stabilize at the set point. If you use your direct control, the temperature will oscillate up and down the set point and will never stop at the set point.

    2. The general control system needs to add feedback to form a closed-loop control system, and there is also an open-loop control system. The open-loop control system, for example, is to calculate the approximate amount of heat needed in advance when you heat, and then consider the environmental impact, calculate the heating time, and then control the heating system to heat according to your time. Do you think such a system will work stably?

    The environment has changed slightly! Open-loop control systems are susceptible to environmental influences; The closed-loop control system is much more stable, you can use 1L of water, 2L of water, 500W electric energy, 1000W electric furnace can also be used, this is the advantage of closed-loop.

    Therefore, most control systems are closed-loop, and open-loop is rarely used alone, and even when used, there is a closed-loop. Open loop actually has advantages, open loop in the control system is called feedforward (corresponding to feedback), for example, the power supply voltage in your system rises, the heating speed will definitely become faster, if you sample the power supply voltage, input the sampling results into the closed loop, make a slight correction to the closed loop, the accuracy of control will be better, this is the advantage of open loop, it is advanced, can predict the result (according to the increase of ground source voltage, you can know the need to reduce the output power).

    With all that said, you should understand that feedback is required (feedforward is also required, but it is not required) and PID cannot be replaced (unless you use other, more complex control algorithms).

Related questions
16 answers2024-03-16

Scheduled interrupts, external interrupts?

5 answers2024-03-16

8086 belongs to the micro process unit (MPU) and is a microprocessor. >>>More

4 answers2024-03-16

The introductory course of MCU C language programming is not difficult, it is not easy to say, and the first thing to understand is to understand what these two things are when learning MCU C language? The introductory programming of single-chip microcomputer is mainly to learn C language, followed by circuit and programming language. >>>More

20 answers2024-03-16

This can be done with the 3-terminal chips of the 78xx series. The peripheral circuit is very simple, and there is no need to use a single-chip microcomputer. If you make a constant current source with adjustable current, you can consider single-chip microcomputer +78xx (such as ).

5 answers2024-03-16

For how to learn single-chip microcomputer, I think it is still necessary to master its hardware, storage structure, assembly must be proficient, because the introduction of assembly is very helpful and understand the hardware, if you learn single-chip microcomputer, and then learn other, such as arm, at this time you can only be familiar with the assembly, but the first time to learn single-chip microcomputer, assembly, must be proficient, I mean at first do not use c to write programs, write with sinks, write can 10 20 assemblers, your assembly will go to the next level, In the future, you'll be using C for development, and you'll see how comfortable it is to optimize C with a solid foundation for assembly. Regarding the study of microcontrollers, I think it is necessary to have: >>>More