PIC 到 PIC UART 通信使 LED 闪烁
PIC to PIC UART communication to Blink the LED
我想通过UART将PIC12F1572的命令发送到另一个PIC12F1572
并且我想发送一个函数,使从 PIC 上的 LED 闪烁。
我已经完成了一些代码,但我有些困惑
有人能帮我一下吗?
P.S- 我正在使用 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);
}
}
}
有几件事需要考虑:
- 您无法通过 UART "send a function"。因此,
LED_Blink()
函数需要在接收端。在执行任何其他操作之前,请验证该函数在接收器端是否有效,无需任何 UART 交互。
接下来,您可以定义一个 protocol,它基本上决定您通过 UART 发送的字节值应该触发接收方的 LED_Blink()
调用。例如,如果您决定使用字节值 42 来触发 LED 闪烁,您的发送方将调用 UART_write(42)
,而在接收方,您将调用如下内容:
data = UART_ReadOneByte();
if (data == 42) {
LED_Blink();
}
所以为了从 UART 接收数据并将其保存在数组中并使用数据 vlink nthe LED 我已经完成了这段代码:任何有兴趣的人都可以看看
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] = '[=10=]'; // 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);
}
}
}
我想通过UART将PIC12F1572的命令发送到另一个PIC12F1572 并且我想发送一个函数,使从 PIC 上的 LED 闪烁。 我已经完成了一些代码,但我有些困惑 有人能帮我一下吗? P.S- 我正在使用 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);
}
}
}
有几件事需要考虑:
- 您无法通过 UART "send a function"。因此,
LED_Blink()
函数需要在接收端。在执行任何其他操作之前,请验证该函数在接收器端是否有效,无需任何 UART 交互。 接下来,您可以定义一个 protocol,它基本上决定您通过 UART 发送的字节值应该触发接收方的
LED_Blink()
调用。例如,如果您决定使用字节值 42 来触发 LED 闪烁,您的发送方将调用UART_write(42)
,而在接收方,您将调用如下内容:data = UART_ReadOneByte(); if (data == 42) { LED_Blink(); }
所以为了从 UART 接收数据并将其保存在数组中并使用数据 vlink nthe LED 我已经完成了这段代码:任何有兴趣的人都可以看看
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] = '[=10=]'; // 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);
}
}
}