|
|
View previous topic :: View next topic |
Author |
Message |
sergio3214
Joined: 15 Mar 2017 Posts: 25 Location: brasil
|
|
Posted: Fri Mar 31, 2017 11:05 am |
|
|
Friend what I want is to do the radio control for the electric cart that I build.
I want to make the servo motor control go right, center, and left
and an acceleration ramp for the traction motor.
And a button to reverse the motor direction forward or backward. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9244 Location: Greensville,Ontario
|
|
Posted: Sat Apr 01, 2017 5:31 am |
|
|
As I said before, you can do ALL that without the RF modules. Build/code/test it all on the bench. Just 'simulate' the RF modules with 3 short wires to connect the PICs together.
I'd order 5 of the HC-12 modules and while waiting for them to arrive,code and test your program. That way you'll have working code when the HC12s come.
Then, simply add the HC12s (5 wires each) and your project is done.
Jay |
|
|
sergio3214
Joined: 15 Mar 2017 Posts: 25 Location: brasil
|
|
Posted: Sat Apr 01, 2017 1:04 pm |
|
|
"temtronic"Only you did not understand what I can not do in my code.
I want to control the bike but I can not do it, you could give me an example code so that it would be a guide for me, because until now I can only turn on the LED and the conversion, but I could not control the engine.
Code: |
#include <12f675.h>
#DEVICE ADC=8
#fuses INTRC_IO,NOWDT,NOPROTECT, NOMCLR ,BROWNOUT, PUT
#use delay(clock=4000000) // 4 MHz crystal on PCB
//#use rs232(baud=19200, xmit=PIN_A0, rcv=PIN_A1) // you can use any pins for software uart..
#use rs232(baud=9600, xmit=PIN_A4, rcv=PIN_A2,STREAM=Wireless )
// characters transmitted faster than the pic eats them will cause UART to hang.
#include <stdlib.h>
#define ADC_0 PIN_A0
#define RS232_TX_PIN PIN_A4
#define RS232_RX_PIN PIN_A2
#define VALOR_v PIN_A5
int8 c;
int8 valor_alto;
int8 valor_baixo;
void main()
{
setup_adc_ports(sAN0);
setup_adc(ADC_CLOCK_DIV_8);
set_adc_channel(0);
delay_us(20);
while(1)
{
{
valor_alto = (read_adc()>>8);
fprintf(Wireless,"%c", valor_alto);
delay_ms(100);
valor_baixo = (int)(read_adc());
fprintf(Wireless,"%c", valor_baixo);
}
}
}
|
Receiver:
Code: |
#include <12f675.h>
#fuses INTRC_IO,NOWDT,NOPROTECT, NOMCLR ,BROWNOUT, PUT
#use delay(clock=4000000) // 4 MHz crystal on PCB
//#use rs232(baud=19200, xmit=PIN_A0, rcv=PIN_A1) // you can use any pins for software uart..
#use rs232(baud=9600, xmit=PIN_A4, rcv=PIN_A2,Errors,STREAM=Wireless )
// characters transmitted faster than the pic eats them will cause UART to hang.
#include <stdlib.h>
#define LED_0 PIN_A0
#define RS232_TX_PIN PIN_A4
#define RS232_RX_PIN PIN_A2
#define LED_5 PIN_A5
int8 n;
int8 vetor[3];
int16 resultado;
int1 data;
int8 termo_1;
int8 termo_2;
int16 resultado_1;
void RDA_isr(void)
{
vetor[n] = getc();
if (n == 1);{output_low(LED_5);}
{
data = 1;
n = 0;
}
n++;
}
void main()
{
enable_interrupts(global);
while(TRUE)
{
if (data == 1); {
}
{
resultado = (((int16)vetor[0]<<8) | vetor[1]);
fprintf (Wireless, "\n\r%lu", resultado);
data++;
}
}
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19543
|
|
Posted: Sat Apr 01, 2017 1:45 pm |
|
|
Temtronic does understand. You need to actually start to write for yourself, and he is telling you how to start this more easily.
You need first of all to layout what your task needs to do. Probably send a number of 'messages' for different parts of the job.
So perhaps a 'button' message, and a 'steer' message.
Then you need to design the parts of these messages. Perhaps 'Bxc\n, where 'B' is the letter 'B', 'x' is a code for what button has operated, 'c' is a checksum, and `\n', is a line feed. Then perhaps 'Snnnc\n', again where 'S' is the letter 'S', 'nnn' is a number giving the steering position, 'c' is a checksum, and then again a line feed. Then your receive code, can look for the 'key' letters to know what to do with what follows, and when it sees the line feed, test the checksum, and if it is OK, then use the number to either say a button (or more than one button) has been pressed, or to set the steering to a position. You need to have a logic and design to your messages, so you can tell one from another, and verify that the message is correct before doing anything. The design needs to be yours, and suit what you need the messages to do.
Now, nobody is going to design this for you. Examples. There are hundreds of examples, both with the compiler, and here on the forum (and also some of the code library).
You need though also to understand the limitations. This is why I said to get a chip with a UART. Problem is that if your receive chip is going to do any sort of job 'with' the messages, if it doesn't have a UART, it will have to stop receiving to do the job. The UART helps to make it possible to actually do something with a received message, while the chip is receiving further messages.
Now, some comments on what you post:
1) The 12F675, doesn't have a UART. So it doesn't have an INT_RDA. This is what makes receiving hard on such chips....
2) Then assuming you switched to a chip with a UART, 'think microprocessor'. You are returning the data back to the sending chip (why?), using printf. The format you send back will 'print' the data as ASCII (so text a human can read). However much easier for a processor to receive, if you sent as (perhaps ) hex (then one character is four 'bits' of the number, without having to fiddle around. Also much easier if the data is sent is a 'fixed' width. So your 16bit value can be sent using the format %04X\n, and will then always be four characters wide, followed by a line feed. Makes receiving this _much_ easier and quicker to decode.
3) You have the comment:
"// characters transmitted faster than the pic eats them will cause UART to hang."
This is fundamentally wrong. The PIC will only transmit characters when it can. Won't hang. What will cause the UART to hang, is if characters are _received_ faster than they are read by the chip. Add 'ERRORS' to the UART setup (if you were on a chip with a UART....), to automatically prevent this.
Since neither chip you are using _has_ a UART, this doesn't apply anyway. All you will get if you miss a character is garbage.
Honestly if you are actually going to do anything with this data, you need a PIC with more pins. The 675, has 8pins. Two used by the power, two by the crystal. Using two more for TX/RX, only leaves two pins. Assuming you want to do anything at all with the data something larger is needed....
A PIC like the 16F1704, gives you 14 pins, and a UART. There are any number of more capable chips, but this is at least able to do what you are asking.
If you must use 8pin then the 12F1840, could be used without a crystal, and probably do what you are asking.
The second big reason nobody is answering your 'give me code' call, is that it is not really practical on your chip.... |
|
|
sergio3214
Joined: 15 Mar 2017 Posts: 25 Location: brasil
|
|
Posted: Thu Apr 06, 2017 7:09 am |
|
|
Good morning, Ttelmah.
Still can not develop the project, and my great difficulty is in the knowledge after all knowledge that I have acquired and through the internet, put this I could not leave the light and erase an LED through the communication, so ask for a sample code to follow as Base, I would like but a little of your help I know you can this me helping. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9244 Location: Greensville,Ontario
|
|
Posted: Thu Apr 06, 2017 9:12 am |
|
|
As Mr. T points out, you've only got 2 available pins for 'control'.
1 could be the steering 'servo' assuming it's an RC model servo,
the other could be the 'traction motor', and IF I assume it's also an RC model style 'servo' motor, from a hardware perspective a 'possible' project.
The HUGE problem is software. Now it does have 1KW of program space so with very,very clever and careful coding you 'might' get it to work. The MAJOR probem is the communications functions. As pointed out, without a hardware UART a lot of code and time will be spent there.
As much as I love a challenge, I do not have the time to cut code for this.
However I would start by placing cart on blocks..
1) cutting code for the steering servo. RC servos generally respond like 1ms=left, 1.5ms=center, 2ms=right. So cut code to do that.
2) cut code for the traction motor. Without any specs of that, I'll assume an RC servo motor so 1ms=off, 1.5ms=1/2 speed, 2ms=full speed. Cut code and see what happens.
These tests can be in a loop that automatically does one 'command', pause for 2 seconds, then the next 'command'. Once you've confirmed it works, then step 3..
3) comunications. Design a 'protocol' to send data to the cart. Choose soemthing very simple, say 'L' for left, 'C' for center, 'R' for right, 'S' for stop, 'G' for go. Speed is difficult but you could use '1,2,3,4' as preset speeds, similar to gear shift selection. The key point is it needs to be simple as you don't have a lot of memory.
Once this all works on the bench, hardwired to a PC, then install the RF modems. If you use the HC-12 style, it'll work fine.
Jay |
|
|
sergio3214
Joined: 15 Mar 2017 Posts: 25 Location: brasil
|
comunicação ADC |
Posted: Thu Apr 06, 2017 11:34 am |
|
|
Ttelmah
I changed the microcontroller to pic18f252, that and my start code would like you to continue me helping to get control the motor
This is the transmitter code
Code: | #include <18F252.h>
#device adc=10
#fuses HS, NOWDT, NOLVP, NOPROTECT
//Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8, errors)
void main()
{
int16 adc_value;
float volts;
setup_adc_ports(AN0);
// setup_timer_3(T3_DISABLED | T3_DIV_BY_1);
//setup_comparator(NC_NC_NC_NC);
// setup_adc(ADC_CLOCK_INTERNAL);
setup_adc(ADC_CLOCK_DIV_8);
while(TRUE)
{
set_adc_channel(0);
delay_us(50);
adc_value = read_adc();
volts = (float)(adc_value*2.0*3.0)/0x3FF;
printf( "%3.2f\r\n", volts);
// set_adc_channel(1); // just for test purposes
delay_ms(50);
}
}
|
Now I intend to make the receiver. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Apr 06, 2017 5:29 pm |
|
|
What temtronic and Ttelmah don't understand is that he can't write code.
He has said repeatedly "but I can not do it".
temtronic, he can't take your well-written description and turn it into code,
because he can't write code. |
|
|
sergio3214
Joined: 15 Mar 2017 Posts: 25 Location: brasil
|
adc |
Posted: Thu Apr 06, 2017 7:21 pm |
|
|
PCM programmer wrote: | What temtronic and Ttelmah don't understand is that he can't write code.
He has said repeatedly "but I can not do it".
temtronic, he can't take your well-written description and turn it into code,
because he can't write code. |
Do not understand what you said. |
|
|
gjs_rsdi
Joined: 06 Feb 2006 Posts: 468 Location: Bali
|
|
Posted: Sat Apr 08, 2017 11:11 pm |
|
|
Hi Sergio
You have written:
Quote: | Friend what I want is to do the radio control for the electric cart that I build.
I want to make the servo motor control go right, center, and left
and an acceleration ramp for the traction motor.
And a button to reverse the motor direction forward or backward. |
You got good detailed advice, but you are not answering to some basic question.
To start with, before any code:
1. Are you using a model aircraft/car servo-actuator for straight/left/right?
2. Are you using the traction motor with a model aircraft/car speed controller to increase/decrease the speed?
What PCM programmer said (because you are repeating "but I can not do it") that you can't write the code.
Can you write a code? You ever wrote code in CCS compiler before this project?
Best wishes
Joe |
|
|
sergio3214
Joined: 15 Mar 2017 Posts: 25 Location: brasil
|
comunicação ADC |
Posted: Sun Apr 09, 2017 4:48 pm |
|
|
Good night!
I already wrote yes using MPLAB delivered ccs compile.
I have also done communication between two PICs, to turn on an LED.
What I want to control is a traction motor and a servomotor for a model
auto that I'm finishing.
This code was also the one I wrote.
Code: |
#include <18F252.h>
#device adc=10
#fuses HS, NOWDT, NOLVP, NOPROTECT
//Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8, errors)
void main()
{
int16 adc_value;
float volts;
setup_adc_ports(AN0);
// setup_timer_3(T3_DISABLED | T3_DIV_BY_1);
//setup_comparator(NC_NC_NC_NC);
// setup_adc(ADC_CLOCK_INTERNAL);
setup_adc(ADC_CLOCK_DIV_8);
while(TRUE)
{
set_adc_channel(0);
delay_us(50);
adc_value = read_adc();
volts = (float)(adc_value*2.0*3.0)/0x3FF;
printf( "%3.2f\r\n", volts);
// set_adc_channel(1); // just for test purposes
delay_ms(50);
}
} |
|
|
|
gjs_rsdi
Joined: 06 Feb 2006 Posts: 468 Location: Bali
|
|
Posted: Mon Apr 10, 2017 7:45 pm |
|
|
Hi Sergio
I hope you know that RCM servo actuators will work by pulse width modulation.
In general, you have to provide a pulse of 1000us for one side, 1500us for the middle and 2000us for the other side. The pulse may vary slightly per the manufacturer. The period is 20ms.
For the "traction motor" you need a electronic speed controller that works with pulses same as the servo. You have to buy a speed controller that fits the specific motor.
You should start writing code for the above PWM.
Regarding the program you posted, why you are using floats? It takes a lot of time to make them on those microcontrollers.
You can start from reading a potentiometer in 8 bit resolution as the command for the servo and generate the pulse accordingly.
Also working at 4MHz is giving you a 1us program cycle so your resolution for the pulse will be not so good. If working at 32MHz one PC=0.125us, 8 times faster.
By the way, PIC18F252 is not recommended by Microchip for new designs.
Best wishes
Joe |
|
|
sergio3214
Joined: 15 Mar 2017 Posts: 25 Location: brasil
|
comunicação adc |
Posted: Tue Apr 11, 2017 2:23 pm |
|
|
Good afternoon my friend enlarges balcony and I do not have enough skills and knowledge to write the code, all my knowledge and the base of the internet and the forum, so far only be able to make simple codes, with LED lighting, serial communication, unfortunately my knowledge Still and little for if you can only write the code in that size.
I thank you for the guidance and will be trying other code, until you reach this level of development. |
|
|
gjs_rsdi
Joined: 06 Feb 2006 Posts: 468 Location: Bali
|
|
Posted: Tue Apr 11, 2017 11:32 pm |
|
|
PCM Programmer is 100% right
Quote: | What temtronic and Ttelmah don't understand is that he can't write code.
He has said repeatedly "but I can not do it".
temtronic, he can't take your well-written description and turn it into code,
because he can't write code. |
Best wishes
Joe |
|
|
sergio3214
Joined: 15 Mar 2017 Posts: 25 Location: brasil
|
adc via rf |
Posted: Sun Apr 16, 2017 5:17 pm |
|
|
GOOD NIGHT !!
Based on some research I wrote this code, it works but I can not send the data through the serial port, someone who can help me, and what I'm doing wrong Code: |
#include<16f628a.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES INTRC_IO //Clock <=4Mhz
#FUSES PUT //Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#use delay(clock=4000000) // clock internal
#use rs232(baud=9600, xmit=PIN_B2,rcv=PIN_B1)
#define RF_OUTPUT PIN_B1 // O receptor RF é conectado
#define RF_INPUT PIN_B2 // O Transmissor RF é conectado
#define botao_incremento PIN_B0
#define botao_decremento PIN_B4
#define botao_start PIN_A3
#define led_ligado PIN_A2
int cont,ligado,tempo;
void main()
{
setup_ccp1(CCP_OFF);
setup_timer_2(T2_DIV_BY_4,249,1);
setup_vref(FALSE);
ligado=0;
cont=0;
tempo=10;
while (true)
{
// botão start
if ((input(botao_start)) && (ligado==0))
{
output_high(led_ligado);
ligado=1;
while (input(botao_start)){};
delay_ms(tempo);
}
if ((input(botao_start)) && (ligado==1))
{
output_low(led_ligado);
ligado=0;
cont=0;
while (input(botao_start)){};
delay_ms(tempo);
}
// botão incremento
if ((input(botao_incremento)) && (ligado==1))
{
if ((cont>=0) && (cont<=249))
{
cont++;
}
if (cont>=249)
{
cont=249;
}
delay_ms(tempo);
}
// botão decremento
if ((input(botao_decremento) && (ligado==1)))
{
if ((cont>=1) && (cont<=249))
{
cont--;
}
if (cont<=0)
{
cont=0;
}
delay_ms(tempo);
}
// Controle do PWM
if (ligado==1)
{
set_pwm1_duty(cont);
}
if (ligado==0)
{
set_pwm1_duty(0);
}
}
} |
|
|
|
|
|
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
|
Powered by phpBB © 2001, 2005 phpBB Group
|