|
|
View previous topic :: View next topic |
Author |
Message |
Markdem
Joined: 24 Jun 2005 Posts: 206
|
gets()problem. |
Posted: Sun Feb 12, 2006 2:56 am |
|
|
Hi All, i am tring to get some pic to pic comms going using serial. I cant use the hardware uart as i am already using it for PC to PIC comms. Problem is that the when i use the following code, the PIC will never detect a char 13 (return) so it never prints out the output. Can any one see why?? If i use hardware, this works OK.
Code: |
#include <16f877a.h>
#device adc=8
#fuses NOWDT,HS, NOPROTECT, BROWNOUT, NOPUT, NODEBUG, NOLVP
#use delay(clock=20000000)
#use i2c(MASTER, SDA=PIN_C4, SCL=PIN_C3)
#use rs232(baud=2400,parity=N,bits=8,xmit=PIN_C0,rcv=PIN_C1)
#include <STDLIB.H>
char data[5];
void main()
{
while(1) //start of main loop
{
gets(data);
printf("recived = %u\r\n",data);
}
}
|
However,i i try doing this, it works fine
Code: |
#include <16f877a.h>
#device adc=8
#fuses NOWDT,HS, NOPROTECT, BROWNOUT, NOPUT, NODEBUG, NOLVP
#use delay(clock=20000000)
#use i2c(MASTER, SDA=PIN_C4, SCL=PIN_C3)
#use rs232(baud=2400,parity=N,bits=8,xmit=PIN_C0,rcv=PIN_C1)
#include <STDLIB.H>
int data;
void main()
{
while(1) //start of main loop
{
data = getc();
printf("recived = %u\r\n",data);
}
}
|
Thanks all for the help
Mark |
|
|
gjs_rsdi
Joined: 06 Feb 2006 Posts: 468 Location: Bali
|
Interrupt flags clear |
Posted: Sun Feb 12, 2006 3:20 am |
|
|
Hi all
How can I clear an interrupt flag ?
I don't find nothing in the help or in 16F876A header file
I am new with C, long years working with PIC assembler.
Have some simple flag clear in C ?
Joseph Gross |
|
|
Markdem
Joined: 24 Jun 2005 Posts: 206
|
|
Posted: Sun Feb 12, 2006 3:25 am |
|
|
Hi gjs_rsdi, i think you may want to start a new thred to find a answer to that question
Mark |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Feb 12, 2006 10:08 am |
|
|
How long is the string that you are receiving ? You have a very small
buffer. There's little margin for error.
You need a buffer that has room for:
1. Your message.
2. The CR at the end of the message.
3. Finally, you need room for the 0x00 byte, which marks the
end of the string.
Also, if your incoming message happens to be larger than the intended
length (for any reason), you will blow past the end of the buffer and
trash some of your other variables. I suggest using a larger buffer,
just for safety reasons.
-------
I'd like to add that using gets() is very unsafe. If you miss the <CR>
character, you likely blow past the end of the buffer and clobber all
your variables. At at minimum, your program will behave erratically.
It will likely crash. CCS has a get_string() function in INPUT.C,
which is in this folder: c:\Program Files\Picc\Drivers
It has a parameter which sets the maximum number of characters
to accept. It won't let you blow past the end of the buffer. It also
has a backspace feature in the code. You don't really want that
feature for a program that's taking in messages. You should edit that
part out of it. |
|
|
Markdem
Joined: 24 Jun 2005 Posts: 206
|
|
Posted: Mon Feb 13, 2006 2:44 am |
|
|
Thanks for that PCM programmer. I am not very good at C programing, so the next question i ask my sound a bit dumb :(. All i need to transfer is 1 int8 and it will always be positive. I dont realy want to transfer a string because i need to convert it to a int later anyway. Is there maybe a easer way of what i am doing here??. If not, i will just make a buffer to buff the incoming string.
Thanks, Mark |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Feb 13, 2006 2:56 am |
|
|
Quote: | Problem is that the when i use the following code, the PIC will
never detect a char 13.
All i need to transfer is 1 int8 and it will always be positive |
If all you need to do is to send one 8-bit integer value, then using
getc() to receive it is fine. Example: If one PIC sends 0x55 to
the other PIC, that's only one byte, and getc() will get it. |
|
|
Markdem
Joined: 24 Jun 2005 Posts: 206
|
|
Posted: Wed Feb 15, 2006 3:05 am |
|
|
Hi All, i feel so stupid asking this. I cant belive how much i am written in this project without knowing this. All i need to go is send a int8 from pic to pic using rs232 ( i would like to use i2c, but i dont know how). I know that if i do printf("%U",int8number) it will sent the ascii code to the other pic. What should i be doing. My cod now looks like this,
PIC1
Code: |
#use rs232(baud=2400,parity=N,bits=8,xmit=PIN_C0,rcv=PIN_C1)
printf("1\r");
while(!kbhit()&&(++timeout<50000))
{
delay_us(5);
}
if(kbhit())
{
currentPH = getch();
}
else
{
output_high(reset_adc);
}
while(!kbhit()&&(++timeout<50000))
{
delay_us(5);
}
if(kbhit())
{
currentKH = getch();
}
else
{
output_high(reset_adc);
}
while(!kbhit()&&(++timeout<50000))
{
delay_us(5);
}
if(kbhit())
{
currentCO2 = getch();
}
else
{
output_high(reset_adc);
}
while(!kbhit()&&(++timeout<50000))
{
delay_us(5);
}
if(kbhit())
{
currenttemp = getch();
}
else
{
output_high(reset_adc);
}
while(!kbhit()&&(++timeout<50000))
{
delay_us(5);
}
if(kbhit())
{
currenthoodtemp = getch();
}
else
{
output_high(reset_adc);
}
|
PIC2
Code: |
data = 69;
while (TRUE)
{
gets(serialreq);
printf("%u",data);
printf("%u",data);
printf("%u",data);
printf("%u",data);
printf("%u",data);
}
|
in the above code, data will be 5 diffrent vars later. This is only for testing. How should i be sending the data without useing atio()??
Thanks so much for your help. I am soooo close to finnishing this thanks to you guys
Mark Demczuk |
|
|
Mattr0
Joined: 27 Mar 2005 Posts: 30
|
|
Posted: Wed Feb 15, 2006 6:11 am |
|
|
Code: |
{
gets(data);
printf("recived = %u\r\n",data);
}
}
|
markdem I notice that the printf comment is not properly used in this case
it should be
Code: |
printf("recived = %s\r\n",data);
|
This will make it print out the whole string. Not sure what it will do above if it will compile at all |
|
|
Ttelmah Guest
|
|
Posted: Wed Feb 15, 2006 7:42 am |
|
|
I think it'll print the LSB of the address of the string!. The name of a string, is a shortcut for it's address. The address is a short integer value on a 16x chip, or a long integer is '*=16' is used in the configuration, or with the 18x chips. %u, expects a 8 bit integer, so it'll probably print the LSB of the long address form.
Best Wishes |
|
|
|
|
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
|