This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

PWM assembly code for generation of 5 PWM signals in 8051 mc

I am doing a project in 8051 mc in which I have to generate atleast 5 PWM signals with varying period and duration. Is it possible? What is the logic to be used. Is anyone having the assembly code in 8051 or in C language for this? If so please send it to me at epey2p158@nitc.ac.in
thanks in advance.

Parents
  • I exactly want what Mr.Graham told. A software method. I don't want a very fast system. The period can be even upto 2 second. But the duration of PWM about 20-100ms. Is it possible to have such a system? Is it possible using only one timer? Can you explain some logic for that. Actually, I want that both period and duration of all the 5 PWMs to be different. The values for duration and period for 5 signals(5*2=10 values) should be taken from some memory location where we are already stored the corresponding values for period and duration.

Reply
  • I exactly want what Mr.Graham told. A software method. I don't want a very fast system. The period can be even upto 2 second. But the duration of PWM about 20-100ms. Is it possible to have such a system? Is it possible using only one timer? Can you explain some logic for that. Actually, I want that both period and duration of all the 5 PWMs to be different. The values for duration and period for 5 signals(5*2=10 values) should be taken from some memory location where we are already stored the corresponding values for period and duration.

Children
  • "I exactly want what Mr.Graham told."

    So what's your problem now?
    Graham's explanation was perfectly clear - short of him actually writing the whole thing for you, what more do you need?!

    "Is it possible to have such a system?"

    See the previous answers

    "it possible using only one timer?"

    How many timers did Graham suggest?
    Count 'em!

  • I exactly want what Mr.Graham told. A software method. I don't want a very fast system. The period can be even upto 2 second. But the duration of PWM about 20-100ms. Is it possible to have such a system? Is it possible using only one timer? Can you explain some logic for that. Actually, I want that both period and duration of all the 5 PWMs to be different. The values for duration and period for 5 signals(5*2=10 values) should be taken from some memory location where we are already stored the corresponding values for period and duration.

    One way or another, you need five timers - one for each PWM. You don't have five hardware timers, so it is necessary to "simulate" these timers in software. Each timer is simply a variable in memory.

    If it helps, think of each timer as being like the second hand on a clock. The second hand moves with every tick and after 60 ticks it is back where it started. To generate a PWM from the second hand of a clock, the output is simply a matter of how far round the clock the second hand has travelled. To put out 25% PWM, the output will be on for the first 15 seconds and then off for 45 seconds, and so on.

    The hardware timer you do have can provide a "tick" that triggers an interrupt. It is slightly more efficient to count down than up, therefore I suggest a simulated timer that runs backwards. You will need something like this for each PWM timer:

        MOV    A,pwm_timer_n
        DJNZ   A,xxx
        MOV    A,#pwm_timer_n_period
    xxx:
        MOV    pwm_timer_n,A
    
    Now, the only remaining question is whether the coresponding PWM output should be on or off. This is simply a question of whether the value of the timer is greater or less than a threshold value. You will need something like this for each PWM timer:
        MOV    A,pwm_timer_n
        CJNE   A,pwm_on_time_n,$+3
        MOV    pwm_output_bit,C
    
    Note that here the CJNE instruction is just being used to set or clear the carry as a result of the comparison.

    That should be enough to get you started although there are plenty of details to to fill in.