usart.c

来自「his project was built and tested with Wi」· C语言 代码 · 共 79 行

C
79
字号
/* Interface an Atmel AVR MCU with Freescales MC1319x ZigBee chip */

/*
 * Author: johan@anteeo.se
 *
 * Copyright (C) 2005 by Anteeo Systems HB, Sweden
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * 
 * Created:     Fri May 13 22:46:58 2005, johan@anteeo.se
 *
 */

#include <avr/io.h>
#include <avr/interrupt.h>
#include <string.h>

#include "usart.h"

void USART_Init( unsigned int ubrr)
{
	/*Set baud rate */
	//UBRR0H = (unsigned char)(ubrr>>8);
	//UBRR0L = (unsigned char)ubrr;
	UCSR0A|=_BV(U2X0);
	UBRR0H=0;
	//UBRR0L=16; // 115k@16MHz
	//UBRR0L=25; // 38k4@8MHz
	UBRR0L=8; // 115k@8MHz

	// Enable receiver and transmitter
	UCSR0B = (1<<RXEN0)|(1<<TXEN0);
	/* Set frame format: 8data, 1stop bit */
	UCSR0C = (3<<UCSZ00);
}

void USART_TransmitStr( unsigned char *data)
{
	USART_Transmit(data, strlen(data));
}

void USART_Transmit( unsigned char *data, unsigned char len )
{
	for(unsigned char i=0; i<len; ++i)
	{
		/* Wait for empty transmit buffer */
		while ( !( UCSR0A & (1<<UDRE0)) )
		;
		/* Put data into buffer, sends the data */
		UDR0 = data[i];
	}
}

void USART_EnableIRQ(void)
{
	// Enable receive irq
	UCSR0B|=_BV(RXCIE0);
	sei();
}

// Int from USART
ISR (SIG_USART_RECV)
{
	USART_ReceivedByte(UDR0);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?