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)
@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.
|
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)
|
pcbbc
Super Member
- Total Posts : 1381
- 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)
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.
|
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)
@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.
|
pcbbc
Super Member
- Total Posts : 1381
- 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)
And lo, the txstr function required zero modification. So what was the problem?
|
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)
@AMPS: Your code give 2 compilation errors. I'll come back, I have to leave the office. Thanks all.
|
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)
@PStechPaul: I compile your code and with xc8 v1.33.
|
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)
|
pcbbc
Super Member
- Total Posts : 1381
- 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)
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.
|
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)
@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
|
PStechPaul
Super Member
- Total Posts : 2437
- 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)
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>).
|
ric
Super Member
- Total Posts : 24579
- Reward points : 0
- Joined: 2003/11/07 12:41:26
- Location: Australia, Melbourne
- Status: online
Re:Serial communication between PIC16F877 and computer
2019/11/13 16:32:21
(permalink)
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.
To get a useful answer, always state which PIC you are using!
|
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)
@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
|
ric
Super Member
- Total Posts : 24579
- Reward points : 0
- Joined: 2003/11/07 12:41:26
- Location: Australia, Melbourne
- Status: online
Re:Serial communication between PIC16F877 and computer
2019/11/13 22:42:08
(permalink)
That output seems to be working fine. Do you still have a problem?
To get a useful answer, always state which PIC you are using!
|
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)
@ric: No, I replay to your post #92.
|
ric
Super Member
- Total Posts : 24579
- Reward points : 0
- Joined: 2003/11/07 12:41:26
- Location: Australia, Melbourne
- Status: online
Re:Serial communication between PIC16F877 and computer
2019/11/13 22:52:04
(permalink)
nidac @ric: No, I replay to your post #92.
Post#92 was addressed to pcbbc, as they had made a mistake interpreting your settings.
To get a useful answer, always state which PIC you are using!
|
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)
|
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)
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 *.*.*.*.*.*.*.*.*.*.*.*.*
|