📄 main.lst
字号:
204 6 }
205 5 }
206 4 }
207 3 else
208 3 {
209 4 // 非升降速,如果频率已改变
210 4 if ( Timer[1].CurFreq != MotorStatus[idx].SetFreq )
211 4 {
212 5 if ( !Timer[1].SecConstFlag )
213 5 {
214 6 Timer[1].CurFreq = MotorStatus[idx].SetFreq;
215 6 CalSecFreqTimerConst( 1 );
216 6 }
217 5 }
218 4 }
219 3 }
220 2 }
221 1 }
222
223 void StartMotor( void )
224 {
225 1 bit dir;
226 1 BYTE i, tidx;
227 1
228 1 for( i = 0; i < MAX_MOTOR_NUM; i++ )
229 1 {
230 2 // 已启动
231 2 if ( MotorStatus[i].Status & MOTOR_RUN_MASK )
232 2 continue;
233 2
234 2 // 无命令
235 2 if ( !(MotorStatus[i].Dir & DIR_FLAG_MASK) )
236 2 continue;
237 2
238 2 tidx = 2;
239 2 if ( !Timer[0].MotorIdx )
240 2 tidx = 0;
C51 COMPILER V7.04 MAIN 01/23/2007 12:25:24 PAGE 5
241 2 if ( !Timer[1].MotorIdx )
242 2 tidx = 1;
243 2
244 2 // 没有空余定时器
245 2 if ( tidx == 2 )
246 2 break;
247 2
248 2 // 设定升降速
249 2 Timer[tidx].DwonPluse = MotorStatus[i].TotalPluseCnt;
250 2 if ( MotorStatus[i].UpDwon )
251 2 {
252 3 Timer[tidx].CurFreq = START_FREQ;
253 3 Timer[tidx].FreqInc = MotorStatus[i].UpDwon >> 4;
254 3 Timer[tidx].UpDwonCnt = (MotorStatus[i].UpDwon & 0x0f) << 1;
255 3 }
256 2 else
257 2 {
258 3 Timer[tidx].CurFreq = MotorStatus[i].SetFreq;
259 3 Timer[tidx].FreqInc = 0;
260 3 Timer[tidx].UpDwonCnt = 0;
261 3 }
262 2
263 2 // 计算定时器常数
264 2 CalSecFreqTimerConst( tidx );
265 2 Timer[tidx].TimerConstCntH = Timer[tidx].SecTimerConstCntH;
266 2 Timer[tidx].TimerConstH = Timer[tidx].SecTimerConstH;
267 2 Timer[tidx].TimerConstL = Timer[tidx].SecTimerConstL;
268 2 Timer[tidx].SecConstFlag = 0;
269 2 // 记录当前总脉冲,供双向使用
270 2 Timer[tidx].CenrerPluse = (WORD)MotorStatus[i].TotalPluseCnt;
271 2
272 2 // 启动标志
273 2 MotorStatus[i].Status |= MOTOR_RUN_MASK;
274 2
275 2 switch( MotorStatus[i].Dir & DIR_FLAG_MASK )
276 2 {
277 3 case 1: // 正转
278 3 case 2: // 反转
279 3 if ( (MotorStatus[i].Dir & DIR_FLAG_MASK) == 1 )
280 3 dir = 0;
281 3 else
282 3 dir = 1;
283 3
284 3 // 脉冲数量
285 3 Timer[tidx].CurPluseNum = MotorStatus[i].CmdPluseNum * 2;
286 3 break;
287 3
288 3 case 3: // 双向
289 3 dir = 0;
290 3
291 3 // 脉冲数量
292 3 Timer[tidx].CurPluseNum = (MotorStatus[i].CmdPluseNum >> 16) * 2;
293 3
294 3 // 更新命令脉冲,减少中断程序运算
295 3 MotorStatus[i].CmdPluseNum = (MotorStatus[i].CmdPluseNum & 0xffff) * 2 + Timer[tidx].CurPluseNum;
296 3 break;
297 3 }
298 2
299 2 // 置运转方向
300 2 switch( i )
301 2 {
302 3 case 0:
C51 COMPILER V7.04 MAIN 01/23/2007 12:25:24 PAGE 6
303 3 DIR0 = dir;
*** ERROR C202 IN LINE 303 OF MAIN.C: 'DIR0': undefined identifier
304 3 break;
305 3 case 1:
306 3 DIR1 = dir;
*** ERROR C202 IN LINE 306 OF MAIN.C: 'DIR1': undefined identifier
307 3 break;
308 3 case 2:
309 3 DIR2 = dir;
*** ERROR C202 IN LINE 309 OF MAIN.C: 'DIR2': undefined identifier
310 3 break;
311 3 case 3:
312 3 DIR3 = dir;
*** ERROR C202 IN LINE 312 OF MAIN.C: 'DIR3': undefined identifier
313 3 break;
314 3 case 4:
315 3 DIR4 = dir;
*** ERROR C202 IN LINE 315 OF MAIN.C: 'DIR4': undefined identifier
316 3 break;
317 3 case 5:
318 3 DIR5 = dir;
*** ERROR C202 IN LINE 318 OF MAIN.C: 'DIR5': undefined identifier
319 3 break;
320 3 }
321 2
322 2 // 启动定时器
323 2 Timer[tidx].MotorIdx = i + 1;
324 2 Timer[tidx].IntCnt = 0;
325 2 if ( !tidx )
326 2 {
327 3 LastTimer0ST = 1;
328 3 LastTimer0WST = 1;
329 3 Timer0EndIntFlag = 0;
330 3 TL0 = 0;
331 3 TH0 = 0;
332 3 ET0 = 1;
333 3 // 模拟中断
334 3 TF0 = 1;
335 3 }
336 2 else
337 2 {
338 3 LastTimer1ST = 1;
339 3 LastTimer1WST = 1;
340 3 Timer1EndIntFlag = 0;
341 3 TL1 = 0;
342 3 TH1 = 0;
343 3 ET1 = 1;
344 3 // 模拟中断
345 3 TF1 = 1;
346 3 }
347 2 }
348 1 }
349
350 void CalSecFreqTimerConst( u_char tidx )
351 {
352 1 DWORD tmp;
353 1
354 1 // 0x196e6b - 定时器每秒能计数次数除2
355 1 tmp = 0x196e6b / (DWORD)Timer[tidx].CurFreq - 12;
356 1 Timer[tidx].SecTimerConstCntH = *((BYTE idata*)&tmp + NMSB);
357 1
358 1 tmp &= 0xffff;
C51 COMPILER V7.04 MAIN 01/23/2007 12:25:24 PAGE 7
359 1 tmp = 0 - tmp;
360 1 Timer[tidx].SecTimerConstH = *((BYTE idata*)&tmp + NLSB);
361 1 Timer[tidx].SecTimerConstL = tmp;
362 1 Timer[tidx].SecConstFlag = 1;
363 1 }
364
365 // 中断号
366 // 外部中断0 0
367 // 定时器0 1
368 // 外部中断1 2
369 // 定时器1 3
370 // 串行通信 4
371 // 定时器2 5
372
373 #define DWordDec( ) \
374 do { \
375 if ( *tmpptr ) \
376 (*tmpptr)--; \
377 else \
378 { \
379 *tmpptr = 0xff; \
380 tmpptr--; \
381 if ( *tmpptr ) \
382 (*tmpptr)--; \
383 else \
384 { \
385 *tmpptr = 0xff; \
386 tmpptr--; \
387 if ( *tmpptr ) \
388 (*tmpptr)--; \
389 else \
390 { \
391 *tmpptr = 0xff; \
392 tmpptr--; \
393 if ( *tmpptr ) \
394 (*tmpptr)--; \
395 else \
396 *tmpptr = 0xff; \
397 } \
398 } \
399 } \
400 } while( 0 )
401
402 #define DWordInc( ) \
403 do { \
404 (*tmpptr)++; \
405 if ( !(*tmpptr) ) \
406 { \
407 tmpptr--; \
408 (*tmpptr)++; \
409 if ( !(*tmpptr) ) \
410 { \
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -