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 CCS Technical Support

Codes to receive RS232 data from COM 1 of PC FAILED

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



Joined: 11 Feb 2004
Posts: 51

View user's profile Send private message

Codes to receive RS232 data from COM 1 of PC FAILED
PostPosted: Thu Apr 01, 2004 2:45 am     Reply with quote

I have written the following codes to check the data received by the PIC16F877A from COM 1. Basically, it echoes the data signal that it received back to the PC for us to view and verify.

However, this program does not work most of the time. On a few occasion, it echoes some irrelevant data.

Please help...


#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use RS232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#include <input.c> //found in the CCS drivers example//

#define KEYHIT_DELAY 500 // in milliseconds ie 500 ms


char timed_getc() {
long timeout;
char retval;

timeout=0;
while(!kbhit() && (++timeout< (KEYHIT_DELAY*100)))
delay_us(10);
if(kbhit())
retval = getc();
else
retval = 0;
return(retval);
}



//================================
void main()
{
int status, i;
char value;
//char valid[11] = {'1','2','3','4','5','6','7','8','9','0'}; tried this but can't work
char valid;


long int temp, hum;
long int store[3];


while(TRUE)
{
status=1;
printf("\r\nStart typing:\r\n");
while(!kbhit());

while(status==1)
{
value=timed_getc();
if(value==0)
status=0;
else
{
status=1;
putc(value);


switch (value)
{
//Tried the following but can't work
// CASE valid:
// store[i] = buffer - 0x30 (with this you will store the integer )
// i++;
// BREAK;

CASE '0':
store[i] = value - 0x30; //Value(ASCII format) - 0x30 = Value(decimal)
i++;
BREAK;

CASE '1':
store[i] = value - 0x30;
i++;
BREAK;

CASE '2':
store[i] = value - 0x30;
i++;
BREAK;

CASE '3':
store[i] = value - 0x30;
i++;
BREAK;

CASE '4':
store[i] = value - 0x30;
i++;
BREAK;


CASE '5':
store[i] = value - 0x30;
i++;
BREAK;


CASE '6':
store[i] = value - 0x30;
i++;
BREAK;

CASE '7':
store[i] = value - 0x30;
i++;
BREAK;

CASE '8':
store[i] = value - 0x30;
i++;
BREAK;


CASE '9':
store[i] = value - 0x30;
i++;
BREAK;

CASE 'C':
temp = 100*store[0] + 10*store[1] + store[2];
i = 0;
printf("%04ld Celcius",temp);
BREAK;

CASE '%':
hum = 100*store[0] + 10*store[1] + store[2];
i = 0;
printf("%04d percent",hum);
BREAK;
}
}


}
printf("\r\nToo slow!\r\n");
}
}
Kasper



Joined: 14 Jan 2004
Posts: 88
Location: Aurora, Ontario, Canada

View user's profile Send private message Visit poster's website

PostPosted: Thu Apr 01, 2004 8:40 am     Reply with quote

try making it interrupt based, this way you can also make the pic do other stuff while waiting for incomming characters.

here is a code snippet, which does work for returning your characters.
It is set up to use the two hardware UARTS in the 18F6720, but on the 16F877 if you have only one, comment out the one you don't have.

As far as I am concerned, Interrupts are the way to go for RS232 communications Smile

Code:


#include <18F6720.H> //chip we are using
#include <string.h>
#fuses HS,NOPROTECT,NOWDT,NOLVP   //define fuse settings
#use delay(clock=6000000,RESTART_WDT)   //define system clock
#use rs232(baud=38400, parity=N, xmit=PIN_G1, rcv=PIN_G2, stream=PORT2, bits=8, errors) //,errors)//RS232/USB/Ethernet
#use rs232(baud=1200,  parity=N, xmit=PIN_C6, rcv=PIN_C7, stream=PORT1, bits=8, errors) //,ERRORS RF Receiver

void RF_IN_Handler(void);
void rs232_handler(void);


void main(void) {
char Received;
char Received2;
int rxflag=0;
set_tris_c(0xBF); // Set pin C6 as an output
set_tris_g(0xFD); // Set pin G1 as an output
   enable_interrupts(INT_RDA);   // RF Receiver Interrupt
   enable_interrupts(INT_RDA2);// RS232/USB/Ethernet Receiver Interrupt. Will crash the program if left floating.
   enable_interrupts(GLOBAL);            //enable global Interrupts
fprintf(PORT2,"\n\rSystem Initialized:\n\r"); // tells me that the port is OK

while(true){

   } // end while
}// end main


#int_rda   // This is the RS232 input from the rf receiver.
void RF_IN_Handler(void) {
char Received;
Received=fgetc(PORT1);
fprintf(PORT2,"%X",Received);
}

#int_rda2   // This is the RS232 input from the PC   
void rs232_handler(void){
char Received2;
Received2=fgetc(PORT2);
fprintf(PORT2,"%c",Received2);
}


add your code in the interrupt handeler to check the incomming data.
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