• AVR Freaks

Hot!Serial communication between PIC16F877 and computer

Page: 12345 > Showing page 1 of 5
Author
Phil93
New Member
  • Total Posts : 2
  • Reward points : 0
  • Joined: 2011/10/04 02:29:42
  • Location: Padova, Italy
  • Status: offline
2011/10/10 12:08:14 (permalink)
0

Serial communication between PIC16F877 and computer

Hi there. I'm a (very) newbie in PIC microcontrollers and I need help interfacing a 16F877 with a pc using RS232 serial communication. How can it be done? I read some existing threads here but I didn't find so much help. I specify that I've never had to deal with serial ports and my knowledge of PIC micros is only scholastic, so I would appreciate some basic guides and examples. The project I wanna realize is composed by some sensors that read physical quantities such as temperature, humidity, pressure, ect, that are sent to the computer by the microcontroller. The transmission, therefore, is only from PIC to PC. The programs that runs into the computer and acquires the data is written in Microsoft Visual C#. I program PIC in both C or ASM.
 
I sincerely hope in your help, cause I actually don't know where to start...
Thank you!
#1

97 Replies Related Threads

    DarioG
    Allmächtig.
    • Total Posts : 54081
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: offline
    Re:Serial communication between PIC16F877 and computer 2011/10/10 12:16:53 (permalink)
    0
    First, hardware: you need a TTL to RS232 levels converter, such as MAX232.
    Second, software: study the USART in your PIC - or better, first start with some tutorial about blinking one led etc, so that you get used to Clocking, power, ports etc.

    GENOVA :D :D ! GODO
    #2
    WaltR
    Super Member
    • Total Posts : 3759
    • Reward points : 0
    • Joined: 2003/11/07 12:38:21
    • Status: offline
    Re:Serial communication between PIC16F877 and computer 2011/10/10 12:46:08 (permalink)
    0
    See Microchip App Notes: AN774 with example code to learn how to use a PIC's UART. Refer to the PIC Demo board fro the 16F877 for schematics on connecting the PIC to a PC.
    http://www.microchip.com/...&dDocName=en010072
    #3
    Phil93
    New Member
    • Total Posts : 2
    • Reward points : 0
    • Joined: 2011/10/04 02:29:42
    • Location: Padova, Italy
    • Status: offline
    Re:Serial communication between PIC16F877 and computer 2011/10/11 02:36:53 (permalink)
    0
    Thank you very much for the answers.

    Ok, so I need a TTL - RS232 converter to interface micro (TTL) and PC (RS232) right? Now I read the MC app notes and I'll let you know.
    #4
    nidac
    Starting Member
    • Total Posts : 49
    • Reward points : 0
    • Joined: 2019/11/03 00:54:11
    • Location: 0
    • Status: online
    Re:Serial communication between PIC16F877 and computer 2019/11/06 22:49:45 (permalink)
    0
    I want to use the program below to send messages from the USART pic PIC16f877 to the PC, in another larger program. The program is found on the Internet and also makes the return of any character from the keyboard, through echo.
    I did the compilation with HT-PIC 7.85 PL3, there are no errors, the keyboard echoes, but the message "(EmbeTronicX): Enter the letters in keyboard" is not displayed, instead an unknown character is displayed.
    I will be grateful for an answer.
    Thank you in advance.

    #include <pic.h>
    __CONFIG(0x3F36);
    void ser_int();
    void tx(unsigned char);
    unsigned char rx();
    void txstr(unsigned char *);
    void main()
    {
    TRISC6=TRISC7=1;
    ser_int();
    txstr("(EmbeTronicX): Enter the letters in keyboard\n\r\r");
    while(1)
    {
      tx(rx());
    }
    }
    void ser_int()
    {
    TXSTA=0x20; //BRGH=0, TXEN = 1, Asynchronous Mode, 8-bit mode
    RCSTA=0b10010000; //Serial Port enabled,8-bit reception
    SPBRG=17;   //9600 baudrate for 11.0592Mhz
    TXIF=RCIF=0;
    }
    void tx(unsigned char a)
    {
    TXREG=a;
    while(!TXIF);
    TXIF = 0;
    }
    unsigned char rx()
    {
    while(!RCIF);
    RCIF=0;
    return RCREG;
    }
    void txstr(unsigned char *s)
    {
    while(*s)
    {
      tx(*s++);
    }
    }
    #5
    pcbbc
    Super Member
    • Total Posts : 1373
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: online
    Re:Serial communication between PIC16F877 and computer 2019/11/07 10:40:20 (permalink)
    +1 (1)
    Is there a reason you dug up an 8 year old thread to ask this question, rather than starting a new thread?
    No matter...
     
    Try adding a delay when your PIC starts up after you have configured the serial port, but before you send the prompt string.
     
    I'm going to assume for now the device that you have at the other end (PC?) is quick enough to be able to receive the message without any flow control.
    #6
    ric
    Super Member
    • Total Posts : 24202
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re:Serial communication between PIC16F877 and computer 2019/11/07 12:33:14 (permalink)
    +1 (1)
    nidac
    ...
    I did the compilation with HT-PIC 7.85 PL3

    Why use an ancient compiler that was discontinued years ago?
    XC8 is the compiler available from Microchip for PIC16F devices, and the free version is perfectly adequate for doing most projects.
     

    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!
    #7
    nidac
    Starting Member
    • Total Posts : 49
    • Reward points : 0
    • Joined: 2019/11/03 00:54:11
    • Location: 0
    • Status: online
    Re:Serial communication between PIC16F877 and computer 2019/11/08 06:30:37 (permalink)
    0
    Thanks @pcbbc, @ric.
    I inserted a new line ((DelayMs (1); ... DelayMs (10);), after the line: TXIF = RCIF = 0; but no change: the message is not displayed.
    I tried another solution:
     
    #include <pic.h>
    #include <stdio.h>
    #include "delay.h"
    #include "delay.c"
    __CONFIG(0xd06);
    void main()
    {
            SPBRG = 25;
            TXSTA = 0b00100100; //transmit enable, async, high speed mode
            RCSTA = 0b10000000; //serial port enable
            DelayMs(100);
            printf("Hello World!");
    }
    void putch(char data)
    {
            while(TRMT)
            {
                   TXREG = data;
            }
    }


    But it sends to PC only the first character (H) in the entire string (Hello World!).
    I wait new suggestions.
    Thanks.
    #8
    NKurzman
    A Guy on the Net
    • Total Posts : 17916
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re:Serial communication between PIC16F877 and computer 2019/11/08 06:48:49 (permalink)
    +1 (1)
    You need a while(1);
    after the printf() line. But that is not the issue.

    Your putchar function looks wrong to me.
    The while should be separate from the assign.
    #9
    pcbbc
    Super Member
    • Total Posts : 1373
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: online
    Re:Serial communication between PIC16F877 and computer 2019/11/08 07:47:14 (permalink)
    +1 (1)
    NKurzman
    You need a while(1);
    after the printf() line. But that is not the issue.

    Your putchar function looks wrong to me.
    The while should be separate from the assign.

    Indeed.  Your code was better the first time.
     
    You also need to understand the difference between the flags TRMT and TXIF.
     
    TRMT: Transmit Shift Register Status bit
    1 = TSR empty
    0 = TSR full
     
    TXIF: USART Transmit Interrupt Flag bit
    1 = The USART transmit buffer is empty
    0 = The USART transmit buffer is full
     
    They are not the same.
    TRMT = 1 says that the transmission is complete.
    TXIF = 1 says that there is space in the buffer for another byte (the current byte may still being sent, see TRMT).
     
    Either of these should work for you.  The first example is better because it always has the next byte placed in the buffer ready to send.  The second example sends staccato style with little gaps in between:
    void tx(unsigned char c)
    {
        while(TXIF == 0);
        TXREG = c;
    }

    void tx(unsigned char c)
    {
        while(TRMT == 0);
        TXREG = c;
    }

     
    Also you cannot clear TXIF (or TRMT either) with software.  These flags are set/cleared by the hardware automatically depending on the state of the buffer and shift register.  If you try to write to these flags (as you did in the first example) it will have no effect.
     
    How are you powering the PIC?  How are you connecting it to the serial port of the PC?
    What is the timing of powering the PIC, connecting to the PC via serial, and starting whatever terminal emulator or program you are using to watch for results at the PC end?
    In your example the PIC will start sending as soon as it powers up.  If it's not connected to the PC, or the PC is not ready to receive, it will not wait.  There is not default flow control.  Everything you send if the other end is not connected or ready will get lost.
     
    Either have a key you press on the PIC side to start transmission, or wait for some incoming data from the PC.  For example CR and/or LF.
    #10
    nidac
    Starting Member
    • Total Posts : 49
    • Reward points : 0
    • Joined: 2019/11/03 00:54:11
    • Location: 0
    • Status: online
    Re:Serial communication between PIC16F877 and computer 2019/11/08 08:36:27 (permalink)
    0
    So, the final code(last code) is:
     
    #include <pic.h>
    #include <stdio.h>
    #include "delay.h"
    #include "delay.c"
    __CONFIG(0x3F32);//Q=4Mhz;16F877A
    void main()
    {
            SPBRG = 25;
            TXSTA = 0b00100100; //transmit enable, async, high speed mode
            RCSTA = 0b10000000; //serial port enable
            DelayMs(100);
            printf("    Hello World!\n\r");
    }

    void putch(char data)
    {
            while(!TRMT);
                TXREG = data;
    }
    uchar getch(void)
    {
        if (OERR)
            CREN=0;
            CREN=1;
        while(!RCIF);
        return (RCREG);
    }
     
    Thanks all for suggestions.
    post edited by nidac - 2019/11/08 09:04:40

    Attached Image(s)

    #11
    pcbbc
    Super Member
    • Total Posts : 1373
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: online
    Re:Serial communication between PIC16F877 and computer 2019/11/08 10:56:58 (permalink)
    +2 (2)
    So you are working now?
    What did you do to fix it?
    I suspect it was removing the echo routine at the end of main - see point 1 below.
     
    There still seem to be some errors and mistakes...
    1. Your main is missing an infinite loop.  The PIC will crash and restart when it reaches the end of main.
    2. You are missing a { } code block around your CREN = 0; CREN = 1; statements.  C is not Python.  It will work, but only because CREN = 1 being set on every TX does not matter.
    3. So you ignored my advice of using TXIF over TRMT - fair enough.
    4. Please learn how to put [​code]//code tags[​/code]  to format you code correctly.
     
    #include <pic.h>
    #include <stdio.h>
    #include "delay.h"
    #include "delay.c"
    __CONFIG(0x3F32);//Q=4Mhz;16F877A
    void main()
    {
            SPBRG = 25;
            TXSTA = 0b00100100; //transmit enable, async, high speed mode
            RCSTA = 0b10000000; //serial port enable
            while (1) {
                DelayMs(100);
                printf("    Hello World!\n\r");
            }
    }

    void putch(char data)
    {
            while(!TXIF);
            TXREG = data;
    }

    uchar getch(void)
    {
        if (OERR) {
            CREN=0;
            CREN=1;
        }
        while(!RCIF);
        return RCREG;
    }

    post edited by pcbbc - 2019/11/08 10:59:42
    #12
    nidac
    Starting Member
    • Total Posts : 49
    • Reward points : 0
    • Joined: 2019/11/03 00:54:11
    • Location: 0
    • Status: online
    Re:Serial communication between PIC16F877 and computer 2019/11/09 02:12:10 (permalink)
    0
    Thanks @pcbbc for ideas.
    Initially, you saw, that I tried the code below to send a string to the serial using pointers, but I abandoned the idea because it didn't work. You might look over this code and find an explanation why it doesn't work?
    This code is from:
    https://embetronicx.com/t...ommunication-tutorial/
     
    #include <pic.h>
    __CONFIG(0X3F36);
    void ser_int();void tx(unsigned char);
    unsigned char rx();
    void txstr(unsigned char *);
    void main(){
    TRISC6=TRISC7=1;
    ser_int();
    txstr("(EmbeTronicX): Enter the letters in keyboard\n\r\r");
    while(1) {
    tx(rx());
    }
    }
     void ser_int()
    {
    TXSTA=0x20; //BRGH=0, TXEN = 1, Asynchronous Mode, 8-bit mode
    RCSTA=0b10010000; //Serial Port enabled,8-bit reception
    SPBRG=17; //9600 baudrate for 11.0592Mhz
    TXIF=RCIF=0;
    }
     void tx(unsigned char a)
    {
    TXREG=a;
    while(!TXIF);
    TXIF = 0;
    }
     unsigned char rx()
    {
    while(!RCIF);
    RCIF=0;
    return RCREG;
    }
     void txstr(unsigned char *s)
    {
    while(*s)
    {
    tx(*s++);
    }
    }

     
     
    #13
    ric
    Super Member
    • Total Posts : 24202
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re:Serial communication between PIC16F877 and computer 2019/11/09 03:17:22 (permalink)
    0
    You still have this line, after you were told it does nothing
    TXIF=RCIF=0;


     
    Change
    void tx(unsigned char a)
    {
      TXREG=a;
      while(!TXIF);
      TXIF = 0;
    }

    to:
    void tx(unsigned char a)
    {
      while(!TXIF);
      TXREG=a;
    }

     
    Change:
     unsigned char rx()
    {
    while(!RCIF);
    RCIF=0;
    return RCREG;
    }
    to
     unsigned char rx(void)
    {
      while(!RCIF);
      return RCREG;
    }

     
     

    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!
    #14
    nidac
    Starting Member
    • Total Posts : 49
    • Reward points : 0
    • Joined: 2019/11/03 00:54:11
    • Location: 0
    • Status: online
    Re:Serial communication between PIC16F877 and computer 2019/11/09 22:53:24 (permalink)
    0
    Thank you for the answer.
    I deleted the line: TXIF = RCIF = 0; and I made the changes sent. Same answer: the echo is good but the string is sent as an unknown character(see the file attached).
    post edited by nidac - 2019/11/09 22:56:49
    #15
    ric
    Super Member
    • Total Posts : 24202
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re:Serial communication between PIC16F877 and computer 2019/11/10 00:39:17 (permalink)
    0
    It may well be that ancient compiler you are using.
    You never answered why you don't just use XC8, which you can download for free.
     

    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!
    #16
    nidac
    Starting Member
    • Total Posts : 49
    • Reward points : 0
    • Joined: 2019/11/03 00:54:11
    • Location: 0
    • Status: online
    Re:Serial communication between PIC16F877 and computer 2019/11/10 09:56:50 (permalink)
    0
    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?
    #17
    NKurzman
    A Guy on the Net
    • Total Posts : 17916
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re:Serial communication between PIC16F877 and computer 2019/11/10 10:35:12 (permalink)
    0
    You want to use it in command line mode?
    You can run it from a batch file. It has no user interface. MPLab provides that.
    #18
    nidac
    Starting Member
    • Total Posts : 49
    • Reward points : 0
    • Joined: 2019/11/03 00:54:11
    • Location: 0
    • Status: online
    Re:Serial communication between PIC16F877 and computer 2019/11/10 10:53:29 (permalink)
    0
    ...without MPLAB-IDE.
    #19
    nidac
    Starting Member
    • Total Posts : 49
    • Reward points : 0
    • Joined: 2019/11/03 00:54:11
    • Location: 0
    • Status: online
    Re:Serial communication between PIC16F877 and computer 2019/11/10 10:57:08 (permalink)
    0
    Yes, I want to use it with command line, but I do not know(find) the compilation directive (or user manual).
    Thanks.
    #20
    Page: 12345 > Showing page 1 of 5
    Jump to:
    © 2019 APG vNext Commercial Version 4.5