📄 pumpctl.h
字号:
/****************************************************************
*
* Microchip 16-bit Embedded Control Design Contest
*
* Entry # MT2268
*
* Spa Pump Controller
*
*****************************************************************
*
* Systemwide definitions
*
*****************************************************************/
#include <p30f2020.h>
#define WDOG // Enable watchdog
// #define EXTENDED_TEMP // Use extended temperature range part
// #define HIGH_SPEED // Use high range FRC oscillator frequency
/*****************************************************************************/
#ifdef MAIN
#define EXTERN
#else
#define EXTERN extern
#endif
#ifdef EXTENDED_TEMP
#ifdef HIGH_SPEED
#error "High speed clock (30 MIPS) not available in extended temperature range part"
#else // HIGH_SPEED
#define FRC_CLK_RANGE FRC_HI_RANGE // Select high range (9.7 MHz)
#define FRC_CLK_FREQ 9700UL // Clock oscillator frequency (kHz)
#endif // HIGH_SPEED
#else // EXTENDED_TEMP
#ifdef HIGH_SPEED
#define FRC_CLK_RANGE FRC_HI_RANGE // Select high range (14.55 MHz)
#define FRC_CLK_FREQ 14550UL // Clock oscillator frequency (kHz)
#else // HIGH_SPEED
#define FRC_CLK_RANGE FRC_LO_RANGE // Select low range (9.7 MHz)
#define FRC_CLK_FREQ 9700UL // Clock oscillator frequency (kHz)
#endif // HIGH_SPEED
#endif // EXTENDED_TEMP
#ifdef WDOG
#define WDOG_CLR __asm__ volatile ("clrwdt")
#else
#define WDOG_CLR
#endif
typedef unsigned char BYTE;
typedef unsigned int WORD;
typedef unsigned long LONG;
typedef signed char SBYTE;
typedef signed int SWORD;
typedef signed long SLONG;
/*
All of the 16-bit control variables are stored in Q.14 notation,
which means there are 14 bits of fractional value. This leaves
room for one bit of overflow plus one sign bit. We keep the
sign bit even for unsigned values, which lets us convert
between unsigned and signed without worrying about overflow.
This corresponds to a range of -2.0 to +1.999939 for signed,
and 0 to 1.999939 for unsigned values.
*/
// Some convenient constants
#define Q14_ONE 0x4000 // 1.0 in Q.14 format
#define Q14_ONE_L 0x4000L // Long version
#define Q14_ONE_UL 0x4000UL // Unsigned long version
#define Q14_SQRT2 0x5A82 // SQRT(2)
// The compiler seems to choke on a 16x16 multiply with 32 bit result.
// It treats it as having a 16-bit result, clobbering the result high word.
// It also gets confused trying to multiply a signed with an unsigned value.
// No amount of (cast)-ing seems to help, so we have to do it the hard way.
#define MUL_SS(src1, src2) __builtin_mulss (src1, src2)
#define MUL_UU(src1, src2) __builtin_muluu (src1, src2)
#define DIV_SS(top, bot) __builtin_divsd (top, bot)
#define DIV_UU(top, bot) __builtin_divud (top, bot)
#define SQUARE(src) __builtin_mulss (src, src)
// The following macros multiply or divide with a 14 or 15-bit shift,
// which maintains the proper fractional scaling.
#define MUL_Q14(src1, src2) (MUL_SS (src1, src2) >> 14L)
#define MUL_Q14U(src1, src2) (MUL_UU (src1, src2) >> 14L)
#define MUL_Q15(src1, src2) (MUL_SS (src1, src2) >> 15L)
#define DIV_Q14(top, bot) (DIV_SS (((SLONG) top) << 14L, bot))
// Some other useful macros
// Take the absolute value of a word
#define WABS(x) ((WORD)(((x) >= 0) ? (x) : - (x)))
#define DISABLE_INTS __asm__ volatile ("disi #0x3FFF")
#define ENABLE_INTS __asm__ volatile ("disi #0x0000")
EXTERN enum { // System state
S_INIT = 0, // Initializing
S_STARTUP = 1, // Starting up
S_INRUSH = 2, // Waiting for bus caps to charge up
S_PFC = 3, // Waiting for PFC to stabilize
S_RUNNING = 4, // Normal running state
S_SHUTDOWN = 5, // Normal shutdown
S_FAULT = 6 // Fault shutdown
} sys_state;
struct SYSMODE { // System mode settings
// Inverter modes:
BYTE dt_comp :1; // Enable deadtime compensation
BYTE vbus_comp :1; // Enable bus voltage compensation
BYTE vbus_hold :1; // Enable bus voltage-based ramp hold
BYTE delta_adj :1; // Enable automatic motor phase tuning
BYTE offset_adj :1; // Enable automatic motor voltage tuning
// PFC modes:
BYTE load_comp :1; // Enable feed-forward compensation from inverter
// System modes:
BYTE freq_meas :1; // Enable line frequency-based system clock tuning
WORD unused :9;
};
EXTERN union {
struct SYSMODE b;
WORD w;
} sys_mode;
EXTERN enum {
FAULT_ping_time = 0x0001, // Ping timeout
FAULT_ser_xmit = 0x0002, // Serial xmit buffer not empty
FAULT_inrush_fast = 0x0040, // Inrush charging time too short
FAULT_inrush_time = 0x0080, // Inrush charging timeout
FAULT_inv_temp = 0x0100, // Inverter overheat
FAULT_pfc_temp = 0x0200, // PFC overheat
FAULT_inv_fault = 0x0400, // Inverter PWM fault
FAULT_pfc_fault = 0x0800, // PFC PWM fault
FAULT_pwr_fail = 0x1000, // Line power failure
FAULT_ack = 0x8000 // Fault acknowledged in main routine
} sys_fault; // Global system fault flags
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -