我正在编写代码,以便使用终端模拟器从计算机通过UART外围设备运行字符串格式(TurnONLED(的命令,但该代码只打印UART_SendString函数中的标头,而忽略接收部分(命令(。我纠正了代码,但仍然不响应字符串命令,只有数字命令才能工作,所以问题是如何使条件语句使用字符串而不是数字值。输出已附加在此处输入图像描述UART模块初始化并激活
UART>gt;1
UART>gt;2
UART>gt;TurnONLED
UART>gt;关闭
#include "DIO_interface.h"
#include "PORT_interface.h"
#include "USART_interface.h"
const char TurnONLED = '1';
const char TurnOFFLED = '2';
void PORT_voidInit(void);
void USART_voidInit(void); //baud rate: 9600 frame size to 8 bits 1 stop bit no parity
unsigned char USART_Receive(void) //function receives a character in Ascii
void gets_UART1(unsigned char *string); //function concatenates characters to get a string (command)
void main (void)
{
unsigned char Str[72];
PORT_voidInit(); //Rx input (PD0) and Tx output (PD1)
USART_voidInit();
UART_Send_String("UART Module Initialized and Active");
USART_voidSend('r'); //carriage return
UART_Send_String("UART>>"); //command console
while(1)
{
gets_UART1(Str); //get command as a string
UART_Send_String("UART>>"); //command console
USART_voidSend('n'); //line feed
if(*Str==TurnONLED) //data in ASCI as PC sends ASCI data
{
DIO_u8SetPinValue (PORTA, PIN0, PIN_HIGH); //LED ON;
}
else if(*Str==TurnOFFLED)
{
DIO_u8SetPinValue (PORTA, PIN0, PIN_LOW); //LED OFF;
}
}
}
void gets_UART1(unsigned char *string) //Receive a character until carriage return or newline
{
unsigned char i=0,J=0;
do
{
string[i] = USART_u8Receive();
J = string[i];
i++;
}
while(J!='r');
string[i] = ' ';
}
unsigned char USART_Receive(void)
{
while(GET_BIT(UCSRA,UCSRA_RXC)==0);
return UDR; //USART I/O Data Register
}
#define TurnONLED 1
->CCD_ 2#define TurnOFFLED 2
->const char TurnOFFLED = '2';
USART_voidSend(10); //ASCII value 10 for carriage return (print a new line)
->USART_voidSend('n'); // line feed
*(string+i)
->string[i]
- 您需要将
PORTA
对应的数据方向寄存器设置为某个位置的输出 gets_UART1
不应该在遇到r
时停止,它应该丢弃它并继续阅读。否则,您会在UART rx缓冲区中留下一个n
,这将扰乱连续读取或导致溢出错误gets_UART1
中的最后一个i++;
不正确,应删除