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