How to get a PIC and a PC to communicate with using a Uart program

Discussion in 'Embedded Systems and Microcontrollers' started by Fanfire174, Mar 16, 2019.

  1. Fanfire174

    Thread Starter Member

    Mar 13, 2018
    204
    0
    I am trying to control led's from PC I did google search and found this link http://www.studentcompanion.co.za/c...troller-from-pc-graphical-user-interface-gui/

    I had some issues with C# program but I have been resolved it

    I can see the following windows and I have configured the required parameter

    upload_2019-3-16_14-36-31.png


    I am looking help to implement uart program for LED's I have pic16f877a and I am using MLAB XC8 to write my C code
     
  2. ericgibbs

    Moderator

    Jan 29, 2010
    8,558
    1,726
    hi 174,
    Do you have a problem with the project.? or a question.?
    E
     
    Fanfire174 likes this.
  3. Fanfire174

    Thread Starter Member

    Mar 13, 2018
    204
    0
    Yes I asked at the bottom of my first post

    I am trying to make uart program to control three led. In the given link they are using different microcontroller and I have pic16f877a

    Code (C):
    1. // PIC16F877A Configuration Bit Settings
    2.  
    3. // 'C' source line config statements
    4.  
    5. // CONFIG
    6. #pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
    7. #pragma config WDTE = ON        // Watchdog Timer Enable bit (WDT enabled)
    8. #pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
    9. #pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
    10. #pragma config LVP = OFF        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
    11. #pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
    12. #pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
    13. #pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)
    14.  
    15. // #pragma config statements should precede project file includes.
    16. // Use project enums instead of #define for ON and OFF.
    17.  
    18. #include <xc.h>
    19. #define _XTAL_FREQ 20000000
    20. #define Baud_rate 9600
    21.  
    22. //***Initializing UART module for PIC16F877A***//
    23. void Initialize_UART(void)
    24. {
    25.  
    26.     TRISC6 = 0; // TX Pin set as output
    27.     TRISC7 = 1; // RX Pin set as input
    28.  
    29.     SPBRG = ((_XTAL_FREQ/16)/Baud_rate) - 1;
    30.     BRGH  = 1;
    31.  
    32.     SYNC  = 0;    // Asynchronous
    33.     SPEN  = 1;    // Enable serial port pins
    34.  
    35.     TXEN  = 1;    // enable transmission
    36.     CREN  = 1;    // enable reception
    37.  
    38.     TX9   = 0;    // 8-bit reception selected
    39.     RX9   = 0;    // 8-bit reception mode selected
    40.  
    41. }
    42.  
    43. //**Function to send one byte of date to UART**//
    44. void UART_send_char(char bt)
    45. {
    46.     while(!TXIF);  // hold the program till TX buffer is free
    47.     TXREG = bt;   //Load the transmitter buffer with the received value
    48. }
    49.  
    50. //**Function to get one byte of date from UART**//
    51. char UART_get_char()
    52. {
    53.     if(OERR)    // check for Error
    54.     {
    55.         CREN = 0; //If error -> Reset
    56.         CREN = 1; //If error -> Reset
    57.     }
    58.  
    59.     while(!RCIF);  // hold the program till RX buffer is free
    60.  
    61.     return RCREG; //receive the value and send it to main function
    62. }
    63.  
    64.  
    65. //**Function to convert string to byte**//
    66. void UART_send_string(char* st_pt)
    67. {
    68.     while(*st_pt) //if there is a char
    69.         UART_send_char(*st_pt++); //process it as a byte data
    70. }
    71. // **********START of Main Function**************//
    72. void main(void)
    73. {
    74.     int get_value;
    75.  
    76.     TRISB = 0x00; //Initialize PortB as output
    77.     Initialize_UART();    //Initialize UART module                  
    78.  
    79.     UART_send_string("UART Module Initialized and active");    // Introductory Text
    80.  
    81.     while(1) //Infinite loop
    82.     {
    83.       get_value = UART_get_char();
    84.      
    85.         if (get_value == '1') //If the user sends "1"
    86.         {
    87.             RB3=1; //Turn on LED
    88.             UART_send_string("RED LED -> ON"); //Send notification to the computer
    89.             UART_send_char(10);//ASCII value 10 is used for carriage return (to print in new line)
    90.         }
    91.      
    92.         if (get_value == '0') //If the user sends "0"
    93.         {
    94.            RB3=0; //Turn off LED
    95.            UART_send_string("RED -> OFF"); //Send notification to the computer    
    96.            UART_send_char(10);//ASCII value 10 is used for carriage return (to print in new line)
    97.         }
    98.      
    99.     }
    100. }
    101.  
    102.  
    I tried above code but none of any led's are blinking
     
    Last edited by a moderator: Mar 16, 2019
  4. Picbuster

    Well-Known Member

    Dec 2, 2013
    980
    126
    You expect a integer get_value = UART_get_char();
    that's ok but not needed int = 16 bits char 8 bits in line with ASCII chars.
    However; if you send an ASCII one(1) char (49) you will receive not a one (1) but 49 as an int unless correct (-48) in UART_get_char

    I did not look at the rest.
    Picbuster
     
    Fanfire174 likes this.
  5. Fanfire174

    Thread Starter Member

    Mar 13, 2018
    204
    0
    This is sample code given in link

    Code (C):
    1. #include "mcc_generated_files/mcc.h"
    2.  
    3. /*
    4.                          Main application
    5. */
    6.  
    7. char data;
    8. void send_string(const char *x)
    9. {
    10.      while(*x)
    11.      {
    12.         EUSART_Write(*x++);
    13.      }
    14. }
    15.  
    16.  
    17. void main(void) {
    18.     // Initialize the device
    19.     SYSTEM_Initialize();
    20.  
    21.     // If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts
    22.     // If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global and Peripheral Interrupts
    23.     // Use the following macros to:
    24.  
    25.     // Enable high priority global interrupts
    26.     //INTERRUPT_GlobalInterruptHighEnable();
    27.  
    28.     // Enable low priority global interrupts.
    29.     //INTERRUPT_GlobalInterruptLowEnable();
    30.  
    31.     // Disable high priority global interrupts
    32.     //INTERRUPT_GlobalInterruptHighDisable();
    33.  
    34.     // Disable low priority global interrupts.
    35.     //INTERRUPT_GlobalInterruptLowDisable();
    36.  
    37.     // Enable the Global Interrupts
    38.     //INTERRUPT_GlobalInterruptEnable();
    39.  
    40.     // Enable the Peripheral Interrupts
    41.     //INTERRUPT_PeripheralInterruptEnable();
    42.  
    43.     // Disable the Global Interrupts
    44.     //INTERRUPT_GlobalInterruptDisable();
    45.  
    46.     // Disable the Peripheral Interrupts
    47.     //INTERRUPT_PeripheralInterruptDisable();
    48.  
    49.     //Send text to USART
    50. send_string("Welcome to studentcompanion.co.za\r\n");
    51. for (int x=0; x<=10; x++) __delay_ms(50);  //Generate 500ms delay
    52. send_string("Press 1 for Red LED, 2 for Yellow LED and 3 for Green LED \r\n");
    53.    
    54.  
    55.  
    56.     while (1) {
    57.       //  EUSART1_Write('s');
    58.  
    59.          data = EUSART_Read();
    60.              switch(data){
    61.              case '1':Red_SetHigh();
    62.                       Yellow_SetLow();
    63.                        Green_SetLow();
    64.                              
    65.              break;
    66.        
    67.              case '2':Red_SetLow();
    68.                       Yellow_SetHigh();
    69.                      Green_SetLow();
    70.  
    71.              break;
    72.              case '3':Red_SetLow();
    73.                       Yellow_SetLow();
    74.                       Green_SetHigh();
    75.  
    76.              break;
    77.               default:Red_SetLow();
    78.                       Yellow_SetLow();
    79.                       Green_SetLow();
    80.              break;
    81.  
    82.  
    83.     }
    84. }
    85. }
    86. /**
    87. End of File
    88. */
     
    Last edited by a moderator: Mar 16, 2019
  6. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    7,815
    3,716
    Are you going to describe the problem you are having? Or maybe you want use to guess?
     
    MaxHeadRoom likes this.
  7. shteii01

    AAC Fanatic!

    Feb 19, 2010
    4,667
    745
  8. Fanfire174

    Thread Starter Member

    Mar 13, 2018
    204
    0
    The problem is in Uart code post # 3

    when I compile code I don't get any error but when I click on any LED it's doesn't on/off

    So i don't know what's wrong in code
     
  9. JohnInTX

    Moderator

    Jun 26, 2012
    3,768
    1,982
    For starters, turn the watchdog timer OFF in line 7.
    #pragma config WDTE = OFF

    Do you have a debugger like PICkit2, PICkit3, ICD-3 etc? You can use that to debug your code.
     
  10. Papabravo

    Expert

    Feb 24, 2006
    12,288
    2,727
    Do you seriously expect your code to work the first time it compiles without error?
     
    spinnaker and Fanfire174 like this.
  11. JohnInTX

    Moderator

    Jun 26, 2012
    3,768
    1,982
    Let's cut the newbie some slack please.
     
    Fanfire174 likes this.
  12. Fanfire174

    Thread Starter Member

    Mar 13, 2018
    204
    0
    I have PICkit3 and I am trying to resolve issues. I have been spent time on c# .net program to sort out issues and now trying to resolve uart program
     
  13. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    7,815
    3,716

    171 posts is not a "newbie". This person should have learned some time ago how things work. TS is putting forth almost no effort on helping himself.

    You need to help others for them to be able to help you.
     
    Last edited: Mar 16, 2019
  14. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    7,815
    3,716

    Forget about the C# code for now. That just complicates things. Write a simple program on the pic to communicate to the serial port. Use terminal software to communicate to the pic. In fact do nothing but send a string from the pic to the terminal something like Hello World. Get that working and move on to getting data from the terminal. Maybe something like ask for a character then do something like turn on an LED. Only then start looking in to using the C# program.
     
    Papabravo likes this.
  15. shteii01

    AAC Fanatic!

    Feb 19, 2010
    4,667
    745
  16. shteii01

    AAC Fanatic!

    Feb 19, 2010
    4,667
    745
  17. Papabravo

    Expert

    Feb 24, 2006
    12,288
    2,727
    This is the kind of useful advice that says in effect: "learn to crawl and walk, before trying to run a marathon". Newbie or not, reasonable expectations can be discovered or learned. In a sense asking a question prods the newbie to examine those expectations and adjust them. Answering questions is a far better way to learn than drinking from the fire hose.

    BTW this is the exact procedure I follow when trying to get anything UART related working on a new piece of hardware. Tried and true wins the day. Also works for SPI, I2C and ethernet.
     
    Last edited: Mar 16, 2019
    spinnaker likes this.
  18. Fanfire174

    Thread Starter Member

    Mar 13, 2018
    204
    0
    just read your post in my recent thread https://forum.allaboutcircuits.com/threads/mplab-and-pic16f877a-error.157488/page-2

    It's up to you how do you judge people Everyone has a different style and different nature
    I have no issue with c# .net program
    thank's for posting a link I have been seen link before your post
     
  19. Ian Rogers

    Well-Known Member

    Dec 12, 2012
    645
    178
    Did you copy and paste that code into MPLABX or did you generate it?? That copy looks identical to the one on the net... That means it didn't compile successfully and the generated support files are not there...

    I'm sorry if I am wrong about this, but this is a byproduct of copy and paste...
     
  20. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    7,815
    3,716
Loading...