/*
* AVRGCC1.c
*
* Created: 2016/10/23 17:11:04
* Author: xuyuntonge
*/
#include <avr/io.h>
#include <avr/pgmspace.h>
//#include <avr/interrupt.h>
#include <avr/signal.h>
#include <string.h>
#include <stdio.h>
#include <util/delay.h>
#define F_CPU 4000000UL //初始化晶振频率
void USART_Init(unsigned char baud );
void USART_Transmit( unsigned char data );
unsigned char USART_Receive( void );
void USART_Init(unsigned char baud)
{
unsigned int tmp;
tmp= F_CPU/baud/16-1;
UBRR0H = (unsigned char)(tmp>>8);
UBRR0L = (unsigned char)tmp;
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
UCSR0C = (1<<UMSEL00)|(3<<UCSZ00);
}
void USART_Transmit( unsigned char data )
{
while ( !( UCSR0A & (1<<UDRE0)) );
UDR0 = data;
}
unsigned char USART_Receive( void )
{
while ( !(UCSR0A & (1<<RXC0)) );
return UDR0;
}
int main(void)
{
DDRD=0xff;
USART_Init(9600);
while (1)
{
USART_Transmit(1);
_delay_ms(300);
}
}
代码如上
此帖出自
小平头技术问答
{
unsigned int tmp;
tmp= F_CPU/baud/16-1;
UBRRH =(unsigned char)(tmp>>8);
UBRRL = (unsigned char)tmp;
UCSRB = (1<<RXEN)|(1<<TXEN)|(1<<RXCIE); //接收完成中断使能
UCSRC = (1<<URSEL)|(3<<UCSZ0); //UCSRS=(3<<1)=0000 0110
sei();
}
以前写过的MEGA16看看配置差不多也是可以用的,但是到了mega48稍微改了下就不能用了感觉很奇怪,
是不是哪里疏漏了,求解
一周热门 更多>