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.

koolguy

Active Member
Hi,

I working on serial comm from uC to PC i am using this **broken link removed** and this terminal program window 7....
here is the showing error........


Code:
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000
#define NINE 0     /* Use 9bit communication? FALSE=8bit */

#define BAUD 9600  

    #define RX_PIN TRISB2
    #define TX_PIN TRISB5
#define DIVIDER ((int)(_XTAL_FREQ/(16UL * BAUD) -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
 Initialize ();







void main ()
{
    Initialize ()
{   
    while(1)
    {
        putchar('H');   //print the letter 'H'
        putchar('i');   //print 'i'
        putchar('\r');  // return to beginning of line
        putchar('\n');  //new line
    }
}    
}



Initialize ()
{
    RX_PIN = 1;    
    TX_PIN = 1;          
    SPBRG = DIVIDER;         
    RCSTA = (NINE_BITS|0x90);
    TXSTA = (SPEED|NINE_BITS|0x20);
}



Make: The target "C:\Users\Abc\Documents\mplab\UART.p1" is out of date.
Executing: "C:\Program Files (x86)\HI-TECH Software\PICC\9.83\bin\picc.exe" --pass1 C:\Users\Abc\Documents\mplab\UART.c -q --chip=16F877A -P --runtime=default --opt=default -D__DEBUG=1 -g --asmlist "--errformat=Error [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s"
Error [312] C:\Users\Abc\Documents\mplab\UART.c; 32.1 ";" expected
Warning [361] C:\Users\Abc\Documents\mplab\UART.c; 36.1 function declared implicit int
Error [285] C:\Users\Abc\Documents\mplab\UART.c; 40.1 no identifier in declaration
Warning [374] C:\Users\Abc\Documents\mplab\UART.c; 40.1 missing basic type; int assumed
Error [314] C:\Users\Abc\Documents\mplab\UART.c; 40.1 ";" expected
Error [285] C:\Users\Abc\Documents\mplab\UART.c; 41.1 no identifier in declaration
Warning [374] C:\Users\Abc\Documents\mplab\UART.c; 41.1 missing basic type; int assumed
Error [314] C:\Users\Abc\Documents\mplab\UART.c; 41.1 ";" expected

********** Build failed! **********
 
Code:
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000
#define NINE 0     /* Use 9bit communication? FALSE=8bit */
 
#define BAUD 9600  
 
    #define RX_PIN TRISB2
    #define TX_PIN TRISB5
#define DIVIDER ((int)(_XTAL_FREQ/(16UL * BAUD) -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
void Initialize (void);  <----- YOU FORGOT THE "void's"
 
 
 
 
void main ()
{
    Initialize ();  <---- YOU FORGOT THE";"
{   
    while(1)
    {
        putchar('H');   //print the letter 'H'
        putchar('i');   //print 'i'
        putchar('\r');  // return to beginning of line
        putchar('\n');  //new line
    }
}    
}
 
 
 
void Initialize ()  <----- YOU FORGOT THE "void"
{
    RX_PIN = 1;    
    TX_PIN = 1;          
    SPBRG = DIVIDER;         
    RCSTA = (NINE_BITS|0x90);
    TXSTA = (SPEED|NINE_BITS|0x20);
}

You might need to provide the putchar routine... I'll check and get back to you...


Yep!!! I thought so.... putch() and getch() are empty functions.... You need to place the code into them... If you go to the Hitech directory you'll find them in "sources" you need to modify them so they output and input to the serial port
 
Last edited:
#define RX_PIN TRISB2
#define TX_PIN TRISB5

I have modified the code so, i want to ask one thing in datasheet pin 25 and 26 where Rx and Tx but here portb is used, why?
 
again showing error.........

Error [1098] C:\Program Files (x86)\HI-TECH Software\PICC\9.83\sources\putchar.c; 16. conflicting declarations for variable "_putchar" (C:\Users\Abc\Documents\mplab\main.c:31)
 
Have you re-declared putchar ??? Why..

Hi,

What do you mean by this and i am using code as it is only removing comments..!!

The Rx and Tx are on PORTC.6 and 7 not PORTB

Then what is this :

#define BAUD 9600

#define RX_PIN TRISB2
#define TX_PIN TRISB5
 
Then what is this :

#define BAUD 9600

#define RX_PIN TRISB2
#define TX_PIN TRISB5

I don't know..... I don't know where you got it from....

In my tutorials 7,7... There is hardware serial...
 
I have got it from hi tech c samples-> UART...
this was the problem in this pat..

#if defined(_16F87) || defined(_16F88)
#define RX_PIN TRISB2
#define TX_PIN TRISB5
#else
#define RX_PIN TRISC7
#define TX_PIN TRISC6
#endif
 
Why don't you use the tutorial.... It's VERY easy to understand and it works..
This is for a pic16f877a..

C:
include <pic.h>				// pic specific identifiers
#define _XTAL_FREQ  20000000		// Xtal speed
__CONFIG(0x3F71);				// Config bits

#define LCD_PORT 	PORTB		// constants
#define LCD_TRIS	TRISB		//
#define LCD_RS 		RB4
#define LCD_RW		RB6
#define LCD_E		RB7

		// Required prototypes.. each function needs to be declared
		// if called BEFORE definition.

void LCD_init(void), LCD_cmd(unsigned char ch);
void LCD_goto(char line, char column), LCD_clr(void);
void LCD_cur(unsigned char ch), pulse_E(void), LCD_hex(int value);
void LCD_char(unsigned char ch), LCD_charD(unsigned char ch);
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
	LCD_TRIS = 0b00000000;			// Led port as outputs
	__delay_ms(150);				// let LCD stabilise
	LCD_init();						// Initalise screen
	LCD_goto(1,0);					// line 1.
	HSerinit();
	while(1)						// endless Loop
		{
		ch = HSerin();				// wait for a character

		if(index == 16)				// new line
			LCD_goto(2,0);
		if(index == 32)				// goto line 1
			{
			LCD_clr();
			index = 0;				// re-set
			}
		LCD_char(ch);				// display the received byte
		HSerout(ch);				// Echo back
		index++;					// move on
		}
	}

void HSerinit()
	{
	TRISC = 0xC0;					// 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

	}

void LCD_init()
	{
	LCD_cmd(0x20);					// 4 bit
	LCD_cmd(0x28);					// display shift
	LCD_cmd(0x6);					// character mode
	LCD_cmd(0xc);					// display on / off and cursor
	LCD_clr();						// clear display
	}


void LCD_hex(int value)
	{
	char data;								// dispay hex values.
	data = value >> 4 & 0xf;				// send upper nibble
	data = HEX_Table[data]; 				// hex lookup
	LCD_char(data);
	data = value & 0xf;						// send lower nibble
	data = HEX_Table[data]; 				// hex lookup
	LCD_char(data);
	}

void LCD_goto(char line, char column)		// combines line and lineW
	{
	unsigned char data = 0x80;				// default to 1
	if(line == 2)data = 0xc0;				// change to 2
	data += column;							// add in  column
	LCD_cmd(data);
	}

void LCD_clr()
	{
	LCD_cmd(1);								// Clr screen
	}

void LCD_cur(char on)
	{
	unsigned char cur = 0xc;				// cursor off
	if(on) cur = 0xd;						// cursor on
	LCD_cmd(cur);
	}

void LCD_cmd(unsigned char ch)
	{
	LCD_PORT = ch >> 4 & 0xf;			// write high nibble
	LCD_RS = 0;
	pulse_E();
	LCD_PORT = ch & 0xf;				// write low nibble
	LCD_RS = 0;
	pulse_E();
	__delay_ms(5);
	}

void LCD_charD(unsigned char ch)
	{
	ch+=0x30;
	LCD_char(ch);						// convert to ascii
	}

void LCD_char(unsigned char ch)
	{
	LCD_PORT = ch >> 4 & 0xf;			// High nibble
	LCD_RS = 1;
	pulse_E();
	LCD_PORT = ch & 0xf;				// low nibble
	LCD_RS = 1;
	pulse_E();
	__delay_ms(5);
	}

void pulse_E()
	{
	LCD_E = 1;
	__delay_us(1);
	LCD_E = 0;
	}

Hserout() Hserin() and Hserinit()... This will output / input 9600 Baud at 20Mhz...
 
Hi,

I had removed some part......

Code:
 pulse_E(void), 

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
					// line 1.
	HSerinit();
	while(1)						// endless Loop
		{
		ch = HSerin();				// wait for a character
 
		
						// display the received byte
		HSerout(ch);				// Echo back
		index++;					// move on
		}
	}
 
void HSerinit()
	{
	TRISC = 0xC0;					// 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
 
	}
 
OK, i will remove that part also now please tell how to send data to Pc via uC and from PC to uc I am using terminal software linked in previous post!!
 
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 = 0xC0;					// 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
 
	}

A simple echo... Any character typed in hyper terminal will be echo'd back..
 
Hi,


I have connected the power supply of USB to uC board and the rx to tx and tx to rx of both!!
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top