Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PWN signal read on microprocessor

Status
Not open for further replies.

SimonTHK

Member
I want to read the PWM signal on my microprocessor. I could build a PWM to analog converter but that wouldnt be so accurate.

Can anyone try and explain me how I should start looking at this small project? I am using PSOC Cypress microcontroller.

What would you do? :)

thanks in advance
 
I want to read the PWM signal on my microprocessor. I could build a PWM to analog converter but that wouldnt be so accurate.

How do you know that PWM to analog converter wouldn't be so accurate?
Do you need to know the exact time periods (on time and off time) of the PWM or only the average DC level?
 
I would suggest you need to give more details, it's far too vague - what are the specifications of the signal, indeed what is the signal?.

Accuracy aside, for most purposes it would be pretty pointless to convert the signal to analogue then convert it to digital, when it's easy to directly read the timing of the pulses.
 
I would suggest you need to give more details, it's far too vague - what are the specifications of the signal, indeed what is the signal?.

Accuracy aside, for most purposes it would be pretty pointless to convert the signal to analogue then convert it to digital, when it's easy to directly read the timing of the pulses.

Depends on the PWM frequency whether it is easier (or more accurate) to use a timer or an AD-converter. Also with adequate ADC it is possible to keep the microcontroller clock frequency low.. depending on the underlying analog signal (of the pwm).
 
Last edited:
The pulsewidth are from 0,5 ms to 2,5 ms at 50 hz, so one period is 20 ms :)

So I will send a signal that is in between this cycle and the microprocessor has to read this. I am new to this stuff, and I am not working with it RIGHT now but I know I will be soon, so I thought I would ask abit for preparation.
 
Hi,

Simpler to tell : Radio Control pulses !!!!

easy solution is to use the CCP module in capture mode, and free running TMR1 :

you read TMR1 on rising edge capture interrupt , change config to read TMR1 on falling edge capture interrupt and make the difference beetween both measurements ...

BTW you also can verify the signal period to detect wrong signal ... or no signal


a PBP ( easy to read ... ) example code ...

Code:
'*****************************************************************************
Capture: ' ISR to manage edge detection
'*****************************************************************************

    If RisingEdge Then
    
        CCP1CON   = Capture_FE                   ' set up to detect next edge       
        StartEdge = CCPR1L + ( CCPR1H << 8 )     'Compteur Re
              
        IF DCEdge <= StartEdge THEN
         
        	Period = StartEdge - DCEdge + DUTY
        	
        ELSE
        
        	PERIOD =  StartEdge + ( 65535 - DCEdge ) + DUTY	+ 1  ' Period en µs
  
        ENDIF	
        
    Else
    
        CCP1CON   = Capture_RE                   ' set up to detect next edge    
        DCEdge    = CCPR1L + ( CCPR1H << 8 )
        
        IF StartEdge <= DCEdge THEN
         
        	DUTY = DCEdge - StartEdge
        	
        ELSE
        
        	DUTY = DCEdge + ( 65535 - StartEdge ) + 1		  ' Duty en µs
 
        ENDIF
        
    EndIf
    
    
    RisingEdge    = Not(RisingEdge)               ' toggle edge detection flag
 
    Catched = 1
	
@ INT_RETURN

Alain


no more precise measure ...
 
Last edited:
easy solution is to use the CCP module in capture mode, and free running TMR1 :
you read TMR1 on rising edge capture interrupt , change config to read TMR1 on falling edge capture interrupt and make the difference beetween both measurements ...

Wouldn't it be possible to clear the timer on rising edge and read it on falling edge? This would lead to much smaller and efficient interrupt routine. If you have to handle timer overflows do it in a separate interrupt. I don't know if PICs support these kind of features, but they are pretty basic things in microcontrollers today.

edit: OP uses PSOC Cypress microcontrollers, but I don't know much about their features either.
 
Last edited:
No real need to be that clever about it, it's trivial to time it with a simple software loop, which allows you to use multiple pins for multiple channels - this is how the BASIC STAMP does it (which even provides an actual instruction for reading servo pulses.
 
Yes Nigel ...

just resolution @ 4 Mhz drops from 1 µs to 10 µs ...

soooo little a difference ...

It doesn't drop to as low as 10uS (probably 4 or 5?), and in any case why would you want 1uS resolution on servo pulses? - and if you did, simply use a 20MHz clock - but I don't think the resolution of servo pulses themselves is anywhere near 1uS.

Just done a quick google - according to sites I found resolution of the transmitters is a MAXIMUM of 1024 (2uS resolution), and I doubt the servos themselves manage even 1% accuracy?.
 
It doesn't drop to as low as 10uS (probably 4 or 5?),

best seen on the web( amateur progs ) is 11 µs ... basics ( pros ) reach 10 !

and in any case why would you want 1uS resolution on servo pulses? - and if you did, simply use a 20MHz clock - but I don't think the resolution of servo pulses themselves is anywhere near 1uS.

Best servos easily give 2 µs today ... my old analogic servos ( Futaba S9201/Graupner C 4051 ) already are 3-4 µs resolution ( measured ! ...) and they are 20 years old !!!

Just done a quick google - according to sites I found resolution of the transmitters is a MAXIMUM of 1024 (2uS resolution), and I doubt the servos themselves manage even 1% accuracy?.

.5% is a very common value for the last 10 years ... Digital ones are better than that, as there's no more deadband ...

Telling it gently : You should update your knowledge on the subject ... and many Web sites are a bit late too ... - not easy to write and learn at the same time ... don't you agree ???

1024 Steps ??? Really sure ??? ... **broken link removed**

Alain ... 40 years of R/C using and building behind him ...
 
Last edited:
Hi peeps
I now started trying to make the timer work and do some calculations for it :) Im playing around to learn the basic stuff, so I would really like if you could agree or disagree of what I have done until now.

Sysclk = 6 mhz
Vc1= sysclk/16
Vc1= 375.000 hz
Im using a 16 bit timer 0 – 65536

I set 1 period to 37500 (1/10 of 375.000 hz) so 1 period is equal to 0,1s

0,1s / 65537= 0,000002s = 0,002 ms per bit or 2 us

Then I want to figure out how many bits I need to have the 0,5 ms (which is the shortest pulse I will receive):

0,5 ms / 0,002 ms = 250

0,5 ms = 250 value

2,5 ms is the longest pulse I will receive: 2,5 / 0,002 = 1250 value
 
Im using a 16 bit timer 0 – 65536
0,1s / 65537= 0,000002s = 0,002 ms per bit or 2 us

16 bit timer gets values from 0 to 65535.
 
Status
Not open for further replies.

Latest threads

Back
Top