UART_RxChar() 一直在等待,直到接收到数据。但是我想停止等待接收到的数据并连续运行我的 while(1) 循环。所以我想在等待大约 1 秒后停止 UART_RxChar() 并连续运行 while(1) 循环。这是我的代码。我应该如何改变它......有人可以帮我解决这个问题。
#define F_CPU 8000000UL /* Define frequency here its 8MHz */
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
void UART_init(void)
{
UBRRH = (BAUD_PRESCALE >> 8); /* Load upper 8-bits*/
UBRRL = BAUD_PRESCALE; /* Load lower 8-bits of the baud rate value */
UCSRB |= (1 << RXEN) | (1 << TXEN);/* Turn on transmission and reception */
UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1);/* Use 8-bit character sizes */
}
unsigned char UART_RxChar(void)
{
while ((UCSRA & (1 << RXC)) == 0);/* Wait till data is received */
return UDR; /* Return the byte*/
}
void UART_TxChar(uint8_t data)
{
while (! (UCSRA & (1<<UDRE))); /* Wait for empty transmit buffer*/
UDR = data ;
}
void UART_SendString(char *str)
{
for(int i=0;i<strlen(str);i++){
UART_TxChar(str[i]);
}
}
int main()
{
char RecievedByte;
UART_init();
DDRA=0x00;// for input port-LED
DDRB=0xff;// for output port-Switch
while(1)
{
if((PINA==0x01))// checking the status of PIN PA0 (whether push button is pressed), if it is '1', turns on the LED
{
_delay_ms(100); // for debouncing of switch
PORTB=0x01; // Turning on the LED PB0
_delay_ms(200);
PORTB=0x00;
}
else if( (UART_RxChar()=='s'))// else checking whether 's' is received, if it is '1', turns on the LED
{
//want to ignore UART_RxChar()=='s' is waiting untill 's' is received after sometime and continiously run while(1) loop
_delay_ms(100); // for debouncing of switch
PORTB=0x02; // Turning on the LED PB1
_delay_ms(200);
PORTB=0x00;
}
}
}