|
#include <mega16.h>
unsigned char data;
bit flag = 0;
void Port_Init()
{
DDRD = 0X02;
PORTD = 0X03;
}
void USART_Init(unsigned int xtal,unsigned int baud)
{
UBRRH = (xtal/16/baud-1)>>8;
UBRRL = (xtal/16/baud-1) ;
UCSRA = 0X00;
UCSRB = (1<<TXEN)|(1<<RXEN)|(1<<RXCIE);
UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);
}
void USART_Transmit(unsigned char data)
{
while(!(UCSRA & (1<<UDRE))) ;
UDR = data;
}
interrupt [USART_RXC] void usart_rx_isr(void)
{
data = UDR;
flag = 1;
}
void main()
{
unsigned char xtal = 12000000 , baud = 9600;
Port_Init();
USART_Init(xtal,baud);
#asm("sei")
while(1)
{
if(flag)
{
flag = 0;
USART_Transmit(data);
}
}
} |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|