|
|
View previous topic :: View next topic |
Author |
Message |
nabil
Joined: 17 Nov 2011 Posts: 4
|
Aurel module and remote control servomotor |
Posted: Thu Nov 17, 2011 4:34 am |
|
|
Dear sir,
I need your help to use Aurel module and PIC 16F4550 to remote control sevomotors. I have make the code below for transmitter:
Code: | #include "send.h"
#include <stdlib.h>
int16 ADC1,ADC2,ADC3,ADC4;
int i;
int16 ch1_result(int i)
{
set_adc_channel(i); //A0
delay_us(100);
return( read_adc());
}
void main()
{
setup_adc_ports( ALL_ANALOG );
setup_adc(ADC_CLOCK_DIV_32 );
while(true)
{
ADC1=ch1_result(0)*30/1023;
ADC2=ch1_result(1)*30/1023;
ADC3=ch1_result(2)*30/1023;
ADC4=ch1_result(3)*30/1023;
printf("$%Lu-%Lu-%Lu-%Lu\n\r",ADC1,ADC2,ADC3,ADC4);
}
{
delay_ms(10);
}
} |
and the code below for receiver :
Code: | #include "recev.h"
#include <stdlib.h>
int16 i;
int iposv0,iposv1,iposv2,iposv3;
int16 donneem1, donneem2,donneem3,donneem4;
char string[35],read[35], var1[5],var2[5],var3[5],var4[5];
int etat[32];
#define servo1 PIN_B0
#define servo2 PIN_B1
#define servo3 PIN_B2
#define servo4 PIN_B3
#INT_RDA
void main_rs232rx()
{
if(kbhit())
{
restart_wdt();
gets(read);
strcpy (string, read);
for (i=0;i<=strlen(string);++i)
if(string[i]=='$')
{
iposv0=i;
for (i=iposv0+1;i<=strlen(string);++i)
{
if(string[i]=='-')
{
iposv1=i;
for (i=iposv1+1;i<=strlen(string);++i)
if(string[i]=='-')
{
iposv2=i;
for (i=iposv2+1;i<=strlen(string);++i)
if(string[i]=='-')
{
iposv3=i;
}
}}}}
}
for (i=iposv0+1;i<=iposv1-1;i++)
var1[i-1]=string[i];
//printf("%s\r\n",var1);
for (i=iposv1+1;i<=iposv2-1;i++)
var2[i-iposv1-1]=string[i];
//printf("%s\r\n",var2);
for (i=iposv2+1;i<=iposv3-1;i++)//
var3[i-iposv2-1]=string[i];
//printf("%s\r\n",var3);
for (i=iposv3+1;i<=strlen(string);i++)//
var4[i-iposv3-1]=string[i];
//printf("%s\r\n",var5);
donneem1=atol(var1);
donneem2=atol(var2);
donneem3=atol(var3);
donneem4=atol(var4);
for (i=0;i<=3;i++)
{
var1[i]=' ';
var2[i]=' ';
var3[i]=' ';
var4[i]=' ';
}
}
#INT_TIMER1
void pulse_freq()
{
set_timer1(40536); // So that the interrupt will overflow after every 20 ms
//output_high(PIN_A6);
etat[30]=1;
}
void main ()
{
int temps_ecoule=0;
setup_timer_1(T1_INTERNAL|T1_DIV_BY_4);
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
while (TRUE)
{
if (etat[30] == 1)
{
etat[30] =0;
output_high(servo1); etat[0]= 1;
output_high(servo2); etat[1]= 1;
output_high(servo3); etat[2]= 1;
output_high(servo4); etat[3]= 1;
delay_us(800);
for (temps_ecoule = 0; temps_ecoule < 30; temps_ecoule++)
{
if (etat[0] == 1 && temps_ecoule >= donneem1) { output_low(servo1); etat[0] = 0; }
if (etat[1] == 1 && temps_ecoule >= donneem2) { output_low(servo2); etat[1] = 0; }
if (etat[2] == 1 && temps_ecoule >= donneem3) { output_low(servo3); etat[2] = 0; }
if (etat[3] == 1 && temps_ecoule >= donneem4) { output_low(servo4); etat[3] = 0; }
delay_us(45);
}
}
}
} |
but it can work well.
Can your help me and look at the code to fix the problem.
best regards. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Thu Nov 17, 2011 4:49 am |
|
|
Start again.....
Without even looking far:
1) What is the value printed going to be for the ADC's?. You don't show us your #device ADC= line, so the default will be a number from 0 to 255. *30/1023 will then give a value from 0 to 7. If you have got 10 bit mode selected, still only going to give a value from 0 to 30.
2) Understand that the RDA interrupt means just _one_ character is waiting to be received. Don't use gets in an RDA interrupt, will stop everything else from running.
3) How does atol, expect a string to be terminated?. Are you terminating your strings this way?.
You need to step back, and change the whole approach. Look at ex_SISR.c for how to handle the RDA interrupt, then put the 'decoder' code in the main, when the end of line is received. Then terminate your strings correctly, for atol, and get the value you send to a reasonable number.....
Best Wishes |
|
|
nabil
Joined: 17 Nov 2011 Posts: 4
|
|
Posted: Thu Nov 17, 2011 6:32 am |
|
|
Dear Sir,
Thanks for your reply and help.
It will not be easy for me to develop as I just start with PIC and PCW.
At the first time your recommandation is about Transmitter code or receiver code or both ?
About atol, looking at the library STDLIB.h, i find atol fonction:
Quote: | signed long atol(char *s);
/* Standard template: float32 strtol(char * s,char *endptr)
* converts the initial portion of the string s to a float32
* returns the converted value if any, 0 otherwise
* the final string is returned in the endptr, if endptr is not null |
About RDA interrupt, how can i do?
Thanks for your help
Last edited by nabil on Thu Nov 17, 2011 11:45 pm; edited 1 time in total |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Thu Nov 17, 2011 9:26 am |
|
|
Hi Nabil,
I recommend that you delete the link to the file shown in your last post as you are violating the forum rules by posting CCS example code. Besides, Ttelmah gave you the name of the file, and if you own the compiler, you've got it. Why would you think that is not the correct file??
John |
|
|
nabil
Joined: 17 Nov 2011 Posts: 4
|
|
Posted: Thu Nov 17, 2011 11:59 pm |
|
|
Dear Sir,
Can I use the functions in servos.c file in drivers folder to control servomotors with PWM ?
Best Regards. |
|
|
nabil
Joined: 17 Nov 2011 Posts: 4
|
|
Posted: Tue Nov 22, 2011 7:18 am |
|
|
Dear Sir;
About ADC, please find below the used code:
Code: | #include <18F4550.h>
#device adc=10
#FUSES HS, WDT256, NOPROTECT, NOLVP
#use delay(clock=12000000)
#use rs232(baud=2400,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8) |
About gets and RDA interrupt, how can we change it ? Must I use getc() ?
Please help me |
|
|
|
|
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
|