PDA

View Full Version : Newbie remote control question?



Clodoaldo
- 14th July 2009, 00:49
Dear friends I have a remote control transmitter / receiver and I trying to convert the signal (1ms - 2ms) to a binary value and store in a variable eight-bit (BYTE) (0 to 255).:eek:

When using a potentiometer and converts a voltage is easy ... However I can not understand how to convert the pulse width!?

I need to multiply or divide a value to having the other binary value?? And store in variable?!?:confused:

I will use this value to control the speed of a small motor connected in (PORTC.2)... And the pulse is read in (PORTB.0)... PIC16F877 is used.

Look at the code I'm trying to use:


DEFINE OSC 10
DEFINE CCP1_REG PORTC ' Hpwm 1 pin port pin 17 rc2 ccp1
DEFINE CCP1_BIT 2 ' Hpwm 1 pin bit
ADCON1 = 2 ' Config. all porta as analog and porte as digital

TRISA = %11111111 ' Set all porta as input (analogs)
TRISB = %11111111 ' Set all portb as input
TRISC = %00000011 ' Set all portc as output except RC0 and RC1
TRISD = %00000000 ' Set all portd as output
TRISE = %00000000 ' Set all porte as output

rx_sp VAR PORTB.0 ‘ Pin port connected RX remote control
speed VAR BYTE ‘ My var to storage the pulse value ????????????

led1 VAR PORTD.0 ‘ Pin’s portD driving LED’s no used now:).
led2 VAR PORTD.1
led3 VAR PORTD.2
led4 VAR PORTD.3
my_pwm VAR BYTE ‘ Used to update value of (HPWM) ‘command…

start:
PULSIN rx_sp,1,speed
My_pwm = speed
HPWM 1,my_pwm,1000
GOTO start
END

Thanks for the help.

aratti
- 14th July 2009, 07:05
Clodoaldo, you cannot transfer the pulsin value to the duty cycle without some math manipulation.

Let's assume you want keep the velocity constant.

You measure the pulse width using the pulsin function (use a word variable)

You will have your motor running with duty cycle = 50%

Now the millisecs returned in the word variable is the speed of your motor and you have to decide which value rappresent the speed that you want to keep put this value into the variable Your_Speed.

Once you have done that, then the math could be as follow:


Read_Speed Var Word
Your_Speed Var Word
Deviation Var Word
Duty var Byte
Error_Flag Var Bit

Duty = 50

Your_Speed = ?????

Calculation:

Deviation = Read_Speed * 100 / Your_speed

Error_Flag = 0

If Deviation > 100 then Duty=Duty-(Deviation-100)

If Deviation <100 the Duty = Duty +(100-Deviation)

If Duty > 100 the Error_Flag = 1

Return


Check Error_Flag before passing the new duty value to the PWM comand.
A Pause value, to give the motor the time to gain the new speed is recommended.


Al.

aratti
- 14th July 2009, 08:45
Let's assume you want to controll the velocity and you have no feedback from the motor.

You will give the word variable Max_Speed an arbitrary value that you will never exceed (this will reppresent duty = 100%).

You measure the pulse width using the pulsin function (using a word variable)

Once you have done that, then the math could be as follow:




Read_Speed Var Word ' value that you will send via RC
Max_Speed Var Word ' arbitrary value = to 100 Duty cycle
Duty var Byte
Error_Flag Var Bit



Max_Speed = ????? ' you enter here your arbitrary value

Calculation:

Error_Flag = 0

Duty = (Read_Speed * 100 / Max_speed)

IF Duty > 100 Then Duty = 100 : Error_Flag = 1

Return

If Error_Flag = 1 then you have sent a value greater than the permissible one.

You can add a line saying that if Read_Speed is minor then X duty = 0

(If Read_Speed < xxx then Duty = 0) in case Read_Speed could never be zero.

Remember that HPWM duty cycle 100% = 255.

Al.

Ioannis
- 14th July 2009, 08:48
If you loose your RF signal, what should your motor do?

Please note that frequency in HPWM is a word sized variable, so it must in Pulsin also.

Ioannis

Clodoaldo
- 14th July 2009, 17:20
See the speed control does NOT return automatically. It is a simple equipment so if I fail to reset the speed the engine should not stop...

Now I did NOT know that the variable for (HPWM) is (WORD) :D ... I try to use the examples of ARATTI and change the code to new test...

For now thanks for the support. Once back with results.