新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > 用于ATmega128的软件UART范例程序

用于ATmega128的软件UART范例程序

作者:时间:2012-10-29来源:网络收藏

一般教科书上提供的收发的往往是一段采用轮循(Polling)方式完成收发的简单代码。但对于高速的AVR来讲,采用这种方式大大降低了 MUC的效率。在使用AVR时,应根据芯片本身的特点(片内大容量数据存储器RAM,更适合采用高级语言编写系统),编写高效可靠的收发接口(低层)。下面是一个典型的USART的接口程序。

本文引用地址:http://www.amcfsurvey.com/article/170727.htm

#include

#define RXB8 1

#define TXB8 0

#define UPE 2

#define OVR 3

#define FE 4

#define UDRE 5

#define RXC 7

#define FRAMING_ERROR (1

#define PARITY_ERROR (1

#define DATA_OVERRUN (1

#define DATA_REGISTER_EMPTY (1

#define RX_COMPLEte (1

// USART0 Receiver buffer

#define RX_BUFFER_SIZE0 8

char rx_buffer0[RX_BUFFER_SIZE0];

unsigned char rx_wr_index0,rx_rd_index0,rx_counter0;

// This flag is set ON USART0 Receiver buffer overflow

bit rx_buffer_overflow0;

// USART0 Receiver interrupt service routine

#pragma savereg-

interrupt [USART0_RXC] void uart0_rx_isr(void)

{

char status,data;

#asm

push r26

push r27

push r30

push r31

in r26,sreg

push r26

#endasm

status=UCSR0A;

data=UDR0;

if ((status (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)

{

rx_buffer0[rx_wr_index0]=data;

if (++rx_wr_index0 == RX_BUFFER_SIZE0) rx_wr_index0=0;

if (++rx_counter0 == RX_BUFFER_SIZE0)

{

rx_counter0=0;

rx_buffer_overflow0=1;

};

};

#asm

pop r26

out sreg,r26

pop r31

pop r30

pop r27

pop r26

#endasm

}

#pragma savereg+

#ifndef _DEBUG_TERMINAL_IO_

// Get a character from the USART0 Receiver buffer

#define _ALTERNATE_GETCHAR_

#pragma used+

char getchar(void)

{

char data;

while (rx_counter0==0);

data=rx_buffer0[rx_rd_index0];

if (++rx_rd_index0 == RX_BUFFER_SIZE0) rx_rd_index0=0;

#asm(cli)

--rx_counter0;

#asm(sei)

return data;

}

#pragma used-

#endif

// USART0 Transmitter buffer

#define TX_BUFFER_SIZE0 8

char tx_buffer0[TX_BUFFER_SIZE0];

unsigned char tx_wr_index0,tx_rd_index0,tx_counter0;

// USART0 Transmitter interrupt service routine

#pragma savereg-

interrupt [USART0_TXC] void uart0_tx_isr(void)

{

#asm

push r26

push r27

push r30

push r31

in r26,sreg

push r26

#edasm

if (tx_counter0)

{

--tx_counter0;

UDR0=tx_buffer0[tx_rd_index0];

if (++tx_rd_index0 == TX_BUFFER_SIZE0) tx_rd_index0=0;

};

#asm

pop r26

out sreg,r26

pop r31

pop r30

pop r27

pop r26

#endasm

}

#pragma savereg+

#ifndef _DEBUG_TERMINAL_IO_

// Write a character to the USART0 Transmitter buffer

#define _ALTERNATE_PUTCHAR_

#pragma used+

void putchar(char c)

{

while (tx_counter0 == TX_BUFFER_SIZE0);

#asm(cli)

if (tx_counter0 || ((UCSR0A DATA_REGISTER_EMPTY)==0))

{

tx_buffer0[tx_wr_index0]=c;

if (++tx_wr_index0 == TX_BUFFER_SIZE0) tx_wr_index0=0;

++tx_counter0;

}

else

UDR0=c;

#asm(sei)

}

#pragma used-

#endif

// Standard Input/Output functions

#include

// Declare your global variables here

void main(void)

{

// USART0 initialization

// Communication Parameters: 8 Data, 1 Stop, No Parity

// USART0 Receiver: On

// USART0 Transmitter: On

// USART0 Mode: Asynchronous

// USART0 Baud rate: 9600

UCSR0A=0x00;

UCSR0B=0xD8;

UCSR0C=0x06;

UBRR0H=0x00;

UBRR0L=0x67;

// Global enable interrupts

#asm(sei)

while (1)

{

// Place your code here

};

}


上一页 1 2 下一页

评论


相关推荐

技术专区

关闭