ATmega328p Xplained Mini 串口发送数据

作者: ddllxxrr
上传时间为: 2015-01-12 09:57 AM
2015-01-12
阅读:

这个小板首先得搞清楚工作的晶振,我始终糊涂,我一直以为8MRC内部振荡,今天打开图纸一看才恍然大悟。

原来是16M外部晶振,同时给两个芯片。我晕!!!

解决完晶振问题自然是寄存器,同发射有关的寄存器如下:

UCSRnA

BIT7 - RXCn:usart Receive Complete

BIT6 - TXCn:USART Transmit Complete

Bit5 - UDREn:USART Data Register Empty

Bit4 - FEn:Frame Error

Bit3 - DORn:Data OverRun

Bit2 - UPEn:USART Parity Error

Bit1 - U2Xn:Double the USART Transmission Speed

Bit0 - MPCMn:Mutlti-processor Communication Mode

UCSRnB

Bit7 - RXCIEn:RX Complete Interrupt Enable n

Bit6 - TXCIEn:TX Complete Interrupt Enable n

Bit5 - UDRIEn:USART Data Register Empty Interrupt Enable n

Bit4 - RXENn:Receiver Enable n

Bit3 - TXENn:Transmitter Enable n

Bit2 - UCSZn2:Character Size n

Bit1 - RXB8n:Receive Data Bit 8n

Bit0 - TXB8n:Transmit Data Bit8n

UCSRnC

Bits 7:6 - UMSELn1:0 USART Mode Select

00异步

01同步

10空

11SPI

Bits 5:4 - UPMn1:0:Parity Mode

Bit3 - USBSn:Stop Bit Select

Bit2:1 - UCSZn1:0:Character Size

Bit 0 - UCPOLn:Clock Polarity

这里边只有掌握几个位就行:

以下是程序:

/*

* GccApplication2.c

*

* Created: 2015/1/8 星期四 21:20:24

* Author: Administrator

*/

#define F_CPU 16000000UL

#define BAUD 9600

#define PUTBAUD F_CPU/16/BAUD-1

#include <avr/io.h>

#include <stdio.h>

#include <stdint.h>

#include <util/delay.h>

char dd[]="Hello eeboard!!!";

void Init_USART(uint16_t ubrr)

{

/*Set baud rate */

UBRR0H = (unsigned char)(ubrr>>8);

UBRR0L = (unsigned char)ubrr;

/*Enable receiver and transmitter */

UCSR0B = (1<<RXEN0)|(1<<TXEN0);

/* Set frame format: 8data, 2stop bit */

UCSR0C = (1<<USBS0)|(3<<UCSZ00);

}

void PutChar(char c)

{

/* Wait for empty transmit buffer */

while ( !( UCSR0A & (1<<UDRE0)) )

;

/* Put data into buffer, sends the data */

UDR0 = c;

}

void PutStr(char *s)

{

while(*s)PutChar(*s++);

}

int main(void)

{

Init_USART( PUTBAUD);

while(1)

{

PutStr(dd);

_delay_ms(1000);

//TODO:: Please write your application code

}

}

全部评论 ()
条评论
写评论

创建讨论帖子

登录 后参与评论
系统提示