UART_RxChar()一直在等待直到收到数据。但是我想停止等待接收到的数据,继续运行我的while(1)循环。因此,我希望在等待大约1秒后停止UART_RxChar(),并在(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;
}
}
}发布于 2020-01-30 16:40:22
您的代码中有几个设计问题:
delay_ms和类似的代码将阻止代码的其他部分执行。虽然这样的延迟很容易让一个简单的程序第一次运行,但是随着应用程序的增长和处理更复杂的任务,保留这样的设计问题将是一件非常痛苦的事情。例如,关键故障的阻塞实现。最好是让一个计时器中断运行,然后根据这个检查PORTA每10毫秒.20毫秒(50赫兹.100赫兹)。这就足够了。根据读取值的不同,主程序决定它是短键还是长键,还是双击。
while(wait_for_event)也阻塞了您的应用程序。例如,
while ((UCSRA & (1 << RXC)) == 0);/* Wait till data is received */正在阻塞整个应用程序,直到UART接收到一些东西。它适用于简单的程序,但对于除了琐碎程序之外的程序,它通常是一个问题。在UART接收/传输的情况下,您还使用了一些UART中断。如果您更喜欢轮询而不是中断,那么编写轮询时不会永远阻止其他内容。
一般来说,您的主循环的结构将是
while (1)
{
if (condition_1)
{
reset_condition_1();
nonblocking_action_for_condition_1();
}
if (condition_2)
{
reset_condition_2();
nonblocking_action_for_condition_2();
}
}例如,有时我对简单的UART接收使用轮询(在您的设备上,SFRs可能有所不同):
int uart_getc_nowait (void)
{
return (UCSRA & (1 << RXC)) ? 0xff & UDR : -1;
}这是非阻塞的:只有当这个函数返回0.255时,代码的其他部分才会采取行动。
在传输数据时,可以考虑使用FIFO:您可以将字符串写入FIFO,而ISR将选择单个字节并发送它们。伪码:
ISR (uart-dataregister-is-empty)
{
if (fifo-is-empty)
{
disable-uart-dataregister-is-empty-interrupt;
}
else
{
byte = read-one-byte-from-fifo;
uart-dataregister = byte;
}
}
void uart_putc (byte)
{
if (fifo-is-full)
{
do-something-if-fifo-is-full;
}
else
{
write-byte-to-fifo;
// Following action is no problem if respective IRQ is already enabled.
enable-uart-dataregister-is-empty-interrupt;
}
}https://stackoverflow.com/questions/59915919
复制相似问题