1 2 Previous Next 16 Replies Latest reply on Dec 10, 2013 10:09 AM by user_14586677

    How do you read PWM signals?

    simon.kristensen
              Hello I know that in Arduino you can make it read a PWM signal by just writing "read pwm signal", or kinda like that. How would you read PWM signals? I can do it, but I just thought if there was an easier way. The way I do it is to let a counter run when a signal input is high (the Square wave). Then I read the value on the counter and I know the signal length. I havnt been able to find an easy way to reset the counter, so I save the value and compare it to next time it reads a signal. I can either read the signal when the input makes interrupt on Falling edge or just read it sometimes in the code, if the input is low (though the last one is only preferred in small programs. How do you read PWM signals?   
        • 1. Re: How do you read PWM signals?
          user_14586677

          Are you concerned with the duty cycle of the PWM signal or the period/frequency ?

             

           

             

          If the latter attached is an example of a freq counter, Period = 1 / Freq or vice versa.

             

           

             

          If you need the duty cycle then use a timer to capture high time, and use it again to

             

          capture low time.

             

           

             

          Regards, Dana.

          • 2. Re: How do you read PWM signals?
            user_14586677

            Attached is some ref material on basic concepts of freq/period measurement.

               

             

               

            Regards, Dana.

            • 3. Re: How do you read PWM signals?
              simon.kristensen
                      I want to read the length of a duty cycle. Ill look at what you linked thanks.   
              • 4. Re: How do you read PWM signals?
                simon.kristensen
                        Can you explain what you mean exactly with the timer? I have done it many different ways, but I cant quite understand what you mean. Have the timer running all the time and reading the state of an input meanwhile? Thanks   
                • 5. Re: How do you read PWM signals?
                  user_14586677

                  Actually might be better to use a counter because it has an enable.

                     

                  So feed PWM signal to counter enable. Counter clock will determine

                     

                  resolution of measurement. When enable goes high, counter starts

                     

                  counting down (you preloeaded it with full scale, full scale is 2**n - 1)

                     

                  then when enable goes low read counter, subtract from  full scale, and that

                     

                  is counts(time) PWM was high. You can do this by interrupt or polling, freq

                     

                  of PWM may determine which method. Also reload counter when enable is

                     

                  low.

                     

                   

                     

                  Now you have to measure low, so could use another counter whose enable

                     

                  is inverted from PWM. Same as above but measures low time/counts of PWM.

                     

                   

                     

                  Or use one counter, running countinuously. When PWM edge goes high, use pin

                     

                  ISR to read counter, that subtracted from full scale is high period. Counter keeps

                     

                  going. Use another pin also connected to PWM signal, set for falling edge ISR.

                     

                  When ISR triggers read counter again, subtract that from earlier reading, you have

                     

                  high time. Also this is the start of low time, so next time pin ISR edge goes high,

                     

                  read counter and subtract from falling edge reading, that becomes low time.

                     

                   

                     

                  As you can see, various ways of doing this.

                     

                   

                     

                  What is the range of PWM period you have to measure ? Resolution of measurment ?

                     

                   

                     

                  Regards, Dana.

                  • 6. Re: How do you read PWM signals?
                    user_14586677

                    An error in prior post -

                       

                     

                       

                    Or use one counter, running countinuously. When PWM edge goes high, use pin

                       

                    ISR to read counter, that subtracted from full scale is high period. Counter keeps

                       

                    going. Use another pin also connected to PWM signal, set for falling edge ISR.

                       

                    When ISR triggers read counter again, subtract that from earlier reading, you have

                       

                    high time. Also this is the start of low time, so next time pin ISR edge goes high,

                       

                    read counter and subtract from falling edge reading, that becomes low time.

                       

                     

                       

                    Should be

                       

                     

                       

                    Or use one counter, running continuously. When PWM edge goes high, use pin

                       

                    ISR to read counter. Counter keeps going. Use another pin also connected to PWM

                       

                    signal, set for falling edge ISR. When ISR triggers read counter again, subtract that

                       

                    from earlier reading, you have high time. Also this is the start of low time, so next time

                       

                    pin ISR edge goes high, read counter and subtract from falling edge reading, that

                       

                    becomes low time.

                       

                     

                       

                    If you are pin limited, place two buffers, and set them to gen ISR on opposing edges.

                       

                     

                       

                    Regards, Dana.

                    • 7. Re: How do you read PWM signals?
                      simon.kristensen
                              “when enable goes low read counter, subtract from full scale, and that is counts(time) PWM was high. You can do this by interrupt or polling” What is polling? I can use the global interrupt yes, on falling edge, is there any other way to get an interrupt when the input to the counter gets low? And you cannot reset the counter, so after first read you are stuck at (lets say 8bit counter) some number between 255-0. This above mentioned way to di it is what I do, but since I cant reset the counter back to 255, I have to use this big code to do it. Cant cypress implement a function to reset counters ^^ or being able to change the value. I only need to measure the duty cycle. Also for what I understand, there is only 1 interrupt function for all inputs. So that if I use the falling edge method (which is ok) then I shouldn’t be using other interrupt´s on any other inputs, because they trigger the same function right? Thanks in advance   
                      • 8. Re: How do you read PWM signals?
                        simon.kristensen
                                It is not that I don't know how to do it. But I want to figure out the easiest way to measure a duty cycle. It could be quite simple if forexample a counter that stopped counting made an interrupt and if you where able to reset the counters value. But I don't think I am able to do any of this, atleast not in a human like way. Thanks   
                        • 9. Re: How do you read PWM signals?
                          simon.kristensen
                                  I have to read abit more up on pin interrupts :) Thanks for your help so far   
                          • 10. Re: How do you read PWM signals?
                            user_14586677

                            “when enable goes low read counter, subtract from full scale, and that is counts(time) PWM was high. You can do this by interrupt or polling” What is polling?

                               

                             

                               

                            Polling is repeatedly reading a HW status, like a pins value in this case. Essentially a do or while

                               

                            loop.

                               

                             

                               

                             

                               

                            I can use the global interrupt yes, on falling edge, is there any other way to get an interrupt when the input to the counter gets low?

                               

                             

                               

                            Yes, place a buffer in digital array, route PWM in to its input, and set up its ISR for falling edge

                               

                            of PWM input.

                               

                             

                               

                            And you cannot reset the counter, so after first read you are stuck at (lets say 8bit counter) some number between 255-0. This above mentioned way to di it is what I do, but since I cant reset the counter back to 255, I have to use this big code to do it. Cant cypress implement a function to reset counters ^^ or being able to change the value. I only need to measure the duty cycle.

                               

                             

                               

                            There is a simple API in counter datasheet to reload the period.

                               

                             

                               

                            Also for what I understand, there is only 1 interrupt function for all inputs. So that if I use the falling edge method (which is ok) then I shouldn’t be using other interrupt´s on any other inputs, because they trigger the same function right? Thanks in advance

                               

                             

                               

                            Via register writes you can change a pins ISR response from rising to falling edge. You find

                               

                            this in the register TRM for the part you are using. For example see below for a 29466.

                               

                             

                               

                            Regards, Dana.

                               

                             

                               

                             

                            • 12. Re: How do you read PWM signals?
                              user_14586677

                              This can help with GPIO interrupts -

                                 

                               

                                 

                                  

                                 

                                        

                                 

                              http://www.cypress.com/?rID=2900    AN2094

                                 

                               

                                 

                              Regards, Dana.

                              • 13. Re: How do you read PWM signals?
                                simon.kristensen
                                        This is a lot for my small brain :) huge help thanks. So let me understand this right. """Counter8_WritePeriod: Writes the Period register with the period value. The period value is transferred from the Period register to the Counter register immediately, if the Counter8 is stopped or when the counter reaches the zero count.""" This means, that I have (when I want to reset a counter) do like this: Counter8_WritePeriod(255); Counter8_Stop(void); Counter8_Start(void); This will make the counter start from 255 again? I could more easily test this if I had the right tools. I have no function generator, nor any scope. Thanks   
                                • 14. Re: How do you read PWM signals?
                                  simon.kristensen
                                          Please tell me if there is any way that I an answear without all the text being written in one long line. Thanks   
                                  1 2 Previous Next