新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 用于ATmega128的軟件UART范例程序

用于ATmega128的軟件UART范例程序

作者: 時間:2012-10-29 來源:網(wǎng)絡(luò) 收藏

一般教科書上提供的收發(fā)的往往是一段采用輪循(Polling)方式完成收發(fā)的簡單代碼。但對于高速的AVR來講,采用這種方式大大降低了 MUC的效率。在使用AVR時,應(yīng)根據(jù)芯片本身的特點(片內(nèi)大容量數(shù)據(jù)存儲器RAM,更適合采用高級語言編寫系統(tǒng)),編寫高效可靠的收發(fā)接口(低層)。下面是一個典型的USART的接口程序。

本文引用地址:http://butianyuan.cn/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 下一頁

關(guān)鍵詞: 范例 程序 UART 軟件 ATmega128 用于

評論


相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉