|
|
View previous topic :: View next topic |
Author |
Message |
circle
Joined: 02 Jan 2005 Posts: 3
|
communication with PC through RS232 |
Posted: Fri Jan 07, 2005 2:31 am |
|
|
The following are my ccs program:
#include <16f877.h>
#use delay (clock=4000000)
#USE RS232(BAUD=9600, XMIT=PIN_C6, RCV=PIN_C7,PARITY=N,BITS=8,STREAM=COM_1)
void main()
{
char c;
fprintf(COM_1,"OK!\n");
while(1) {
if(kbhit()){
c=fgetc(COM_1);
if(c==00)fprintf(COM_1,"I have received 00H\n");
else fputc(c, COM_1);}
}
}
The problem is :whatever PC sent any word to PIC16F877,PIC only can receive 00H,then return "I have received 00H".I don't know why?Can any one help me. |
|
|
dvsoft
Joined: 28 Nov 2003 Posts: 46
|
|
Posted: Fri Jan 07, 2005 5:41 am |
|
|
bonjour
try this
int8 Cmd;
while(TRUE) {
if (kbhit()) {
Cmd = fgets(COM_1);
fprintf (COM_1,"I Have received :%d\r\n",Cmd);
}// End If
}// End While
Alain |
|
|
circle
Joined: 02 Jan 2005 Posts: 3
|
Thank you and anther question |
Posted: Fri Jan 07, 2005 8:33 pm |
|
|
Thank you.But there is something wrong with my ICD2,it can't programe
MCU.When I want to put my program into PIC16F877,How should I
set appropriatel configuration bits for MCU?By far,I setup all item "OFF" or "disable".
The version of MPLAB which I use is 6.20. |
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Sat Jan 08, 2005 11:34 am |
|
|
#fuses NOWDT, NOLVP, PUT, NODEBUG, XT _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
|
circle
Joined: 02 Jan 2005 Posts: 3
|
PIC still can only received 00H |
Posted: Sat Jan 08, 2005 11:50 pm |
|
|
#include <16f877.h>
#fuses NOWDT, NOLVP, PUT, NODEBUG, XT
#use delay (clock=4000000)
#USE RS232(BAUD=9600, XMIT=PIN_C6, RCV=PIN_C7,PARITY=N,BITS=8,STREAM=COM_1)
void main()
{
char c;
fprintf(COM_1,"OK!\n");
while(1) {
if(kbhit()){
c=fgetc(COM_1);
fprintf (COM_1,"PIC have received :%d\n",c);
}
}
}
When I run the program above, no matter what word PC sent to PIC16F877,PIC can only output"PIC have recerved:0".
What's wrong with it? |
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Sun Jan 09, 2005 1:38 pm |
|
|
Code: | #include <16f877A.h>
#case
#fuses NOWDT, NOLVP, PUT, NODEBUG, HS
#use delay (clock=20000000)
#use rs232(BAUD=9600, XMIT=PIN_C6, RCV=PIN_C7,PARITY=N,BITS=8,STREAM=COM_1)
void main()
{
char c;
fprintf(COM_1,"\rOK!\r\n");
fprintf(COM_1,"Don't forget to get a copy of K&R and read it! ;-)\r\n");
while(1)
{
if(kbhit())
{
c=fgetc(COM_1);
fprintf(COM_1,"PIC has received character :'%c'\r\n",c);
fprintf(COM_1,"which is ASCII code %d decimal or 0x%02X hex\r\n",c,c);
}
}
}
|
Works just fine. Some small changes for testing on my end, I changed to a 20MHz xtal so the #fuse and #delay statements are a little different. I also added the #case statement because I prefer the case sensitivity. And my board has an 877A so I had to change your #include statement.
I added a line to remind you to buy a copy of Kernigan and Ritchie's C Lanugage book. Get the 2nd edition. A VERY GOOD INVESTMENT.
I changed your fprintf() statement to use the %c format specifier assuming what you wanted to see was the actual character typed. And to further entertain you I added another line where the character is listed as its ASCII code in both decimal and a simply formated hexidecimal number.
And before I forget, the compiler version I used is V3.215 _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
|
LytseFlap
Joined: 22 Dec 2004 Posts: 6 Location: Leeuwarden
|
|
Posted: Mon Jan 10, 2005 2:26 am |
|
|
There's nothing wrong with the software. I think you've got a hardware problem. See page 213 of the reference manual and the sheets from Maxim.
Seeing the first problem, I agree with the poster above me that you need to study more about C and the controller.
Good Luck! |
|
|
|
|
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
|