搜索
bottom↓
回复: 10

ICCAVR库函数USART中断例子

[复制链接]

出0入0汤圆

发表于 2006-5-9 15:48:25 | 显示全部楼层 |阅读模式
//ICC-AVR application builder : 09-05-2006 8:59:17

// Target : M16

// Crystal: 4.0000Mhz

/*

RX程序流程:

没有新字符输入,头指针和尾指针相同

有新字符输入,中断处理,头指针加一,取UDR到RX-BUFER

之后RECEIVE程序中对尾指针加一,准备接收下一个字符,同时返回当前字符

TX程序流程:

TRANS接收到RX的输入字符,头指针加一,将数据存入发送缓冲区,打开UDRIE寄存器空中断,进入中断程序。

尾指针加一,之后发送字符,直到头尾指针相同,即发送完毕,关闭UDRIE中断寄存器。



*/



#include <iom16v.h>

#include <macros.h>

/* UART Buffer Defines */

#define UART_RX_BUFFER_SIZE 128 /* 1,2,4,8,16,32,64,128 or 256 bytes */

#define UART_RX_BUFFER_MASK ( UART_RX_BUFFER_SIZE - 1 )

#define UART_TX_BUFFER_SIZE 128 /* 1,2,4,8,16,32,64,128 or 256 bytes */

#define UART_TX_BUFFER_MASK ( UART_TX_BUFFER_SIZE - 1 )



#if ( UART_RX_BUFFER_SIZE & UART_RX_BUFFER_MASK )

#error RX buffer size is not a power of 2

#endif



/* Static Variables */

static unsigned char UART_RxBuf[UART_RX_BUFFER_SIZE];

static volatile unsigned char UART_RxHead;

static volatile unsigned char UART_RxTail;

static unsigned char UART_TxBuf[UART_TX_BUFFER_SIZE];

static volatile unsigned char UART_TxHead;

static volatile unsigned char UART_TxTail;

static volatile unsigned char CHECK=0;

void port_init(void)

{

PORTA = 0x00;

DDRA  = 0x00;

PORTB = 0x00;

DDRB  = 0x00;

PORTC = 0x00; //m103 output only

DDRC  = 0x00;

PORTD = 0x00;

DDRD  = 0x00;

}



//UART0 initialize

// desired baud rate: 9600

// actual: baud rate:2404 (0.2%)

// char size: 5 bits

// parity: Disabled

void uart0_init(void)

{

unsigned char x;

x=0;

UART_RxTail = x;

UART_RxHead = x;

UART_TxTail = x;

UART_TxHead = x;

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

UCSRA = 0x00;

UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);

UBRRL = 0x19; //set baud rate lo

UBRRH = 0x00; //set baud rate hi

UCSRB = (1<<RXCIE)|(1<<TXEN)|(1<<RXEN);//|(1<<TXCIE)

}

unsigned char DataInReceiveBuffer( void )

        {

        return ( UART_TxHead != UART_TxTail );

                /* return 0 (FALSE) if the TX complete */

        }

#pragma interrupt_handler uart0_rx_isr:12

void uart0_rx_isr(void)

{

//uart has received a character in UDR

        unsigned char data;

        unsigned char tmphead;

        data = UDR; /* read the received data */

        /* calculate buffer index */

        tmphead = ( UART_RxHead + 1 ) & UART_RX_BUFFER_MASK;

        UART_RxHead = tmphead; /* store new index */

        if ( tmphead == UART_RxTail )

                {

                /* ERROR! Receive buffer overflow */

                /*仿真中没有出现这种情况*/

                }

        UART_RxBuf[tmphead] = data; /* store received data in buffer */

}



#pragma interrupt_handler uart0_tx_isr:14

void uart0_tx_isr(void)

{

//character has been transmitted

        unsigned char tmptail;

    CHECK++;

        /* check if all data is transmitted */

        if ( DataInReceiveBuffer( ))//UART_TxHead != UART_TxTail

                {

                /* calculate buffer index */

                tmptail = ( UART_TxTail + 1 ) & UART_TX_BUFFER_MASK;

                UART_TxTail = tmptail; /* store new index */

                UDR = UART_TxBuf[tmptail]; /* start transmition */

                }

        else

                {

                UCSRB &= ~(1<<UDRIE); /* disable UDRE interrupt */

                }

       

}



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



MCUCR = 0x00;

GICR  = 0x00;

TIMSK = 0x00; //timer interrupt sources

SEI(); //re-enable interrupts

//all peripherals are now initialized

}

/* Read and write functions */

unsigned char ReceiveByte( void )

        {

        unsigned char tmptail;



        while ( UART_RxHead == UART_RxTail ) /* wait for incomming data */

                ;

        tmptail = ( UART_RxTail + 1 ) & UART_RX_BUFFER_MASK;/* calculate buffer index */

        UART_RxTail = tmptail; /* store new index */

        return UART_RxBuf[tmptail]; /* return data */

        }



void TransmitByte( unsigned char data )

        {

        unsigned char tmphead;

        /* calculate buffer index */

        tmphead = ( UART_TxHead + 1 ) & UART_TX_BUFFER_MASK;

                /* wait for free space in buffer */



        while ( tmphead == UART_TxTail )

                ;

        UART_TxBuf[tmphead] = data; /* store data in buffer */

        UART_TxHead = tmphead; /* store new index */

        UCSRB |= (1<<UDRIE); /* enable UDRE interrupt */

        }







void main(void)

{

init_devices();

while ( 1 ) /* forever */

                {

                TransmitByte( ReceiveByte() ); /* echo the received character */

                }

}

出0入0汤圆

 楼主| 发表于 2006-5-10 10:20:45 | 显示全部楼层
点击此处下载armok01116888.rar

出0入0汤圆

发表于 2006-5-27 17:31:48 | 显示全部楼层
请问:不知楼主是在什么版本下仿真的,我用6.7sp3不成功。谢谢!

出0入0汤圆

 楼主| 发表于 2006-5-28 14:57:08 | 显示全部楼层
6.7sp3

出0入0汤圆

发表于 2006-5-28 23:06:22 | 显示全部楼层
不错,谢谢!!

出0入0汤圆

发表于 2006-5-29 19:17:58 | 显示全部楼层
alisha 大侠:

你好!

能把AVR.DLL这个文件传上来吗?

我怀疑我那个有问题

谢谢

出0入0汤圆

 楼主| 发表于 2006-5-30 14:09:11 | 显示全部楼层
你从新安装一下就应该可以吧!

出0入0汤圆

发表于 2006-5-30 15:54:50 | 显示全部楼层
谢谢alisha !

我改用6.9了

出0入0汤圆

发表于 2006-9-11 11:43:04 | 显示全部楼层
怎么不行呢

好像没有任何显示

出0入0汤圆

发表于 2014-9-25 17:03:46 | 显示全部楼层
刚刚看了一下,程序这里是否存在问题啊  这里是开 UDRE 中断,UCSRB |= (1<<UDRIE); /* enable UDRE interrupt */ ,而实际中断地址是14应该不是UDRE中断,而是TX结束中断。 不知道楼主有没有注意这个问题?还是我理解的有问题?

出0入0汤圆

发表于 2014-9-25 17:22:49 | 显示全部楼层
不知道有没有人回复啊
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-4-19 21:56

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表