#include <iom16v.h>
#include <macros.h>
#define FOSC 8000000
#define BAUD 9600
void delay(unsigned int n)
{
unsigned int i;
while (n--)
for (i=0;i<1140;i++);
}
void UART_Init(void)
{
UCSRB=0x00; //禁止发送和接收
UCSRA=0x02; //倍速异步模式USX=1
UCSRC=0x06;
UBRRL=(FOSC/8/(BAUD+1))%256;
UBRRH=(FOSC/8/(BAUD+1))/256;
UCSRB=0xd8; //允许发送和接收,接收和发送结束中断使能
}
void UART_Send(unsigned char Data)
{
while (!(UCSRA&BIT(UDRE))); //等待数据寄存器为空
UDR=Data;
}
void UART_SendString(char* pStr)
{
while (*pStr!='\0')
{
UART_Send(*pStr);
pStr++;
}
}
void main(void)
{
DDRD=0xf3; //两个外中断口设为输入
PORTD=0xff;
UART_Init();
while (1)
{
UART_SendString("This is a string sent by ATMega16.\n");
delay(1000);
}
}