PIC到PIC UART通信闪烁LED

时间:2017-01-23 01:13:34

标签: pic uart led

我想通过UART将PIC12F1572的命令发送到另一个PIC12F1572 并且我希望发送一个功能,它将使从PIC上的LED闪烁。 我做了一些代码,但我有点困惑 有人能帮我一下吗? P.S-我正在使用MPLAB X IDE v3.50

主机/发送器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);
        }
     }
}

2 个答案:

答案 0 :(得分:1)

有几件事需要考虑:

  • 你不能发送功能&#34;通过UART。因此,virtual destructor函数需要在接收方。在执行任何其他操作之前,请验证该功能是否在接收器端工作,而不进行任何UART交互。
  • 接下来,您可以定义protocol,这基本上决定了您通过UART发送的字节值是否应该触发接收方的virtual调用。例如,如果您决定使用42的字节值来触发LED闪烁,则发送方将调用LED_Blink(),而在接收方,您将具有以下内容:

    LED_Blink()

答案 1 :(得分:0)

因此,从UART接收数据并将其保存在阵列中并使用数据对LED进行vlink我已完成此代码:任何有兴趣的人都可以看看

 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);
          }
        }

  }