PDA

View Full Version : Rc signal help



geckogrotto
- 25th August 2006, 06:24
I have read quite a bit on this and most sources point to using PulsIn.

I found some code on the web for another chip and am trying to modify it for 12F675 but am getting no love.



'12F675
@ __CONFIG _INTRC_OSC_CLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF
OPTION_REG.5 = 0 ' clock source internal

Pulselen var Byte ' Pulselen can be 0 - 255, 100 = 1 ms, 200 = 2 ms
Init var Byte ' Init used to flash LED
Clear ' set all variables = 0

Input GPIO.3 ' set pin 5 to RX signal

ReadPWM:
PulsIn GPIO.3, 1,Pulselen ' pin 4 - read high pulse length, times out after .65535 seconds
pause 15
If Pulselen < 50 Then GoTo Blink ' no signal -> blink led
If Pulselen > 50 Then GoTo Solid ' signal -> Solid led
GoTo ReadPWM

Blink:
For Init = 1 To 3 ' blink led 3 times
High GPIO.1
pause 200
Low GPIO.1
pause 200
Next
pause 1000
GoTo ReadPWM

Solid:
High GPIO.1
pause 3000
Low GPIO.1
pause 200
GoTo ReadPWM


It should blink when no signal and be solid when there is but it seems to do whatever it wants no matter what signal I try and send it.

Is it the config settings?

HenrikOlsson
- 25th August 2006, 06:34
Hi,
Your program jumps to the same location whatever the pulsewidth is:


ReadPWM:
PulsIn GPIO.3, 1,Pulselen ' pin 4 - read high pulse length, times out after .65535 seconds
pause 15
If Pulselen < 50 Then GoTo Blink '<----HERE......
If Pulselen > 50 Then GoTo Blink '<----and HERE.....
GoTo ReadPWM


Change the the second If statement to


If Pulselen > 50 then Goto Solid


HTH
/Henrik Olsson.

geckogrotto
- 25th August 2006, 12:24
Yea that was an accident when I retyped to simplify the code but its fixed now and still doesn't work.

I also added this
CMCON = 7 ' PortA Digital inputs

I think this chip starts as analog or something still no help.

HenrikOlsson
- 25th August 2006, 13:58
Hi,
The 'F675 has one comparator AND an A/D converter.
The comparator is turned off with CMCON=7 and the the pins for the A/D is set to digital only with ANSEL=0.

However....

You're using GP3 for your input and that pin is not connected to either the comparator or the A/D so none of the above should really matter but I'm not sure. GP3 is however the MCLR pin and it seems you have a typo in your config setting. I THINK it should be MCLR_OFF and not MCLRE_OFF. - try it.

Also, you're using a byte-size variable to store the pulsewidth, try switching that to a Word instead.

/Henrik Olsson.

geckogrotto
- 25th August 2006, 19:06
Hi,
The 'F675 has one comparator AND an A/D converter.
The comparator is turned off with CMCON=7 and the the pins for the A/D is set to digital only with ANSEL=0.

However....

You're using GP3 for your input and that pin is not connected to either the comparator or the A/D so none of the above should really matter but I'm not sure. GP3 is however the MCLR pin and it seems you have a typo in your config setting. I THINK it should be MCLR_OFF and not MCLRE_OFF. - try it.

Also, you're using a byte-size variable to store the pulsewidth, try switching that to a Word instead.

/Henrik Olsson.


I tried swapping back and forth on ANSEL and CMCON neither seem to make a difference. Also when I change to MCLR_OFF it errors and will not even compile.

I tried using word as well no luck.

Acetronics2
- 25th August 2006, 20:06
Hi, gecko

No problem compiling with MPASM 7.41 and PbP 2.46a ...

Error [ 118 ] is to forget when using @ __config

May be add END ... at the end !!!, but that's not compulsory ( good habit then ... )

Alain

geckogrotto
- 25th August 2006, 20:17
Hi, gecko

No problem compiling with MPASM 7.41 and PbP 2.46a ...

Error [ 118 ] is to forget when using @ __config

May be add END ... at the end !!!, but that's not compulsory ( good habit then ... )

Alain


I'm not sure I understand what your saying exactly but.


'12F675
@ __CONFIG _INTRC_OSC_CLKOUT & _WDT_OFF & _MCLR_OFF & _CP_OFF
DEFINE OSC 4
OPTION_REG.5 = 0 ' clock source internal
CMCON = 7
ANSEL=0

Pulselen var word ' Pulselen can be 0 - 255, 100 = 1 ms, 200 = 2 ms
Init var word ' Init used to flash LED
Clear ' set all variables = 0

Input GPIO.3 ' set pin 5 to RX signal

ReadPWM:
PulsIn GPIO.3, 1,Pulselen ' pin 4 - read high pulse length, times out after .65535 seconds
pause 15
If Pulselen < 50 Then GoTo Blink ' no signal -> blink led
If Pulselen > 75 Then GoTo solid ' signal -> Solid led
GoTo ReadPWM

Blink:
For Init = 1 To 3 ' blink led 3 times
High GPIO.1
pause 200
Low GPIO.1
pause 200
Next
pause 1000
GoTo ReadPWM

Solid:
High GPIO.1
pause 3000
Low GPIO.1
pause 200
GoTo ReadPWM
END

fails compile saying

error 113 Symbol not previously defined (MLCR_OFF)

When I use _MCLRE_OFF instead it compiles with no error.

malc-c
- 25th August 2006, 23:51
Just a few comments from my basic understanding of RC



Pulselen var word ' Pulselen can be 0 - 255, 100 = 1 ms, 200 = 2 ms
Init var word ' Init used to flash LED
Clear ' set all variables = 0

Input GPIO.4 ' set pin 3 to RX signal - changed as GPIO3 is not ideal

ReadPWM:
PulsIn GPIO.4, 1,Pulselen ' pin 3 - read high pulse length, times out after .65535 seconds
pause 15
If Pulselen < 50 Then GoTo Blink ' no signal -> blink led
If Pulselen > 75 Then GoTo solid ' signal -> Solid led
GoTo ReadPWM



If you read the comments for pulselen it states that 100 = 1ms and 200 = 2ms, but your if / then statement states that if its less than 50 go blink. If you are monitoring a "normal" PPM signal from a receiver, the pulse length will be around 1ms for low stick and 2ms for full stick, with 1.5ms at centre. So even at low stick the signal will be 1ms or a pulse length of 100, it will never be lower than 50, and this the code will never jump to flash.

I too had to change the config line to MCLRE_OFF for it to program without error

EDIT:
Oh and one other thing that may be causing the problem is that the RX will be sending the PPM pulse every 20ms, so you will need to get the timing right so that the pin is checked for a pulse at the right time. According to code it times out after .65535 seconds, maybe you need to make this time out after 20ms so it loops back round. Most of my experiments with other languages used the pre-scaler to get the timer to roll over at the desired timing if a pulse wasn't present. Maybe some of the more experienced guys can jump in here as I don't know if PBP features this option ?

Archangel
- 26th August 2006, 00:47
'12F675
@ DEVICE pic12F675, INTRC_OSC_NOCLKOUT
' System Clock Options
@ DEVICE pic12F675, MCLR_OFF
' Master Clear Options (Internal)
@ DEVICE pic12F675, WDT_OFF
' Watchdog Timer
@ DEVICE pic12F675, CPD_OFF


DEFINE OSC 4
OPTION_REG.5 = 0 ' clock source internal
CMCON = 7
ANSEL=0

Pulselen var word ' Pulselen can be 0 - 255, 100 = 1 ms, 200 = 2 ms
Init var word ' Init used to flash LED
Clear ' set all variables = 0

Input GPIO.3 ' set pin 5 to RX signal

ReadPWM:
PulsIn GPIO.3, 1,Pulselen ' pin 4 - read high pulse length, times out after .65535 seconds
pause 15
If Pulselen < 50 Then GoTo Blink ' no signal -> blink led
If Pulselen > 75 Then GoTo solid ' signal -> Solid led
GoTo ReadPWM

Blink:
For Init = 1 To 3 ' blink led 3 times
High GPIO.1
pause 200
Low GPIO.1
pause 200
Next
pause 1000
GoTo ReadPWM

Solid:
High GPIO.1
pause 3000
Low GPIO.1
pause 200
GoTo ReadPWM
END

geckogrotto
- 26th August 2006, 01:55
ugg that doesn't compile either :(
A bunch of illegal opcode warnings and errors
207 and 122.

geckogrotto
- 26th August 2006, 02:00
This is what the author of the original code said to me.

That clock is much faster (meaning the 12F657) . The 12c508 has a 4Mhz clock. The return from Pulsin for the 12c508 is: 100 = 1.0 ms, 150 = 1.5 ms, 200 = 2.0 ms. You need to adjust the numbers to whatever your PIC returns in Pulsin. I have not looked at the data sheet. It will be there somewhere, or in the help data for your compiler.

Maybe this is the problem?

Archangel
- 26th August 2006, 06:49
It compiled without error on my PBP ver 2.47, I don't mean to be rude, Did you remember to select the appropriate device in the compiler (IDE) up at the top? I am confused are we talking about a 12F675, a 12f657, or a 12c508?
It shows 199 words compiled for 12F675, it will not compile for a 12c508 or 12f508 as is. As for if it will work or not, that's beyond the scope of my examination, and likely ability, but it does compile as is.

malc-c
- 26th August 2006, 07:43
The reason it fails to compile is due to the config lines beginning with the @ sign. Change it to the format

@ __CONFIG _INTRC_OSC_CLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF

and it compiles OK. This is due to using either PBP or MPSAM as the compiler.

geckogrotto
- 26th August 2006, 07:59
What Malc said.
whenever I use
@ DEVICE pic12F675, INTRC_OSC_NOCLKOUT
' System Clock Options
@ DEVICE pic12F675, MCLR_OFF
' Master Clear Options (Internal)
@ DEVICE pic12F675, WDT_OFF
' Watchdog Timer
@ DEVICE pic12F675, CPD_OFF

it errors all over the place.

@ __CONFIG _INTRC_OSC_CLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF
Works fine but HenrikOlsson said "I THINK it should be MCLR_OFF and not MCLRE_OFF. - try it."

I'm not even sure thats were the problem is or why having the @ device lines like that fails.
I have PBP 2.46 and MPLAB 7.41 MPSAM 5.04

I still have no idea if thats the actual problem im having where the RC signal is basically ignored.

Is anyone able to get this chip to actually work with RC signal with any settings?

geckogrotto
- 26th August 2006, 08:03
It compiled without error on my PBP ver 2.47, I don't mean to be rude, Did you remember to select the appropriate device in the compiler (IDE) up at the top? I am confused are we talking about a 12F675, a 12f657, or a 12c508?
It shows 199 words compiled for 12F675, it will not compile for a 12c508 or 12f508 as is. As for if it will work or not, that's beyond the scope of my examination, and likely ability, but it does compile as is.


Oh and yes I chose and am using 12F675

malc-c
- 26th August 2006, 08:10
I've used the 12F675 as an RC switch and it works fine. The only thing is it was programmed in Assembler, and JAL (which I lost the notes on due to a PC crash).

I'm sure its just a matter of getting the timing correct, so the PIC is able to detect and measure the pusle width of every pulse. As I mentioned above, the RX will be sending a pulse every 20ms, which is generally 1 - 2 ms in duration.

geckogrotto
- 26th August 2006, 08:19
Wouldn't holding the stick cause the signal to repeat and therefore catch it at some point?

I have tried and assumed that it would eventually see the signal but never seems to see anything.

I may have to go picaxe as I can't seem to dissect a data sheet.

malc-c
- 26th August 2006, 08:21
Well I just compiled the following and I get flashing LEDs (I haven't got the time this morning to hook up a receiver and test the reading of the PPM signal)



'12F675
@ __CONFIG _INTRC_OSC_CLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF


DEFINE OSC 4
OPTION_REG.5 = 0 ' clock source internal
CMCON = 7
ANSEL=0

Pulselen var word ' Pulselen can be 0 - 255, 100 = 1 ms, 200 = 2 ms
Init var word ' Init used to flash LED
Clear ' set all variables = 0

Input GPIO.4 ' set pin 5 to RX signal

ReadPWM:
PulsIn GPIO.4, 1,Pulselen ' pin 4 - read high pulse length, times out after .65535 seconds
pause 15
If Pulselen < 50 Then GoTo Blink ' no signal -> blink led
If Pulselen > 75 Then GoTo solid ' signal -> Solid led
GoTo ReadPWM

Blink:
For Init = 1 To 3 ' blink led 3 times
High GPIO.1
pause 200
Low GPIO.1
pause 200
Next
pause 1000
GoTo ReadPWM

Solid:
High GPIO.1
pause 3000
Low GPIO.1
pause 200
GoTo ReadPWM
END


The only change I made was to use GPIO.4 and not GPIO.3

EDIT:
OK so I couldn't go out without testing the code to see if it reads the PPM signal.. sorry to say it doesn't detect it :(

geckogrotto
- 26th August 2006, 08:39
Malc bud when you get back could you try it for me?

I get the blinking light as well that means it is seeing no signal. I can't seem to get it to go solid tho, well actually it does when it feels like it sometimes but not when im pressing the stick.

This has been the problem the whole time just blinks how it wants and seemingly ignores any signal no matter what changes I make to the config or anything.

geckogrotto
- 26th August 2006, 08:40
EDIT:
OK so I couldn't go out without testing the code to see if it reads the PPM signal.. sorry to say it doesn't detect it :(


Ahh posted edit right as I posted hehe. Yea thats the same thing I get :(

Acetronics2
- 26th August 2006, 10:15
Hi, Gecko

Some little gags here and there ... but try it !!!


'12F675
@ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _CP_OFF ' clock source internal
OPTION_REG.5 = 0

DEFINE PULSIN_MAX 2500
DEFINE OSCCAL_1K 1

CMCON = 7 'Tout digital pour 12F675
ADCON0 = 0 'ADC off "
ANSEL = 0
VRCON = 0



Pulselen var Byte ' Pulselen can be 0 - 255, 100 = 1 ms, 200 = 2 ms
Init var Byte ' Init used to flash LED
Compte var Byte

In var GPIO.3
Led var GPIO.1

Clear ' set all variables = 0

TRISIO = %001000

High Led

ReadPWM:

PulsIn In, 1,Pulselen ' pin 4 - read high pulse length, times out after .65535 seconds

If Pulselen < 80 OR Pulselen > 220 Then

Pause 15

Compte = Compte + 1

IF Compte // 16 THEN

Pulsout Led, 10000

ENDIF

Else

HIGH Led

ENDIF

GoTo ReadPWM

END

I'll do better next time ...

Alain

PS: ... breadboard verified .... Yess !!!

malc-c
- 26th August 2006, 10:16
Looking at the manual it states



If the pulse edge never happens
or the width of the pulse is too great to measure, Var is set to zero.


Maybe the the pulsein statement is reaching the 65535 counts before the end of the pulse width and thus is setting the value of pulselen to 0 which will always cause the code to jump to the flash led section.

malc-c
- 26th August 2006, 10:27
Alain,

Now we're getting somewhere... when the TX is powered the LED is constant, when the TX is off (ie no signal beeing transmitted) the LED falshes...

However when the TX is on and the channel that is connected has its state changed (ie gear switch on CH5) nothing happens. Now could this be down to the fact that its detecting a sync pulse and not the actual channel PPM pulse ?

malc-c
- 26th August 2006, 10:49
Well there are no sync pulses coming out of the rx, just a nice train of 1ms pulses at 20ms intervals

Acetronics2
- 26th August 2006, 10:52
Hi, Malc - the - bass - beat - filter - forum ...

I do not understant your question so well ...

When the transmitter is on ( and good link to Rx !!! ) the received channels durations are ALWAYS in the 800 - 2200 µS range ...

WHATEVER you do with your sticks and switches ...

THE ONLY exception is the LM1872 IC coder where pulses can " disappear" from the frame. But I didn't see such circuit in any of our R/C dedicated radios for, at least 30 years ...

I Think you're mixing memory switch and missing ( or wrong ) pulse detector ...

Nothing has to happend on a stick position change, here ...

Alain

malc-c
- 26th August 2006, 11:11
Don't talk to me about base beat filters :) !!

Let me explain the test rig. I,m using a simple GWS 8ch RX. Ch5 is a straight switched channel on the TX. Ch5 on the RX is connected to the input pin on the PIC. With power applied to the RX and the TX turned on with the switch in one position you get a nice train of 1ms pulses appearing on the PIC input. - There is no mixining involved.

I think whats happening here is we are getting confused over Dan's requirements are. He doesn't want to simply detect the presence of a signal, he needs to detect the change of state of that pulse from 1ms width to 2ms. So maybe the IF/THEN statement should read if pulselen <150 flash LED else LEDS is solid ??

Does pulsein measure the time between positive going pulses, or is it triggered by the positive going pulse, then measures the time in 10us chuncks until it goes negative ? The reason I ask is that if it measures from one positive going pulse to the next then the value will be 20ms (or 2000 counts) and will thus always invoke a "no signal" responce ??

Acetronics2
- 26th August 2006, 11:33
Heuuuuuu,

Who's that DAN ????

and what does he do in this thread ... may be your castle's ghost ???

gecko was looking for a signal/no signal indicator ...

Alain

malc-c
- 26th August 2006, 11:48
Gecko and Dan are one and the same :)

Acetronics2
- 26th August 2006, 12:51
soooooo,

If Pulselen < 50 Then GoTo Blink ' no signal -> blink led
If Pulselen > 50 Then GoTo Solid ' signal -> Solid led

that's all !!!

malc-c
- 26th August 2006, 12:58
Something is really screwed up !



ReadPWM:
pulsin GPIO.4,1,Pulselen ' pin 3 - read high pulse length
If Pulselen > 0 Then GoTo Blink ' on -> blink led
GoTo ReadPWM


I've tried various settings for "if pulselen >" and it makes no difference, the code just won't jump to the blink led section. I also tried the RCTIME as it mentions in the manual that RCTIME measures the time it takes for a pulse to change state from on to off.. even that didn't work.

Dan (Gecko) - maybe you can use your 16F628 as a test bead and hook up your LCD to display the value of pulselen for a 1ms signal, and 2ms signal and then use that to set the threshold ?

Acetronics2
- 26th August 2006, 13:10
Nothing surprising Malc ...

If Pulselen > 0 Then GoTo Blink ... means the only "Solid led" possibility is Tx ON, but no frame sent ( pupil switch ON, but
no pupil Tx )

Too Bad !!!

Now, ... understand you've received info from Gecko ... and we didn't !!! .... difficult to understand each others then !!!

malc-c
- 26th August 2006, 13:15
varified that the output from the RX is being received on input pin of the PIC and still no joy :(

Acetronics2
- 26th August 2006, 13:19
Malc,

An Update of this Thread would be welcome ...

We do not know any more what is looked for, and what Gecko really wants ...
or what YOU're looking for.

Thanks for Everyone

Alain

malc-c
- 26th August 2006, 13:20
Now, ... understand you've received info from Gecko ... and we didn't !!! .... difficult to understand each others then !!!

We've been communicating via e-mail when he started his pellet counter, and it was me that suggested he joined this forum... not received anything on this project so you are still in the loop !

geckogrotto
- 26th August 2006, 17:58
Im in US so was sleeping... Your in the loop you have missed nothing other than Malc knows my name lol.

I want to be able to get the RX signal and tell weather the stick is left or right or center and flash the LED acordingly. Thats it, nothing overly complicated. I just got up and will be checking the latest code. From what I read from Malcs response its still no go.

Ok to make it make more sense lets think of it as a blinker on a car.
Left on the stick will cause the 1 blinker to blink right on the stick will cause another to blink and center will cause none to blink.

I have been just trying to blink or solid a led but that may make more sense.

Malc I will try the LCD and see what I get. Wish I had a serial connect some how.

Jumper
- 26th August 2006, 18:33
Hi,

I am not a big fan of PULSIN. It is sometimes hard to know what is happening so I prefer setting up timers to measure things myself. There are several posts in this forum how to use timers and it is really not that hard. If you pick a 16 bit timer and set the prescaler to 1:1 you can measure up to 65 ms without overflowing at 4 Mhz and 2.55 ms with a 8-bit timer and getting a nice resolution. The RC signal shouldn't be hard to catch this way.

If you have tried everything else, Timers might be worth a try.



/me

geckogrotto
- 26th August 2006, 18:54
Ok im feeling really dumb but may have it worked out. Doing some tests

geckogrotto
- 26th August 2006, 19:29
Ok man I found the problem it was infinite resistance between the keyboard and chair :D

Its working now. Working on cleaning up the code and will post new code when i'm done... It wasn't my code that was the problem it was me lol

Acetronics2
- 26th August 2006, 19:59
Hi, Gremlin - lotto ( ! )

The blinking rate could help ( US, European ... ??? ) ... thanks.

Alain

geckogrotto
- 26th August 2006, 20:59
Ok so here it is Im stupid was the main problem. Somehow I figured that the signal would just come pouring out of that little white wire without any ground to the signal cable... Once I got over that blonde moment everything fell into place. The code below could be done a lot better and some saftys put in for wild RC signals but I wanted to get out some working code.
Basically by moving the stick back and forth it will figure out whats the max high and low and then accept anything thats about 1/3 of that in that direction. Now that its clear as mud here is the code :)


'12F675
@ __CONFIG _INTRC_OSC_CLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF
OPTION_REG.5 = 0 ' clock source internal

Pulselen var Byte ' Pulselen can be 0 - 255, 100 = 1 ms, 200 = 2 ms
Init var Byte ' Init used to flash LED
HighCode Var Byte
LowCode Var Byte
clear
highcode = 0 ' setup vars to be changed
lowcode = 200 ' setup vars to be changed
Input GPIO.3 ' set pin 5 to RX signal

ReadPWM:
PulsIn GPIO.3, 1,Pulselen ' pin 4 - read high pulse length, times out after .65535 seconds
If pulselen > highcode then Highcode = Pulselen ' make sure highcode is at full throw This will avoid having to go though a setup
If pulselen < LowCode and Pulselen > 50 then Lowcode = Pulselen ' make sure lowcode is at full throw This will avoid having to go though a setup
if (Pulselen + 20) >= highcode then
For Init = 1 To 6
High GPIO.1
pause 60
Low GPIO.1
pause 60
Next
pause 1000
endif
If (Pulselen - 20) <= LowCode Then
High GPIO.1
pause 3000
Low GPIO.1
pause 200
endif

GoTo ReadPWM

malc-c
- 26th August 2006, 21:29
Yup - works here too - using a JR X3810 transmitter and cheap and chearful GWS RX

We got there in the end ;)

malc-c
- 26th August 2006, 21:44
I've changed your code a bit to make a simple RC Switch that turns on GPIO.1 in real time (ie activate the switch on the TX and the LED lights or goes out)

I've also removed some parts that are not needed (input GPIO.3 for example as PULSIN automatically makes the pin an input)



'12F675
@ __CONFIG _INTRC_OSC_CLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF
OPTION_REG.5 = 0

Pulselen var Byte
HighCode Var Byte
LowCode Var Byte
clear
highcode = 0 ' setup vars to be changed
lowcode = 200 ' setup vars to be changed


ReadPWM:
PulsIn GPIO.3, 1,Pulselen
If pulselen > highcode then Highcode = Pulselen
If pulselen < LowCode and Pulselen > 50 then Lowcode = Pulselen
if (Pulselen + 20) >= highcode then High GPIO.1
If (Pulselen - 20) <= LowCode Then low GPIO.1
GoTo ReadPWM


Good result for the end of the day.

geckogrotto
- 27th August 2006, 00:10
There ya go thanks for the help didn't notice the 2nd page or would have replied sooner :)