Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

UART interface between PC and PIC 16F877A

Status
Not open for further replies.

lcs81

Member level 3
Joined
Aug 2, 2005
Messages
57
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,683
Hi,

I am really a new to interfacing. Lot of thing dont understand. Hope your can guide me on this.

I am doing a project to interface the PIC and PC. The PC should send a data via C program from serial port and to PIC and PIC display it on the LCD screen.

For PIC to LCD, i already manage to setup up.

For the hardware between the PIC and PC also know what to do. But how can i program using C at the PC site to send data to pic and on the PIC, how i manage those data from PC?

Please guide me or bring me to useful reading material. TQ.
 

You need to read up on serial communication. I'm not a real C programmer, but on the PC side I know there are C methodologies for sending data serially. I think it depends on whether your using Linux or Windows, etc. On the PIC side, there is lots of info from Microchip about how to implement a serial link. You will need an RS-232 interface chip to convert the RS-232 voltages (+/- 12 V) to logic levels used by the PIC.
 
For PIC side, where can i find the protocol of interfacing with PC? Because I did try to find about UART interfacing. I am seeing lot of code using thing like "TXIE", "RCIE". I have no idea on this.

I mean i need to know how the pic know the data is comming from PC, and what the PIC going to deal with it?

Thanks
 

Do you intend on writing the PIC code in C?

If so, you can download the Hi-Tech C Compiler for the PIC16 series from the following link:

**broken link removed**

The above compiler is freely downloadable and will function in "Lite" mode with a serial number.

Study the user manual of the compiler for examples of configuring the UART for serial communication.


Jan Axelson's Site offer's a good resource for PC to PIC serial communications:

**broken link removed**

The following tutorial discusses the PIC18F series RS-232 communications, however the majority of the contents are directly applicable to the PIC16F series:

RS232 Communication using PIC18F4520′s USART – PIC Microcontroller Tutorial


BigDog
 

You need to establish some kind of message structure/protocol. The serial communications just handles transfer of bytes. It depends on what you are exactly trying to do.

For your stated case, the PC will send a message that is to be displayed on the PIC's LCD. If the displayed data is always ASCII characters, you can use a 'start byte' (something that falls outside the range of the alphanumeric characters of ASCII) to indicate the start of the message, then the display data and then an 'end byte' to indicate the end of the message. The PIC software would have to look for a 'start byte', gather the display data, etc.

This is just one example of how it can be done. If your messages are a fixed length, then you don't need an 'end byte', for example.
 

Hello

Hi,
But how can i program using C at the PC site to send data to pic and on the PIC, how i manage those data from PC?

To establish a link with a PC , you don't need at first, to program in C langage
you can use this software for a terminal RS232 on PC ( Windows ?)
https://sites.google.com/site/terminalbpp/
it' s free !
There is 12 touches to define Macros
you can put every data : Ascii, binary .. with the macro
you can use an internal timer for each macro to send data every xxx mS
or wahtever you want by using script.

For Hardware level translator between PIC Rx pin and Pin Tx from the (UART) PIC like TTL level
to RS232 level +-3V upt to +-12V , MAX232 or if you can find it in DIP8 case
the very easy to use DS275 , no capacitor needed.
 
  • Like
Reactions: lcs81

    lcs81

    Points: 2
    Helpful Answer Positive Rating
You need to read up on PIC hardware, without knowing how the USART module works you will never manage to drive it.

There is no 'protocol' as such, a protocol is a high level construct which you have to write code for, the PIC only understands "send a byte" and "receive a byte". For most purposes, including talking to a PC, this is all you need.

The best place for information is the 16F877A data sheet from the Microchip web site, it shows step-by-step instructions for send and receiving data. Basically, you set up the baud rate generator to match the speed your PC is using (for example 9600 bauds) then for transmitting from the PIC to the PC:

1. Check the transmit buffer is empty so you know the last byte has had time to be sent
2. Put the byte you want to send in the transmit buffer.
3. If there is more to send, go back to step 1.

For receiving from the PC:
1. check if the RCIF bit =1 in the interrupt register. This tells you a byte has been received.
2. read the byte from the RCREG register, which also clears the RCIF bit ready for next time.

You can either sit in a loop waiting for RCIF = 1 or set up an interrupt so your PIC can do something else while waiting for a byte to come.

Brian.
 
  • Like
Reactions: lcs81

    lcs81

    Points: 2
    Helpful Answer Positive Rating
Dear All,
I'm Facing a Problem with UART Communication between PIC and PC.I mentioned my System Spec,
1,PIC16F877A
2.4mHz
3.Mikro C for PIC
Here I Placed my Code.Kindly help to complete this.I Tried this with Proteus, It was working,While testing with the Actual Hardware Its sending data from PIC to PC.But Its Not Recieving my data (PC to PIC). Thanks in Advance.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
char read_data;
void main() {
adcon1=0x06;
adcon0=0x0f;
cmcon=0x07;
ccp1con=0;
ccp2con=0;
TRISC=0x80;
TRISB=0x02;
UART1_Init(9600);
Delay_ms(100);
UART1_Write_Text("s");
  while(1)
  {
 if (UART1_Data_Ready()==1)
    {
     UART1_Write_Text("A");
       read_data= UART1_Read();
       UART1_Write(read_data+1);
    }
       }
}

 
Last edited by a moderator:

You are opening a very old thread!

You will have to show a schematic to get a good answer. Code that works in a simulator but not in the real World tends to have a hardware problem. For example, what are you using to interface between the PIC and PC?

Brian.
 

Dear Brain,
Thanks for your Reply. I'm using profolic USB to Serial Converter for Communicating with PIC. Where as in my PIC Side I'm Using MAX 232 RS-232 to TTL Converetr IC. When I'm Testing the Loop Back Connection Upto the TTL end I'm getting the Eco. The PIC also sending the Value. The Problem is If I'm Sending Data from my PC The Function UART1_Data_ready() is not giving value 1 in my if Condition.
 

I understand - I've come across people forgetting the RS232-TTL conversion in the past and wanted to make sure you weren't doing the same.

I'm going to guess the problem is the TRISC settings, this is what the data sheet says:

"Bit SPEN (RCSTA<7>) and bits TRISC<7:6> have to be
set in order to configure pins RC6/TX/CK and RC7/RX/DT
as the Universal Synchronous Asynchronous Receiver
Transmitter."

You only have bit 7 of TRISC set.

Brian.
 

The baudrate at the PC side should be set properly. In mikroC Compiler go to Project>Edit Project... and set XT or HS according to you crystal speed. Set clock frequency properly.

Try these codes.

What does read_data+1 do? It should be read_data as you are echoing the received data which is single character. If you have enabled MCLR then you have to pull up MCLR pin. Also pull down MCUs Rx and Tx lines.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//Without ISR
 
char uart_rcv[32], uart_rd;
 
void main() {
 
    TRISC = 0x80;
    TRISB = 0x02;
    ADCON0 = 0x0F;
    ADCON1 = 0x06;
    CMCON = 0x07;
        
    UART1_Init(9600);
    Delay_ms(100);
 
    UART1_Write_Text("UART Test...");
 
    while(1) {
 
        if (UART1_Data_Ready()) {
            uart_rd = UART1_Read();         //Receive single character
            UART1_Write(uart_rd);           //Echo back received character
            UART1_Write(0x0D);
            UART1_Write(0x0A);
            UART1_Read_Text(uart_rcv, "\r\n", 32);  //Receive string
            strcat(uart_rcv, "\r\n");
            UART1_Write_Text(uart_rcv);     //Echo back received string       
            }
       }
}
 
 
//using ISR
 
char uart_rcv[32], uart_rd, i = 0;
 
void Interrupt() {
    if(RCIF_bit){
        uart_rcv[i++] = RCREG;
        uart_rcv[i] = '\0';
 
        RCIF_bit = 0;
    }
}
 
void main() {
 
    TRISC = 0x80;
    TRISB = 0x02;
    ADCON0 = 0x0F;
    ADCON1 = 0x06;
    CMCON = 0x07;
        
    UART1_Init(9600);
    Delay_ms(100);
 
    RCIE_bit = 1;
 
    UART1_Write_Text("UART Test...");
 
    while(1) {
 
        if(uart_rcv[0]){
            UART1_Write_Text(uart_rcv);
            UART1_Write(0x0D);
            UART1_Write(0x0A);
        }
            
        
       }
}




Working code. Receives data till "XX" is received. Change "XX" to "\r\n" if you send CR+LF from PC then it receives data till CR+LF is received and then echos back.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
typedef unsigned char u8int_t;
 
u8int_t uart_rcv[32], uart_rd, i = 0;
 
void Interrupt() {
    if(RCIF_bit){
        uart_rcv[i++] = RCREG;
        uart_rcv[i] = '\0';
 
        RCIF_bit = 0;
        }
}
 
void main() {
 
    TRISC = 0x80;
    TRISB = 0x02;
    ADCON0 = 0x0F;
    ADCON1 = 0x06;
    CMCON = 0x07;
    INTCON = 0xC0;
 
    UART1_Init(9600);
    Delay_ms(100);
 
    RCIE_bit = 1;
 
    UART1_Write_Text("UART Test...");
    UART1_Write(0x0D);
    UART1_Write(0x0A);
 
    while(1) {
 
        if((uart_rcv[i - 2] == 'X') && (uart_rcv[i - 1] == 'X')){
                UART1_Write(0x0D);
            UART1_Write(0x0A);
                UART1_Write_Text(uart_rcv);
                        uart_rcv[i - 2] = '\0';
                        uart_rcv[i - 1] = '\0';
                }
       }
}

 
Last edited:

Dear Jayanth,
Thanks for your Reply. I have tried your suggestions. As I checked my Clock OSC Config. I configured as XT OSC, I Also Tried with HS OSC. Still my problem in that condition only. In Your Reply You mentioned that MCLR pull down and RX TX Pull Down. Kindly Explain it Briefly.my UART1_Data_Ready() is enabling every my first Run, So Its displays my data in the if statement at very first run of my PIC.After the iteration I'm not getting that function is give 1,even I I send data to my serial Port. I Also Tested the Baud rate of PC well.
 

hello,


Maybe your transmit buffer is not empty just after power on.
and after you get this kind of Uart error :

Code:
FERR: Framing Error bit
1 = Framing error (can be updated by reading RCREG register and receive next valid byte)
0 = No framing error
bit 1 OERR
 Overrun Error bit
1 = Overrun error (can be cleared by clearing bit CREN)
0 = No overrun error

if this error flags goes "ON",you will be stuck inside your program..
impossible to go out of the loop
without treat the error ..

check these flags in your program, you can affect outputs with leds to show if errord occured or not.

read 16F877A datasheet an details on UART

example of error treatment (to adapt with your MCU )

Code:
......
  c1 =Read1USART(); // le lire => RAZ  RCIF
  if(RCSTA1bits.FERR==1 )
   {
     RCSTA1bits.SPEN = 0 ;
     RCSTA1bits.SPEN= 1 ;
     c1=0;
      }
   if (RCSTA1bits.OERR==1)    // voir parag 16.1.26 p273
   {
       RCSTA1bits.CREN = 0 ;
       RCSTA1bits.CREN = 1 ;
     c1=0;
     } 
 .....
 

Dear All,
I'm Facing a Problem with UART Communication between PIC and PC.I mentioned my System Spec,
1,PIC16F877A
2.4mHz
3.Mikro C for PIC
Here I Placed my Code.Kindly help to complete this.I Tried this with Proteus, It was working,While testing with the Actual Hardware Its sending data from PIC to PC.But Its Not Recieving my data (PC to PIC). Thanks in Advance.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
char read_data;
void main() {
adcon1=0x06;
adcon0=0x0f;
cmcon=0x07;
ccp1con=0;
ccp2con=0;
TRISC=0x80;
TRISB=0x02;
UART1_Init(9600);
Delay_ms(100);
UART1_Write_Text("s");
  while(1)
  {
 if (UART1_Data_Ready()==1)
    {
     UART1_Write_Text("A");
       read_data= UART1_Read();
       UART1_Write(read_data+1);
    }
       }
}


Greetings,
Here are some good tutorials which might help you debug your program:
- https://microcontrollerprojects00.blogspot.in/2012/03/pic-serial-communication-tutorial-uart.html
- https://bharatbalar.wordpress.com/2012/02/17/hi-tech_c_code_for_usart_of_pic16f877a/

Good Luck..
 

Dear tushki7,
Thanks for link. It was Very helpful to understand some key points regarding my application and issues.

Dear All,
I have a clarification about my configuration bit selection. While I'm using Hitech C Compiler, I use __CONFIG(0x3F3A) as my configuration register value. Where as in my mikro C its Shows like 0x2F4A, Is there any difference on these both. But I checked the each bit configuration between mikro C and Hitech C. Kindly clarify my Doubt.
 

You should learn to walk before you start to run!
First of all get your serial interface working, connect the RX and TX lines together and use Hyper terminal or real terminal to check that you can communicate via the serial port, if this is successful then connect the serial lines to your PIC and use the terminal program to send data to it, if and when that works you can then try and communicate to the PIC with your C program.
To try and write a C program and expect it to work is a bit optimistic as there are many things that can go wrong and you don't know if it is your program or not.
 

Dear Pjmelect,
I already Tested those Loop back Connection Testings,Those Things are working fine. In my PIC Also Its Transmitting data to serial port. The thing iss its not recieving the data whatever i sent from the PC. This the Issue I'm Facing with my System. Thanks for your Reply.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top