# Software UART for PIC16f877a

Status
Not open for further replies.

#### akhilskb

##### Newbie level 4
I am using two serial communication in a single PIC , so i need a software uart .So, Can anyone post the code for Software Uart in PIC16f877a

#### bigdogguru

Are you coding in assembler or C? If you're using a C compiler, what version and company's?

shareef suliman

### shareef suliman

points: 2

#### hussain_kiet

##### Full Member level 5
CCS compiler has built in functions for software serial...

shareef suliman

### shareef suliman

points: 2

#### akhilskb

##### Newbie level 4
yes , I am using C compiler. HI-TECH C Complier for pic 10/12/16 version, MCus V9.70PLO.

---------- Post added at 17:44 ---------- Previous post was at 17:43 ----------

can you give me the material

points: 2

### shareef suliman

points: 2

#### hussain_kiet

##### Full Member level 5
check in the samples come with the Hi-tech C compiler about software serials..
"\ht-pic\samples"

shareef suliman

### shareef suliman

points: 2

#### akhilskb

##### Newbie level 4
I did'nt get that correctly. i got that but its not working. what can i do..?

shareef suliman

### shareef suliman

points: 2

#### bigdogguru

By the way, what are each of the two serial ports communicating with? PC and some sort of sensor, modem, etc?

#### hussain_kiet

##### Full Member level 5
well i haven't used Hi-Tech compiler but you can post the code here than we will try to solve it....

#### bigdogguru

Attached is a soft UART routine for 16FXXX family. I have tested it and it does function corretly. Coded for a 4MHz OSC, so adjustments must be made for a different OSC frequency. I recommend using the soft UART on the port with the least traffic.

#### Attachments

• 1.8 KB Views: 69
• 1.7 KB Views: 113
prajeeshxrd

### prajeeshxrd

points: 2

#### akhilskb

##### Newbie level 4
thanks bigdogguru, let me try it .

#### bigdogguru

Hi akhilskb,

Did you manage to get the softUART to compile and function properly?

#### Gokul_India

##### Junior Member level 2
Attached is a soft UART routine for 16FXXX family. I have tested it and it does function corretly. Coded for a 4MHz OSC, so adjustments must be made for a different OSC frequency. I recommend using the soft UART on the port with the least traffic.
is this all the code??? Nothing else needed for Software UART sir...?? If i just program this in my PIC16f877a, can i use the 2 pins for my read and write using UART...?? I am going to connect a GSM to these pins through Soft UART..., so only, i am asking...!!!

Last edited:

#### bigdogguru

Actually I posted a different version, the complete MPLAB project, with an example app as well on your original thread:

Can i use any other pins for UART in PIC? other than Tx,Rx??

You'll need to change the MCU type in MPLAB.

Set the parameters to match your project's specifications:

Code:
/*	Xtal frequency */
#define	XTAL		4000000

/*	Baud rate	*/
#define	BRATE		4800

/*	Transmit and Receive port bits */
#define	TxData		RA1				/* Map TxData to pin */
#define	RxData		RA2				/* Map RxData to pin */
#define	INIT_PORT	TRISA = 1<<2	/* set up I/O direction */
And I believe that should do it for you. Let me know if you have any other problems.

---------- Post added at 05:05 ---------- Previous post was at 05:02 ----------

By the way, is your GSM a 5v device? If not you'll need a level shifter between the PIC and the GSM, unless you can run your PIC at 3.3v.

#### tiwari.sachin

##### Full Member level 6
Gokul,

If i get it right, you are using 16F877 which has a single UART and if you are using TX and RX both pins and you connect a GSM. well... you are done with the UART pins and you cannot use it any further for other devices. You can use multiplexing but than thats a different thing.

One more UART... Bit Bang is the option but than needs a bit of coding.

#### Gokul_India

##### Junior Member level 2
Gokul,

If i get it right, you are using 16F877 which has a single UART and if you are using TX and RX both pins and you connect a GSM. well... you are done with the UART pins and you cannot use it any further for other devices. You can use multiplexing but than thats a different thing.

One more UART... Bit Bang is the option but than needs a bit of coding.
what is that Bit bang??? and, i am using the Tx, Rx pins for interfacing with GPS.., so i need to use some other pins for GSM with a software UART...

---------- Post added at 11:34 ---------- Previous post was at 11:08 ----------

Actually I posted a different version, the complete MPLAB project, with an example app as well on your original thread:

Can i use any other pins for UART in PIC? other than Tx,Rx??

You'll need to change the MCU type in MPLAB.

Set the parameters to match your project's specifications:

Code:
/*	Xtal frequency */
#define	XTAL		4000000

/*	Baud rate	*/
#define	BRATE		4800

/*	Transmit and Receive port bits */
#define	TxData		RA1				/* Map TxData to pin */
#define	RxData		RA2				/* Map RxData to pin */
#define	INIT_PORT	TRISA = 1<<2	/* set up I/O direction */
And I believe that should do it for you. Let me know if you have any other problems.

---------- Post added at 05:05 ---------- Previous post was at 05:02 ----------

By the way, is your GSM a 5v device? If not you'll need a level shifter between the PIC and the GSM, unless you can run your PIC at 3.3v.

Y should i modify like this? is there any reasons?? And, PORTA is a 6 pin port as i am using PIC16f877a... and, i tried to simulate it with Proteus Software., by just loading this program and connecting a virtual terminal to the RB6., RB7 pin, but i couldnt get the output??

Last edited:

#### bigdogguru

Y should i modify like this? is there any reasons?? And, PORTA is a 6 pin port as i am using PIC16f877a...
No, you can use any free digital I/O pins you have available on the PIC. However make sure you disable any ADC, Comparator, etc, peripherals you may have using those pins.

The example code is setting the clock frequency as 4MHz and the baud rate to 4800, change appropriately as well as assign the pin you would like to use as TX and RX. The INIT_PORT is just setting digital I/0 direction using the appropriate TRIS register.

Gokul_India

### Gokul_India

points: 2

#### Gokul_India

##### Junior Member level 2
Hi guru sir.,
i changed the code as u said..., used RA1 and RA2...., and changed the BRATE as 4800 and..., where all it was compared with the old 1200., i changed it as 4800......, i saw in Proteus..., there was some statement written, but, it was scrambled., may be, its not in correct baud rate i hope...., so, pls help me sir....

And, is it that., its TRISA = 1<<7 or 1<<2...???

Last edited:

#### bigdogguru

Zip and upload the entire Proteus project with code. I'll take a look at it, most likely just a setting error.

#### Gokul_India

##### Junior Member level 2
here is the proteus design sir.... In the program, u had sent, i had just modified Tx as RA1 and Rx as RA2 as u said... And, BRATE as 4800., and also, wherever i saw 1200., i modified as 4800 sir...

#### Attachments

• 11.9 KB Views: 47

#### bigdogguru

Ok, I've got it.

It getting late here so I most likely won't get it back to you till tomorrow.

Hi guru sir.,
And, is it that., its TRISA = 1<<7 or 1<<2...???
This is just setting the seventh, "1<<7" or the second, "1<<2" bit of the TRISA to '1', the same as TRISA = 0b01000000; or TRISA = 0b00000010; for the input/output direction.

Ciao

Status
Not open for further replies.