📄 ti_cc_mega128.h
字号:
#include "common.h"
/*******************************************************************************************************
*******************************************************************************************************
************************** AVR I/O PORTS **************************
*******************************************************************************************************
*******************************************************************************************************/
//-------------------------------------------------------------------------------------------------------
// Port A
#define PWM0 0 // PA.0
#define PWM1 1 // PA.1
#define PWM2 2 // PA.2
#define PWM3 3 // PA.3
#define PWM4 4 // PA.4
#define PWM5 5 // PA.5
#define PWM6 6 // PA.5
#define PWM7 7 // PA.5
#define PA_INIT do {DDRA=0xFF;PORTA=0x00;} while(0)
//-------------------------------------------------------------------------------------------------------
// Port B
#define CSN 0 // PB.0 - Output: SPI Chip Select (CS_N)
#define SCK 1 // PB.1 - Output: SPI Serial Clock (SCLK)
#define MOSI 2 // PB.2 - Output: SPI Master out - slave in (MOSI)
#define MISO 3 // PB.3 - Input: SPI Master in - slave out (MISO)
#define PB4_NC 4 // PB.4 - NC;
#define PB5_NC 5 // PB.5 - NC
#define PB_INIT do {DDRB= BM(MOSI) | BM(SCK) | BM(CSN);PORTB=0x3F;} while(0)
//-------------------------------------------------------------------------------------------------------
// Port C
#define PWM8 0 // PC.0
#define PWM9 1 // PC.1
#define PWM10 2 // PC.2
#define PWM11 3 // PC.5
#define PWM12 4 // PC.4
#define PWM13 5 // PC.5
#define PWM14 6 // PC.5
#define PWM15 7 // PC.5
#define PC_INIT do {DDRC=0xFF;PORTC=0x00;} while(0)
//-------------------------------------------------------------------------------------------------------
// Port D
#define PD0_NC 0 // PD.0 - Input: INT0,
#define PD1_NC 1 // PD.1 - Input : INT1,
#define UART1_RXD 2 // PD.2 - Input: UART1 RXD
#define UART1_TXD 3 // PD.3 - Output: UART1 TXD
#define PD4_NC 4 // PD.4 - NC
#define PD5_NC 5 // PD.5 - NC
#define PD6_NC 6 // PD.6 - NC;
#define LED_RDY 7 // PD.7 - Output: LED_RDY
#define PD_INIT do {DDRD =( BM(UART1_TXD))|(BM(LED_RDY)) ; PORTD = 0x7F;} while(0)
// Enables/disables the SPI interface
#define SPI_ENABLE() (PORTB &= ~BM(CSN))
#define SPI_DISABLE() (PORTB |= BM(CSN))
//-------------------------------------------------------------------------------------------------------
/*******************************************************************************************************
*******************************************************************************************************
************************** LEDS **************************
*******************************************************************************************************
*******************************************************************************************************/
//-------------------------------------------------------------------------------------------------------
//define ready_led
#define SET_LED_RDY() (PORTD |= BM(LED_RDY))
#define CLR_LED_RDY() (PORTD &= 0x7F)
#define TOGGLE_LED_RDY() (PORTD ^= BM(LED_RDY))
/*******************************************************************************************************
************************** SERIAL PORT (UART1) **************************
*******************************************************************************************************
*******************************************************************************************************/
//-------------------------------------------------------------------------------------------------------
// INIT_UART1(baUDRate,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_UART1(baUDRate,options) \
do \
{ \
UBRR1H=(baUDRate)>>8; \
UBRR1L=(baUDRate); \
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_UART1() (UCSR1B |= (BM(RXEN1) | BM(TXEN1)))
#define DISABLE_UART1() (UCSR1B &= ~(BM(RXEN1) | BM(TXEN1)))
#define ENABLE_UART1_RX() (UCSR1B |= (BM(RXEN1)))
#define DISABLE_UART1_RX() (UCSR1B &= ~(BM(RXEN1)))
//-------------------------------------------------------------------------------------------------------
// Macros which are helful when transmitting and receiving data over the serial interface.
//-------------------------------------------------------------------------------------------------------
#define UART1_WAIT() do { while (!(UCSR1A & BM(UDRIE1))); CLEAR_UART1_TX_INT(); } while (0)
#define UART1_SEND(x) do { UDR1 = (x); } while (0)
#define UART1_WAIT_AND_SEND(x) do { UART1_WAIT(); UART1_SEND(x); } while (0)
#define UART1_RECEIVE(x) do { (x) = UDR1; } while (0)
#define UART1_WAIT_AND_RECEIVE(x) do { UDR1 = 0; UART1_WAIT(); UART1_RECEIVE(x); } 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_UART1_INT() do { UCSR1B |= (BM(UDRIE1) | BM(RXCIE1)); } while (0)
#define DISABLE_UART1_INT() do { UCSR1B &= ~(BM(UDRIE1) | BM(RXCIE1)); } while (0)
#define ENABLE_UART1_TX_INT() do { UCSR1B |= BM(UDRIE1); } while (0)
#define DISABLE_UART1_TX_INT() do { UCSR1B &= ~BM(UDRIE1); } while (0)
#define CLEAR_UART1_TX_INT() do { UCSR1A &= ~BM(UDRE1); } while (0)
#define SET_UART1_TX_INT() do { UCSR1A |= BM(UDRE1); } while (0)
#define ENABLE_UART1_RX_INT() do { UCSR1A &= ~BM(RXC1); UCSR1B |= BM(RXCIE1); } while (0)
#define DISABLE_UART1_RX_INT() do { UCSR1B &= ~BM(RXCIE1); } while (0)
#define CLEAR_UART1_RX_INT() do { UCSR1A &= ~BM(RXC1); } while (0)
//-------------------------------------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -