您的位置 首页 电路

AVR单片机atmega16串口中止接纳和发送程序

//Target:M16//Crystal:8.0000Mhz//AVR单片机atmega16串口中断接收和发送程序。接收到字符后再发送出去#includeiom16v.h>#inc…

// Target : M16

// Crystal: 8.0000Mhz
//AVR单片机atmega16串口中止接纳和发送程序。接纳到字符后再发送出去
#include
#include
unsigned char com_in_buf[1];
void port_init(void)
{
PORTA = 0xFF;
DDRA = 0xFF;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00;
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}

//UART0 initialize
// desired baud rate: 9600
// actual: baud rate:9615 (0.2%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00;
UCSRC = BIT(URSEL) | 0x06;
UBRRL = 0x33; //set baud rate lo
UBRRH = 0x00; //set baud rate hi
UCSRB = 0x98;
}

void send_data(unsigned char back)
{
while( !( UCSRA & (1< UDR=back;
}

#pragma interrupt_handler uart0_rx_isr:12
void uart0_rx_isr(void)
{
//uart has received a character in UDR
com_in_buf[0]=UDR;
send_data(com_in_buf[0]);
}

//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
}

void main(void)
{
char j;
init_devices();
while(1)
{
for(j=1;j<100;j++)
;
}
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/fangan/dianlu/273196.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部