⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uart.h

📁 avr dual uart communication api
💻 H
字号:
/*
  Jesper Hansen <jesperh@telia.com>
  
  Rewritten by:	Nikolai Vorontsov <nickviz@mail.be>

  Original Author: Volker Oth <volkeroth@gmx.de>

  This file is part of the yampp system.

  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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/


#ifndef __UART_H__
#define __UART_H__

#include "types.h"
#include "Constants.h"


/* UART Baud rate calculation */
#define UART_CPU				F_CPU
#define UART_BAUD_RATE			115200
#define UART_BAUD_SELECT		(UART_CPU / (UART_BAUD_RATE * 16L) - 1)

#define USART_BAUD_L 				(UART_BAUD_SELECT & 0x00ff)
#define USART_BAUD_H 				((UART_BAUD_SELECT & 0xff00) >> 8)


#define UART1_BAUD_RATE			115200
#define UART1_BAUD_SELECT		(UART_CPU / (UART1_BAUD_RATE * 16L) - 1)

#define USART1_BAUD_L 			(UART1_BAUD_SELECT & 0x00ff)
#define USART1_BAUD_H 			((UART1_BAUD_SELECT & 0xff00) >> 8)


#define USART_TX_EN_CH		    	(1<<TXEN0)
#define USART_TXINT_EN_CH		(1<<TXCIE0)
#define USART_RX_EN_CH		    	(1<<RXEN0)
#define USART_RXINT_EN_CH		(1<<RXCIE0)
#define USART_FRM_8BIT_CH		(1<<UCSZ01)|(1<<UCSZ00)
#define USART_FRM_NOPARITY_CH		(0<<UPM01)|(0<<UPM00)
#define USART_FRM_1STOP_CH		(0<<USBS0)
#define BAUD_RATE_LOW_REG		UBRR0L
#define UART_CONTROL_REG		UCSR0B
#define ENABLE_TRANSMITTER_BIT	TXEN0
#define ENABLE_RECEIVER_BIT		RXEN0
#define UART_STATUS_REG	        	UCSR0A
#define TRANSMIT_COMPLETE_BIT		TXC0
#define RECEIVE_COMPLETE_BIT		RXC0
#define UART_DATA_REG			UDR0   

#define USART1_TX_EN_CH		    	(1<<TXEN1)
#define USART1_TXINT_EN_CH		(1<<TXCIE1)
#define USART1_RX_EN_CH		    	(1<<RXEN1)
#define USART1_RXINT_EN_CH		(1<<RXCIE1)
#define USART1_FRM_8BIT_CH		(1<<UCSZ11)|(1<<UCSZ10)
#define USART1_FRM_NOPARITY_CH	(0<<UPM11)|(0<<UPM10)
#define USART1_FRM_1STOP_CH		(0<<USBS1)
#define BAUD1_RATE_LOW_REG		UBRR1L
#define UART1_CONTROL_REG		UCSR1B
#define ENABLE1_TRANSMITTER_BIT	TXEN1
#define ENABLE1_RECEIVER_BIT		RXEN1
#define UART1_STATUS_REG	        	UCSR1A
#define TRANSMIT1_COMPLETE_BIT		TXC1
#define RECEIVE1_COMPLETE_BIT		RXC1
#define UART1_DATA_REG			UDR1   


/* Global functions */
#ifdef ENABLE_SERIAL
extern volatile bool UART_CharReceived;

// uart0 setting
void UART_Init(void);
#define UART_HasChar() UART_CharReceived
u08  UART_ReceiveByte(void);
bool UART_CheckRxBuf(void);
extern void UART_SendByte(u08 Data);
extern void UART_Puts(u08* pBuf);
extern void UART_Puts_p(u08* p);
extern void UART_Putsln(u08* pBuf);
extern void UART_PrintfEndOfLine(void);
extern void UART_Printfu08(u08 Data);
extern void UART_Printfu16(u16 Data);
extern void UART_Printfu32(u32 Data);

extern void UART1_Printfu08(u08 Data);
extern void UART1_Printfu16(u16 Data);

#define PRINT(string)	UART_Puts(string)
#define PRINT_p(addr)	UART_Puts_p(addr)
#define EOL()		UART_PrintfEndOfLine()


// uart1_setting
extern volatile bool UART1_CharReceived;

//void UART1_Init(void);
#define UART1_HasChar() UART1_CharReceived
//u08  UART1_ReceiveByte(void);
//bool UART1_CheckRxBuf(void);
extern void UART1_SendByte(u08 Data);
extern void UART1_Puts(u08* pBuf);
extern void UART1_Puts_p(u08* p);
extern void UART1_Putsln(u08* pBuf);
extern void UART1_PrintfEndOfLine(void);
extern void UART1_Printfu08(u08 Data);
extern void UART1_Printfu16(u16 Data);
extern void UART1_Printfu32(u32 Data);

#define PRINT1(string)	UART1_Puts(string)
#define PRINT1_p(addr)	UART1_Puts_p(addr)
#define EOL1()		UART1_PrintfEndOfLine()


#else // !ENABLE_SERIAL

#define UART_Init()
#define UART_HasChar() false
#define UART_SendByte(Data)
#define UART_ReceiveByte() 0
#define UART_Puts(pBuf)
#define UART_Puts_p(p)
#define UART_Putsln(pBuf)
#define UART_PrintfEndOfLine()
#define UART_Printfu08()
#define UART_Printfu16(Data)
#define UART_Printfu32(Data)

#define PRINT(string)	
#define PRINT_p(addr)	
#define EOL()

// uart1
//#define UART1_Init()
#define UART1_HasChar() false
#define UART1_SendByte(Data)
//#define UART1_ReceiveByte() 0
#define UART1_Puts(pBuf)
#define UART1_Puts_p(p)
#define UART1_Putsln(pBuf)
#define UART1_PrintfEndOfLine()
#define UART1_Printfu08()
#define UART1_Printfu16(Data)
#define UART1_Printfu32(Data)

#define PRINT1(string)	
#define PRINT1_p(addr)	
#define EOL1()

#endif // ifdef ENABLE_SERIAL

#endif

⌨️ 快捷键说明

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