Jump to content

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:




#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) {







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 -


#pragma TRUE_RS232 1


#pragma TRUE_RS232 2

to reverse.

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
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';

Link to post
Share on other sites
  • 10 months later...

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.

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.

  • Create New...