Jump to content
Sign in to follow this  
Hugo

Simple RS-232 Question

Recommended Posts

The serial routines are so simple I'm not quite sure where I've gone wrong, but I have. If someone could set me straight I'd really appreciate it.

 

I'm programming a 16F876 connected to my PC. I get output in my terminal, but it's garbled, as sometimes happens with incorrect baud rates. I've double-checked baud rate, turned off handshaking, etc., but I still get garbage. I assume that getchar() will begin working when I fix this problem, as the receive routines also don't respond (the program hangs waiting for input no matter what I send down the line). Here is my source code:

 

#include

 

#pragma CLOCK_FREQ 4000000

#pragma RS232_RXPORT PORTA

#pragma RS232_TXPORT PORTA

#pragma RS232_RXPIN 1

#pragma RS232_TXPIN 2

#pragma RS232_BAUD 9600

#pragma TRUE_RS232 1

 

char _PORTA@0x05 ;

char _TRISA@0x85 ;

char _ADCON1@0x9f ;

 

void main (void)

{

int i=0;

const char string[] = {'P','r','e','s','s',' ','E','n','t','e','r','.','.','.',0x00};

_ADCON1 = 0x06 ;        // SWITCH OFF ADC

_TRISA = 00000011b ;     

 

while (1)

{

  while(string != 0x00) {

putchar(string);

i++;

  }

  getchar();

}

}

Share this post


Link to post
Share on other sites

Hugo

 

In some cases that you explain is caused by electrical

noise in PIC supply voltage. Why not try put any capacitor

to filter it ?

 

I hope help

 

Daniel

Share this post


Link to post
Share on other sites

What components are being used in the electrical interface to your PC ? - Some kind of voltage level convertor is required. Sometimes this kind of interface circuit invertors the signal, sometimes it does not.

 

Consider the RS232 polarity -

change

#pragma TRUE_RS232 1

to

#pragma TRUE_RS232 2

to reverse.

Share this post


Link to post
Share on other sites

OK, I've taken Steve's suggestion and used the UART. I've written this code, to send a continuous stream of characters, with slight pauses between. When I run *this* one, data is definitely being sent, but the terminal acts like it's a string of carriage returns and the cursor just blinks faster but nothing gets printed. Is this the proper way to deal with the UART?

 

/*
* serial routines for 16f87x with UART
*/

char _TRISB@0x86;
char _SPBRG@0x99;
char _RCSTA@0x18;
char _TXSTA@0x98;
char _PIE1@0x8C;
char _INTCON@0x0B;
char _TXREG@0x19;

#define         GIE             0x0007                                                                                         
#define         PEIE            0x0006                                                                                         
#define         T0IE            0x0005                                                                                         
#define         INTE            0x0004                                                                                         
#define         RBIE            0x0003                                                                                         
#define         T0IF            0x0002                                                                                         
#define         INTF            0x0001                                                                                         
#define         RBIF            0x0000 

void main(void)	{
_TRISB = 0;
_SPBRG = 25;    

set_bit(_RCSTA, SPEN);                // set serial port enable
clear_bit(_TXSTA,SYNC);
set_bit(_TXSTA,TXEN);                // set transmit enable
set_bit(_TXSTA,BRGH);                // set high baud rate speed
set_bit(_PIE1,TXIE);                // enable serial transmit interrupts
while (1 == 1)	{
 _TXREG = 'x';
 delay_ms(200);
}
}

Share this post


Link to post
Share on other sites

The only other things I did with mine were:

clear_bit(txsta,TX9);

clear_bit(txsta,SYNC);

...and those should both default to 0 anyway, so that can't be it.

 

Is it possible that your RS232 driver chip is non-inverting? I used a max233 and didn't have any problems.

Share this post


Link to post
Share on other sites

There is a chance that the software on the pc sends the bytes in reverse like this

// 	 txreg = 0x01; //11000000 dec 192
//    txreg = 0x02; // 1100000 dec 96
//    txreg = 0x03; //11100000 dec 224

Share this post


Link to post
Share on other sites

The other problem can be that the TX an RX pins are connected in reverse.

RB6 has to be connected to TXD

RB7 has to be connected to RXD

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...