• AVR Freaks

Hot!Serial communication between PIC16F877 and computer

Page: < 12345 > Showing page 2 of 5
Author
1and0
Access is Denied
  • Total Posts : 9989
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/10 11:51:38 (permalink)
+1 (1)
nidac
Yes, I want to use it with command line, but I do not know(find) the compilation directive (or user manual).

Then read the XC8 User's Guide (located in your install folder) on how to invoke the compiler. Much easier with MPLAB. ;)
 
#21
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11425
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/10 11:55:00 (permalink)
+1 (1)
You can see what command line the IDE is generating in the output window.
#22
1and0
Access is Denied
  • Total Posts : 9989
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/10 12:14:06 (permalink)
0
jtemples
You can see what command line the IDE is generating in the output window.

nidac
...without MPLAB-IDE.

I was under the impression MPLAB was not even installed and used. Haha!
 
#23
ric
Super Member
  • Total Posts : 24593
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/10 12:14:32 (permalink)
+1 (1)
nidac
Thanks @ric.
I want to use XC8 now, but only the compiler (without MPLAB). Do you know how to launch and what is the compilation directive?

Do you enjoy doing things the hard way?
Yes, it is possible to invoke the compiler on the command line, but you're going to have to research a lot of arcane command line options to get it working right.
 
 

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!
#24
PStechPaul
Super Member
  • Total Posts : 2441
  • 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/10 13:28:13 (permalink)
0
Commander Unconvincible? mr green: mr green
 
If MPLABX is too unwieldy, perhaps due to an ancient computer, it might be advisable to install MPLAB 8.92 and use an earlier version of XC8, at least long enough to see the command line for XC8.

 
#25
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/10 15:44:13 (permalink)
0
Thanks to all.
In the original code (EmbetroniX), the first lines were:
 
#include <htc.h>
__CONFIG (FOSC_HS & WDTE_OFF & PWRTE_OFF & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF & WRT_OFF & DEBUG_OFF);

1. What compiler do you think was used?
2. If I install MPLAB8.92 what earlier version of xc8 can I install.
#26
ric
Super Member
  • Total Posts : 24593
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/10 16:04:34 (permalink)
+1 (1)
That syntax would be from later versions of HiTech C, before it was bought by Microchip and renamed to XC8.
Simply changing
#include <htc.h>
to
#include <xc.h>
Should be enough to let it compile in XC8 v1.xx, or v2.xx when in "C90" mode.
 
You can download MPLAB v8.92 from https://www.microchip.com/development-tools/pic-and-dspic-downloads-archive
XC8 version 1.33 (available on the same page) is the last version that knows how to install itself into MPLAB8, but it is possible to run later 1.xx versions as described here: How to install XC8 version 1.34 and later into MPLAB8.
 

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!
#27
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/11 00:36:46 (permalink)
0
Thanks @PStechPaul, @ric.
Because I think the code was written for HI-TECH PICC Lite Compiler (see #include <htc.h>), I installed this compiler and then MPLAB v. 8.92. I selected the project and the compiler in MPLAB and did the compilation:
 
Executing: "C:\Program Files\HI-TECH Software\PICC-Lite\9.50\BIN\PICL.EXE" -C -E"serial.cce" "C:\Desktop\serial\serial.c" -O"serial.obj" -Zg9 -O -ASMLIST -Q -MPLAB -16F877A
Executing: "C:\Program Files\HI-TECH Software\PICC-Lite\9.50\BIN\PICL.EXE" -E"serial.lde" "C:\Desktop\serial\serial.obj" -M"serial.map" -O"serial.cof" -O"serial.hex" -Q -MPLAB -16F877A
Loaded D:\TEST\SERIAL\serial.cof.
BUILD SUCCEEDED: Mon Nov 11 08:47:13 2019

The resulting HEX echoes me but does not show me the desired text in the source code.
I really don't know what this source code can have.
Do you have any ideas?
Thank you again.
 
Actual code(compilled) is:
#include <htc.h>
#include "delay.h"
#include "delay.c"
//#include <stdio.h>
__CONFIG(0xd06);
void ser_int();
void tx(unsigned char);
unsigned char rx();
void txstr(unsigned char *);
void main()
{
    TRISC6=0;
    TRISC7=1;
    ser_int();
    txstr("Hello World\n\r");
    while(1)
    {
            tx(rx());
    }
}
void ser_int()
{
    TXSTA=0b00100100;    //BRGH=0, TXEN = 1, Asynchronous Mode, 8-bit mode
    RCSTA=0b10010000; //Serial Port enabled,8-bit reception
    SPBRG=25;            //9600 baudrate for 11.0592Mhz
    DelayMs(10);
}
void tx(unsigned char a)
{
    //TXREG=a;
    while(!TXIF);
    TXREG = a;
}
unsigned char rx()
{
    while(!RCIF);
    //RCIF=0;
    return RCREG;
}
void txstr(unsigned char *s)
{
    while(*s)
    {
        tx(*s++);
    }
}


#28
ric
Super Member
  • Total Posts : 24593
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/11 01:08:03 (permalink)
+1 (1)
nidac
...
The resulting HEX echoes me

What does this mean?
 

but does not show me the desired text in the source code.

I assume you mean you are not receiving the expected "Hello world" text.
 

I really don't know what this source code can have.

What does this mean?
 
What PRECISELY do you have connected between the PIC's TX pin and your PC?
Did you read the earlier posts in this topic, talking about the need for a TTL to RS232 converter, assiming you are using a USB-RS232 adaptor on your PC.

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!
#29
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/11 01:20:55 (permalink)
0
Thanks @ric for reply.
1. I have keyboard echo, the code return the character from keyboard.
2. Yes.
3. I don't know why this code not send "Hello World" string to serial interface.
On my PC I see some unknown characters(I use TERMINAL.exe software).
#30
ric
Super Member
  • Total Posts : 24593
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/11 01:28:57 (permalink)
0
I suspect the terminal proram itself might be providing that echo.
What happens if you totally disconnect the PIC?
 
The comment in the code says "9600 baudrate for 11.0592Mhz"
Do you actually have a 11.0592MHz crystal connected to your PIC?
 
 

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!
#31
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/11 01:46:19 (permalink)
0
1. If I disconnect the PIC from serial interface (TTL to USB, CH340), I don't have echo.
2. Now I have Q = 4Mhz crystal.
The code where I use the printf () function works with the same hardware.
I would like to use pointers for sending strings , but...
post edited by nidac - 2019/11/11 01:48:30
#32
AMPS
Super Member
  • Total Posts : 468
  • Reward points : 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/11 01:49:08 (permalink)
0
Here is code i have written long back on PIC16F886 with 8Mhz Crystal. I will recommend to test the code for evaluation. If using 11.0592 kindly change SPBRG  INIT_UART function based on Datasheet . i have used 9600 baudrate.
 
I will recommend to Docklight software and then try to send and receive character . if u getting any values then share screen shot of it.
 
if other saying it might be other issue we would like to see the Screenshot  terminal/Docklight software output when hardware is connected.
 
 
 

 
#include <htc.h>
#include <stdio.h>
#include<pic.h>
#include<stdint.h>
#include"delay.h"
__CONFIG( FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF & DEBUG_OFF&MCLRE_ON &BOREN_ON&FCMEN_ON &WRT_OFF);
 
#define _XTAL_FREQ 8000000
unsigned char get_value;
unsigned long baudRate=9600;
unsigned char c=0;
unsigned char ch;
 
#define LED RC5
#define LED_RX RC7 // Pin assigned RX LED
#define LED_TX RC6 // Pin assigned TX LED
#define DE RB0
 
#define READ_REG 1
#define METER_ID 1
unsigned int cnt, num,Dgt=0;
unsigned char j;
unsigned int i=0;
 

void Serial_1_Send_byte(uint8_t trbuf1) {
TXREG=trbuf1;
while(0==TRMT);
while(0==TXIF);
TXIF=0;
 
 
 
}
 
char Serial_Receive_byte() {
 

while(RCIF==0); // Wait till the data is received
RCIF=0; // Clear receiver flag
return(RCREG); // Return the received data to calling function
 
}
 

void Send_string_uart1(const unsigned char *string) {
unsigned char k=0;
 
do {
TXREG=string[k++];
while(TXIF==0);
TXIF=0;
} while(string[k]!='\0');
}
 
void interrupt isr(void) {
unsigned char ch;
 

Serial_Interrupt();
if(TMR1IF ==1) {
 
TMR1IF=0;
}
 

}
 
 
 

void Timer1_Interrupt() {
 
OPTION_REG=0b00110000;
INTCON = 0b00000000;
PIE1=0b00000001;
PIR1=0x01;
TMR1H=0xFD;
TMR1L=0xE8;
T1CON=0x21;
 
}
 
 
 
void Init_Controller() {
OSCCON=0X38;
OSCTUNE = 0x00;
TRISC = 0x80;
PORTC = 0x80;
 
TRISB=0X00;
PORTB = 0X00;
 
TRISA=0xC0;
PORTA=0x0c0;
 
ADCON0= 0x00;
ANSEL = 0b00000000;
DE=0;
Timer1_Interrupt();
 
}
 
 
 
void InitUART(void) {
unsigned int BAUDRATE=9600;
 
TRISC6 = 1; // TX Pin
TRISC7 = 1; // RX Pin
BRG16=1;
BRGH = 1; // Fast baudrate
SYNC = 0; // Asynchronous
SPBRG =207;
SPEN = 1; // Enable serial port pins
CREN = 1; // Enable reception
SREN = 0; // No effect
TXIE = 0; // Disable tx interrupts
RCIE = 1; // Enable rx interrupts
TX9 = 0; // 8-bit transmission
RX9 = 0; // 8-bit reception
TXEN = 0; // Reset transmitter
TXEN = 1; // Enable the transmitter
}
 
 
 
void main(void) {
 
Init_Controller();
InitUART();
GIE=1;
PEIE=1;
TMR1IE=1;
 
while(1) {
 
 
 
}
 

}
 
 
 

/* Testing Functions*/
 

/*
CASE1:
Serial_1_Send_byte(c);
c++;
__delay_ms(500);
*/
 

/*
CASE2:
ser_data[0]=15;
Serial_1_Send_byte(ser_data[0]);
*/
 

/*
CASE3:
Send_string_uart1("Welcome back");
*/
 
/*
CASE4:
for (j=0;j<=8;j++)
{
rxbuf[j] = Serial_Receive_byte();
Serial_1_Send_byte(rxbuf[j]);
j++;
 
}
 
*/
 
 
 

 
post edited by AMPS - 2019/11/11 01:51:44

Amps
*.*.*.*.*.*.*.*.*.*.*.*.*
#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/11 02:03:13 (permalink)
0
Thanks @AMPS.
To @ric:
Please see attached file.
There you can clearly see the echo characters and the unknown character that is transmitted in place of the string: Hello World.
#34
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/11 02:07:36 (permalink)
0
I will miss 2 or 3 hours from the office ...
#35
ric
Super Member
  • Total Posts : 24593
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/11 02:39:45 (permalink)
+1 (1)
That code should NOT be echoing.
Do you have a ground connection between the PIC and the CH340 adaptor?
 

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!
#36
oliverb
Super Member
  • Total Posts : 243
  • Reward points : 0
  • Joined: 2009/02/16 13:12:38
  • Location: 0
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/11 03:27:20 (permalink)
+1 (1)
Please could you describe the adapter you are using?
Have you tried carrying out a simple loopback test, linking TX and RX on the adapter
Are you using a "USB to TTL serial" adapter
Do you have a GROUND connection?
What restrictions are you working under? Are you required to use certain specific tools?
 
#37
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/11 03:46:06 (permalink)
+1 (1)
ricI suspect the terminal program itself might be providing that echo.

That's a good point and could well be another cause of this problem, if taken in isolation.
 
However surely the code in post 11 by the OP was working?  After all they supplied a screen shot of "Hello World!" repeatedly in the terminal window...
I've no idea why it didn't occur to them that it was displaying multiple times, despite only being output once from the code they posted.  Perhaps they are from Arduino-land and so used to an autorepeating "loop()" function?
 
And their originally posted program was reportedly working for echo, but apparently not sending the initial string (although I'd agree, if take on it's own, that could have been local echo as you say).
 
But all of the above, and the fact that the working code in post 11 is missing an infinite loop (so the PIC will be constantly resetting and constantly sending the string), points me to only one reasonable conclusion:
The original code was not working because the PIC sent the string as soon as it powered up.  The receiving end was not connected, or was not ready.  However by the time the code got to the echo part, then the terminal was connected at the host/PC end, and so apparently "started working".
 
I asked in post 10 how everything was connected, and at what point everything was being powered up.  That, or proper flow control/error checking, is crucial in an application that only sends something once.  Unfortunately I didn't receive a reply.
#38
ric
Super Member
  • Total Posts : 24593
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re:Serial communication between PIC16F877 and computer 2019/11/11 04:15:44 (permalink)
+1 (1)
pcbbc
ricI suspect the terminal program itself might be providing that echo.

That's a good point and could well be another cause of this problem, if taken in isolation.

Nahh, I can now see the OP is using Victor Bray's Terminal program.
That always shows local echo in the lower window, and received characters in the upper window, so there's no chance of confusion.
 
Your following points are valid, and we need the OP's input to confirm now.
I do suspect they also have a bad ground connection.
 

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!
#39
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/11 04:16:42 (permalink)
0
@pcbbc:
PIC is power on by USB port and it has a reset button.
After power on I reset hardware but first character(string) is unknown, please see my 34 post attachement.
#40
Page: < 12345 > Showing page 2 of 5
Jump to:
© 2019 APG vNext Commercial Version 4.5