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 have the assmebly code for the generation of one PWM signal to get it on p1.0 using the timer0. I want to generate some more PWM signals in p1.1, p1.2, p1.3,p1.4 . Is there any method which you can tell for this. I have to write the assembly code for getting these PWM signals. I am not looking for devices which gives 5 PWM. I hope someone of you can help me.
    Boby

Reply
  • I have the assmebly code for the generation of one PWM signal to get it on p1.0 using the timer0. I want to generate some more PWM signals in p1.1, p1.2, p1.3,p1.4 . Is there any method which you can tell for this. I have to write the assembly code for getting these PWM signals. I am not looking for devices which gives 5 PWM. I hope someone of you can help me.
    Boby

Children
  • I assume then that you don't have specialised hardware for generating PWM and that you are going to have to rely on software methods.

    Of course you can generate PWM on any microprocessor, but processing resources will limit the frequencies you can achieve.

    At what frequency to you want the PWM to be put out and how much resolution do you require?

    Using a timer to generate "tick" interrupts every 1ms or so should not be too difficult. All you need then is software to determine whether any particular PWM output should be on or off. With a resolution of 1%, the period of your pwm will be 10Hz. Increasing the frequency or the resolution from this point will require increasingly careful coding and/or faster processing speed.

    Off the top of my head, I'd guess that with an IRS written in assembler say it should be possible to get the interrupt period down to about 100 instruction cycles (i.e. 100us on s standard 8051 running on a 12MHz crystal). Of course, this will leave the processor with relativly little time to do anything else.

  • one PWM signal to get it on p1.0 using the timer0
    scrap your controller and use one that has a PCA (P89C51Rx2, A(T)89C51Rx2 P89C66x, xxx51Fx etc.)

    There is no way you will get 5 PWMs reliably without using a derivative with a PCA.

    Erik

  • "There is no way you will get 5 PWMs reliably without using a derivative with a PCA."

    Nah. I'm sure Graham's software method would work perfectly reliably - just not very fast! ;-)

  • Nah. I'm sure Graham's software method would work perfectly reliably - just not very fast
    Ok, OK agreed; however if a scheme is concocted where one timer drives 5 PWM's - and try to go fast - I have no doubt that setup will stumble frequently.

    Erik

  • 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.

  • "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.

  • ONCE MORE

    1) Throw your uC out and get one with a PCA.
    2) Look at the PCA cookbook
    3) smile and be happy.

    Erik