Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

RC ppm

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
maxneo

Bascom Member



Joined: 07 Sep 2010
Posts: 35

PostPosted: Wed Jan 07, 2015 12:25 pm    Post subject: RC ppm Reply with quote

Hi, I'm trying to decoding ppm from an rc radio and I have a problem. I get three pulses that are 1 or 2 ms I use Config Timer1 = Timer, Capture Edge = Rising, Prescale = 8, Noise Cancel = 1 see attached code. The problem is the last pulse counted in synch pulse, is there any clever solution to this?

$regfile = "m32def.dat" ' specify the used micro
$crystal = 8000000 ' used crystal frequency
$baud = 9600 ' use baud rate
$hwstack = 50 ' default use 32 for the hardware stack
$swstack = 70 ' default use 10 for the SW stack
$framesize = 70
'$baud = 38400 ' uart speed

Dim X As Byte ' channel index for printing
Dim Z As Byte ' channel index for in side the isr
Dim Chan(3) As Word ' holds the channel values (in uSecs)
Dim Cnt As Word ' hold the current pulse in the isr
Dim Sync As Word ' hold the sync value for debug (not needed)

Const Syncpulse = 14000 ' the sync pulse is somthing over 5000 uSecs

' configure port
Config Portd.6 = Input ' imput from vex (ICP)
Portd.6 = 1 ' turn on pullup resistor (no external parts needed)

' Configuring Timer1 for 1 uSec ticks - crystal 8000000 / 8 = 1 uSec
Config Timer1 = Timer , Capture Edge = Rising , Prescale = 8 , Noise Cancel = 1
On Capture1 Pulsecapture_isr ' main code for getting pulse values
Enable Capture1
Enable Interrupts

'Print Chr(27) ; "[2J"; ' ansi clear screen...
Print "starting..."

' Main loop...
Do
'print chr(27) ; "[2;1H"; ' move cursor to the second line
For X = 1 To 3
Print "Chan" ; X ; ": " ; Chan(x) ; " " ' show the value of a give (x) channal
Next
Print "Sync : " ; Sync ; " " ' show the leanth of the sync pulse
Waitms 500
Loop


' All work is handled in the following isr
Pulsecapture_isr:
Cnt = Capture1 ' get the current pulse value
Timer1 = 0 ' reset the timer for the next pulse
If Cnt > Syncpulse Then ' check for sync pulse
Sync = Cnt ' save the current pulse value for debug
Z = 1 ' set the index to the first channel
Elseif Z < 4 Then ' assign 1-6 and then wait for sync pulse
Chan(z) = Cnt ' assign the pulse value to the proper channel
Incr Z ' next channel...
End If
Return

-------- -------- --------
| | | | | |
| 1 | | 2 | | 3 | synkpulse
__| |___| |___| |_________________________________


when the channel three is changed, only the sync pulse because the timer 1 meter from high to high.
Grateful for the help.
Maxneo



[/img]

(BASCOM-AVR version : 2.0.7.8 )
Back to top
View user's profile
jrkahrhoff

Bascom Member



Joined: 25 Jan 2006
Posts: 169

usa.gif
PostPosted: Thu Jan 08, 2015 4:00 am    Post subject: Reply with quote

Hello Mazneo,

Here is some information about the input capture function:

"The Input Capture function of Timer/Counter1 provides a capture of the Timer/Counter1 contents
to the Input Capture Register - ICR1, triggered by an external event on the Input Capture Pin - ICP.
The actual capture event settings are defined by the Timer/Counter1 Control Register -TCCR1B.
In addition, the Analog Comparator can be set to trigger the Input Capture."


And here is an Atmel Application note on using the Input Capture on Port D Pin 6 (the input capture pin).
Although the example code is written in C, it is easy to see how they are calculating the pulses:

http://www.atmel.com/images/doc8014.pdf

Of special note is this section:

"The PWM Decoder uses the Atmel AVR’s Input Capture Unit (ICP) to measure the
PWM pulse width and period. The ICP provides an edge-triggered interrupt based on
the state of the AVR’s ICPn pin, along with an internal register (ICRn), which contains
a snapshot of the respective Timer/Counter (TCNTn) register at the moment of the
trigger. By subtracting successive trigger timestamps, the pulse width and period may
be computed.
"


I am not certain how this feature is implemented in Bascom, but is CAPTURE1 the value of the timer register?
That is what you want to PRINT on the first pass. I would try to simply get one channel working before you try to print them all.

Good luck.
John
Back to top
View user's profile
Deanus

Bascom Member



Joined: 26 May 2006
Posts: 188
Location: Adelaide

australia.gif
PostPosted: Thu Jan 08, 2015 10:09 am    Post subject: Reply with quote

Hi jrkahrhoff,


Pulsein ?

Action
Returns the number of units between two occurrences of an edge of a pulse.


Syntax
PULSEIN var , PINX , PIN , STATE


Remarks

var A word variable that is assigned with the result.
PINX A PIN register like PIND
PIN The pin number(0-7) to get the pulse time of.
STATE May be 0 or 1.0 means sample 0 to 1 transition.1 means sample 1 to 0 transition.
ERR variable will be set to 1 in case of a time out. A time out will occur after 65535 unit counts. With 10 uS units this will be after 655.35 mS.

You can add a bitwait statement to be sure that the PULSEIN statement will wait for the start condition. But when using the BITWAIT statement and the start condition will never occur, your program will stay in a loop.

The PULSIN statement will wait for the specified edge.

When state 0 is used, the routine will wait until the level on the specified input pin is 0. Then a counter is started and stopped until the input level gets 1.

No hardware timer is used. A 16 bit counter is used. It will increase in 10 uS units. But this depends on the XTAL. You can change the library routine to adjust the units.


Dean
Back to top
View user's profile
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1257
Location: SYDNEY

australia.gif
PostPosted: Thu Jan 08, 2015 11:22 am    Post subject: Reply with quote

Hello maxneo

Have a look at this http://www.mcselec.com/index2.php?option=com_forum&Itemid=59&page=viewtopic&t=11304
I did this for infra red you should be able to do it for the RC.

Regards Paul
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum