How to write the timing program of 51 single chip microcomputer for novices? or some other way .

Updated on technology 2024-04-08
21 answers
  1. Anonymous users2024-02-07

    Use the timer to make a 100Hz PWM signal, and set another timer in the timer at the same time, and the pulse width is halved after half an hour of PWM, that is, the power is halved.

  2. Anonymous users2024-02-06

    You can use a digital potentiometer, reduce the current, doesn't it reduce the power, this is my idea, you can also try!

  3. Anonymous users2024-02-05

    There is no doubt about using PWM signals, but I think the frequency is lower at 100Hz. Another problem is that you don't necessarily have to use two timers, you can also use the PWM signal period to add up.

  4. Anonymous users2024-02-04

    Hello dear, the process of initialization of the 51 microcontroller timer is as follows:1Select the timer working mode:

    There are usually two working modes of timer and counter, which can be selected according to the needs. 2.Set the count value for the timer:

    The higher the timer count value, the longer the timer time. 3.Select Timer Clock Source:

    Select an internal or external clock source. 4.Set the timer clock division factor:

    The higher the clock division factor, the slower the timer counts. 5.Enable timer interrupt:

    Enable interrupt triggers an interrupt when the timer reaches a set point. 6.Start Timer:

    If an interrupt occurs, execute the appropriate interrupt service subroutine. The above is the main process of 51 microcontroller timer initialization setting. The specific steps can be adapted and supplemented according to the development requirements and the data sheet of the specific timer module.

  5. Anonymous users2024-02-03

    Once the timer is set, it will be counted automatically.

  6. Anonymous users2024-02-02

    tmod = (tmod & 0xf0) |0x01;Timer 0 works in mode 1, 16bit timer mode.

    What is the role of 0xf0 in the above expression?

    1 (tmod & 0xf0) where & is logic and instruction, bit and operation, the mode register of the timer tmod and the upper 0xf0 function is that the upper 4 bits remain unchanged, and the lower 4 bits are cleared. (1 with any number on the same as 0 with any number on is cleared).

    tmod & 0xf0) |0x01 where |For or instruction, also called logical add. The effect here is that the upper 7 bits or the upper 0 are unchanged.

    The lowest digit or the upper 1 is set to 1

    After the operation is completed, tmod = the upper 4 bits remain unchanged (the mode word of timer 1 remains unchanged) and the lower 4 bits are set to 0001 (that is, timer 0 is set to the timing mode and works in mode 1).

    Summary: The function of the 0xf0 in the expression is that when the mode word of timer 0 is set, it does not affect the mode control word that does not change timer 1.

  7. Anonymous users2024-02-01

    0xf0 is to clear the value of the lower 4 bits and then the upper 0x01 the high 4 bits are not affected, and the transport is all 1 out of 1 with 0 out of 0, so the value of the high 4 bits will not change. The bottom 4 digits all become 0.

    The last |0x01 assign a value of 1 to the lower 4 bits

    The main thing is to reassign t0 without affecting t1.

  8. Anonymous users2024-01-31

    This means that the top four digits of the newspaper tmod are unchanged from the original value, and the lower four digits are all changed to 0Back|0x01 is the 0th position 1

    The overall meaning is that the upper four bits of tmod remain unchanged, and the lower four bits are set to 0x01, i.e. timer 0 works in mode 1

    The tmod assumption is 0x22

    tmod&0xf0 then it becomes 0x20

    Again|0x01 then it becomes 0x21

    Got it, landlord?

  9. Anonymous users2024-01-30

    In the main program:

    tmod = 0x01;Timer 0 working mode 1tl0 = (65536-50000)%256; Timed 50ms at 12MHz external crystal oscillator.

    th0 = (65536-50000)/256;

    et0 = 1;

    tr0 = 1;

    ea = 1;

    Timer Interrupt:

    void timer0() interrupt 1 }

  10. Anonymous users2024-01-29

    Depends on what the tmod value before this expression was.

  11. Anonymous users2024-01-28

    Reboot again.

    Implemented in C language, you must first define the initial value of the timer, no matter how big the crystal oscillator you use, using 51 single-chip microcomputer, it is generally 12 divides the frequency, and you can get a machine cycle, machine cycle = 12 n (n refers to the crystal frequency), assuming that the time you want to time is m, then the initial value of the timing is: m machine cycle = initial value;

    th0=(65536-initial value)%256;

    tl0=(65536-initial value) 256;

    The value obtained by (65536-initial value) is converted into hexadecimal, the high position is the value of th0, and the low value is the value of tl0, for example, the 12m crystal oscillator is used to calculate the 1ms timing as follows:

    Machine cycle = 12 12 * 10 6 = 1us (microseconds).

    Initial value of timing = (1*10 -3) (1*10 -6) = 1000;

    So: th0=(65536-1000)%256;

    tl0=(65536-1000)/256;

    Convert 65536-1000=64536 to hexadecimal system as: 0xfc18, th0=0xfc, tl0=0x18;

    After setting the initial value, a delay parameter should be set for one second, here 1000 is used (the timing is 1ms), and the interrupt program is: timer0() interrupt 1 1ms delay (

  12. Anonymous users2024-01-27

    It's not that hard, right?

    A is bright, delayed, b is bright, and then delayed, using a while(1) can make the bright process only go once.

    I'll make one for you in the evening.

    You think about it first, you can't think of getting one for you at night.

  13. Anonymous users2024-01-26

    When using timer method 1, the calculated value of (65536-x) exceeds 256, that is, the 16-bit timing mode of the timer, so it is necessary to send 8 bits higher to th0 and lower bits to tl0, so to calculate the high 8 bits and the low 8 bits of (65536-x), (65536-x) 256 is to calculate the high 8 bits, and (65536-x)%256 is to calculate the low 8 bits, so it is written.

    th0 = (65536-x) 256 (or th1).

    tl0 = (65536-x)%256 (or tl1).

  14. Anonymous users2024-01-25

    This is actually very simple, you only need to know one condition: crystal frequency.

    Because the machine cycle calculation formula of 51 single-chip microcomputer is 12 crystal oscillator frequency MHz, if the crystal oscillator is 12mhz, then the machine cycle is 12 (12 10 6) = 1us, that is to say, the counter does not count once 1us.

    Generally speaking, under the 12MHz crystal oscillator, the full count of the 16-bit timer is 65536 times, each time 1us, the maximum can be timed, then 65536 minus how much is the timing, such as (65536-x) 256, that is, the timing x microseconds.

    So if the crystal oscillator is not 12MHz, multiply the above x by 12 crystal oscillator, and the value obtained is the timing duration. The principle is the same.

    Feel free to reply to me if you don't understand something.

  15. Anonymous users2024-01-24

    m is the initial value of the timer, that is, a part of the value is added first, and then the counting starts, and the initial value is reassigned after overflowing.

    Working mode 1, i.e. x is 16

    Machine period = 12 * 1 clock cycle = 12 * 1 12 = 1us The problem requires the maximum timing time, then the initial value is m=0, that is, the initial value is not assigned to the timer, and the timer is as large as the original capacity of 2 16us

    The maximum timing time is t=2 16us

  16. Anonymous users2024-01-23

    m is the initial value of the write timer.

  17. Anonymous users2024-01-22

    It should be that there is a problem with your interrupt service program, which may take up too much time, exceeding the scheduled time, so it may cause inaccurate situations, paste your scheduled interrupter program to see, and it is best to do the following when assigning values in the timing program:

    tr2=0;

    Assign tr2=1;

  18. Anonymous users2024-01-21

    51 single-chip microcomputer timer, crystal oscillator is 12MHz, 12 cross-by-frequency, theoretically the shortest timing time is 1 microsecond, however, CPU runs instructions It takes time, as long as the running time of interrupting the service program does not exceed the timer timing time, and there is some margin, it is not a problem, if there is no margin, then the interrupt ends and enters the interrupt, if the timer timing time is exceeded, it is too late to process the timing and it is not accurate.

    Programming in C may cause problems if the timing time is too short, and it is not good to calculate how much time is spent on interruptions.

    If programmed in assembly language, the crystal oscillator is 12MHz, 12-way frequency, just produces a square wave signal, 30kHz frequency is no problem.

    Of course, you have to check the accuracy of the time that will take up after joining the counting program, and when generally programming, try to take up as little time as possible in the interrupt service program, and try not to do anything that can be processed in the interrupt service program.

    Interrupt the service program in the process.

  19. Anonymous users2024-01-20

    If you want to improve the timing accuracy, you can increase the crystal frequency such as: 24MHz, is it able to solve your problem.

  20. Anonymous users2024-01-19

    This single-chip microcomputer is not a traditional 51 single-chip microcomputer, and the clock is 12 times faster than the traditional 51 clock.

  21. Anonymous users2024-01-18

    1 microsecond is the time required for each assembly instruction, the timer interrupt involves the stack addressing out of the stack and other operations, God knows how many assembly instructions are used, you set a 20 subtle of course there is a problem, the program has other things to do, such as digital tube display (this thing is also time-consuming), can not just respond to your interruption to go...

    If you want to have a certain accuracy with interrupts, 10 20k will reach the top of the general single-chip microcomputer.

    However, if you just want to output a high-frequency square wave signal, I suggest that you do not use a timer, add a loop directly to the main program, remove the necessary **, and add a different number of nop instructions according to the required frequency, which should be able to reach a higher frequency.

Related questions
16 answers2024-04-08

Scheduled interrupts, external interrupts?

6 answers2024-04-08

In this case, it depends on sp, ret is equal to (sp) pch, sp-1 sp (sp) pcl, sp-1 sp >>>More

12 answers2024-04-08

If the timer count value is not enough, you can first set the sum number of 1, such as 1ms, and then add another number to 10 to 100ms to get the 1s time.

5 answers2024-04-08

The above answer is fine.

12 answers2024-04-08

1.Not all microcontrollers require crystal oscillators.

The crystal oscillator provides an accurate frequency, but it can also be connected to an off-the-shelf clock signal, using a ceramic oscillator, using an RC oscillator, setting it to use an internal oscillator (usually an internal RC), etc. >>>More