新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > atmega16串口通信

atmega16串口通信

作者: 时间:2016-12-01 来源:网络 收藏

USART 波特率寄存器- UBRRL和UBRRH

UCSRC寄存器与UBRRH寄存器共用相同的I/O地址。

• Bit 15 – URSEL: 寄存器选择

通过该位选择访问UCSRC 寄存器或UBRRH 寄存器。当读UBRRH 时,该位为0 ;当写UBRRH 时, URSEL 为0。

• Bit 14:12 – 保留位

这些位是为以后的使用而保留的。为了与以后的器件兼容,写UBRRH 时将这些位清零。

• Bit 11:0 – UBRR11:0: USART 波特率寄存器

这个12 位的寄存器包含了USART 的波特率信息。其中UBRRH 包含了USART 波特率高4 位,UBRRL 包含了低8 位。波特率的改变将造成正在进行的数据传输受到破坏。写UBRRL 将立即更新波特率分频器。

进行通信之前首先要对USART 进行初始化。初始化过程通常包括波特率的设定,帧结构的设定,以及根据需要使能接收器或发送器。对于中断驱动的USART 操作,在初始化时首先要清零全局中断标志位( 全局中断被屏蔽)

串口初始化:

使用串口->使能接收 ->使能发送->波特率(本例使用 9600)->奇偶校验(disable)->数据位数(8bit)->中断(RX Complete interrupt)

//ICC-AVR application builder : 2007-5-10 下午 08:51:56

// Target : M16

// Crystal: 11.059Mhz

//UART0 initialisation

// desired baud rate: 9600

// actual: baud rate:9600 (0.0%)

// char size: 8 bit

// parity: Disabled

void uart0_init(void)

{

UCSRB = 0x00; //disable while setting baud rate

UCSRA = 0x00;

UCSRC = 0x86;

UBRRL = 0x47; //set baud rate lo

UBRRH = 0x00; //set baud rate hi

UCSRB = 0x98;

}

//省略了端口初始化

//call this routine to initialise 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 initialised


上一页 1 2 3 下一页

关键词: atmega16串口通

评论


技术专区

关闭