• 已解决 73482 个问题
  • 已帮助 5993 位优秀工程师

ATmega32A串口通信奇怪问题

cloudou 2015-09-05 浏览量:1492
测试ATmega32A串口通信,使用内部1M振荡,
熔丝设置  通信发送ASCII码0~2,返回十六进制均为两个字节,3e 00, 4c 00, 66 00,如此简单的东西都无**常工作,令人费解。请大家帮帮忙,谢谢啦!
//ICC-AVR application builder : 2015-09-05 10:23:35
// Target : M32
// Crystal: 1.0000Mhz

#include
#include

void USART_Transmit( unsigned char data );

void port_init(void)
{
PORTA = 0x00;
DDRA  = 0x00;
PORTB = 0x00;
DDRB  = 0x00;
PORTC = 0x00; 
DDRC  = 0x00;
PORTD = 0x00;
DDRD  = 0x00;
}

//UART0 initialize
// desired baud rate: 9600
// actual: baud rate:9615 (0.2%)
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x02;
UCSRC = BIT(URSEL) | 0x06;
UBRRL = 0x0C; //set baud rate lo
UBRRH = 0x00; //set baud rate hi
UCSRB = 0x98;
}

#pragma interrupt_handler uart0_rx_isr:iv_USART0_RXC
void uart0_rx_isr(void)
{
//uart has received a character in UDR
unsigned char i=0;
i=UDR;
USART_Transmit(i);
}

//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
uart0_init();

MCUCR = 0x00;
GICR  = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}


void USART_Transmit( unsigned char data )
{
/* 等待发送缓冲器为空 */
while ( !( UCSRA & (1<
;
/* 将数据放入缓冲器,发送数据 */
UDR = data;
}

void main(void)
{
        init_devices();
        while(1)
        {
        }
}

0 0 收起

我来回答

上传资料:
选择文件 文件大小不超过15M(格式支持:doc、ppt、xls、pdf、zip、rar、txt)
所有亮答 数量:9
  • 看起来是你的设置问题。全部是直接赋值寄存器,没有用位设置,这样的程序看起来很累啊

  • 检查一下时钟配置和波特率配置这部分程序;

    至于说发一个字节返回两个字节,检查一下串口调试助手上,发送接收的相关设置;

  • 程序是ICCAVR软件中的ApplicationBuilder应用产生的

  • 还没解决啊,检查了很多次,都觉得没问题的,这很简单的啊,看来要借示波器看看了。

  • 你把完整程序发给我。不用示波器,用Proteus仿真效果也不错。

    不过我用AVRGCC。

  • 最近忙,现在才请教,对不起了,请问你的联系方式,我发程序给你帮看看,谢谢啦

  • 再其它社区回答了这个问题。我用proteus仿真没有问题,检查一下硬件,特别是地线。

  • 谢谢啦!晚上有时间查下啦。

相关问题

问题达人换一批

ATmega32A串口通信奇怪问题