?? pid.c
字號:
#include "DSP281x_Device.h" // DSP281x Headerfile Include File
#include "DSP281x_Examples.h" // DSP281x Examples Include File
#include "ctr.h"
#include "PID.h"
// Prototype statements for functions found within this file.
interrupt void cpu_timer0_isr(void);
// 大液晶控制板控制字 ----------------------------------------------------------
#define CTRSTATUS (*(unsigned int *)0x108000) //port8000
#define CTRLED (*(unsigned int *)0x108004) // port8004
#define MCTRKEY (*(unsigned int *)0x108005) // port8005
#define CTRCLKEY (*(unsigned int *)0x108006) // port8006
#define CTRSTATUS (*(unsigned int *)0x108000) //port8000
#define CTRGR (*(unsigned int *)0x108000) //port8000
#define CTRMOTORBSPEED (*(unsigned int *)0x108003) //port8003
void Gpio_select(void);
void Gpio_PortB(void);
void ShowParameters();
void LCDPutString(unsigned int *pData,int x,int y,unsigned int nCharNumber,unsigned color);
void PIDControl(int rk,int yk);
void Delay(unsigned int nTime); // 延時子程序
void PrintParameters();
Uint16 var1 = 0;
Uint16 var2 = 0;
Uint16 var3 = 0;
Uint16 test_count = 0;
Uint16 Test_flag = 0;
Uint16 Test_var = 0;
Uint16 Test_status[32];
int i=0,nnn;
unsigned int nCount,nCount_1=1,nCount1,nCount2,nCount3,nCount4,nJSSpeed,nJSSpeed_1=1,uWork;
unsigned int uLBD;
float a=0.6f,b=0.2f,c=0.1f,duk;
int ek,ek1,ek2,tz;
int nInput;
int nSSS,pwm,pwm1;
int md,wc;
unsigned int nScreenBuffer[30*128],pidnCount3,piduN;
int m_nSpeed,m_bPCSet;
unsigned char ccc;
int uN;
void main(void)
{
unsigned int uWork1;
int inputspeed;
int breakflage;
int speed[100],sp,lj;
float ljh;
int i,w1,w2,w3;
InitSysCtrl();
for ( sp=0;sp<100;sp++ ) speed[sp]=0;
for ( sp=0;sp<(30*128);sp++ ) nScreenBuffer[sp]=0;
sp=nSSS=nCount=nCount1=nCount2=nCount3=nCount4=nJSSpeed=0; nInput=tz=wc=0;
ek=ek1=ek2=0;
uN=40; md=70; pwm1=60;
m_nSpeed=70; m_bPCSet=0;
inputspeed=0;
uWork1=0;
breakflage=0;
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP281x_SysCtrl.c file.
// Step 2. Initalize GPIO:
// This example function is found in the DSP281x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
// InitGpio(); // Skipped for this example
// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
DINT;
// Initialize the PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
// This function is found in the DSP281x_PieCtrl.c file.
InitPieCtrl();
// Disable CPU interrupts and clear all CPU interrupt flags:
IER = 0x0000;
IFR = 0x0000;
// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example. This is useful for debug purposes.
// The shell ISR routines are found in DSP281x_DefaultIsr.c.
// This function is found in DSP281x_PieVect.c.
InitPieVectTable();
// Interrupts that are used in this example are re-mapped to
// ISR functions found within this file.
EALLOW; // This is needed to write to EALLOW protected registers
PieVectTable.TINT0 = &cpu_timer0_isr;
EDIS; // This is needed to disable write to EALLOW protected registers
// Step 4. Initialize all the Device Peripherals:
// This function is found in DSP281x_InitPeripherals.c
// InitPeripherals(); // Not required for this example
/////InitCpuTimers(); // For this example, only initialize the Cpu Timers
CpuTimer0.RegsAddr = &CpuTimer0Regs;
// Initialize timer period to maximum:
CpuTimer0Regs.PRD.all = 3600*2;
//CpuTimer0Regs.PRD.all = 3600*20;
// Initialize pre-scale counter to divide by 1 (SYSCLKOUT):
CpuTimer0Regs.TPR.all = 0;
CpuTimer0Regs.TIM.all = 0;
CpuTimer0Regs.TPRH.all = 0;
// Make sure timer is stopped:
CpuTimer0Regs.TCR.bit.TSS = 1;
CpuTimer0Regs.TCR.bit.SOFT = 1;
CpuTimer0Regs.TCR.bit.FREE = 1;
// Reload all counter register with period value:
CpuTimer0Regs.TCR.bit.TRB = 1;
CpuTimer0Regs.TCR.bit.TIE = 1;
// Reset interrupt counters:
CpuTimer0.InterruptCount = 0;
// Step 5. User specific code, enable interrupts:
// Enable CPU INT1 which is connected to CPU-Timer 0:
IER |= M_INT1;
// Enable TINT0 in the PIE: Group 1 interrupt 7
PieCtrlRegs.PIEIER1.bit.INTx7 = 1;
// Enable global Interrupts and higher priority real-time debug events:
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
Gpio_PortB();
CTRGR=0x80;
CTRGR=0;
CTRGR=0x80;
// 設置顯示參數(shù)和內(nèi)容
LCDSetDelay(1); // 設置延時等待參數(shù)
LCDSetScreenBuffer(nScreenBuffer); // 顯示緩沖區(qū)
LCDTurnOn(); // 打開顯示
LCDCLS(); // 清除顯示內(nèi)存
LCDPutCString(str1,0,127,8,0);
LCDPutCString(str2,0,111,2,1);
LCDPutCString(str3,68,111,2,1);
LCDPutCString(str6,0,95,2,1);
LCDPutCString(str5,68,95,2,1);
LCDPutCString(str7,0,79,3,1);
LCDPutCString(str4,68,79,2,1);
ShowParameters(); // 參數(shù)顯示
CTRGR=01;
StartCpuTimer0();
while ( 1 )
{
//讀鍵盤值
uWork1=MCTRKEY;
uWork1 &=0xff;
CTRCLKEY=0;
if(uWork1==4)
{ Delay(500);
uWork1=MCTRKEY;
uWork1 &=0xff;
CTRCLKEY=0;
md+=10;
ShowParameters(); // 顯示各參數(shù)值到LCD
}
if(uWork1==8)
{ Delay(500);
uWork1=MCTRKEY;
uWork1 &=0xff;
CTRCLKEY=0;
md-=10;
ShowParameters(); // 顯示各參數(shù)值到LCD
}
if(uWork1==1)
{
Delay(500);
uWork1=MCTRKEY;
uWork1 &=0xff;
CTRCLKEY=0;
md+=1;
ShowParameters(); // 顯示各參數(shù)值到LCD
}
if(uWork1==2)
{
Delay(500);
uWork1=MCTRKEY;
uWork1 &=0xff;
CTRCLKEY=0;
md-=1;
ShowParameters(); // 顯示各參數(shù)值到LCD
}
Delay(25);//延時,
if (nJSSpeed_1==0) // 讀取速度標志
{
nJSSpeed_1=1;
nJSSpeed=0;
ccc=CTRMOTORBSPEED; // 從端口讀取速度計數(shù)
ccc=ccc&0xff;
nSSS=ccc;
if ( nSSS>=0 && nSSS<400 ) // 合法性檢測
{
speed[sp]=nSSS; // 讀取66個計數(shù)值
sp++; sp%=66;
}
if ( sp==0 ) // 是否已經(jīng)讀了66個速度?
{ // 以下求速度平均值
lj=0; ljh=0;
for ( i=0;i<66;i++ )
{
if ( speed[i]>=0 && speed[i]<400 )
{
ljh+=speed[i];
lj++;
}
}
wc=( lj==0 )?(0):(ljh/lj);
if ( wc>300 )
{
wc=0;
}
//nCount3++; nCount3%=3;
//if ( nCount3==2 )
//{
PIDControl(md,wc); // 調(diào)用PID算法控制程序進行控制
uN=(100-pwm1)/2; // 利用占空比調(diào)整控制
ShowParameters(); // 顯示各參數(shù)值到LCD
// }
//PrintParameters();
nJSSpeed_1=1;
}
}
}
}
// PID算法控制子程序-------------------------------------------------------------------------
void PIDControl(int rk,int yk)
{
ek=rk-yk;
duk=a*ek+b*ek1+c*ek2; // 計算控制輸出
ek2=ek1; ek1=ek;
if ( duk>2 ) duk=1; // 幅度限制
if ( duk<-2 ) duk=-1; // 幅度限制
tz=(int)duk;
pwm1+=tz; // 計算當前占空比
if ( pwm1<0 ) pwm1=0;
else if ( pwm1>99 ) pwm1=99;
}
#define Hz200 50
#define Hz1 10000
interrupt void cpu_timer0_isr(void)
{
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
CpuTimer0Regs.TCR.bit.TIF = 1;
CpuTimer0Regs.TCR.bit.TRB = 1;
nCount1++; nCount1%=Hz200;
GpioDataRegs.GPBDAT.bit.GPIOB4=( nCount1>uN )?1:0;
if ( nCount2==0 )
{
GpioDataRegs.GPBDAT.bit.GPIOB5=nnn;
nnn=1-nnn;
}
nCount2++; nCount2%=Hz1;
nJSSpeed++; nJSSpeed%=12000/20; // 讀取速度標志
if(nJSSpeed==0)
{nJSSpeed_1=0;}
nCount++; nCount%=51200;
if(nCount==0)
{nCount_1=0;}
}
void Gpio_select(void)
{
EALLOW;
GpioMuxRegs.GPAMUX.all=var1; // Configure MUXs as digital I/Os or
GpioMuxRegs.GPBMUX.all=var1; // peripheral I/Os
GpioMuxRegs.GPDMUX.all=var1;
GpioMuxRegs.GPFMUX.all=var1;
GpioMuxRegs.GPEMUX.all=var1;
GpioMuxRegs.GPGMUX.all=var1;
GpioMuxRegs.GPADIR.all=var2; // GPIO PORTs as output
GpioMuxRegs.GPBDIR.all=var2; // GPIO DIR select GPIOs as output
GpioMuxRegs.GPDDIR.all=var2;
GpioMuxRegs.GPEDIR.all=var2;
GpioMuxRegs.GPFDIR.all=var2;
GpioMuxRegs.GPGDIR.all=var2;
GpioMuxRegs.GPAQUAL.all=var3; // Set GPIO input qualifier values
GpioMuxRegs.GPBQUAL.all=var3;
GpioMuxRegs.GPDQUAL.all=var3;
GpioMuxRegs.GPEQUAL.all=var3;
EDIS;
}
void Gpio_PortB(void)
{
// GPIO Test #2:
// Configure Upper 8 bits of Port as inputs and lower 8 bits as outputs
// Loop back bits [7:0] to bits [15:8]
// Don't set any input qualifier
var1= 0x0000; // sets GPIO Muxs as I/Os
var2= 0x00FF; // sets GPIO 15-8 DIR as inputs, 7-0 DIR as outputs
var3= 0x0000; // Don't set any input qualifier
Gpio_select();
test_count = 0;
Test_status[Test_var] = 0x0002;
Test_var++;
Test_status[Test_var] = 0xD0BE; // Set the default value of status
// to "PASSED"
GpioDataRegs.GPBCLEAR.all = 0x00FF; // Test Clear
asm(" RPT #5 ||NOP");
GpioDataRegs.GPBSET.bit.GPIOB5=1;
GpioDataRegs.GPBSET.bit.GPIOB4=1;
}
void LCDPutString(unsigned int *pData,int x,int y,unsigned int nCharNumber,unsigned color)
{
int i,j,l;
unsigned int k,mcolor;
for ( l=0;l<nCharNumber;l++ )
for ( i=0;i<8;i++ )
{
k=1;
for ( j=0;j<16;j++,k<<=1 )
{
if ( color==2 ) mcolor=2;
else
{
mcolor=( pData[l*8+i]&k )?(1):(0);
if ( color==0 ) mcolor=1-mcolor;
}
LCDPutPixel(x+l*8+i,y-j,mcolor);
}
}
}
// 顯示參數(shù)到LCD
void ShowParameters()
{
int w1,w2,w3;
w1=md%1000/100; w2=md%100/10; w3=md%10;
LCDPutString(numbers+w1*8,36,111,1,1);
LCDPutString(numbers+w2*8,44,111,1,1);
LCDPutString(numbers+w3*8,52,111,1,1);
w1=wc%1000/100; w2=wc%100/10; w3=wc%10;
LCDPutString(numbers+w1*8,104,111,1,1);
LCDPutString(numbers+w2*8,112,111,1,1);
LCDPutString(numbers+w3*8,120,111,1,1);
if ( ek>=0 )
{
LCDPutString(numbers+88,36,95,1,1);
w3=((int)ek)%100;
}
else
{
LCDPutString(numbers+96,36,95,1,1);
w3=((int)(-ek))%100;
}
w1=w3%100/10; w2=w3%10;
LCDPutString(numbers+w1*8,44,95,1,1);
LCDPutString(numbers+w2*8,52,95,1,1);
if ( tz>=0 )
{
LCDPutString(numbers+88,104,95,1,1);
w3=tz;
}
else
{
LCDPutString(numbers+96,104,95,1,1);
w3=(-tz);
}
w1=w3%100/10; w2=w3%10;
LCDPutString(numbers+w1*8,112,95,1,1);
LCDPutString(numbers+w2*8,120,95,1,1);
w1=pwm1%100/10; w2=pwm1%10;
LCDPutString(numbers+w1*8,44,79,1,1);
LCDPutString(numbers+w2*8,52,79,1,1);
LCDPutString(numbers+80,60,79,1,1);
LCDRefreshScreen();
}
int wwcc;
void PrintParameters()
{
wwcc=wc-md;
printf("測速[%3d] 設置[%3d] 誤差[%+4d] PID調(diào)整量[%+3d] 占空比[%3d%%]\n",
wc,md,wwcc,tz,pwm1);
}
// No more.
//========================================
void Delay(unsigned int nDelay)
{
int ii,jj,kk=0;
for ( ii=0;ii<nDelay;ii++ )
{
for ( jj=0;jj<64;jj++ )
{
//RefreshLEDArray();
kk++;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -