請教3個問題:(請看以下程序)
1、在主函數中,加while(1){ }和不加while(1){ }的問題,經實踐試過,結果是一
樣的,都是無限循環。那么可以不加while(1){ }嗎?
2、如果不要循環,只要運行一次后停機,主函數該怎么寫?
3、如果只要運行一次后停機,并且某個線圈繼續通電,但電機不轉(即把電機軸用電鎖住不動)
,主函數又該怎么寫?
*****
- #include <reg51.h> //步進電機正反轉運行程序
- #define uchar unsigned char
- #define uint unsigned int
- uchar code up_data[8]={0xE,0xC,0xD,0x9,0xB,0x3,0x7,0x6}; //1相勵磁正轉表
- uchar code down_data[8]={0X6,0X7,0X3,0XB,0X9,0XD,0XC,0XE};//1相勵磁反轉表
-
- /********以下是延時函數********/
- void Delay_ms(uint xms)
- {
- uint i,j;
- for(i=xms;i>0;i--) //i=xms即延時約xms毫秒
- for(j=110;j>0;j--);表
- }
- /********以下是步進電機1相勵磁法正轉函數********/
- void motor_up(uint n)
- {
- uchar i;
- uint j;
- for (j=0; j<509*n; j++) //正轉1圈
- {
- for (i=0; i<8; i++)
- {
- P1 = up_data[i];
- Delay_ms(4);
- }
- }
- }
- /********步進電機1相勵磁法反轉函數********/
- void motor_down(uint n)
- {
- uchar i;
- uint j;
- for (j=0; j<509*n; j++) //反轉1圈
- {
- for (i=0; i<8; i++)
- {
- P1 = down_data[i];
- Delay_ms(4);
- }
- }
- }
- /********以下是主函數********/
- void main()
- {
- while(1) //此語句有或無效果都一樣!
- {
- motor_up(1); //電機正轉1圈
- P1=0xff; //電機停轉
- Delay_ms(4000); //換向延時為4s
- motor_down(1); //電機反轉1圈
- P1=0x00; //電機停轉
- Delay_ms(4000); //換向延時為4s
- P1=0xff; //電機停轉
- Delay_ms(10000); //換向延時為10s
- }
- }
復制代碼 |