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

📄 hal.h

📁 基于CC1100和ATMEGA128开发的无线机器人控制程序
💻 H
字号:
/*******************************************************************************************************
 *                                                                                                    *
 * All HAL library macros and constants are defined here.                                              *
 *******************************************************************************************************
 * Compiler: AVR-GCC                                                                                   *
 * Target platform: CCxx00 + any ATMEGA MCU                                                  *
 * Author:                 Wang Chaoyan IBSS of NUAA
 *******************************************************************************************************
 * Revision history:                                                                                   *
  
 *
 *
 *******************************************************************************************************/

#ifndef HAL_H
#define HAL_H




/*******************************************************************************************************
 *******************************************************************************************************
 **************************            AVR<->CCxx00 SPI INTERFACE             **************************
 *******************************************************************************************************
 *******************************************************************************************************/
//-------------------------------------------------------------------------------------------------------
// Initialization
//SPI ENABLE;
//MSTR置位主机模; 
//CPOL=0,CPHA=0 工作模式为模式0 ;
//SPR1=0,SPR0=0,Focr/4;
#define SPI_INIT() \
    do { \
        SPCR = BM(SPE) | BM(MSTR); \ 
	    } while (0)
//-------------------------------------------------------------------------------------------------------


//-------------------------------------------------------------------------------------------------------
// FAST SPI: Low level functions
//      x = value (BYTE or WORD)
//      p = pointer to the byte array to operate on
//      c = the byte count
//
// SPI_ENABLE() and SPI_DISABLE() are located in the devboard header file (CS_N uses GPIO)

#define FASTSPI_WAIT() \
    do { \
        while (!(SPSR & BM(SPIF))); \
    } while (0) 

#define FASTSPI_TX(x) \
    do { \
        SPDR = x; \
        FASTSPI_WAIT(); \
    } while (0)

#define FASTSPI_RX(x) \
    do { \
        SPDR = 0; \
        FASTSPI_WAIT(); \
        x = SPDR; \
    } while (0)


//-------------------------------------------------------------------------------------------------------


//-------------------------------------------------------------------------------------------------------
//  FAST SPI: Register access
//      s = command strobe
//      a = register address
//      v = register value

#define FASTSPI_STROBE(s) \
    do { \
        SPI_ENABLE(); \
        FASTSPI_TX_ADDR(s); \
        SPI_DISABLE(); \
    } while (0)


//-------------------------------------------------------------------------------------------------------


/*******************************************************************************************************
 *******************************************************************************************************
 **************************                    INTERRUPTS                     **************************
 *******************************************************************************************************
 *******************************************************************************************************/


//-------------------------------------------------------------------------------------------------------
// General
#define ENABLE_GLOBAL_INT()         do { asm ("sei\n\t" ::); } while (0)
#define DISABLE_GLOBAL_INT()        do { asm ("cli\n\t" ::); } while (0)
//-------------------------------------------------------------------------------------------------------


//-------------------------------------------------------------------------------------------------------
// UART1 interrupts
#define ENABLE_UART0_INT()          do { UCSR1B |= (BM(UDRIE1) | BM(RXCIE1)); } while (0)
#define DISABLE_UART0_INT()         do { UCSR1B &= ~(BM(UDRIE1) | BM(RXCIE1)); } while (0) 

#define ENABLE_UART0_TX_INT()       do { UCSR1B |= BM(UDRIE1); } while (0)
#define DISABLE_UART0_TX_INT()      do { UCSR1B &= ~BM(UDRIE1); } while (0) 
#define CLEAR_UART0_TX_INT()        do { UCSR1A &= ~BM(UDRIE1); } while (0)
#define SET_UART0_TX_INT()          do { UCSR1A |= BM(UDRIE1); } while (0)


#define ENABLE_UART0_RX_INT()       do { UCSR1A &= ~BM(RXC1); UCSR1B |= BM(RXCIE1); } while (0)
//#define ENABLE_UART0_RX_INT()       do { UCSR1B |= BM(RXCIE1); } while (0)

#define DISABLE_UART0_RX_INT()      do { UCSR1B &= ~BM(RXCIE1); } while (0) 
#define CLEAR_UART0_RX_INT()        do { UCSR1A &= ~BM(RXC1); } while (0)
//-------------------------------------------------------------------------------------------------------






/*******************************************************************************************************
 *******************************************************************************************************
 **************************               SERIAL PORT (UART1)                 **************************
 *******************************************************************************************************
 *******************************************************************************************************/


//-------------------------------------------------------------------------------------------------------
//  INIT_UART1(baUDR1ate,options)
//
//  DESCRIPTION:
//      A macro which does all the initialization necessary to communicate on UART 1. The UART is
//      configured according to options (defined below). Note that this macro does not call
//      ENABLE_UART1().
//
//  ARGUMENTS:
//      baUDR1ate
//          One of the UART_BAUDR1ATE_... constants defined below
//      options
//          One or more of the UART_OPT constants defined below. The value 0 gives one stop bit, no
//          parity and 5 bits per char.
//-------------------------------------------------------------------------------------------------------
#define INIT_UART0(baUDR1ate,options) \
    do 							\
	{ 							\
		UBRR1H=(baUDR1ate)>>8; 	\
		UBRR1L=(baUDR1ate);		\
		UCSR1C= (options | 0x80);\
	} while (0)

// Baud rate codes for use with the INIT_UART1 macro

#define UART_BAUDRATE_9K6          0x33  // Normal speed, Clk = 8.0MHz



    
// Options for use with the INIT_UART1 macro
#define UART_OPT_ONE_STOP_BIT       0
#define UART_OPT_TWO_STOP_BITS      0x08
#define UART_OPT_NO_PARITY          0
#define UART_OPT_EVEN_PARITY        0x20
#define UART_OPT_ODD_PARITY         0x30
#define UART_OPT_5_BITS_PER_CHAR    0
#define UART_OPT_6_BITS_PER_CHAR    0x02
#define UART_OPT_7_BITS_PER_CHAR    0x04
#define UART_OPT_8_BITS_PER_CHAR    0x06

//-------------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------------
// Enable/disable macros

// Enable/disable UART1
#define ENABLE_UART0()              (UCSR1B |= (BM(RXEN1) | BM(TXEN1))) 
#define DISABLE_UART0()             (UCSR1B &= ~(BM(RXEN1) | BM(TXEN1)))
#define ENABLE_UART0_RX()              (UCSR1B |= (BM(RXEN1))) 
#define DISABLE_UART0_RX()             (UCSR1B &= ~(BM(RXEN1)))

//-------------------------------------------------------------------------------------------------------


//-------------------------------------------------------------------------------------------------------
// Macros which are helful when transmitting and receiving data over the serial interface.
//
// Example of usage:
//      UART0_SEND(pData[0]);
//      for (i = 1; i < len; i++) {
//          UART0_WAIT_AND_SEND(pData[i]);
//      }

#define UART0_WAIT()                do { while (!(UCSR1A & BM(UDRIE1))); CLEAR_UART0_TX_INT(); } while (0)


#define UART0_SEND(x)               do { UDR1 = (x); } while (0)
#define UART0_WAIT_AND_SEND(x)      do { UART0_WAIT(); UART0_SEND(x); } while (0)
#define UART0_RECEIVE(x)            do { (x) = UDR1; } while (0)
#define UART0_WAIT_AND_RECEIVE(x)   do { UDR1 = 0; UART0_WAIT(); UART0_RECEIVE(x); } while (0)
//-------------------------------------------------------------------------------------------------------
//*******************************************************************************************************
//*******************************************************************************************************
//**************************                   RS232                   **************************
//*******************************************************************************************************
//*******************************************************************************************************
//------------------------------------------------------------------------------------------------
// Useful stuff
#define NOP() asm volatile ("nop\n\t" ::)
//-------------------------------------------------------------------------------------------------------




#endif

⌨️ 快捷键说明

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