📄 tecfunctions.c
字号:
/*********************************************************************
* File: TecFunctions.c *
* Description: Contains functions used in the TEC application. *
* DSP: TMS320F2812 *
* Author: David M. Alter *
* Function List: *
* void FirFilter(PID *x, int *a, int n) *
* void InitGptimer2(float32 timer_Hz) *
* void InitPwm(Uint16 period, Uint16 compare) *
* void PidCtrl1(PID* x) *
* History: *
* 11/05/02 - Original, based on DSP28 header files v0.58 (D. Alter)*
* Notes: none *
*********************************************************************/
/*********************************************************************
* THIS PROGRAM IS PROVIDED "AS IS". TI MAKES NO WARRANTIES OR *
* REPRESENTATIONS, EITHER EXPRESS, IMPLIED OR STATUTORY, *
* INCLUDING ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS *
* FOR A PARTICULAR PURPOSE, LACK OF VIRUSES, ACCURACY OR *
* COMPLETENESS OF RESPONSES, RESULTS AND LACK OF NEGLIGENCE. *
* TI DISCLAIMS ANY WARRANTY OF TITLE, QUIET ENJOYMENT, QUIET *
* POSSESSION, AND NON-INFRINGEMENT OF ANY THIRD PARTY *
* INTELLECTUAL PROPERTY RIGHTS WITH REGARD TO THE PROGRAM OR *
* YOUR USE OF THE PROGRAM. *
* *
* IN NO EVENT SHALL TI BE LIABLE FOR ANY SPECIAL, INCIDENTAL, *
* CONSEQUENTIAL OR INDIRECT DAMAGES, HOWEVER CAUSED, ON ANY *
* THEORY OF LIABILITY AND WHETHER OR NOT TI HAS BEEN ADVISED *
* OF THE POSSIBILITY OF SUCH DAMAGES, ARISING IN ANY WAY OUT *
* OF THIS AGREEMENT, THE PROGRAM, OR YOUR USE OF THE PROGRAM. *
* EXCLUDED DAMAGES INCLUDE, BUT ARE NOT LIMITED TO, COST OF *
* REMOVAL OR REINSTALLATION, COMPUTER TIME, LABOR COSTS, LOSS *
* OF GOODWILL, LOSS OF PROFITS, LOSS OF SAVINGS, OR LOSS OF *
* USE OR INTERRUPTION OF BUSINESS. IN NO EVENT WILL TI'S *
* AGGREGATE LIABILITY UNDER THIS AGREEMENT OR ARISING OUT OF *
* YOUR USE OF THE PROGRAM EXCEED FIVE HUNDRED DOLLARS *
* (U.S.$500). *
* *
* Unless otherwise stated, the Program written and copyrighted *
* by Texas Instruments is distributed as "freeware". You may, *
* only under TI's copyright in the Program, use and modify the *
* Program without any charge or restriction. You may *
* distribute to third parties, provided that you transfer a *
* copy of this license to the third party and the third party *
* agrees to these terms by its first use of the Program. You *
* must reproduce the copyright notice and any other legend of *
* ownership on each copy or partial copy, of the Program. *
* *
* You acknowledge and agree that the Program contains *
* copyrighted material, trade secrets and other TI proprietary *
* information and is protected by copyright laws, *
* international copyright treaties, and trade secret laws, as *
* well as other intellectual property laws. To protect TI's *
* rights in the Program, you agree not to decompile, reverse *
* engineer, disassemble or otherwise translate any object code *
* versions of the Program to a human-readable form. You agree *
* that in no event will you alter, remove or destroy any *
* copyright notice included in the Program. TI reserves all *
* rights not specifically granted under this license. Except *
* as specifically provided herein, nothing in this agreement *
* shall be construed as conferring by implication, estoppel, *
* or otherwise, upon you, any license or other right under any *
* TI patents, copyrights or trade secrets. *
* *
* You may not use the Program in non-TI devices. *
*********************************************************************/
#include "Device.h"
/*********************************************************************
* Function: FirFilter() *
* Description: Implements an I1Q15 fixed-point FIR filter on a PID *
* structure. *
* DSP: TMS320F2812 *
* Author: David M. Alter *
* Include files: none *
* Function Prototype: void FirFilter(PID*, int*, int) *
* Useage: PidCtrl1(x, a, n); *
* Input Parameters: x = structure of type PID *
* a = array of coefficients *
* n = number of coefficients *
* Return Value: none *
* Notes: The filtering is done from last tap to first tap. This *
* allows more efficient delay chain updating. The user must make *
* sure the PID structure has at least as many delay chain entries *
* as there are coefficients. *
*********************************************************************/
void FirFilter(PID *x, int *a, int n)
{
/*** Local variables ***/
int32 temp32; // general purpose unsigned long32
int16 *p; // general purpose pointer to int16
Uint16 i; // general purpose unsigned int16
/*** Setup pointers ***/
p = &x->y + (n-1); // pointer setup to end of delay chain
a = a + (n-1); // point to the last coefficient
temp32 = 0; // initialize the sum
/*** Last tap has no delay chain update ***/
temp32 = temp32 + ( ((int32)*a--) * ((int32)*p--) );
/*** Do the rest of the taps with delay chain update ***/
for(i=n-1; i>0; i--)
{
temp32 = temp32 + ( ((int32)*a--) * ((int32)*p--) );
*(p+1) = *p; // update delay chain
}
/*** Convert final value to floating point and put into the PID structure ***/
x->yf = (3.0/0x3FFC0000L)*(float32)(temp32);
} //end FirFilter()
/*********************************************************************
* Function: InitGptimer2() *
* Description: Initializes GP Timer 2 to give periodic interrupt. *
* DSP: TMS320F2812 *
* Author: David M. Alter *
* Include files: none *
* Function Prototype: void InitGptimer2(Uint16) *
* Useage: InitGptimer2(period); *
* Input Parameters: Uint16 period = timer period (in counts) *
* Return Value: none *
* Notes: *
* 1) uses x/128 clock prescaler *
*********************************************************************/
void InitGptimer2(Uint16 period)
{
EvaRegs.T2CON.all = 0x0000; //disable timer
EvaRegs.T2CNT = 0x0000; //clear timer counter
EvaRegs.T2PR = period; //set timer period
EvaRegs.T2CMPR = 0x0006; //set compare for ADC trigger
EvaRegs.GPTCONA.all = (EvaRegs.GPTCONA.all | 0x0620) & 0x0FF3;
/* x = don't change
bit 15 0: reserved
bit 14 0: T2STAT, read-only
bit 13 0: T1STAT, read-only
bit 12 0: T2CTRIPE, 0=disable timer2 compare trip
bit 11 x: T1CTRIPE, 0=disable timer1 compare trip
bit 10-9 11: T2TOADC, 11 = timer2 compare flag starts ADC
bit 8-7 xx: T1TOADC
bit 6 x: TCOMPOE, 0 = Hi-z all timer compare outputs
bit 5 1: T2COMPOE, 0 = timer2 compare HI-z'd
bit 4 x: T1COMPOE, 0 = timer1 compare HI-z'd
bit 3-2 00: T2PIN, 00 = forced low
bit 1-0 xx: T1PIN
*/
EvaRegs.T2CON.all = 0xD782; //enable timer
/*
bit 15-14 11: 11=do not stop on emulator suspend
bit 13 0: reserved
bit 12-11 10: 10 = continuous-up count mode
bit 10-8 111: 111 = x/128 prescaler
bit 7 1: T2SWT1, 1 = use GPTimer1 TENABLE bit
bit 6 0: TENABLE, 1 = enable timer
bit 5-4 00: 00 = HSPCLK is clock source
bit 3-2 00: 00 = reload compare reg on underflow
bit 1 1: 0 = enable timer compare
bit 0 0: SELT1PR, 0 = use own period register
*/
} //end InitGptimer2()
/*********************************************************************
* Function: InitPwm() *
* Description: Initializes PWM1/2 pin using GP Timer 1. *
* DSP: TMS320F2812 *
* Author: David M. Alter *
* Include files: none *
* Function Prototype: void InitPwm(Uint16) *
* Useage: InitPwm(period); *
* Input Parameters: Uint16 period = timer counts per PWM period. *
* Return Value: none *
* Notes: none *
*********************************************************************/
void InitPwm(Uint16 period)
{
EvaRegs.T1CON.all = 0x0000; // disable timer
EvaRegs.T1CNT = 0x0000; // clear timer counter
EvaRegs.T1PR = period; // set timer period
EvaRegs.GPTCONA.all = (EvaRegs.GPTCONA.all | 0x0000) & 0xF66C;
/* x = don't change
bit 15 x: reserved
bit 14 x: T2STAT, read-only
bit 13 x: T1STAT, read-only
bit 12 x: T2CTRIPE
bit 11 0: T1CTRIPE
bit 10-9 xx: T2TOADC
bit 8-7 00: T1TOADC, 00 = no timer1 event starts ADC
bit 6 x: TCOMPOE, 0 = Hi-z all timer compare outputs
bit 5 x: T2CMPOE
bit 4 0: T1CMPOE, 0 = Hi-Z T1CMPR output
bit 3-2 xx: T2PIN
bit 1-0 00: T1PIN, 00 = forced low
*/
EvaRegs.DBTCONA.all = 0x0000; // deadband units off
EvaRegs.CMPR1 = 0x0000; // initial duty cycle is 0%
EvaRegs.ACTRA.all = 0x0000; // all PWM pins forced low initially
/*
bit 15 0: space vector dir is CCW (don't care)
bit 14-12 000: basic space vector is 000 (dont' care)
bit 11-10 00: PWM6 pin forced low
bit 9-8 00: PWM5 pin forced low
bit 7-6 00: PWM4 pin forced low
bit 5-4 00: PWM3 pin forced low
bit 3-2 00: PWM2 pin forced low
bit 1-0 00: PWM1 pin forced low
*/
EvaRegs.COMCONA.all = 0x8220; // configure COMCON register
/*
bit 15 1: CENABLE, 1 = enable compare operation
bit 14-13 00: CLD, 00 = reload CMPRx regs on timer 1 underflow
bit 12 0: SVENABLE, 0 = space vector disabled
bit 11-10 00: ACTRLD, 00 = reload ACTR on timer 1 underflow
bit 9 1: FCMPOE, 1 = enable PWM pins
bit 8 0: PDPINT1 status (read-only)
bit 7 0: FCMP3OE, 0 = PWM5/6 are Hi-Z
bit 6 0: FCMP2OE, 0 = PWM3/4 are Hi-Z
bit 5 1: FCMP1OE, 0 = PWM1/2 are Hi-Z
bit 4-3 00: reserved
bit 2 0: C3TRIPE, 0 = trip disabled
bit 1 0: C2TRIPE, 0 = trip disabled
bit 0 0: C1TRIPE, 0 = trip disabled
*/
EvaRegs.T1CON.all = 0xD040; // enable timer
/*
bit 15-14 11: 11=do not stop on emulator suspend
bit 13 0: reserved
bit 12-11 10: TMODE, 10 = continuous-up count mode
bit 10-8 000: TPS, 000 = x/1 prescaler
bit 7 0: T2SWT1, 0 = use own TENABLE bit
bit 6 1: TENABLE, 1 = enable timer
bit 5-4 00: 00 = HSPCLK is clock source
bit 3-2 00: TCLD, 00 = reload compare reg on underflow
bit 1 0: TECMPR, 0 = disable timer compare
bit 0 0: SELT1PR, 0 = use own period register
*/
} //end InitPwm()
/*********************************************************************
* Function: PidCtrl1() *
* Description: Implements floating point PID control with *
* anti-windup on I-mode and saturation of the total control signal. *
* DSP: TMS320F2812 *
* Author: David M. Alter *
* Include files: none *
* Function Prototype: void PidCtrl1(PID*) *
* Useage: PidCtrl1(x); *
* Input Parameters: x = structure of type PID *
* Return Value: none *
* Notes: none *
*********************************************************************/
void PidCtrl1(PID *x)
{
/*** Local variables ***/
float32 e, up, ui, ud;
/*** Form the error ***/
e = x->r - x->yf;
/*** P-mode calculation ***/
up = x->Kp * e;
/*** I-mode calculation ***/
ui = x->ui1 + (x->Ki * (e + x->e1));
/*** Saturate the I-mode (anti-windup) ***/
if(ui > x->ui_max) ui = x->ui_max;
else if(ui < -(x->ui_max)) ui = -(x->ui_max);
/*** D-mode calculation ***/
ud = x->Kd * (e - x->e1);
/*** update the controller history ***/
x->e1 = e; /* save e to e1 */
x->ui1 = ui; /* save ui to ui1 */
/*** Form the total control signal ***/
x->u = up + ui + ud;
/*** Saturate the total control signal ***/
if(x->u > x->u_max) x->u = x->u_max;
else if(x->u < -(x->u_max)) x->u = -(x->u_max);
} //end PidCtrl1()
/*** end of file *****************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -