我正在尝试通过UART接收从PC到PIC12F1572的数据帧 我的目标是制作一个代码来接收RGBFF0000 \ n这样的数据,这是RGB LED配置..任何人都有关于如何做的想法..我已经做了一些代码,但我想让它在逻辑上正确。 我使用PIC12F1572和MPLAB V3.50 代码,更正和提示非常受欢迎 提前致谢 这是我的代码:
#include "mcc_generated_files/mcc.h"
#define AT_COMMAND_BUFFER_SIZE 256
uint8_t buffer[AT_COMMAND_BUFFER_SIZE];
uint8_t i = 0;
uint8_t receivedByte ;
/*
Main application
*/
/*-----------------R.E.C.E.I.V.E.R PIC------------*/
void main(void)
{
// initialize the device
SYSTEM_Initialize();
EUSART_Write(0x61);
INTERRUPT_GlobalInterruptEnable(); // Enable the Global Interrupts
INTERRUPT_PeripheralInterruptEnable(); // Enable the Peripheral Interrupts
// Disable the Global Interrupts
//INTERRUPT_GlobalInterruptDisable();
// Disable the Peripheral Interrupts
//INTERRUPT_PeripheralInterruptDisable();
while (1)
{
if ( UART_DataReady() ) // If data is received,
{
receivedByte = EUSART_Read();
for (i = 0; i<20 ; i++)
{
if ( receivedByte == '')
{
//My pb is how to start to check the data received over UART RX
}
}
}
}
}
答案 0 :(得分:0)
您在每个命令的末尾发送新的行字符\n
,因此请将recievedByte
读取到\n
并将其存储在新数组中。然后设置一个标志以显示有效的命令接收。处理完新命令后,清除此标志。
if ( UART_DataReady() ) // If data is received,
{
receivedByte = EUSART_Read();
for (i = 0; i<20 ; i++)
{
if ( receivedByte != '\n')
{
data[i] = receivedByte;
}
else{
data[i] = '\0';
data_flag = 1;
break;
}
}
}
答案 1 :(得分:0)
以下是答案:
#include "mcc_generated_files/mcc.h"
#include <stdlib.h>
#include <stdio.h>
#include "atoh.h"
#include "LED.h"
#define _XTAL_FREQ 16000000
#define FRAMESIZE 20
void main(void)
{
uint8_t data,i,j,got_char;
uint8_t value;
uint8_t RX_Buffer[FRAMESIZE];
// initialize the device
SYSTEM_Initialize();
INTERRUPT_GlobalInterruptEnable(); // Enable the Global Interrupts
INTERRUPT_PeripheralInterruptEnable(); // Enable the Peripheral Interrupts
while (1)
{
if (EUSART_DataReady)
{
for (i = 0; i<FRAMESIZE; i++)
{
RX_Buffer[i] = EUSART_Read();
if (RX_Buffer[i] == '\n')
{
break;
}
}
RX_Buffer[18] = '\n';
RX_Buffer[i] = 0;
EUSART_WriteAnArrayOfBytes(RX_Buffer);
}