AVR ATMEGA8 串口USART
avr串口配置很简单,配置就几个寄存器就可以进收发;
但有几点要搞明白的是:
1、串口一但被配置成功IO功能自动被占用,这点与LPC或STM8/32不同(需要寄存配置);
2、没有专门的串口开起或闭关功位(整个串口);
3、读写控制寄存器C需要把URSEL位写成1,而读写UBRRH寄存器时须要URSEL写成0;
1: //晶振频率
2: #define F_CPU 6000000UL
3: //波特率
4: #define BAUD 9600
5:
6: //M8串口1初始化
7: void UsartInit(void)
8: {
9: unsigned int tmp;
10: FifoInit(&RxFifo, sizeof(RxFifo.data));
11: FifoInit(&TxFifo, sizeof(TxFifo.data));
12: tmp= F_CPU/BAUD/16-1;
13: SetBit(SREG, 7);
14: UCSRB = 0;
15: UCSRC = 0;
16: UBRRH=(unsigned char)(tmp>>8);
17: UBRRL=(unsigned char)tmp;
18: UCSRB |= (1<<RXEN)|(1<<TXEN); //允许发送和接收
19: UCSRC |= (1 << URSEL)|(1<<UCSZ1)|(1<<UCSZ0); //8位数据位+1位停止位
20: }
posted on 2013-07-30 15:25 Worldsing 阅读(1008) 评论(0) 编辑 收藏 举报