ATMEGA162串口波特率无法更改问题

2019-03-24 20:54发布

我用ATMEGA162的串口0进行串口实验,但是无论怎么设置波特率,波特率总是1200,无法更改,请高手帮忙解决。谢谢!下面是程序:
//ICC-AVR application builder
// Target : M162
// Crystal: 8Mhz

#include <iom162v.h>
#include <macros.h>

#define uchar unsigned char
#define uint unsigned int

uchar flag_uart0;
uchar rxd0[3];
uchar j0=0;
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x08;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00;
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x02;
PORTE = 0x00;
DDRE = 0x00;
}
//UART0 initialisation
// desired baud rate: 9600
// actual: baud rate:9615 (0.2%)
// CHAR size: 8 bit
// parity: Disabled
void uart0_init(void)
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00; //disable while setting baud rate
UCSR0C =BIT(URSEL0) | 0x06;
UBRR0L =0x33; //set baud rate
UBRR0H = 0x00;

UCSR0A = 0x00; //enable
UCSR0B = 0x98; //enable
}


#pragma interrupt_handler uart0_rx_isr:iv_USART0_RXC
void uart0_rx_isr(void)
{
CLI();
rxd0[0]=UDR0;

flag_uart0=1;

SEI();
}

///////////////////////////////////////////////////////////////////////
//串口0发送程序//
///////////////////////////////////////////////////////////////////////
void send0_one(uchar dat1)
{while ( !( UCSR0A & (1<<UDRE0)) ) ;
/* 将数据放入缓冲器,发送数据 */    
UDR0= dat1;
}


//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();
//uart1_init();

MCUCR= 0x00;
EMCUCR = 0x00;
// GIMSK= 0x00;//这个好像是软件产生的一个错误
TIMSK= 0x00; //timer interrupt sources
ETIMSK=0x00;
GICR= 0x00;
PCMSK0=0x00;
PCMSK1=0x00;
SEI(); //re-enable interrupts
//all peripherals are now initialized
}

void main(void)
{
init_devices();
flag_uart0=0;
while(1)
{
if (flag_uart0==1)
{flag_uart0=0;
  send0_one(rxd0[0]);
}
}

此帖出自小平头技术问答
0条回答

一周热门 更多>