开发者

Reading Virtual Serial Port with MicroC for 8051

开发者 https://www.devze.com 2023-03-08 18:55 出处:网络
I have a problem , please help me. for about a project homework ı need read from virtual serial port with microC and send this info to AT89C52 microconttoller..

I have a problem , please help me. for about a project homework ı need read from virtual serial port with microC and send this info to AT89C52 microconttoller.. This is my source code:

int uart_rd;
void main() {

    P1=0X00;
    UART1_Init(9600);
    delay_ms(100);

    while(1)
    {    
      if(UART1_Data_Ready()){

         uart_rd=UART1_read();

         if(uart_rd=='1')
         {P1=0X01; delay_ms(1500); P1=0X00; }

         if(uart_rd=='2')
         {P1=0X02; delay_ms(1500); P1=0X00; }
      }
   }
}

BUT I cant get 开发者_如何学Pythoninfo from the port. Where is the mistake.Please help me...


You are defining your UART receive variable (uart_rd) as an int, which is a 2 byte variable. I would expect UART1_read() to return a single byte (char).

I am not familiar with your particular setup or debugging/troubleshooting options, but you might try writing some code to assist in debugging your issue. The following example may be useful. It does assume that LEDs are connected to both port 1 and port 2, so some adjustment may be necessary.

char uart_rd;

void main() 
{
    UART1_Init(9600);  // Initialize UART at 9600 bps
    delay_ms(100);     // Wait for UART to stabilize

    while(1)
    {    
        if(UART1_Data_Ready())
        {
            P2 = 0xFF;               // Turn ON PORT2 LEDs upon data ready
            uart_rd = UART1_read();  // Receive data
            P1 = uart_rd;            // Display data on port 1 LEDs
            UART1_write(uart_rd);    // Transmit same data back
            delay_ms(1500);          // Brief delay
            P1 = 0x00;               // Turn OFF port 1 LEDs
            P2 = 0x00;               // Turn OFF port 2 LEDs
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消