我最近重新开始学习C语言,并且正在进行一个“学术性”的练习,以再次磨练一些旧技能。我的项目围绕着生成正弦波的相对简单的过程展开。一开始我只是在x86上使用命令行编码(Fedora Core 20),一切都很顺利。然后我将代码迁移到AVR上,我开始学到了很多东西。例如,如何将UART设置为标准输出设备。但是,由于正弦波是浮点数,我在使用printf和sprintf时遇到了问题。
程序生成30个正弦波,然后将值打印到终端。文本“Sine:”可以正确打印,但是浮点数部分却出现了问号。将变量替换为常量没有任何效果。
第一件事情是有人建议我是否记得链接器选项以支持完整的浮点数-的确,我忘记了。但是,将此添加到我的makefile中没有任何效果。
我不确定这里复制和粘贴代码的政策:我应该在这里粘贴它们和我的makefile以供检查吗?
编辑:抱歉长时间未回复。我做了更多的阅读,包括第一个答案链接到的内容。我之前已经阅读过那个参考文献(GNU),并将链接包含在我的Makefile中,这就是我感到困惑的原因。下面是我的Makefile:
P = sines
OBJ = sines.o
PROGRAMMER = buspirate
PORT = /dev/ttyUSB0
MCU_TARGET = atmega328p
AVRDUDE_TARGET = atmega328p
HZ = 16000000
DEFS =
LIBS = -lprintf_flt -lm
CC = avr-gcc
override CFLAGS = -g -DF_CPU=$(HZ) -Wall -O1 -mmcu=$(MCU_TARGET) $(DEFS)
override LDFLAGS= -Wl,-Map,$(P).map -u,vfprintf
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
all: $(P).elf lst text
$(P).elf: $(OBJ)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
clean:
rm -rf *.hex *.bin *.map *~ sine*.csv *.o $(P).elf *.lst
lst: $(P).lst
%.lst: %.elf
$(OBJDUMP) -h -S $< > $@
text: hex bin
hex: $(P).hex
bin: $(P).bin
%.hex: %.elf
$(OBJCOPY) -j .text -j .data -O ihex $< $@
%.bin: %.elf
$(OBJCOPY) -j .text -j .data -O binary $< $@
install: $(P).hex
avrdude -p $(AVRDUDE_TARGET) -c $(PROGRAMMER) -P $(PORT) -v -U flash:w:$(P).hex
我担心的是链接器参数可能没有按正确顺序排列?从我所了解的情况来看,它们是按照正确的顺序排列的,但是......
我相当确定我的代码本身没问题。如果需要,我也可以在这里发布它。 另外,感谢您将我的问题转移到这里。我不太明白两者之间的区别!
以下是源代码。它正在运行在ATmega328P上。这个当前版本打印一个常量作为调试,而不是从sinescalc()得到的结果,尽管我知道那个函数是工作的(至少应该是的,我非常确定我曾经使用avr-gdb检查过 -- 它在命令行上肯定可以工作,并且在MSP430上也可以工作)。
#include <avr/io.h>
//#include <util/delay.h>
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.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);
while(!(UCSR0A & (1<<UDRE0)));
UDR0 = c;
return 0;
}
void sinescalc(double *sinptr, int cycles, int size) {
double pi = acos(-1);
double step = ((pi * (cycles*2))/ size);
float temp;
double z = step;
int y;
for(y = 0; y<= size; y++) {
temp = sin(z); // calculate the current sine
*sinptr = (double)temp; // pass it into the array from main()
z += step; // add the step value to prepare for next sine
sinptr++; // should move the pointer by correct size of variable
}
}
int main(void) {
unsigned long _fosc = 16000000;
unsigned int _baud = 19200;
unsigned long _myubrr = _fosc/16/_baud-1;
unsigned int array_size = 256;
UBRR0L = (unsigned char)_myubrr;
UCSR0B = (1<<RXEN0)|(1<<TXEN0); //enable receiver and transmitter
stdout = &mystdout;
double sines[array_size];
double *sinepointer = sines; // set sinepointer to first element of sines array
sinescalc(sinepointer, 2, array_size); // calculate two cycles of sine, with 255 data points
int y;
//char msg[6] = ("Sine: ");
char output[40];
for(y = 0; y <= array_size; y++) {
sprintf(output, "Sine:\t %.6f", 1.354462);
printf(output);
printf("\n");
}
return 0;
}