gpt4 book ai didi

c - AVR ATMEGA328p 波特率错误/无串行连接

转载 作者:行者123 更新时间:2023-11-30 16:38:46 25 4
gpt4 key购买 nike

我无法运行串行连接来测试我的 AVR 芯片。我目前认为这是因为 F_CPU 值,但我不能 100% 确定。我为 #error Systematischer Fehler der Baudrate größer 1% 定义的手动错误将程序加载到芯片上时出现。我现在不知道从哪里开始查找错误。连接时我的串行终端的控制台没有显示任何输出。

我创建了一个最初加载的配置文件:

configGl​​obal.h

#ifndef F_CPU
#warning "F_CPU not yet defined, and will be set to 1MHz"
#define F_CPU 1000000UL
#endif

USART.c

#include "configGlobal.h"
#include <avr/io.h>
#include "USART.h"

#define BAUD 9600UL
#define BAUDRATE ((F_CPU) / (BAUD * 8UL) - 1) // set baud rate value for UBRR

void initUSART(void) {
// Requires BAUD
UBRR0H = (BAUDRATE >> 8) // Shift regisgter right by 8 bits to get upper 8 bits
UBRR0L = BAUDRATE;
UCSR0A |= (1 << U2X0);

// Enable USART
UCSR0B = (1 << TXEN0) | (1 << RXEN0); // Activate TX RX
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); // 8 data bits, 1 stop bit
}

ma​​in.c

#include "configGlobal.h"
#include <avr/io.h>
#include <util/delay.h>
#include "USART.h"

int main(void) {

char serialCharacter;

// --- INITS --- //
DDRB = 0xff;

// Set up LED for output
initUSART();
printString("Hello World!\r\n");

return 0;
}

AVRDUDE 的输出

AVR Development Stack
Get input main.c file...
In file included from main.c:1:0:
configGlobal.h:2:2: warning: #warning "F_CPU not yet defined, and will be set to 1MHz" [-Wcpp]
#warning "F_CPU not yet defined, and will be set to 1MHz"
^
In file included from USART.c:1:0:
configGlobal.h:2:2: warning: #warning "F_CPU not yet defined, and will be set to 1MHz" [-Wcpp]
#warning "F_CPU not yet defined, and will be set to 1MHz"
^
In file included from /usr/local/CrossPack-AVR-20131216/avr/include/avr/io.h:99:0,
from USART.c:2:
USART.c: In function 'initUSART':
USART.c:10:2: error: called object is not a function or function pointer
UBRR0L = BAUDRATE;
^
Convert elf file to hex...
Uploading data to microcontroller...

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: Expected signature for ATmega328 is 1E 95 14
Double check chip, or use -F to override this check.

avrdude done. Thank you.

最佳答案

编译器错误是由于缺少“;”造成的在 USART.c(第 10 行)中。如果您不确定可用的波特率,您可以检查波特率兼容性,例如: http://wormfood.net/avrbaudcalc.php

请记住,使用 AVR 内部 RC 振荡器作为时钟源可能会导致 UART 不稳定,因为频率不完全为 8MHz(或 CKDIV8=1 时为 1MHz)。

avrdude-log 的底部表明设备设置不正确。您确定连接的是 ATmega328 而不是 ATmega328P?

关于c - AVR ATMEGA328p 波特率错误/无串行连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47368616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com