Professional Documents
Culture Documents
Code
Code
05us
// Prescaler = 1:8, 1xung nhip timer ~ 0.05*8 = 0.4us, Ts=10ms =10.000us~25000N;
// doc ADC tu AN7, hien thi dong 1 LCD
// xuat pwm kenh OC5, hien thi Ton
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
float R= 2.41; // Ohm
float L= 15.03e-3; // H
float m= 3.02e-3; // kg
float g= 9.81; // m/s^2
float k= 17.31e-9;
float alpha = 2.48;
float beta = 2.92e-4;
float gamma = 0.48;
float x1k, x2k, x3k, uk, yk, zk;
float x1e, x2e, x3e, ue, ye, ze;
float dx1, dx2, dx3, du, dy, dz; //dx1= x1k-x1e,...
float A[3][3], B[3][2], C[2][3], D[2][2], X[3][1],Klqr[1][3];
float Lest[3][1];
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void main() {
float Channel, Ton, Pwm_Period1,i,round3;
char txt_pwm[14];
//
ADPCFG= 0xFFFF;
PORTB = 0xFF80;
TRISB = 0xFF70;
PORTD = 0xFFF0;
TRISD = 0xFFF0;
TRISBbits.TRISB7 = 1;
// LCD
Lcd_Custom_Config(&PORTB, 3,2,1,0, &PORTD, 0,2,1); // khoi tao
Lcd_Custom_Cmd(LCD_CURSOR_OFF);
// tham so cua mo hinh
x1e = 2e-2;
x2e = 0.;
x3e = 1.*m*g*pow(x1e,4) / k;
ze= 1.*x1e;
ue= 1.*R*x3e;
ye= 1.*gamma*x3e + beta*pow(x1e,-2.) + alpha;
// Ma tran trang thai mien lien tuc
A[1][2]= 1.;
A[2][1]= 4.*g*pow((g*m*R), 0.25) / pow((k*ue), 0.25);
A[2][3]= -1.*g*R/ue;
A[3][3]= -1.*R/L;
B[3][1]= 1./L;
C[1][1]= 1.;
C[2][1]= -2.*beta*pow((g*m*R),0.75)/pow((k*ue),0.75);
C[2][3]= 1.*gamma;
// Ma tran trang thai mien roi rac
A[1][1]= 1.0997; A[1][2]= 0.0103; A[1][3]= -0.0011;
A[2][1]= 20.2679; A[2][2]= 1.0997; A[2][3]= -0.1868;
A[3][1]= 0. ; A[3][1]= 0. ; A[3][3]= 0.2012;
B[1][1]= -0.0003;
B[2][1]= -0.0761;
B[3][1]= 0.3315;