占位符
1 #include <msp430.h> 2 #include "sys.h" 3 #include "ps2.h" 4 #include "12864.h" 5 #include "adc.h" 6 #include "tlv5638.h" 7 #include "pid.h" 8 #include "timera.h" 9 #include "uart.h" 10 #include "vosc.h" 11 12 //#define _DISMAIN_ 13 14 #define ADCN 10 // ad采集次数 15 16 // 在ps2.c里面定义 17 extern unsigned char ps2_keyvalue; 18 extern unsigned char ps2_flag; 19 20 unsigned int m = 1300; // TLV5638设置值 21 unsigned char adcnt = 0; 22 unsigned int adcFinish = 0; 23 unsigned int adcValue = 0, advArr[ADCN] = { 0 }; // ADC采集的值,采集13次保存在数组里 24 unsigned int ta_flag = 0, dispFlag = 0; 25 26 // pid参数 27 unsigned int point = 1200; // 目标值 28 float p = 0.1, i = 0, d = 0.1; 29 int inc = 0, tempInc; // 增量 30 unsigned int out2Osc[10] = { 0 }; 31 32 // 获取键盘输入 33 void GetInput() 34 { 35 unsigned char temp; 36 temp = PS2_AsiiConvert(ps2_keyvalue); 37 38 if(temp == '+') 39 { 40 point += 100; 41 if(point >= 2000) 42 point = 2000; 43 IncPIDSetPoint(point); // 44 LCD_Set_Pos(0, 4); 45 LCD_Show_Num(point); 46 } 47 else if(temp == '-') 48 { 49 point -= 100; 50 if(point < 11) 51 point = 10; 52 IncPIDSetPoint(point); 53 LCD_Set_Pos(0, 4); 54 LCD_Show_Num(point); 55 } 56 else if(temp == '7') //p设置 57 { 58 p += 0.01; 59 IncPIDSet(p, i, d); 60 LCD_Set_Pos(3, 0); 61 LCD_Show_Deci2(p); 62 } 63 else if(temp == '1') 64 { 65 p -= 0.01; 66 IncPIDSet(p, i, d); 67 LCD_Set_Pos(3, 0); 68 LCD_Show_Deci2(p); 69 } 70 else if(temp == '8') 71 { 72 i += 0.01; 73 IncPIDSet(p, i, d); 74 LCD_Set_Pos(3, 3); 75 LCD_Show_Deci2(i); 76 } 77 else if(temp == '2') 78 { 79 i -= 0.01; 80 IncPIDSet(p, i, d); 81 LCD_Set_Pos(3, 3); 82 LCD_Show_Deci2(i); 83 } 84 else if(temp == '9') 85 { 86 d += 0.01; 87 IncPIDSet(p, i, d); 88 LCD_Set_Pos(3, 6); 89 LCD_Show_Deci2(d); 90 } 91 else if(temp == '3') 92 { 93 d -= 0.01; 94 IncPIDSet(p, i, d); 95 LCD_Set_Pos(3, 6); 96 LCD_Show_Deci2(d); 97 } 98 else 99 { 100 } 101 P2IFG &= ~BIT4; 102 } 103 104 /* 105 * 加权滤波 106 */ 107 unsigned int filter(unsigned int arr[]) 108 { 109 unsigned char i; 110 111 /* 均值滤波 */ 112 unsigned int temp = 0; 113 114 for (i = 0; i < ADCN; i++) 115 { 116 temp += arr[i]; 117 } 118 temp = (unsigned int) (temp * 1.0 / ADCN); 119 120 return temp; 121 } 122 123 /* 124 * AGC自动调节 125 */ 126 void AGC() 127 { 128 // 测试用 129 // P1OUT |= BIT6; 130 131 adcValue = (unsigned long) (filter(advArr) * 2500.0f / 1024.0f);// * 1000.0f ); // / 1024.0f); 132 // 计算增量 133 inc = IncPIDCalc(adcValue); 134 tempInc += inc; // 增量调节 135 if(tempInc < 0) 136 tempInc = 0; // 输出限制>=0V 137 if(tempInc > 2000) 138 tempInc = 2000; // 输出限制<=2V 139 m = (unsigned int) tempInc; 140 141 // 设置TLV值 142 TLV_WriteData(0xd002); // 写入通道A 143 TLV_WriteData(Volt(m)); 144 145 // 输出到虚拟示波器 146 out2Osc[0] = point; 147 out2Osc[1] = (unsigned int) adcValue; 148 out2Osc[2] = m; 149 out2Osc[3] = 0; 150 VOSC_Out4Data(out2Osc); 151 152 // P1OUT &= ~BIT6; 153 } 154 155 #ifndef _DISMAIN_ 156 void main() 157 { 158 // 测试用 159 P1DIR |= BIT6; 160 P1OUT |= BIT6; 161 // P2DIR |= BIT5; 162 // P2OUT |= BIT5; 163 164 System_Init(); 165 LCD_Init(); 166 PS2_Init(); 167 TLV_Init(); // TLV5638控制电压 168 ADC_Init2(); // P1.310次采集 169 __delay_cycles(10000); 170 TA_Init(); // 定时器初始化,10ms 171 Uart_Init(); // OSC用到串口 172 173 // PID初始化 174 IncPIDInit(p, i, d, point); 175 176 // UI 177 LCD_Clear(); 178 LCD_Set_Pos(0, 0); 179 LCD_Show_String("PointV->"); // 目标值 180 LCD_Set_Pos(0, 4); 181 LCD_Show_Num(point); 182 LCD_Set_Pos(0, 7); 183 LCD_Show_String("mV"); 184 LCD_Set_Pos(1, 0); 185 LCD_Show_String("CurSet->"); // 当前设定值 186 LCD_Set_Pos(1, 4); 187 LCD_Show_Num(m); 188 LCD_Set_Pos(1, 7); 189 LCD_Show_String("mV"); 190 LCD_Set_Pos(2, 0); 191 LCD_Show_String("ADCapV->"); // 采集电压 192 LCD_Set_Pos(2, 4); 193 LCD_Show_Num(adcValue); 194 LCD_Set_Pos(2, 7); 195 LCD_Show_String("mV"); 196 197 // 显示pid参数 198 LCD_Set_Pos(3, 0); 199 LCD_Show_Deci2(p); 200 LCD_Set_Pos(3, 3); 201 LCD_Show_Deci2(i); 202 LCD_Set_Pos(3, 6); 203 LCD_Show_Deci2(d); 204 205 // 写入初始电压 206 TLV_WriteData(0xd002); 207 TLV_WriteData(Volt(m)); 208 LCD_Set_Pos(1, 4); 209 LCD_Show_Num(m); 210 211 // agc初始化 212 tempInc = m; 213 214 _EINT(); // 使能中断,键盘、定时器 215 while (1) 216 { 217 // 键盘 218 if(ps2_flag == 1) 219 { 220 GetInput(); // 获取按键,处理按键事件 221 ps2_flag = 0; 222 } 223 224 // ADC采集 225 // if(ta_flag == 1) 226 // { 227 // ta_flag = 0; 228 // 229 // // AD采集 230 // ADC_Start2(advArr); 231 // } 232 233 // AGC调节 234 // if(adcFinish == 1) // 采集了N次 235 // { 236 // adcFinish = 0; 237 // AGC(); 238 // } 239 240 // // 设置TLV值 241 // TLV_WriteData(0xd002); // 写入通道A 242 // TLV_WriteData(Volt(m)); 243 244 // 显示延时 245 if (dispFlag >= 150) 246 { 247 dispFlag = 0; 248 249 // 显示当前设定值 250 LCD_Set_Pos(1, 4); 251 LCD_Show_Num(m); 252 253 // 显示AD采集结果 254 LCD_Set_Pos(2, 4); 255 LCD_Show_Num(adcValue); 256 } 257 } 258 } 259 260 #pragma vector=TIMER0_A0_VECTOR 261 __interrupt void TIME0_A0_ISR(void) 262 { 263 ta_flag = 1; // 定时时间到 264 dispFlag++; // 用于显示延时 265 266 // P2OUT |= BIT5; // 准备AGC 267 ADC_Start2(advArr); // AD采集 268 269 // _EINT(); 270 271 // 测试用 272 P1OUT ^= BIT6; 273 } 274 275 #pragma vector = ADC10_VECTOR 276 __interrupt void ADC10ISR(void) 277 { 278 // adcFinish = 1; 279 280 ADC10CTL0 &= ~(ENC + ADC10SC); // 停止转换 281 AGC(); // 采集完成,AGC调节 282 // P2OUT &= ~BIT5; 283 284 // adcFinish = 0; 285 } 286 287 #endif