📄 emot.h
字号:
#ifndef EMOT_H
#define EMOT_H
#include <iocc2430.h>
typedef unsigned int WORD;
typedef unsigned char BYTE;
typedef WORD UINT16;
/*****************************************
//IO功能定义
*****************************************/
#define IO_FUNC_PORT_PIN(port, pin, func) \
do { \
if((port == 2) && (pin == 3)){ \
if (func) { \
P2SEL |= 0x02; \
} else { \
P2SEL &= ~0x02; \
} \
} \
else if((port == 2) && (pin == 4)){ \
if (func) { \
P2SEL |= 0x04; \
} else { \
P2SEL &= ~0x04; \
} \
} \
else{ \
if (func) { \
P##port##SEL |= (0x01<<(pin)); \
} else { \
P##port##SEL &= ~(0x01<<(pin));\
} \
} \
} while (0)
// where func is one of:
#define IO_FUNC_GIO 0 // General purpose I/O
#define IO_FUNC_PERIPH 1 // Peripheral function
/*****************************************
//时钟定义
*****************************************/
// Macro for getting the clock division factor
#define CLKSPD (CLKCON & 0x07)
// Macro for getting the timer tick division factor.
#define TICKSPD ((CLKCON & 0x38) >> 3)
// Macro for checking status of the crystal oscillator
#define XOSC_STABLE (SLEEP & 0x40)
// Macro for checking status of the high frequency RC oscillator.
#define HIGH_FREQUENCY_RC_OSC_STABLE (SLEEP & 0x20)
//macro for setting CLKCON.TICKSPD
#define SET_TICKSPD_DIVIDE(val) do{CLKCON &= ~0X38 ; CLKCON |= (unsigned char)(val);}while(0)
/*
// Where _val_ is one of
#define 32M_TICK 0x00
#define 16M_TICK 0x08
#define 8M_TICK 0x10
#define 4M_TICK 0x18
#define 2M_TICK 0x20
#define 1M_TICK 0x28
#define 0_5M_TICK 0x30
#define 0_2_5M_TICK 0x38
*/
// Macro for setting the 32 KHz clock source
#define SET_32KHZ_CLOCK_SOURCE(source) \
do { \
if( source ) { \
CLKCON |= 0x80; \
} else { \
CLKCON &= ~0x80; \
} \
} while (0)
// Where _source_ is one of
#define CRYSTAL 0x00
#define RC 0x01
// Macro for setting the main clock oscillator source,
//turns off the clock source not used
//changing to XOSC will take approx 150 us
#define SET_MAIN_CLOCK_SOURCE(source) \
do { \
if(source) { \
CLKCON |= 0x40; \
while(!HIGH_FREQUENCY_RC_OSC_STABLE); \
if(TICKSPD == 0){ \
CLKCON |= 0x08; \
} \
SLEEP |= 0x04; \
} \
else { \
SLEEP &= ~0x04; \
while(!XOSC_STABLE); \
asm("NOP"); \
CLKCON &= ~0x47; \
SLEEP |= 0x04; \
} \
}while (0)
/*****************************************
//T4配置定义
*****************************************/
// Where _timer_ must be either 3 or 4
// Macro for initialising timer 3 or 4
#define TIMER34_INIT(timer) \
do { \
T##timer##CTL = 0x06; \
T##timer##CCTL0 = 0x00; \
T##timer##CC0 = 0x00; \
T##timer##CCTL1 = 0x00; \
T##timer##CC1 = 0x00; \
} while (0)
//Macro for enabling overflow interrupt
#define TIMER34_ENABLE_OVERFLOW_INT(timer,val) \
do{ (T##timer##CTL = (val) ? T##timer##CTL | 0x08 : T##timer##CTL & ~0x08); \
EA = 1; \
T##timer##IE = 1; \
}while(0)
// Macros for configuring IO peripheral location:
#define IO_PER_LOC_TIMER4_AT_PORT1_PIN01() do { PERCFG = (PERCFG&~0x10)|0x00; } while (0)
#define IO_PER_LOC_TIMER4_AT_PORT2_PIN03() do { PERCFG = (PERCFG&~0x10)|0x10; } while (0)
// Actual MCU pin configuration:
//
// Peripheral I/O signal Alt1 Alt2
// -------------------------------------------
// Timer4 channel0 P1.0 P2.0
// Timer4 channel1 P1.1 P2.3
// Macro for enabling/ disabling interrupts from the channels of timer 1, 3 or 4.
#define TIMER_CHANNEL_INTERRUPT_ENABLE(timer, channel, value) \
do{ \
if(value){ \
T##timer##CCTL##channel## |= 0x40; \
} else { \
T##timer##CCTL##channel## &= ~0x40; \
} \
} while(0)
// Macro for configuring channel 1 of timer 3 or 4 for PWM mode.
#define TIMER34_PWM_CONFIG(timer) \
do{ \
T##timer##CCTL1 = 0x24; \
if(timer == 3){ \
if(PERCFG & 0x20) { \
IO_FUNC_PORT_PIN(1,7,IO_FUNC_PERIPH); \
} \
else { \
IO_FUNC_PORT_PIN(1,4,IO_FUNC_PERIPH); \
} \
} \
else { \
if(PERCFG & 0x10) { \
IO_FUNC_PORT_PIN(2,3,IO_FUNC_PERIPH);\
} \
else { \
IO_FUNC_PORT_PIN(1,1,IO_FUNC_PERIPH); \
} \
} \
} while(0)
// Macro for setting pulse length of the timer in PWM mode
#define TIMER34_SET_PWM_PULSE_LENGTH(timer, value) \
do { \
T##timer##CC1 = (BYTE)value; \
} while (0)
// Macro for setting timer 3 or 4 as a capture timer
#define TIMER34_CAPTURE_TIMER(timer,edge) \
do{ \
T##timer##CCTL1 = edge; \
if(timer == 3){ \
if(PERCFG & 0x20) { \
IO_FUNC_PORT_PIN(1,7,IO_FUNC_PERIPH); \
} \
else { \
IO_FUNC_PORT_PIN(1,4,IO_FUNC_PERIPH); \
} \
} \
else { \
if(PERCFG & 0x10) { \
IO_FUNC_PORT_PIN(2,3,IO_FUNC_PERIPH); \
} \
else { \
IO_FUNC_PORT_PIN(1,1,IO_FUNC_PERIPH); \
} \
} \
}while(0)
//Macro for setting the clock tick for timer3 or 4
#define TIMER34_START(timer,val) \
(T##timer##CTL = (val) ? T##timer##CTL | 0X10 : T##timer##CTL&~0X10)
#define TIMER34_SET_CLOCK_DIVIDE(timer,val) \
do{ \
T##timer##CTL &= ~0XE0; \
(val==2) ? (T##timer##CTL|=0X20): \
(val==4) ? (T##timer##CTL|=0x40): \
(val==8) ? (T##timer##CTL|=0X60): \
(val==16)? (T##timer##CTL|=0x80): \
(val==32)? (T##timer##CTL|=0xa0): \
(val==64) ? (T##timer##CTL|=0xc0): \
(val==128) ? (T##timer##CTL|=0XE0): \
(T##timer##CTL|=0X00); /* 1 */ \
}while(0)
//Macro for setting the mode of timer3 or 4
#define TIMER34_SET_MODE(timer,val) \
do{ \
T##timer##CTL &= ~0X03; \
(val==1)?(T##timer##CTL|=0X01): /*DOWN */ \
(val==2)?(T##timer##CTL|=0X02): /*Modulo */ \
(val==3)?(T##timer##CTL|=0X03): /*UP / DOWN */ \
(T##timer##CTL|=0X00); /*free runing */ \
}while(0)
#define T4_MODE_FREE 0X00
#define T4_MODE_DOWN 0X01
#define T4_MODE_MODULO 0X02
#define T4_MODE_UP_DOWN 0X03
//macro for setting the compare value of timer3 or 4
#define TIMER34_SET_CPM_VALUE(timer,channel,val) \
do{ T##timer##CC##channel## = (unsigned int)(val); }while(0)
// Where _timer_ must be either 3 or 4
// Where _channel_ must be either 1 or 2
/*********************************************************************
//键盘定义
*********************************************************************/
#define KEY1 P1_2
#define KEY2 P1_3
#define ENABLE_KEY_ON_BOARD() \
do{ \
P1SEL &= ~0X0C; \
P1DIR &= ~0X0C; \
P1INP |= 0x0c; \
}while(0)
#endif //emot.h
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -