B题-报告5-程序清单 | ||||
| ||||
B题-报告5-程序清单 #include "include.h" #include "calculation.h"
/************************* 设置系统的全局变量 *************************/ /*定时标志*/ extern u8 TIME0flag_5ms ; extern u8 TIME0flag_10ms ; extern u8 TIME0flag_15ms ; extern u8 TIME0flag_20ms ; extern u8 TIME0flag_80ms ;
/*角度传感器*/ extern u8 AngBuff[8]; extern u8 Angflag; s16 Hangel=0,Pangel=0,Rangel=0;
u8 j=0;//方向控制 u8 i=0;//摆动控制器 u8 rate=62;//摆摆幅控制器 u8 Mod=0;//模式控制器,圆周-摆动
/*sin cos函数表*/ const float Sin[64]={0.0000,0.0996,0.1981,0.2948,0.3884,0.4783,0.5633,0.6428, 0.7159,0.7818,0.8400,0.8899,0.9309,0.9626,0.9848,0.9972, 0.9997,0.9922,0.9749,0.9479,0.9115,0.8660,0.8119,0.7498, 0.6802,0.6038,0.5214,0.4339,0.3420,0.2468,0.1490,0.0498, -0.0498,-0.1490,-0.2468,-0.3420,-0.4339,-0.5214,-0.6038,-0.6802, -0.7498,-0.8119,-0.8660,-0.9115,-0.9479,-0.9749,-0.9922,-0.9997, -0.9972,-0.9848,-0.9626,-0.9309,-0.8899,-0.8400,-0.7818,-0.7159, -0.6428,-0.5633,-0.4783,-0.3884,-0.2948,-0.1981,-0.0996,-0.0000};
const float Cos[64]={1.0000,0.9950,0.9802,0.9556,0.9215,0.8782,0.8262,0.7660, 0.6982,0.6235,0.5425,0.4562,0.3653,0.2708,0.1736,0.0747, -0.0249,-0.1243,-0.2225,-0.3185,-0.4113,-0.5000,-0.5837,-0.6617, -0.7331,-0.7971,-0.8533,-0.9010,-0.9397,-0.9691,-0.9888,-0.9988, -0.9988,-0.9888,-0.9691,-0.9397,-0.9010,-0.8533,-0.7971,-0.7331, -0.6617,-0.5837,-0.5000,-0.4113,-0.3185,-0.2225,-0.1243,-0.0249, 0.0747,0.1736,0.2708,0.3653,0.4562,0.5425,0.6235,0.6982, 0.7660,0.8262,0.8782,0.9215,0.9556,0.9802,0.9950,1.0000};
/*摆动过程量表*/ const s32 TPangeltable[16]={0,700,1300,1700,2000,1700,1300,700,0,-700,-1300,-1700,-2000,-1700,-1300,-700}; const s32 TRangeltable[16]={0,700,1300,1700,2000,1700,1300,700,0,-700,-1300,-1700,-2000,-1700,-1300,-700}; //{0,500,1000,1500,2000,1500,1000,500,0,-500,-1000,-1500,-2000,-1500,-1000,-500};
/*定点控制变量表*/ s32 APangel=0,EPangel_old=0,EPangel_new=0,DPangel=0; s32 ARangel=0,ERangel_old=0,ERangel_new=0,DRangel=0; s32 PWM1=0,PWM2=0,PWM3=0,PWM4=0; /*定点PD控制 P、D参数*/ s16 Kp1=15,Kp2=14;// s16 Kd1=140,Kd2=150;//
void DisPlay0()//OLED显示函数 { //LCD_Print(0,0,"BL");显示字符串坐标X,Y,显示字符串 //Dis_float(0,0,1.61);显示小数坐标X,Y,显示浮点变量(连小数点在内只能显示五个字符) //LCD_WrNum(0,0,DJCSX,4);显示整数坐标X,Y,显示整型变量,位宽<=5 LCD_Print(0,0,"P1:");LCD_Print(65,0,"D1:"); LCD_Print(0,1,"P2:");LCD_Print(65,1,"D2:"); LCD_Print(0,2,"Ra:");LCD_Print(65,2,"J :"); LCD_Print(0,3,"Mo:");//LCD_Print(65,3,"C1:"); //LCD_Print(0,4,"B4:");LCD_Print(65,4,"C2:"); LCD_Print(0,5,"HA:");LCD_Print(65,5,"EP:"); LCD_Print(0,6,"PA:");LCD_Print(65,6,"DP:"); LCD_Print(0,7,"RA:");//LCD_Print(65,7,"C5:"); } void DisPlay1()//OLED显示函数 { LCD_WrNum(25,0,Kp1,5);LCD_WrNum(87,0,Kd1,5); LCD_WrNum(25,1,Kp2,5);LCD_WrNum(87,1,Kd2,5); LCD_WrNum(25,2,rate,5);LCD_WrNum(87,2,j,5); LCD_WrNum(25,3,Mod,5);//Dis_float(87,3,cosa); //LCD_WrNum(25,4,adc4,5);Dis_float(87,4,sina); LCD_WrNum(25,5,Hangel,5);LCD_WrNum(87,5,EPangel_new,5); LCD_WrNum(25,6,Pangel,5); LCD_WrNum(87,6,DPangel,5); LCD_WrNum(25,7,Rangel,5);//Dis_float(87,7,122.5); }
void Key() { if(PTA24_IN==0){ delayms(5); if(PTA24_IN==0){ rate++; if(rate>=200){rate=200;} if(rate<=0){rate=0;} } while(PTA24_IN==0); } if(PTA26_IN==0){ delayms(5); if(PTA26_IN==0){ rate--; if(rate>=200){rate=200;} if(rate<=0){rate=0;} } while(PTA26_IN==0); } if(PTA28_IN==0){ delayms(5); if(PTA28_IN==0){ j++; if(j>=64){j=0;} } while(PTA28_IN==0); } if(PTB8_IN==0){ delayms(5); if(PTB8_IN==0){Mod++; if(Mod==2){Mod=0;} } while(PTB8_IN==0); } }
void ALL_init() { /*程序指示灯使能(在定时中断PIT3中进行,周期1S)*/ gpio_init (PORTB , 7, GPO,HIGH);
/*按键初始化*/ gpio_init (PORTA ,24, GPI_UP,HIGH);//按键1(靠近开关的) gpio_init (PORTA ,26, GPI_UP,HIGH);//按键2 gpio_init (PORTA ,28, GPI_UP,HIGH);//按键3 gpio_init (PORTB , 8, GPI_UP,HIGH);//按键4
/*拨码开关初始化*/ //gpio_init (PORTA ,13, GPI_UP,HIGH);//拨码1 //gpio_init (PORTA ,15, GPI_UP,HIGH);//拨码2 //gpio_init (PORTA ,17, GPI_UP,HIGH);//拨码3 //gpio_init (PORTA ,18, GPI_UP,HIGH);//拨码4
/* PWM初始化模块,通道,频率,占空比N/10000%(同一个模块的频率需要一致)*/ FTM_PWM_init(FTM1,CH0,500,0);//PTA8 FTM_PWM_init(FTM0,CH1,500,0);//PTC2 FTM_PWM_init(FTM1,CH1,500,0);//PTA9(300HZ数字舵机PWM频率) FTM_PWM_init(FTM0,CH5,500,0);//PTD5(50HZ普通舵机PWM频率)
/*定时器初始化模块,时间长度ms*/ pit_init_ms(PIT0, 5);//初始化PIT0,定时分频(5ms,10ms,15ms,20ms,80ms)用于查询方式 pit_init_ms(PIT1, 135);//初始化PIT1,快定时器(中断服务函数空) pit_init_ms(PIT2, 76);//初始化PIT2,中定时器 pit_init_ms(PIT3, 500);//初始化PIT3,慢定时器(中断服务函数中已有程序运行灯,打开该定时器后程序指示灯闪烁)
/*OLED显示屏初始化*/ LCD_Init(); DisPlay0();//固定字符显示
/*ADC初始化*/ //adc_init(ADC0, AD15); //PTC1(与OLED的AD按键相连) //adc_init(ADC0, AD8); //PTB0 //adc_init(ADC0, AD12); //PTB2 //adc_init(ADC1, AD4a); //PTE0 //adc_init(ADC1, AD5a); //PTE1
/*DAC初始化*/ //dac_once_init(DAC0, VDDA);//DAC0 VDDA3.3
/*串口初始化*/ //uart_init (UART0, 19200); //初始化UART0,TXD-PTD7,RXD-PTD6
UART_IRQ_DIS(UART1); //串口0 关接收中断 uart_init (UART1, 115200); //初始化UART0,TXD-PTC4,RXD-PTC3 UART_IRQ_EN(UART1); //串口0 开接收中断
}
void main() {
DisableInterrupts; //禁止总中断 ALL_init(); EnableInterrupts; //开总中断
/****************************************** 执行程序 ******************************************/ for(;;) { if(Angflag==1) { Angflag=0; if(AngBuff[0]==0xAA&&AngBuff[7]==0x55){ Hangel=(AngBuff[1]<<8|AngBuff[2]); Pangel=(AngBuff[3]<<8|AngBuff[4]); Rangel=(AngBuff[5]<<8|AngBuff[6]); } } /*定时器查询方式服务函数,开PIT0有效*/ if(TIME0flag_5ms==1){ TIME0flag_5ms=0; uart_putchar (UART1,0xA5); uart_putchar (UART1,0x51); } if(TIME0flag_10ms==1){ TIME0flag_10ms=0; Key(); } if(TIME0flag_15ms==1){ TIME0flag_15ms=0; } if(TIME0flag_20ms==1){ TIME0flag_20ms=0;
PWM1=0,PWM2=0,PWM3=0,PWM4=0;
EPangel_old=EPangel_new; ERangel_old=ERangel_new;
APangel=Pangel; ARangel=Rangel;
EPangel_new=(s32)(APangel-Cos[j]*TPangeltable[i]*rate/100); ERangel_new=(s32)(ARangel-Sin[j]*TRangeltable[i]*rate/100);
DPangel=EPangel_new-EPangel_old; DRangel=ERangel_new-ERangel_old;
if(EPangel_new>0){PWM1+=Kp1*EPangel_new;} else if(EPangel_new<=0){PWM2+=Kp1*(0-EPangel_new);}
if(ERangel_new>0){PWM3+=Kp2*ERangel_new;} else if(ERangel_new<=0){PWM4+=Kp2*(0-ERangel_new);}
if(DPangel>0){PWM1+=Kd1*DPangel;} else if(DPangel<=0){PWM2+=Kd1*(0-DPangel);}
if(DRangel>0){PWM3+=Kd2*DPangel;} else if(DRangel<=0){PWM4+=Kd2*(0-DRangel);}
if(PWM1>=9000){PWM1=9000;} if(PWM1<=0){PWM1=0;} if(PWM2>=9000){PWM2=9000;} if(PWM2<=0){PWM2=0;}
if(PWM3>=9000){PWM3=9000;} if(PWM3<=0){PWM3=0;} if(PWM4>=9000){PWM4=9000;} if(PWM4<=0){PWM4=0;}
} if(TIME0flag_80ms==1){ TIME0flag_80ms=0; DisPlay1(); } FTM_PWM_Duty(FTM0,CH5,PWM1);//1 FTM_PWM_Duty(FTM0,CH1,PWM2);//3 FTM_PWM_Duty(FTM1,CH0,PWM3);//4 FTM_PWM_Duty(FTM1,CH1,PWM4);//2 } }
中断程序清单 #include "common.h" #include "include.h"
u8 count=0; u8 AngBuff[8]; u8 Angflag=0; void USART1_IRQHandler(void) { char test; UART_IRQ_DIS(UART1); //关总中断 uart_pendchar (UART1,&test); //AngBuff[count]=uart_getchar (UART1); AngBuff[count]=test; count++; if(count>=8) { count=0; Angflag=1; } UART_IRQ_EN(UART1); //开总中断 }
u8 TIME0flag_5ms = 0 ; u8 TIME0flag_10ms = 0 ; u8 TIME0flag_15ms = 0 ; u8 TIME0flag_20ms = 0 ; u8 TIME0flag_80ms = 0 ; u8 TimeCount = 0 ; void PIT0_IRQHandler(void) { PIT_Flag_Clear(PIT0); //清中断标志位 TimeCount ++ ; if(TimeCount%1 == 0 ){TIME0flag_5ms = 1;} if(TimeCount%2 == 0 ){TIME0flag_10ms = 1;} if(TimeCount%3 == 0 ){TIME0flag_15ms = 1;} if(TimeCount%4 == 0 ){TIME0flag_20ms = 1;} if(TimeCount%64 == 0 ){TIME0flag_80ms = 1;} if(TimeCount == 192){TimeCount = 0;} }
extern u8 i; extern u8 j; extern u8 Mod; void PIT1_IRQHandler(void) { PIT_Flag_Clear(PIT1);//清中断标志位 if(Mod==1) { i=4; j++; if(j>=64)j=0; } }
void PIT2_IRQHandler(void) { PIT_Flag_Clear(PIT2); //清中断标志位 if(Mod==0) { i++; if(i>=16)i=0; } }
void PIT3_IRQHandler(void) { PIT_Flag_Clear(PIT3);//清中断标志 PTB7_OUT = ~PTB7_OUT;//程序运行指示灯 }
|