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

RS232 receive errors

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



Joined: 20 Jun 2011
Posts: 5

View user's profile Send private message

RS232 receive errors
PostPosted: Mon Jun 20, 2011 10:44 am     Reply with quote

Hi all,

So I have what seems to be a simple problem, however is being incredibly stubborn. I am working with a PIC 16F946 trying to send and receive data via RS232 to a computer. The send part works brilliantly, as I can send strings to the computer and they display correctly.

My issue seems to stem from reading in what the computer is sending to the PIC. Ultimately, a string will be send to the PIC, the PIC will parse and process the string, and send back a conformation to the computer what operation was run and whether it was run successfully. As I proof of concept, before doing parsing, I am attempting to get the PIC to echo back what the computer says. However, it would appear that only two characters are being read by the PIC. Its been a while since I programmed in C, so I'm trying to determine if it is an issue with my code (perhaps an issue with how I'm using strings?) or something with the RS232...

Here is the code:
Code:
#include <16F946.h>
#define CLOCK_BASE (clock=8,000,000)
#DEVICE ADC=10

#FUSES NOPROTECT
#FUSES NOCPD
#FUSES MCLR
#FUSES NOWDT
#FUSES NOPUT
#FUSES INTRC_IO
#FUSES NOIESO
#FUSES NOFCMEN
#FUSES NOBROWNOUT
#FUSES NODEBUG
#USE DELAY CLOCK_BASE

#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
#use fast_io(D)
#use fast_io(E)
#use fast_io(F)
#use fast_io(G)

// firmware revision
#define FIRMWARE_REV 0x00

//channel definitions go here, excluded for space

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#USE RS232 (baud = 9600, xmit=TX, rcv=RX, errors) // Initialize serial

#include <input.c>

void main( void )
{
   // Initialize which pins are inputs and which are outputs
   set_tris_a( TRIS_A_VAL );
   set_tris_b( TRIS_B_VAL );
   set_tris_c( TRIS_C_VAL );
   set_tris_d( TRIS_D_VAL );
   set_tris_e( TRIS_E_VAL );
   set_tris_f( TRIS_F_VAL );
   set_tris_g( TRIS_G_VAL );

   // Setup ADC, used later
   setup_adc_ports( ALL_ANALOG );
   setup_adc( ADC_CLOCK_INTERNAL );

   // Read in serial information

   char serialInput[30] = ""; // way oversized string for testing purposes


   do{
         delay_ms(1000);
         serialInput = "";
         if (kbhit())
         {
            gets(serialInput);
            delay_ms(10);
            printf("I made it passed!\n"); // Check if I made it passed gets
            printf("%s\n", serialInput);
         }

        }while(TRUE);
}


FYI: I'm using gets() instead of get_string() because get_string() writes to the TX pin, and I do not want to send the original message back as I read it in. However, even with get_string() only two characters would be read in.

For testing, I am using the Serial Input/Output monitor under Tools on the compiler. I send it an ASCII string first, followed by 0x0D hex for a return character.

Sorry for the long explanation. Any help would be greatly appreciated!
qmilloy



Joined: 20 Jun 2011
Posts: 5

View user's profile Send private message

PostPosted: Mon Jun 20, 2011 10:46 am     Reply with quote

if input is this:
ASCII sent: work
followed by
Hex sent: 0D

Sample output looks like this:
Code:
I made it passed!
wo
temtronic



Joined: 01 Jul 2010
Posts: 9162
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Jun 20, 2011 12:09 pm     Reply with quote

quick comments
1) get rid of the use_fast_io and set_tris... stuff, let the compiler automatically handle it for you, it'll save you hours of head scratching !

2) what are TX and RX ? in the use rs232(....), I didn't see them easily if there !
qmilloy



Joined: 20 Jun 2011
Posts: 5

View user's profile Send private message

PostPosted: Mon Jun 20, 2011 12:21 pm     Reply with quote

I excluded the pin declarations from my code, sorry about that.

Here are TX and RX for reference:

Code:
#define TX          PIN_C6   // O
#define RX          PIN_C7   // I


Also, taking use_fast_io out did not help. I set them with use_fast_io because all pins are either dedicated inputs or outputs and never change for my purposes. Is this bad practice?
qmilloy



Joined: 20 Jun 2011
Posts: 5

View user's profile Send private message

PostPosted: Mon Jun 20, 2011 1:00 pm     Reply with quote

Problem solved! I removed the kbhit() and just told it to start reading right away, now it will read in the whole string!
temtronic



Joined: 01 Jul 2010
Posts: 9162
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Jun 20, 2011 5:12 pm     Reply with quote

Glad to hear you've found the error !

As long as you're fully aware that sooner or later, one day, you'll 'rework' or 'revise' the I/O layout AND forget to properly reconfigure the set_tris.. registers then wonder WHY doesn't it work...it USED too ???!!!!
Been there done that, several times in the past 2 decades, I finally learned....

Unless you need a bit more speed or a few bytes of memory it is far safer to let the compiler handle the I/O pin setups.
qmilloy



Joined: 20 Jun 2011
Posts: 5

View user's profile Send private message

PostPosted: Tue Jun 21, 2011 1:24 pm     Reply with quote

Yeah I can definitely see how that can cause problems. I am a little worried about total memory, but I'm not sure that a few bytes are going to make the difference either way.

Thanks for all your help!
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