Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Serial comm between uC and PC...

Status
Not open for further replies.
And as Be80be said,

Code for a 16f873a will run on a 16f877a there the same core but the 16f877a has more pins SO IT MAY NOT WORK FOR THE 28 PIN CHIP OF YOU DON'T WATCH PORTS

This is where you need to think for yourself. Go through the code, make sure the program controls the right pins and ports. Otherwise your chip won't do a thing.
 
Look at the #@$%!*& datasheet, like we've been telling you to do from the start. Look under the ports section. ALL THE INFORMATION YOU'LL EVER NEED is in there.
 
Like I just said:

Look at the #@$%!*& datasheet, like we've been telling you to do from the start. Look under the ports section. ALL THE INFORMATION YOU'LL EVER NEED is in there.
 
OK, after changing the TRISC setting and i have checked SPBRG register it should be 129 only at 20Mhz....but still not working.
I noticed while resetting the PIC null charter are transmitted 00, why??
 
From the data sheet,

Bit SPEN (RCSTA<7>) and bits TRISC<7:6> have to be
set in order to configure pins RC6/TX/CK and RC7/RX/DT
as the Universal Synchronous Asynchronous Receiver
Transmitter.

It's all there if you simply look.

Mike.
 
I think code are fine and the schematic is posted in previous post....



Code:
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000





unsigned char  HSerin(void);
void HSerout(unsigned char ch), HSerinit(void);
 
void main(void)						// program entry
	{
	int index = 0;
	unsigned char ch;
	ADCON1 = 0x6;					// Analogue off					
	HSerinit();
       __delay_ms(250);
	while(1)						// endless Loop
		{
		//ch = HSerin();				// wait for a character
 		HSerout;				// Echo back
 
		}
	}
 
void HSerinit()
	{
	TRISC = 0b10000000;					// should ideally be set
	SPBRG = 129;					// 20Mhz xtal 9600 BAUD
	TXSTA = 0x24;					// TXEN and BRGH
	RCSTA = 0x90;					// SPEN and CREN
	}
 
void HSerout(unsigned char ch)
	{
	while(!TXIF);					// Wait for module to finish
	TXREG = '0';						// ready to send
	}
/
 
I though so.... As you haven't changed anything other than the xtal setting... Whats not working! As this program, as it stands, doesn't do anything!!

Hserout should take a parameter.... "ch" in my example... So send something.

Hserout('A'); Send character A.
 
Beleive me i have tested this code first than moved to other it is not working don't know why???



Code:
include <pic.h>				// pic specific identifiers
#define _XTAL_FREQ  20000000		// Xtal speed
__CONFIG(0x3F71);				// Config bits
 
 
		// Required prototypes.. each function needs to be declared
		// if called BEFORE definition.
 
 
unsigned char  HSerin(void);
void HSerout(unsigned char ch), HSerinit(void);
 
void main(void)						// program entry
	{
	int index = 0;
	unsigned char ch;
	ADCON1 = 0x6;					// Analogue off					
	HSerinit();
       __delay_ms(150);
	while(1)						// endless Loop
		{
		ch = HSerin();				// wait for a character
 		HSerout(ch);				// Echo back
 
		}
	}
 
void HSerinit()
	{
	TRISC = 0b10000000;					// should ideally be set
	SPBRG = 129;					// 20Mhz xtal 9600 BAUD
	TXSTA = 0x24;					// TXEN and BRGH
	RCSTA = 0x90;					// SPEN and CREN
	}
 
void HSerout(unsigned char ch)
	{
	while(!TXIF);					// Wait for module to finish
	TXREG = ch;						// ready to send
	}
unsigned char HSerin()
	{
	while(!RCIF);					// Wait for a character
	return RCREG;					// return character
 
	}
 
Ian hi that code doesn't do anything but echo back what's typed and sent out maybe the keyboard is broke or some one is for getting to type.
 
Last edited:
I just for the heck of it complied this with XC8 and guess what it complies fine but none of the serial stuff works and that maybe what is happening to the OP
 
I have just tested the code above... It is working fine.. I think your Rx and Tx are the wrong way round..

I have tested USB module it is fine and i have connected the rx to tx and tx to rx but no data is transmitted...

and i have changed setting too this b'coz urs where giving me error...

#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000
 
[MODNOTE]Deleted Off Topic[/MODNOTE]
 
Last edited by a moderator:
The only error with the code was for some reason you had missed the first '#' before include... The config code can be both ways..

Remember my code was for a 4Mhz crystal (XT) originally 0x3F71... You have a 20Mhz (HS) crystal 0x3f72..

Are you using P3.6 and P3.7 on the PIC?
 
[MODNOTE]Deleted Off Topic[/MODNOTE]
 
Last edited by a moderator:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top