CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

communication with PC through RS232

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
circle



Joined: 02 Jan 2005
Posts: 3

View user's profile Send private message

communication with PC through RS232
PostPosted: Fri Jan 07, 2005 2:31 am     Reply with quote

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. Shocked
dvsoft



Joined: 28 Nov 2003
Posts: 46

View user's profile Send private message

PostPosted: Fri Jan 07, 2005 5:41 am     Reply with quote

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

View user's profile Send private message

Thank you and anther question
PostPosted: Fri Jan 07, 2005 8:33 pm     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Sat Jan 08, 2005 11:34 am     Reply with quote

#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

View user's profile Send private message

PIC still can only received 00H
PostPosted: Sat Jan 08, 2005 11:50 pm     Reply with quote

#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

View user's profile Send private message Send e-mail

PostPosted: Sun Jan 09, 2005 1:38 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jan 10, 2005 2:26 am     Reply with quote

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!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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