• AVR Freaks

Hot!Serial communication between PIC16F877 and computer

Page: < 12345 Showing page 5 of 5
Author
nidac
Starting Member
  • Total Posts : 53
  • Reward points : 0
  • Joined: 2019/11/03 00:54:11
  • Location: 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/13 03:04:40 (permalink)
-1 (1)
@PStechPaul:
I compiled your file with MPLAB IDE 5.10 and XC8, BUILD SUCCESSFUL (total time: 657ms), but I can't find .hex file.
In serial.c folder, I don't find it.
Thanks.
 
#81
nidac
Starting Member
  • Total Posts : 53
  • Reward points : 0
  • Joined: 2019/11/03 00:54:11
  • Location: 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/13 03:16:15 (permalink)
-1 (1)
I find it.
#82
pcbbc
Super Member
  • Total Posts : 1388
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/13 03:26:01 (permalink)
+1 (1)
nidac
@pcbbc:
Please see attached file.
It is very clear that txstr function is wrong.

 
In your pointer version:
RCSTA=0b10010000;

In your printf version:
RCSTA = 0b10000000;

 
REGISTER 10-1:TXSTA: TRANSMIT STATUS AND CONTROL REGISTER (ADDRESS 98h)
bit 4SYNC: USART Mode Select bit
1 = Synchronous mode
0 = Asynchronous mode

 
You do understand the difference between synchronous and asynchronous mode and realise that...
SPBRG=25;

...selects completely different baud rates in synchronous vs asynchronous modes?
 
TABLE 10-1:BAUD RATE FORMULA
BRGH = 0 (Low Speed)
SYNC = 0 Asynchronous: Baud Rate = FOSC/(64(X+1))
SYNC = 1 Synchronous: Baud Rate = FOSC/(4(X+1))
BRGH = 1 (High Speed)
SYNC = 0 Asynchronous: Baud Rate = FOSC/(16(X+1))
SYNC = 1 Synchronous: N/A

 
Apologies I didn't spot this earlier.  But in my defence, no one else did either.
 
TBH most of us probably assumed you were following a very basic rule of debugging: Change as little as possible between versions.
 
If you arrive at a solution that works, go backwards until you find the change that broke it.  So if following that mantra, if you suspect a problem with the txstr function, the ONLY thing you should be changing is txstr to printf.  That's clearly not the case with the code you posted.
#83
nidac
Starting Member
  • Total Posts : 53
  • Reward points : 0
  • Joined: 2019/11/03 00:54:11
  • Location: 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/13 04:01:44 (permalink)
0
@PStechPaul:
Your code is OK(see attached file)!
The string "Hello world" appears when I power on the PIC or the reset button and waits for echoes from the keyboard That's what the code has to do.
Now I'll test @AMPS code(pointer method).
Thanks all.
 
 
#84
pcbbc
Super Member
  • Total Posts : 1388
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/13 04:22:56 (permalink)
+1 (1)
And lo, the txstr function required zero modification.
So what was the problem?
#85
nidac
Starting Member
  • Total Posts : 53
  • Reward points : 0
  • Joined: 2019/11/03 00:54:11
  • Location: 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/13 04:55:35 (permalink)
-1 (1)
@AMPS:
Your code give 2 compilation errors. I'll come back, I have to leave the office.
Thanks all.
#86
nidac
Starting Member
  • Total Posts : 53
  • Reward points : 0
  • Joined: 2019/11/03 00:54:11
  • Location: 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/13 08:08:04 (permalink)
0
@PStechPaul:
I compile your code and with xc8 v1.33.
#87
nidac
Starting Member
  • Total Posts : 53
  • Reward points : 0
  • Joined: 2019/11/03 00:54:11
  • Location: 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/13 08:17:27 (permalink)
-1 (1)
@pcbbc:
The compiler!?
#88
pcbbc
Super Member
  • Total Posts : 1388
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/13 08:29:20 (permalink)
+1 (1)
nidac
@pcbbc:
The compiler!?

So your code works regardless of if you set synchronous or asynchronous mode for the serial port, with SPBRG=25?
I highly doubt it.
#89
nidac
Starting Member
  • Total Posts : 53
  • Reward points : 0
  • Joined: 2019/11/03 00:54:11
  • Location: 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/13 10:34:36 (permalink)
0
@pcbbc:
Code with printf() sent continuously "Hello World!", no echo.
In "pointers code" if CREN = 0, I don't receive the echo from keyboard.
post edited by nidac - 2019/11/13 14:13:37
#90
PStechPaul
Super Member
  • Total Posts : 2446
  • Reward points : 0
  • Joined: 2006/06/27 16:11:32
  • Location: Cockeysville, MD, USA
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/13 16:22:52 (permalink)
+1 (1)
CREN must be set to receive:
10.2.2 USART ASYNCHRONOUS RECEIVER
...
Once Asynchronous mode is selected, reception is
enabled by setting bit CREN (RCSTA<4>).


 
#91
ric
Super Member
  • Total Posts : 24638
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/13 16:32:21 (permalink)
+1 (1)
pcbbc
nidac
@pcbbc:
The compiler!?

So your code works regardless of if you set synchronous or asynchronous mode for the serial port, with SPBRG=25?
I highly doubt it.

In post#83 you confused RCSTA and TXSTA.
As nidac stated, they have never set the SYNC bit in TXSTA.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#92
nidac
Starting Member
  • Total Posts : 53
  • Reward points : 0
  • Joined: 2019/11/03 00:54:11
  • Location: 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/13 22:26:10 (permalink)
0
@PStechPaul, @ric:
correct.
@pcbbc:
Look at the attached file, it is a code that has printf () and echo simultaneously. The code is compiled with HI-TECH C Compiler v. 7.86 PL3.
post edited by nidac - 2019/11/13 22:37:00
#93
ric
Super Member
  • Total Posts : 24638
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/13 22:42:08 (permalink)
+1 (1)
That output seems to be working fine.
Do you still have a problem?
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#94
nidac
Starting Member
  • Total Posts : 53
  • Reward points : 0
  • Joined: 2019/11/03 00:54:11
  • Location: 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/13 22:47:02 (permalink)
0
@ric:
No, I replay to your post #92.
#95
ric
Super Member
  • Total Posts : 24638
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/13 22:52:04 (permalink)
0
nidac
@ric:
No, I replay to your post #92.

Post#92 was addressed to pcbbc, as they had made a mistake interpreting your settings.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#96
nidac
Starting Member
  • Total Posts : 53
  • Reward points : 0
  • Joined: 2019/11/03 00:54:11
  • Location: 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/13 22:54:54 (permalink)
0
@ric:
OK
 
#97
AMPS
Super Member
  • Total Posts : 468
  • Reward points : 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/13 23:01:38 (permalink)
0
if u r getting answer . you should start working further , not to stick on same issue.
 
The Initialization can be done in several ways.All lead to same. if its working just proceed further.

TXSTA=0b00000100;
TXSTA=0X04;
in XC8 compiler
TX1STAbits.SYNC=0;
TX1STAbits.BRGH=0;
Hitech compiler
SYNC=0;
BRGH=1; 


Amps
*.*.*.*.*.*.*.*.*.*.*.*.*
#98
Page: < 12345 Showing page 5 of 5
Jump to:
© 2019 APG vNext Commercial Version 4.5