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.
Remember my code was for a 4Mhz crystal (XT) originally 0x3F71... You have a 20Mhz (HS) crystal 0x3f72..

But you have mention 20mhz only in code not 4mhz....
and the SPBRG value is 129 for 20mhz ....so, what is the problem in it??


Are you using P3.6 and P3.7 on the PIC?

can you expand this i am not getting it, is this portc7???
 
Are you using P3.6 and P3.7 on the PIC?

Whoops.. a bit of Intel in there.... Yes I mean RC6 and RC7...

I'm doing too much.

You changed the crystal setting not me.... When I wrote the code it was for a 4Mhz like I said....
 
Are you using P3.6 and P3.7 on the PIC?

Yes, connected with USB module RX and TX....

ok, here is the code you posted
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
 
	}

so, i have to make it at 20mhz or 4mhz b'coz as you said __CONFIG(0x3F71); for 4mhz but in code 20mhz is mention...plus is thi s fine
TRISC = 0xC0; ?? or it should be changed to 0b10000000
 
that mean this is correct TRISC = 0xC0;, i am saying b'coz many people on forum are pointing this...
and can you tell me about hardware any suggestion as i have alreday posted the schematic...
 
It's correct..... Have you got a different USB -> TTL convertor.

No, i am using that only and as i said when i short the RX and TX of USB module it work fine i.e. echo back the character.....
then what is the problem in hardware should i connect low value capacitor to rx and tx from gnd for filtering??
 
I don't think you two read my post so I'll say it agin I complied this with XC8 and it doesn't work it complies fine it even debug ok but the Hex doesn't match for some reason. It's not making a Hex with all the goodies in it. I didn't have time to play with it much. But that maybe happening here.
 
I under stand you wrote it in hitech XC8 should work fine using the code and it does but the Hex is useless for some reason the code test fine in Mpsim. I just saying maybe the OP changed to XC8 as the compiler
and its not doing something right. Mine is not for some reason.
 
No, i am also using hi tech C...
Did you change the config settings? If you try the code as is you need a 4Mhz crystal.
OK, i was using it at 20mhz and its setting was also 20Mhz
are you saying taht i should use it at 4MHz then tell what changes is to be done in SBRG register also...
 
I just compiled this in XC8.... It works as expected.

C:
#include <xc.h>				// pic specific identifiers
#define _XTAL_FREQ  20000000		// Xtal speed
#pragma config CONFIG = 0xFF72
 
		// 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
 
	}
 
OK, In Mp Lab showing error......

don't know the problem in it??

Code:
#include <htc.h>				// pic specific identifiers
#define _XTAL_FREQ  20000000		// Xtal speed
#pragma config CONFIG = 0xFF72

Make: The target "C:\Users\Abc\Documents\mplab\uarttest.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\uarttest.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"
Executing: "C:\Program Files (x86)\HI-TECH Software\PICC\9.83\bin\picc.exe" -ouarttest.C.cof -muarttest.C.map --summary=default --output=default uarttest.p1 --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"
HI-TECH C Compiler for PIC10/12/16 MCUs (Lite Mode) V9.83
Copyright (C) 2011 Microchip Technology Inc.
(1273) Omniscient Code Generation not available in Lite mode (warning)
Error [715] C:\Users\Abc\Documents\mplab\uarttest.C; 3. bad pragma "x"
Error [715] C:\Users\Abc\Documents\mplab\uarttest.C; 3. bad pragma "0"
 
No No No!!! The code was for XC8 to prove to Burt.... You need the old code "__CONFIG(0x3F72);"

I said, and I quote
I just compiled this in XC8.... It works as expected.
This was aimed solely for Burt...
 
OK, it is working but few error are there i don't know them...
it is working now thanks...
but i found some problem it display 'a' also but some times other default character , why??


Code:
unsigned char  HSerin(void);
 void HSerout(unsigned char ch),
 HSerinit(void); 

 void main(void)                        // program entry  
   {     
   int index = 0;  
   unsigned char ch = 'a';         // <- LOOK HERE.
   ADCON1 = 0x6;                    // Analogue off    
   HSerinit();      
  __delay_ms(150);    
 while(1)                        // endless Loop       
  {           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   
  }



this work fine..
 

Attachments

  • 1.jpg
    1.jpg
    283.6 KB · Views: 187
  • 2.jpg
    2.jpg
    291.7 KB · Views: 166
  • 3.jpg
    3.jpg
    285.8 KB · Views: 187
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top