Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
12/10/07 20:28
Read: times


 
#148055 - More on the PWM program
Responding to: ???'s previous message
Hi Nima,

One of the reasons to write in assembly language rather than a high level language like C is to make your programs small and fast. Once you get your PWM program working properly, you might want to think about ways to optimize it. Sometimes you'll need to do this sort of thing for practical reasons. If you're a bit of a geek, you might also find that it's just plain fun.

As an example, here's a snippet that works about the same as your program does, but with only eight instructions in the main loop:
MainLoop:   CLR   RI0               ; Acknowledge any recent Rx bytes

            SETB  LED               ; Turn output on
StillOn:    DEC   A                 ; Count down from OnTime (in SBUF)
            CJNE  A,#0,StillOn      ;  to zero

            CLR   LED               ; Turn output off
StillOff:   DEC   A                 ; Count down from 255 to OnTime
            CJNE  A,SBUF,StillOff   ;  (in SBUF)

            JMP   MainLoop          ; Repeat forever
(Folks here are geeky enough that somebody will probably pop up and show how to do it with six instructions, or four :)

Here are a couple of other things to think about once you get your program working properly:
  • Because of the subroutine calls and loop overhead and whatnot in your program, the duty cycle of the output waveform won't be exactly what you'd expect from your TON and TOFF numbers.
  • Since the output waveform depends on the timing loops in your program, it would be really difficult to make the microprocessor do anything else besides generate the PWM output.
You can solve both of these problems by using the hardware timers and their associated interrupts. I'll bet Craig's book explains these things in detail. Or, as Erik mentioned here, if your microcontroller has a Programmable Counter Array (PCA), it will generate PWM signals all day long with no help at all from the processor once you get it iniitalized.

It would be interesting to know what you are doing. What made you want to generate a PWM signal in the first place?

-- Russ


List of 22 messages in thread
TopicAuthorDate
PWM code thoughts and help?            01/01/70 00:00      
   Some thoughts and help            01/01/70 00:00      
      very good help Russ            01/01/70 00:00      
      cheers!            01/01/70 00:00      
      character for 32            01/01/70 00:00      
         Good            01/01/70 00:00      
      A & R4 fatal occurence            01/01/70 00:00      
         Info on interrupts            01/01/70 00:00      
            Right next to me...            01/01/70 00:00      
               Aha            01/01/70 00:00      
      tell the truth!            01/01/70 00:00      
         Take a few deep breaths            01/01/70 00:00      
            no arteries popping            01/01/70 00:00      
   hows this? :)            01/01/70 00:00      
      Better, but still has problems            01/01/70 00:00      
         running out of subject line ideas            01/01/70 00:00      
            re: running out of subject line ideas :)            01/01/70 00:00      
            Some comments            01/01/70 00:00      
   Interrupts and shared resources            01/01/70 00:00      
   a thought - if it is help is up to you            01/01/70 00:00      
   More on the PWM program            01/01/70 00:00      
      Purpose of my question            01/01/70 00:00      

Back to Subject List