Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

Software UART for PIC16f877a

Status
Not open for further replies.

akhilskb

Newbie level 4
Joined
Apr 20, 2011
Messages
7
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,283
Location
Quilon/Trivandrum,India
Activity points
1,334
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
 

akhilskb

Newbie level 4
Joined
Apr 20, 2011
Messages
7
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,283
Location
Quilon/Trivandrum,India
Activity points
1,334
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
 

bigdogguru

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,273
Trophy points
1,413
Location
Southwest, USA
Activity points
62,486
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
Joined
Mar 15, 2007
Messages
262
Helped
45
Reputation
90
Reaction score
28
Trophy points
1,308
Location
Karachi, Pakistan
Activity points
2,813
well i haven't used Hi-Tech compiler but you can post the code here than we will try to solve it....
 

bigdogguru

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,273
Trophy points
1,413
Location
Southwest, USA
Activity points
62,486
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

Gokul_India

Junior Member level 2
Joined
Apr 1, 2011
Messages
23
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Coimbatore, India
Activity points
1,463
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

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,273
Trophy points
1,413
Location
Southwest, USA
Activity points
62,486
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
Joined
Aug 1, 2009
Messages
334
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Location
India
Activity points
4,392
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
Joined
Apr 1, 2011
Messages
23
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Coimbatore, India
Activity points
1,463
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

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,273
Trophy points
1,413
Location
Southwest, USA
Activity points
62,486
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

Junior Member level 2
Joined
Apr 1, 2011
Messages
23
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Coimbatore, India
Activity points
1,463
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

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,273
Trophy points
1,413
Location
Southwest, USA
Activity points
62,486
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
Joined
Apr 1, 2011
Messages
23
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Coimbatore, India
Activity points
1,463
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


bigdogguru

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,273
Trophy points
1,413
Location
Southwest, USA
Activity points
62,486
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.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top