【问题标题】:How do i use the Serial rx/tx on my atmega2560如何在我的 atmega2560 上使用串行 rx/tx
【发布时间】:2021-10-05 19:30:29
【问题描述】:
#include <atmel_start.h>
#include <util/delay.h>
#include <avr/io.h>
#define F_CPU   8000000
#define BAUD    9600
#define BRC     ((F_CPU/(16UL * BAUD)) - 1)
void USART_Transmit_2( unsigned char data )
        {
            /* Wait for empty transmit buffer */
            while ( !( UCSR2A & (1<<UDRE2)) )
            ;
            
            UDR2 = data;/* Put data into buffer, sends the data */
            PORTB = 0b00100000; /* Turn on first LED bit/pin in PORTB */
            _delay_ms(500); /* wait */
            PORTB = 0b00000000; /* Turn off all B pins, including LED */
            _delay_ms(500);
        }
void USART_init_2(void){
        UBRR2H = (BRC >> 8);
        UBRR2L = BRC;
        
        UCSR2B = (1 << TXEN2) | (1 << RXEN2);
        UCSR2C = (1 << UCSZ21) | (1 << UCSZ20);
}
int main(void)
{
    
    /* Initializes MCU, drivers and middleware */
    //atmel_start_init();
    
    USART_init_2();
    DDRB = 0b00000001;
    /* Replace with your application code */
    while (1) {
        
        USART_Transmit_2('h');
        _delay_ms(1000);
        
    }
}

这是我的准系统代码,我无法弄清楚为什么没有信号从引脚输出。在 TX 上,信号始终为高电平。

USART_Transmit_2 中的灯会打开和关闭。

任何帮助将不胜感激。

【问题讨论】:

  • 取消评论atmel_start_init();?
  • 我现在不使用它,即使不注释它也没有太大变化
  • @displayer 将 LED 恰好打开 500 毫秒,然后关闭 500 毫秒?如果不是,则可能是由于错误的保险丝字节设置。在默认(发货)模式下,预分频为 8 (CKDIV8) 的内部 8MHz 振荡器被编程。所以你只有 1MHz,延迟程序需要更长的时间然后 500ms。此外,UART 不适用于错误的 UBRR 值。请您验证这些设置吗?
  • 我查了一下,LED亮250ms灭250ms,UBRR值是正确的。
  • 我的保险丝是这样的:extended=0xFD,HIGH=0xD0,LOW=0xFF

标签: c atmega


【解决方案1】:

所以我完全是个傻瓜,不知何故我没有检查正确的频道,代码在 rx/tx 3 而不是 rx/tx 2 上按原样工作。不太确定为什么数据表另有说明,但是嘿,它现在可以使用了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    • 2012-11-11
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 2017-01-12
    • 1970-01-01
    相关资源
    最近更新 更多