我正在尝试接收一个包含数据从PC到通过UART PIC12F1572的帧我的目标是制作一个代码来接收数据,例如 RGBFF0000,即 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
}
}
}
}
}
您在每个命令的末尾发送新行字符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] = ' ';
data_flag = 1;
break;
}
}
}
这是答案:
#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);
}