新闻中心

EEPW首页>嵌入式系统>设计应用> IAR For AVR 串口中断接收

IAR For AVR 串口中断接收

作者: 时间:2016-12-03 来源:网络 收藏
应用芯片: AT Mega16 晶振: 7.3728MHz

代码文件:uart_int.c

本文引用地址://m.amcfsurvey.com/article/201612/325111.htm

|_________DELAY.H

##############################################

DELAY.H :

#ifndef __IAR_DELAY_H
#define __IAR_DELAY_H

#include

#define XTAL7.3728//可定义为你所用的晶振频率(单位Mhz)


#define delay_us(x) __delay_cycles ( (unsigned long)(x * XTAL) )
#define delay_ms(x) __delay_cycles ( (unsigned long)(x * XTAL*1000) )
#define delay_s(x) __delay_cycles ( (unsigned long)(x * XTAL*1000000) )

#endif

uart_int.c :

#include
#include "delay.h"
#define uchar unsigned char
#define uint unsigned int

uchar c;

//###########################################################
/*串口初始化函数*/
voidUart_Init(void)
{
UCSRB = (1< UCSRC = (1<

UBRRH=0x00; //设置波特率寄存器低位字节
UBRRL=47; //9600 //设置波特率寄存器高位字节

SREG_I= 1; //开总中断
DDRD_Bit1=1; //配置TX为输出(很重要)
}
//###########################################################
/*发送一个字符数据,查询方式*/
voidUart_Transmit(uchar data)
{
while(!(UCSRA&(1< //也可以写成 while(UCSRA_UDRE==0);
UDR = data; // 发送数据
}
//###########################################################
/*中断接收*/
#pragma vector=USART_RXC_vect
__interruptvoid USART_RXC_Server(void)
{
UCSRB_RXCIE = 0; //关串口中断
c = UDR ; //将收到的值赋值给变量
Uart_Transmit(c); //发给串口以检测对错
UCSRB_RXCIE = 1; //开串口中断
}
//###########################################################
/*主函数*/
void main(void)
{
Uart_Init();
delay_us(20); //串口初始化后,必须延时20us以上才能发送数据,否则会出现错误
Uart_Transmit(0x64);

while(1)
{ ; } //此时可以用串口助手发送字符,然后可以正确接收
}



关键词:IARAVR串口中断接

评论


技术专区

关闭