4105|0

6

帖子

0

TA的资源

一粒金砂(中级)

楼主
 

ATMEGA162串口波特率无法更改问题 [复制链接]

我用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]);
}
}

点赞 关注
 

回复
举报
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/8 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表