我有一些别人写的代码,我正在努力理解。我对c非常熟悉,可以编写基本/中间函数,但从未真正学会使用文件处理程序。代码与我如何在ATMEL微控制器上使用/初始化USART有关,我正在为每一行添加注释以帮助跟踪,但我在这方面有点挣扎。
#include <stdio.h>
int USART_0_printCHAR(char character, FILE *stream) // character = the received character, FILE = reference file, *stream = pointer
{
USART_Transmit_Char(character); //send the character over USART
return 0; // return 0 to the caller for exit success.
}
FILE USART_0_stream = FDEV_SETUP_STREAM(USART_0_printCHAR, NULL, _FDEV_SETUP_WRITE); //what do these arguments mean? is it replacing a standard output stream with the character that is written to the buffer.
void USART_Transmit_Char(char data)
{
// code omitted:
// check the Transmit buffer
// wait for free space in the buffer
// put the character into the buffer
// enable the Data Register Empty Interrupt for TX
}谢谢。
发布于 2022-04-29 07:19:55
示例初始化标准IO设施的默认流。它创建一个只写流,该流将其输出写入USART_0_printCHAR。
请参阅标准IO设施
提供了标准流stdin、stdout和stderr,但与C标准相反,由于avr-libc不了解适用的设备,这些流在应用程序启动时还没有预先初始化。此外,由于avr没有任何“文件”的概念,所以没有函数fopen()可以用于将流关联到某个设备。(见附注1)相反,提供函数fdevopen()将流关联到设备,其中设备需要提供发送字符的函数、接收字符的,或者两者兼而有之。作为fdevopen()的另一种方法,可以使用宏fdev_setup_stream()来设置用户提供的文件结构。
#include <stdio.h>
static int uart_putchar(char c, FILE *stream);
static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,
_FDEV_SETUP_WRITE);
static int
uart_putchar(char c, FILE *stream)
{
if (c == '\n')
uart_putchar('\r', stream);
loop_until_bit_is_set(UCSRA, UDRE);
UDR = c;
return 0;
}
int
main(void)
{
init_uart();
stdout = &mystdout;
printf("Hello, world!\n");
return 0;
}此示例使用初始化器表单FDEV_SETUP_STREAM(),而不是类似于函数的fdev_setup_stream(),因此所有数据初始化都在C启动过程中进行。
https://stackoverflow.com/questions/72051963
复制相似问题