cancel
Showing results for 
Search instead for 
Did you mean: 

PSoC 5, 3 & 1 MCU

Anonymous
Not applicable
        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?   
0 Likes
16 Replies
ETRO_SSN583
Esteemed Contributor

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.

0 Likes
ETRO_SSN583
Esteemed Contributor

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

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable
        I want to read the length of a duty cycle. Ill look at what you linked thanks.   
0 Likes
Anonymous
Not applicable
        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   
0 Likes
ETRO_SSN583
Esteemed Contributor

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.

0 Likes
ETRO_SSN583
Esteemed Contributor

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.

0 Likes
Anonymous
Not applicable
        “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   
0 Likes
Anonymous
Not applicable
        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   
0 Likes
Anonymous
Not applicable
        I have to read abit more up on pin interrupts 🙂 Thanks for your help so far   
0 Likes
ETRO_SSN583
Esteemed Contributor

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

   

 

   

0 Likes
ETRO_SSN583
Esteemed Contributor

0 Likes
ETRO_SSN583
Esteemed Contributor

This can help with GPIO interrupts -

   

 

   

    

   

          

   

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

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable
        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   
0 Likes
Anonymous
Not applicable
        Please tell me if there is any way that I an answear without all the text being written in one long line. Thanks   
0 Likes
ETRO_SSN583
Esteemed Contributor

The forum software has an issue that sometimes a small window

   

appears and thats all you get to type into. I find using Firefox that

   

most of the time this does not occur, and when it does, clear all history

   

and reload page, that usually fixes problem.

   

 

   

Regards, prior posts, I think if you run counter continuously, you will not

   

have to relaod counter. You just keep reading. The only issue you have

   

is to test for rollover. Say counter is at 100 (last reading), next measurement

   

is at 150, that means counter rolled over, so your answer = last reading +

   

256 - new reading. For 8 bits.

   

 

   

Regards, Dana.

0 Likes
ETRO_SSN583
Esteemed Contributor

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

   

 

   

If you do not have a scope signal verification, insuring PWMunknown is clean to make

   

measurements on difficult.

   

 

   

If you have a pc, you have a signal generator and oscilloscope, low speed, via use of

   

your sound card. YOU HAVE TO BE VERY CAREFUL WITH SIGNAL LEVELS BEING

   

INPUT TO SOUND CARD, USE A R DIVIDER TO MAKE SURE SIGNAL IS SMALL TO

   

PROTECT YOUR SOUND CARD INPUT. Same with output.

   

 

   

    

   

          http://www.zeitnitz.de/Christian/scope_en

   

    

   

          http://www.radio.imradioha.org/PC_Based_Test_Gear.htm

   

 

   

You can slow down signals so they fall in range of PC Scope, then raise them

   

after you have finished debug.

   

 

   

Regards, Dana.

0 Likes