View previous topic :: View next topic |
Author |
Message |
qmilloy
Joined: 20 Jun 2011 Posts: 5
|
RS232 receive errors |
Posted: Mon Jun 20, 2011 10:44 am |
|
|
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
|
|
Posted: Mon Jun 20, 2011 10:46 am |
|
|
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: 9243 Location: Greensville,Ontario
|
|
Posted: Mon Jun 20, 2011 12:09 pm |
|
|
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
|
|
Posted: Mon Jun 20, 2011 12:21 pm |
|
|
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
|
|
Posted: Mon Jun 20, 2011 1:00 pm |
|
|
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: 9243 Location: Greensville,Ontario
|
|
Posted: Mon Jun 20, 2011 5:12 pm |
|
|
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
|
|
Posted: Tue Jun 21, 2011 1:24 pm |
|
|
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! |
|
|
|