實(shí)用的 28BYJ-48 步進(jìn)電機(jī)控制程序
上面我們雖然完成了用中斷控制電機(jī)轉(zhuǎn)動(dòng)的程序,但實(shí)際上這個(gè)程序還是沒(méi)多少實(shí)用價(jià)值的,我們不能每次想讓它轉(zhuǎn)動(dòng)的時(shí)候都上下電啊,是吧。還有就是它不但能正轉(zhuǎn)還得能反轉(zhuǎn)啊,也就是說(shuō)不但能轉(zhuǎn)過(guò)去,還得能轉(zhuǎn)回來(lái)呀。好吧,我們就來(lái)做一個(gè)實(shí)例程序吧,結(jié)合第8章的按鍵程序,我們?cè)O(shè)計(jì)這樣一個(gè)功能程序:按數(shù)字鍵1~9,控制電機(jī)轉(zhuǎn)過(guò)1~9圈;配合上下鍵改變轉(zhuǎn)動(dòng)方向,按向上鍵后正向轉(zhuǎn)1~9圈,向下鍵則反向轉(zhuǎn)1~9圈;左鍵固定正轉(zhuǎn)90度,右鍵固定反轉(zhuǎn)90;Esc 鍵終止轉(zhuǎn)動(dòng)。通過(guò)這個(gè)程序,我們也可以進(jìn)一步體會(huì)到如何用按鍵來(lái)控制程序完成復(fù)雜的功能,以及控制和執(zhí)行模塊之間如何協(xié)調(diào)工作,而你的編程水平也可以在這樣的實(shí)踐練習(xí)中得到鍛煉和提升。
#includesbitKEY_IN_1=P2^4;sbitKEY_IN_2=P2^5;sbitKEY_IN_3=P2^6;sbitKEY_IN_4=P2^7;sbitKEY_OUT_1=P2^3;sbitKEY_OUT_2=P2^2;sbitKEY_OUT_3=P2^1;sbitKEY_OUT_4=P2^0;unsignedcharcodeKeyCodeMap[4][4]={//矩陣按鍵編號(hào)到標(biāo)準(zhǔn)鍵盤(pán)鍵碼的映射表{0x31,0x32,0x33,0x26},//數(shù)字鍵1、數(shù)字鍵2、數(shù)字鍵3、向上鍵{0x34,0x35,0x36,0x25},//數(shù)字鍵4、數(shù)字鍵5、數(shù)字鍵6、向左鍵{0x37,0x38,0x39,0x28},//數(shù)字鍵7、數(shù)字鍵8、數(shù)字鍵9、向下鍵{0x30,0x1B,0x0D,0x27}//數(shù)字鍵0、ESC鍵、回車(chē)鍵、向右鍵};unsignedcharKeySta[4][4]={//全部矩陣按鍵的當(dāng)前狀態(tài){1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}};signedlongbeats=0;//電機(jī)轉(zhuǎn)動(dòng)節(jié)拍總數(shù)voidKeyDriver();voidmain(){EA=1;//使能總中斷TMOD=0x01;//設(shè)置T0為模式1TH0=0xFC;//為T(mén)0賦初值0xFC67,定時(shí)1msTL0=0x67;ET0=1;//使能T0中斷TR0=1;//啟動(dòng)T0while(1){KeyDriver();//調(diào)用按鍵驅(qū)動(dòng)函數(shù)}}/*步進(jìn)電機(jī)啟動(dòng)函數(shù),angle-需轉(zhuǎn)過(guò)的角度 */void StartMotor(signed long angle){ //在計(jì)算前關(guān)閉中斷,完成后再打開(kāi),以避免中斷打斷計(jì)算過(guò)程而造成錯(cuò)誤 EA = 0; beats = (angle * 4076) / 360; //實(shí)測(cè)為4076拍轉(zhuǎn)動(dòng)一圈 EA = 1;}/* 步進(jìn)電機(jī)停止函數(shù) */void StopMotor(){ EA = 0; beats = 0; EA = 1;}/* 按鍵動(dòng)作函數(shù),根據(jù)鍵碼執(zhí)行相應(yīng)的操作,keycode-按鍵鍵碼 */void KeyAction(unsigned char keycode){ static bit dirMotor = 0; //電機(jī)轉(zhuǎn)動(dòng)方向 //控制電機(jī)轉(zhuǎn)動(dòng) 1-9 圈 if ((keycode>=0x30) && (keycode<=0x39)){ if (dirMotor == 0){ StartMotor(360*(keycode-0x30)); }else{ StartMotor(-360*(keycode-0x30)); } }else if (keycode == 0x26){ //向上鍵,控制轉(zhuǎn)動(dòng)方向?yàn)檎D(zhuǎn) dirMotor = 0; }else if (keycode == 0x28){ //向下鍵,控制轉(zhuǎn)動(dòng)方向?yàn)榉崔D(zhuǎn) dirMotor = 1; }else if (keycode == 0x25){ //向左鍵,固定正轉(zhuǎn)90度 StartMotor(90); }else if (keycode == 0x27){ //向右鍵,固定反轉(zhuǎn)90度 StartMotor(-90); }else if (keycode == 0x1B){ //Esc 鍵,停止轉(zhuǎn)動(dòng) StopMotor(); }}/* 按鍵驅(qū)動(dòng)函數(shù),檢測(cè)按鍵動(dòng)作,調(diào)度相應(yīng)動(dòng)作函數(shù),需在主循環(huán)中調(diào)用 */void KeyDriver(){ unsigned char i, j; static unsigned char backup[4][4] = { //按鍵值備份,保存前一次的值 {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1} }; for (i=0; i<4; i++){ //循環(huán)檢測(cè)4*4的矩陣按鍵 for (j=0; j<4; j++){ if (backup[i][j] != KeySta[i][j]){ //檢測(cè)按鍵動(dòng)作 if (backup[i][j] != 0){ //按鍵按下時(shí)執(zhí)行動(dòng)作 KeyAction(KeyCodeMap[i][j]); //調(diào)用按鍵動(dòng)作函數(shù) } backup[i][j] = KeySta[i][j]; //刷新前一次的備份值 } } }}/* 按鍵掃描函數(shù),需在定時(shí)中斷中調(diào)用,推薦調(diào)用間隔 1 ms */void KeyScan(){ unsigned char i; static unsigned char keyout = 0; //矩陣按鍵掃描輸出索引 static unsigned char keybuf[4][4] = { //矩陣按鍵掃描緩沖區(qū) {0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF} }; //將一行的4個(gè)按鍵值移入緩沖區(qū) keybuf[keyout][0] = (keybuf[keyout][0] << 1) | KEY_IN_1; keybuf[keyout][1] = (keybuf[keyout][1] << 1) | KEY_IN_2; keybuf[keyout][2] = (keybuf[keyout][2] << 1) | KEY_IN_3; keybuf[keyout][3] = (keybuf[keyout][3] << 1) | KEY_IN_4; //消抖后更新按鍵狀態(tài) for (i=0; i<4; i++){ //每行4個(gè)按鍵,所以循環(huán)4次 if ((keybuf[keyout][i] & 0x0F) == 0x00){ //連續(xù)4次掃描值為0,即 4*4 ms 內(nèi)都是按下?tīng)顟B(tài)時(shí),可認(rèn)為按鍵已穩(wěn)定的按下 KeySta[keyout][i] = 0; }else if ((keybuf[keyout][i] & 0x0F) == 0x0F){ //連續(xù)4次掃描值為1,即 4*4 ms 內(nèi)都是彈起狀態(tài)時(shí),可認(rèn)為按鍵已穩(wěn)定的彈起 KeySta[keyout][i] = 1; } } //執(zhí)行下一次的掃描輸出 keyout++; //輸出索引遞增 keyout = keyout & 0x03; //索引值加到4即歸零 //根據(jù)索引,釋放當(dāng)前輸出引腳,拉低下次的輸出引腳 switch (keyout){ case 0: KEY_OUT_4 = 1; KEY_OUT_1 = 0; break; case 1: KEY_OUT_1 = 1; KEY_OUT_2 = 0; break; case 2: KEY_OUT_2 = 1; KEY_OUT_3 = 0; break; case 3: KEY_OUT_3 = 1; KEY_OUT_4 = 0; break; default: break; }}/* 電機(jī)轉(zhuǎn)動(dòng)控制函數(shù) */void TurnMotor(){ unsigned char tmp; //臨時(shí)變量 static unsigned char index = 0; //節(jié)拍輸出索引 unsigned char code BeatCode[8] = { //步進(jìn)電機(jī)節(jié)拍對(duì)應(yīng)的 IO 控制代碼 0xE, 0xC, 0xD, 0x9, 0xB, 0x3, 0x7, 0x6 }; if (beats != 0){ //節(jié)拍數(shù)不為0則產(chǎn)生一個(gè)驅(qū)動(dòng)節(jié)拍 if (beats > 0){ //節(jié)拍數(shù)大于0時(shí)正轉(zhuǎn) index++; //正轉(zhuǎn)時(shí)節(jié)拍輸出索引遞增 index = index & 0x07; //用&操作實(shí)現(xiàn)到8歸零 beats--; //正轉(zhuǎn)時(shí)節(jié)拍計(jì)數(shù)遞減 }else{ //節(jié)拍數(shù)小于0時(shí)反轉(zhuǎn) index--; //反轉(zhuǎn)時(shí)節(jié)拍輸出索引遞減 index = index & 0x07; //用&操作同樣可以實(shí)現(xiàn)到-1時(shí)歸7 beats++; //反轉(zhuǎn)時(shí)節(jié)拍計(jì)數(shù)遞增 } tmp = P1; //用 tmp 把 P1 口當(dāng)前值暫存 tmp = tmp & 0xF0; //用&操作清零低4位 tmp = tmp | BeatCode[index]; //用|操作把節(jié)拍代碼寫(xiě)到低4位 P1 = tmp; //把低4位的節(jié)拍代碼和高4位的原值送回 P1 }else{ //節(jié)拍數(shù)為0則關(guān)閉電機(jī)所有的相 P1 = P1 | 0x0F; }}/* T0 中斷服務(wù)函數(shù),用于按鍵掃描與電機(jī)轉(zhuǎn)動(dòng)控制 */void InterruptTimer0() interrupt 1{ static bit div = 0; TH0 = 0xFC; //重新加載初值 TL0 = 0x67; KeyScan(); //執(zhí)行按鍵掃描 //用一個(gè)靜態(tài) bit 變量實(shí)現(xiàn)二分頻,即 2 ms 定時(shí),用于控制電機(jī) div = ~div; if (div == 1){ TurnMotor(); }}