# Question about serial comms between a pic and a pc using a max232

#### hunterage2000

Joined May 2, 2010
487
Hi all, I'm trying to output a single character to Teraterm using a pic16f690 and a max232. I have used the following code to output the letter 'C'.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <pic16f690.h>
#include <xc.h>

#pragma config WDTE = OFF
#pragma config PWRTE = OFF

void main()
{

OSCCON = 0b01111000;    //internal 8MHz oscillator

BAUDCTLbits.SCKP = 0;   //0 = invert data to TX pins //1 = noninvert
BAUDCTLbits.BRG16 = 0;  //8 bit baud gen used
BAUDCTLbits.WUE = 0;    //Receiver is operating normally
BAUDCTLbits.ABDEN = 1;  //0 = auto baud detect disabled  //1 = enabled

TXSTAbits.TX9 = 0;      //Selects 8 bit transmission
TXSTAbits.TXEN = 1;     //Enables transmit
TXSTAbits.SYNC = 0;     //Select async mode
TXSTAbits.SENDB = 0;    //0 = sync break complete   1 = send break on next TX
TXSTAbits.BRGH = 0;     //Low baud rate speed
TXSTAbits.TX9D = 0;     //only for 9 bit transmission

RCSTAbits.SPEN = 1;     //Enables serial port pins TX and RX
RCSTAbits.RX9 = 0;      //Selects 8 bit transmission

while(1)
{
TXREG = 'C';

while(PIR1bits.TXIF == 0);

}
}
When I probed the input and output to the max232 I think I got what I expected. For the letter 'C', the byte is 11000010.

The attached waveform shows this. I have labelled the start and stop bits as 0* and 1*. Apologies for the order of the byte.

Looking at Teraterm, the terminal is giving garbage symbols and not 'C'.

Anyone have any experience with this?

#### Attachments

• 90.9 KB Views: 13

#### StayatHomeElectronics

Joined Sep 25, 2008
1,070
The letter C is 0100 0011. The ascii code that you show is A with a pointy hat, not sure the technical name.

#### StayatHomeElectronics

Joined Sep 25, 2008
1,070
Does it give the correct response if you assign TXREG = 0b01000011?

#### AlbertHall

Joined Jun 4, 2014
10,237
You haven't set the baud rate. The code sets auto baud detect but you do not go through the sequence to actually set it up.
Set SRBG to 12 decimal and ABDEN to 0.
Set teraterm for 9600 baud.
BAUDCTLbits.SCKP = 0; //0 = invert data to TX pins //1 = noninvert
For SCKP, 0 is non-inverted and 1 is inverted but 0 is the correct value as the data is going through a MAX232 this chip inverts the data.

#### MMcLaren

Joined Feb 14, 2010
845
Did you set the configuration bits somewhere for Internal Oscillator?

#### hunterage2000

Joined May 2, 2010
487
Hi all, sorry I didn't get back sooner just been busy with work. I have set the internal oscillator using the SCS bit. I forgot to put the SPBRH value in but now its in its kind of working I've tried different characters and this is what I'm getting:

1) The number '9' (00111001) gave 5 9's then it changed to 'N' (01001110) which I noticed is '9' shifted right 2 bits
2) The letter 'F' (01000110) gave 5 F's then stopped, restarting new connection gave all F's
3) The letter 'G' (01000111) gave a zero with a caret (^)
4) The letter 'X' (01011000) gave 5 a's then all X's

Anyone know why this is happening. The noise on the waveform is not that bad.

#### AlbertHall

Joined Jun 4, 2014
10,237
Hi all, sorry I didn't get back sooner just been busy with work. I have set the internal oscillator using the SCS bit. I forgot to put the SPBRH value in but now its in its kind of working I've tried different characters and this is what I'm getting:

1) The number '9' (00111001) gave 5 9's then it changed to 'N' (01001110) which I noticed is '9' shifted right 2 bits
2) The letter 'F' (01000110) gave 5 F's then stopped, restarting new connection gave all F's
3) The letter 'G' (01000111) gave a zero with a caret (^)
4) The letter 'X' (01011000) gave 5 a's then all X's

Anyone know why this is happening. The noise on the waveform is not that bad.
Add a delay between the characters so the start and stop bits are clearly distinguishable by the terminal software.

#### hunterage2000

Joined May 2, 2010
487
ok Albert will do. You don't know what the SENDB bit is for do you? It says

0 = sync break complete
1 = send break on next TX

#### hunterage2000

Joined May 2, 2010
487
Right I see. Using a delay is giving me sensible characters on Teraterm so I think I can progress with sending measured values from the adc to it. Thanks all.

#### Picbuster

Joined Dec 2, 2013
1,020
Hi all, I'm trying to output a single character to Teraterm using a pic16f690 and a max232. I have used the following code to output the letter 'C'.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <pic16f690.h>
#include <xc.h>

#pragma config WDTE = OFF
#pragma config PWRTE = OFF

void main()
{

OSCCON = 0b01111000;    //internal 8MHz oscillator

BAUDCTLbits.SCKP = 0;   //0 = invert data to TX pins //1 = noninvert
BAUDCTLbits.BRG16 = 0;  //8 bit baud gen used
BAUDCTLbits.WUE = 0;    //Receiver is operating normally
BAUDCTLbits.ABDEN = 1;  //0 = auto baud detect disabled  //1 = enabled

TXSTAbits.TX9 = 0;      //Selects 8 bit transmission
TXSTAbits.TXEN = 1;     //Enables transmit
TXSTAbits.SYNC = 0;     //Select async mode
TXSTAbits.SENDB = 0;    //0 = sync break complete   1 = send break on next TX
TXSTAbits.BRGH = 0;     //Low baud rate speed
TXSTAbits.TX9D = 0;     //only for 9 bit transmission

RCSTAbits.SPEN = 1;     //Enables serial port pins TX and RX
RCSTAbits.RX9 = 0;      //Selects 8 bit transmission

while(1)
{
TXREG = 'C';

while(PIR1bits.TXIF == 0);

}
}
When I probed the input and output to the max232 I think I got what I expected. For the letter 'C', the byte is 11000010.

The attached waveform shows this. I have labelled the start and stop bits as 0* and 1*. Apologies for the order of the byte.

Looking at Teraterm, the terminal is giving garbage symbols and not 'C'.

Anyone have any experience with this?

Not all baud rates are possible see manual.
Always use Interrupts to collect data. Let me know if a sample program is needed.
Below is a simple example to calculate settings for baud rates but again check manual.
You are able to fiddle the 8MHz internal clock to adjust baud rate ( see manual).
Picbuster

#define _XTAL_FREQ 8000000

#define FOSC 8000000L
#define NINE 0 /* Use 9bit communication ? FALSE=8bit */

#define BAUD 4800
#define DIVIDER ((int)(FOSC/(16UL * BAUD) -1))

#define HIGH_SPEED 1

#if NINE == 1
#define NINE_BITS 0x40
#else
#define NINE_BITS 0
#endif

#if HIGH_SPEED == 1
#define SPEED 0x4
#else
#define SPEED 0
#endif

#define RX_PIN TRISB5 //pin 12
#define TX_PIN TRISB7 //pin 10

/* Serial initialization */
#define init_comms() \
RX_PIN = 1; \
TX_PIN = 1; \
SPBRG = DIVIDER; \
RCSTA = (NINE_BITS|0x90); \
TXSTA = (SPEED|NINE_BITS|0x20)