Search code examples
picuartled

PIC to PIC UART communication to Blink the LED


I want to send a command by PIC12F1572 to another PIC12F1572 through UART and in that I want to send a function which will blink the LED on the slave PIC. I have done some code but I am somewhat confusing can anyone help me here? P.S- I am Using MPLAB X IDE v3.50

Master/Transmitter PIC12F1572:

    #include <xc.h>
    #include "main.h"
    #include "config.h"
    #include "TYPEDEF_Definitions.h"
    #include "PIC_12_Timer0.h"
    #include "PIC_12_UART.h"
    #include "System_init.h"
    #include "Pin_manager.h"
    #include "LED.h"

    #define _XTAL_FREQ 16000000
void main()
{   
    SYSTEM_Initialize();
    //pin_config();
    LATA = 0x00;  
    UART_Init();                  //Initialize UART
  // StartLedBlinkingWithColor(color);

    TRISAbits.TRISA2  = 1; //make RA2 as input

    while (1)
    {   
      //LATAbits.LATA2 = 1;
      uint8_t Var = 0x61;
       //uint8_t Var = LATAbits.LATA2;

        if(TXSTAbits.TRMT == 1)
            {

                  UART_write(LED_Blink()); // is it possible?? or how it will be possible

                 // __delay_ms(1000);
            }

       LATAbits.LATA2 = 1;


    }
}

void LED_Blink()
{
       LATAbits.LATA2   = 1; 
       LATAbits.LATA5   = 1; 

       __delay_ms(1000);


       LATAbits.LATA2   = 0; 
       LATAbits.LATA5   = 0;


}

RECEIVER/SLAVE PIC12F1572:

#include <xc.h>
#include "config.h"
#include "PIC_12F_GPIO.h"
#include "Led.h"
#include "Interruptmanage.h"
#include "PIC_12F_UART.h"
#include "PIC_12F_TIMER0.h"
#include "main.h"

void main( void)
{
    uint8 data;   

    InterruptInit();
    TIMER0_Init();
    UART_Init();
    InitLeds(); // here I init GPIO pin..no prb here

    // SetLedOff();

    /*-------------R E C E I V E R*------------*/
    while (1) 
    {   // Endless loop

        if (UART_DataReady() )     // I get prob here ..
        { 
          PORTA = UART_ReadOneByte(); // read the received data, [how can I receive my Led_Blink() function??
          LATAbits.LATA2   = 1;      
          //LATAbits.LATA2 = data;

          //SendByteSerially(data);
        }
     }
}

Solution

  • So for Receiving data from UART and saving it in array and use the data to vlink nthe LED I Have done this code: Anyone interested can have a look

     while (1)
     {
    
            if (EUSART_DataReady)
            {
                for (i = 0; i<FRAMESIZE; i++)   //#define FRAMESIZE 19
                {
                    RX_Buffer[i] = EUSART_Read();
                    if (RX_Buffer[i] == '\n')        //check'\n' in the end of frame
                       break;
                }
    
                RX_Buffer[i] = '\n';                        //append '\n' at the end of stoaring array for detection of frame
                RX_Buffer[i+1] = '\0';                      // End of an array
                EUSART_WriteAnArrayOfBytes(RX_Buffer);
    
    
            if(RX_Buffer[0]=='R' && RX_Buffer[FRAMESIZE-2] == '\n')   //check for correct frame
              {
    
                LATAbits.LATA2 = 1;
                __delay_ms(2000);
                LATAbits.LATA2 = 0;
                __delay_ms(1000);
              }
            }
    
      }