使用微控制器“Atmel32”通过 UART 以字符串格式运行命令的代码

发布于 2025-01-16 05:28:32 字数 2034 浏览 4 评论 0原文

我正在编写代码,以使用终端仿真器从计算机上的 UART 外设运行字符串格式 (TurnONLED) 命令,但代码仅打印 UART_SendString 函数中的标头,并忽略接收部分(命令)。我纠正了代码,但仍然不响应字符串命令,只有数字命令才有效,所以问题是如何使条件语句使用字符串而不是数字值。输出已附加在此处输入图像描述 UART 模块已初始化并处于活动状态

UART>>1

UART>>2

UART>>TurnONLED

UART>>TurnOFFLED

#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] = '\0';
}


unsigned char USART_Receive(void) 
{
    while(GET_BIT(UCSRA,UCSRA_RXC)==0); 
    return UDR;        //USART I/O Data Register 
}

I'm writing code to run commands in string format (TurnONLED) over UART peripheral from a computer using terminal emulator, but the code prints only the header which is in UART_SendString function and ignores the receive part (command). I rectified the code, but still does not respond to string command only numeric commands work so the question is how to make conditional statement work with a string rather numeric values. output is attached enter image description here
UART Module Initialized and Active

UART>>1

UART>>2

UART>>TurnONLED

UART>>TurnOFFLED

#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] = '\0';
}


unsigned char USART_Receive(void) 
{
    while(GET_BIT(UCSRA,UCSRA_RXC)==0); 
    return UDR;        //USART I/O Data Register 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枉心 2025-01-23 05:28:33
  • #define TurnONLED 1 -> const char TurnONLED = '1';
  • #define TurnOFFLED 2 -> const char TurnOFFLED = '2';
  • USART_voidSend(10); //ASCII 值 10 表示回车(打印新行) ->
    USART_voidSend('\n'); // 换行
  • *(string+i) -> string[i]
  • 需要将PORTA对应的数据方向寄存器设置为输出到某处。
  • gets_UART1 不应在遇到 \r 时停止,它应该丢弃它并继续读取。否则,您会在 UART rx 缓冲区中留下 \n,这会扰乱连续读取或导致溢出错误。
  • gets_UART1 中的最后一个 i++; 不正确,应删除。
  • #define TurnONLED 1 -> const char TurnONLED = '1';.
  • #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]
  • You need to set the data direction register corresponding to PORTA to outputs somewhere.
  • gets_UART1 should not stop upon encountering \r, it should just discard it and keep reading. Otherwise you leave a \n in the UART rx buffer which will mess up continuous reads or cause overrun errors.
  • The last i++; in gets_UART1 is incorrect and should be removed.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文