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

Thread Starter

Fanfire174

Joined Mar 13, 2018
240

Thread Starter

Fanfire174

Joined Mar 13, 2018
240
hi 174,
Do you have a problem with the project.? or a question.?
E
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

C:
// PIC16F877A Configuration Bit Settings

// 'C' source line config statements

// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = ON        // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#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)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#define _XTAL_FREQ 20000000
#define Baud_rate 9600

//***Initializing UART module for PIC16F877A***//
void Initialize_UART(void)
{

    TRISC6 = 0; // TX Pin set as output
    TRISC7 = 1; // RX Pin set as input
  
    SPBRG = ((_XTAL_FREQ/16)/Baud_rate) - 1;
    BRGH  = 1;
  
    SYNC  = 0;    // Asynchronous
    SPEN  = 1;    // Enable serial port pins

    TXEN  = 1;    // enable transmission
    CREN  = 1;    // enable reception
  
    TX9   = 0;    // 8-bit reception selected
    RX9   = 0;    // 8-bit reception mode selected

}

//**Function to send one byte of date to UART**//
void UART_send_char(char bt)
{
    while(!TXIF);  // hold the program till TX buffer is free
    TXREG = bt;   //Load the transmitter buffer with the received value
}

//**Function to get one byte of date from UART**//
char UART_get_char() 
{
    if(OERR)    // check for Error
    {
        CREN = 0; //If error -> Reset
        CREN = 1; //If error -> Reset
    }
  
    while(!RCIF);  // hold the program till RX buffer is free
  
    return RCREG; //receive the value and send it to main function
}


//**Function to convert string to byte**//
void UART_send_string(char* st_pt)
{
    while(*st_pt) //if there is a char
        UART_send_char(*st_pt++); //process it as a byte data
}
// **********START of Main Function**************//
void main(void)
{
    int get_value;
  
    TRISB = 0x00; //Initialize PortB as output
    Initialize_UART();    //Initialize UART module                  
  
    UART_send_string("UART Module Initialized and active");    // Introductory Text
  
    while(1) //Infinite loop
    {
      get_value = UART_get_char();
      
        if (get_value == '1') //If the user sends "1"
        {
            RB3=1; //Turn on LED
            UART_send_string("RED LED -> ON"); //Send notification to the computer
            UART_send_char(10);//ASCII value 10 is used for carriage return (to print in new line)
        }
      
        if (get_value == '0') //If the user sends "0"
        {
           RB3=0; //Turn off LED
           UART_send_string("RED -> OFF"); //Send notification to the computer    
           UART_send_char(10);//ASCII value 10 is used for carriage return (to print in new line)
        }
     
    }
}
I tried above code but none of any led's are blinking
 
Last edited by a moderator:

Picbuster

Joined Dec 2, 2013
996
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
 

Thread Starter

Fanfire174

Joined Mar 13, 2018
240
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
This is sample code given in link

C:
#include "mcc_generated_files/mcc.h"

/*
                         Main application
*/

char data;
void send_string(const char *x)
{
     while(*x)
     {
        EUSART_Write(*x++);
     }
}


void main(void) {
    // Initialize the device
    SYSTEM_Initialize();

    // If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts
    // If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global and Peripheral Interrupts
    // Use the following macros to:

    // Enable high priority global interrupts
    //INTERRUPT_GlobalInterruptHighEnable();

    // Enable low priority global interrupts.
    //INTERRUPT_GlobalInterruptLowEnable();

    // Disable high priority global interrupts
    //INTERRUPT_GlobalInterruptHighDisable();

    // Disable low priority global interrupts.
    //INTERRUPT_GlobalInterruptLowDisable();

    // Enable the Global Interrupts
    //INTERRUPT_GlobalInterruptEnable();

    // Enable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptEnable();

    // Disable the Global Interrupts
    //INTERRUPT_GlobalInterruptDisable();

    // Disable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptDisable();

    //Send text to USART
send_string("Welcome to studentcompanion.co.za\r\n");
for (int x=0; x<=10; x++) __delay_ms(50);  //Generate 500ms delay
send_string("Press 1 for Red LED, 2 for Yellow LED and 3 for Green LED \r\n");
   


    while (1) {
      //  EUSART1_Write('s');
  
         data = EUSART_Read();
             switch(data){
             case '1':Red_SetHigh();
                      Yellow_SetLow();
                       Green_SetLow();
                             
             break;
       
             case '2':Red_SetLow();
                      Yellow_SetHigh();
                     Green_SetLow();

             break;
             case '3':Red_SetLow();
                      Yellow_SetLow();
                      Green_SetHigh();

             break;
              default:Red_SetLow();
                      Yellow_SetLow();
                      Green_SetLow();
             break;

  
    }
}
}
/**
End of File
*/
 

Attachments

Last edited by a moderator:

Thread Starter

Fanfire174

Joined Mar 13, 2018
240
Are you going to describe the problem you are having? Or maybe you want use to guess?
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
 

JohnInTX

Joined Jun 26, 2012
4,008
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
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.
 

Thread Starter

Fanfire174

Joined Mar 13, 2018
240
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.
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
 

spinnaker

Joined Oct 29, 2009
7,835
Let's cut the newbie some slack please.

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:

spinnaker

Joined Oct 29, 2009
7,835
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

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

Joined Feb 24, 2006
13,502
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.
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:

Thread Starter

Fanfire174

Joined Mar 13, 2018
240
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.
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
Forget about the C# code for now. Only then start looking in to using the C# program.
I have no issue with c# .net program
thank's for posting a link I have been seen link before your post
 

Ian Rogers

Joined Dec 12, 2012
747
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...
 
Top