|
|
View previous topic :: View next topic |
Author |
Message |
filjoa
Joined: 04 May 2008 Posts: 260
|
get string from RS232 |
Posted: Sun Oct 03, 2010 6:54 pm |
|
|
Hi
I use 241024", 1ยบ 4bits is to control a servo and last 4bits is to control on my program function "get_long()" and then I give this "10her...
For last 4 bits I think this solution:
Quote: |
aux= get_long();
sp = 1000 + (aux >> 4);
|
and my result is 2024 write?
But now to make same operation but with first 4 bits how I can make? Someone have an idea?
best regards
Last edited by filjoa on Tue Oct 05, 2010 6:08 am; edited 1 time in total |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Mon Oct 04, 2010 6:26 am |
|
|
It depends on how sp and aux are declared.
Give us a small compilable program so we can see what we are working with. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
filjoa
Joined: 04 May 2008 Posts: 260
|
|
Posted: Mon Oct 04, 2010 12:55 pm |
|
|
hi
I have problems and I thinks it's because aux, sp and dr is an LONG (16bits).
So, now on hyperterminal I write some like this "s1024d1024\n". I need identify this two numbers (my range is s0000d0000 to s1024d1024) and put this on my variable "sp" and "dr".
Code: |
char string[11], sp[4], dr[4];
int8 i;
while(1)
{
gets(string);
for(i=0;i<=3;i++)
{
sp[i]=string[i+1];
}
for(i=0;i<=3;i++)
{
dr[i]=string[i+5];
}
SPEED = atol(sp);
STEERING = atol(dr);
}
|
Is correct I have my code like this?
best regards |
|
|
filjoa
Joined: 04 May 2008 Posts: 260
|
|
Posted: Tue Oct 05, 2010 6:08 am |
|
|
hi
when I use "gets(string)" function my PIC stop work, why?
I include stdlib.h and active INT_RDA...
I forget anything?
PS: my PIC is an 16F628A at 20Mhz.
best regards |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19476
|
|
Posted: Tue Oct 05, 2010 12:25 pm |
|
|
You can't use 'gets', with interrupt driven RS232.
gets, expects to sit and wait for an entire string. If you put it inside an interrupt receive routine, you 'throw away' the point of having interrupt driven receive, locking the program till the string arrives. Yetch.
Put it outside the interrupt, and the PIC will hang forever, since characters will never become available to this routine....
The solution, is to add a single flag to the interrupt receive routine. Have this global, and set it when a carriage return is seen. Then in your main code do your other tasks, _till this flag is set_. Once it is, clear it, and use the bgetc routine, to get the available characters, and assemble the string.
Best Wishes |
|
|
filjoa
Joined: 04 May 2008 Posts: 260
|
|
Posted: Wed Oct 06, 2010 9:17 am |
|
|
Hi
I understand idea and I try make it, but result don't the best....
My program:
Code: |
#include <18F252.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,BROWNOUT
#use delay(clock=20000000)
#use rs232(baud=115200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#build(reset=0x200)
#build(interrupt=0x208)
#org 0x0000,0x01ff
void bootloader() {
#asm
nop
#endasm
} // Reserve space for the bootloader
#include <stdlib.h>
// ### VARIAVEIS GLOBAIS ###
int16 SPEED = 1600;
int16 STEERING = 1400;
int1 data_available = FALSE;
char c;
// ### READ RS232 ###
#int_rda
void rda_isr(void)
{
c = getc();
data_available = TRUE;
}
void TIMER1_isr(void)
{
output_high(PIN_B7);
delay_us(SPEED);
output_low(PIN_B7);
}
#int_TIMER2
void TIMER2isr(void)
{
output_high(PIN_B6);
delay_us(STEERING);
output_low(PIN_B6);
}
void read_str()
{
int8 i=0;
char string[11], sp[4], dr[4];
while (c != '#');
data_available = FALSE;
do {
while (data_available == FALSE);
data_available = FALSE;
string[i]=c;
i++;
} while (c != 13);
printf("string: %s\n\r",string);
for (i=0; i<=3; i++)
{
sp[i]=string[i+1];
}
for (i=0;i<=3;i++)
{
dr[i]=string[i+6];
}
printf("speed_t: %s\n\r", sp);
printf("steerin_t_b: %s\n\r",dr);
SPEED = atol(sp);
STEERING = atol(dr);
printf("speed_b: %lu\n\r", SPEED);
printf("steering_b: %lu\n\r",STEERING);
SPEED=1100 + SPEED; // gama de 1200 - 1600 - 2000
STEERING=900 + STEERING; // gama de 1000 - 1400 - 2000
printf("speed: %lu\n\r", SPEED);
printf("steering: %lu\n\r",STEERING);
}
void main()
{
enable_interrupts(GLOBAL);
enable_interrupts(INT_RDA);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);
enable_interrupts(INT_TIMER1);
setup_timer_2(T2_DIV_BY_16,243,16);
enable_interrupts(INT_TIMER2);
while(1)
{
read_str();
}
}
|
When I put on hyperminal this "#s1000d1000\n" the return is:
Quote: |
1000100061000d1000
speed_t: 100010006
steerin_t_b: 10006
speed_b: 39528
steering_b: 10002
speed: 40628
steering: 10902
|
Someone know why?
best regards |
|
|
|
|
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
|