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

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
07/24/09 18:48
Modified:
  07/24/09 20:52

Read: times


 
#167840 - Wrong.
Responding to: ???'s previous message
Chico Magalhães said:

As I got, the code dont LOOK much diferent than mine, just more complicated.

My code is right, this I have certain.
It can be put in a timer interruption.
But putting it in a timer interruption will just slow it even more

Cant see a faster way to do a multiple software PWM.
I think my code is VERY clean, easy to use, and special to use with LEDs, as it gives full low and full high output with 0 and 255 respectively.



Its "clean" but it won't do what you want, so its useless. How helpful is "clean" ? It is certainly not "right", because, again, it doesn't do what you want. You don't know HOW to write an interrupt version yet you say putting in an interrupt will make it slower !

You are completely wrong. THINK about it. The servicing of the on and off of the port bits takes MICRO seconds, the interval between the servicing is hundreds of microseconds, the interrupt SPEEDS UP the program. Lets modulate the LEDS at 30Hz, 33mSec, 33,000 usec.

Lets have a timer interrupt firing every 1/255 of 33,000, or every 130 usec. With a 24Meg, straight clock processor, that gives you about 260 instructions between servicings.

Keep 8 registers with your PWM levels, preset by your channel setting routine. Every time you service the interrupt, you have a counter variable in idata which you decrement. Decrement the PWM channel. If the PWMregister is 0 you turn off the bit corresponding to that channel. When the counter variable rolls over to 00 you turn all the outputs back on.

I think its about 5 instructions per channel, 48 instructions in the service routine. So you are in your ISR for about 18% of the time, and that's a fixed quantity for all channels,
;Destroys Accumulator
;Demonstration only. Timer setup is your problem, you may want your PWM ports to work differently. 
       XCH A,r0    ;Get r0
       DEC A
       JNZ nxtr1   ;Is it Zero ? Turn off the bit, else next bit
       clr P2.0 
nxtr1: XCH A,r0    ;PUT DECREMENTED register back !
       XCH A,r1    ; next register
       DEC A
       JNZ nxtr2
       clr P2.1
nxtr2: XCH A,r1
       XCH A,r2
       DEC A.
       JNZ nxtr3
       clr P2.2 
nxtr3: ................

       DEC CTR     ; Reload registers with PWM values
       JNZ ENDi
       MOV r0,PWM0 
       MOV r1,PWM1
       .......
       MOV P2,#255 ; turn all bits on again.

ENDi:  RETI



 


As Kai says, if you use a proper modern variant, you'll have a real PWM generator anyway, and you may even have reduced clock cycles too. The NXP ones will run 6 clocks/ instruction, the Dallas ones manage 1/instruction, so an NXP processor in X2 mode will only have 9% interrupt overhead, a Dallas chip less than 1%.

Your original solution uses 100% cpu time whatever the chip can do. So its dumb.



List of 29 messages in thread
TopicAuthorDate
Simple PWM to drive led            01/01/70 00:00      
   Loop lasts too long!            01/01/70 00:00      
      I put the delay...            01/01/70 00:00      
         Did you see?            01/01/70 00:00      
            didnt noticed!            01/01/70 00:00      
               Ok, try the following...            01/01/70 00:00      
                  the JMP            01/01/70 00:00      
                     I thought this was your intention...            01/01/70 00:00      
   Another try            01/01/70 00:00      
   is possible to filter the pwm output...            01/01/70 00:00      
      PWM from a Timer interrupt?            01/01/70 00:00      
      questions            01/01/70 00:00      
   best code until now            01/01/70 00:00      
      read replies            01/01/70 00:00      
         will lower refresh rate            01/01/70 00:00      
            Time to switch...            01/01/70 00:00      
            Wrong.            01/01/70 00:00      
               my code is doing what I want            01/01/70 00:00      
                  TIming            01/01/70 00:00      
                     FOR NOW!!!!            01/01/70 00:00      
                        The old story...            01/01/70 00:00      
               DJNZ            01/01/70 00:00      
                  Dec            01/01/70 00:00      
                     Do nothing?            01/01/70 00:00      
                        Speed            01/01/70 00:00      
                           slowdown            01/01/70 00:00      
                              Chico            01/01/70 00:00      
                                 Not yet...            01/01/70 00:00      
                                    good catch            01/01/70 00:00      

Back to Subject List