




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、步進電機(鍵盤控制可調(diào)速)#in elude <reg51.h>#defi ne uchar un sig ned charstatic un sig ned int count;計數(shù)static int stepndex;步進索引數(shù),值為 0 7static bit turn; 步進電機轉(zhuǎn)動方向static bit stop_flag;/步進電機停止標志static int speedlevel; /步進電機轉(zhuǎn)速參數(shù),數(shù)值越大速度越大,最小值為1,速度最慢static int spcou nt;/步進電機轉(zhuǎn)速參數(shù)計數(shù)void ddelay(void); 鍵盤掃描延時函數(shù)void
2、delay(unsigned int endcount);延時函數(shù),延時為 endcount*1 毫秒void goru n();步進電機控制步進函數(shù)sbit P10=P2A0 ;電機端口定義sbit P1 仁 P2A1 ;sbit P12=P2A2;sbit P13=P2A3 ;/*鍵消抖延時函數(shù)*/void ddelay(void)uchar i;for (i=300;i>0;i-);/*鍵掃描函數(shù)*/uchar keysca n(v oid)uchar sca ncode;uchar tmpcode;P1 = 0xf8;/發(fā)全0行掃描碼if (P1 &0 xf8)!=0xf
3、8)/ 若有鍵按下ddelay(); /延時去抖動if (P1 &0 xf8)!=0xf8) / 延時后再判斷一次,去除抖動影響sca ncode = 0xfe;while(sca ncode&0 x08)!=0) /逐行掃描P1 = sea ncode; / 輸出行掃描碼if (P1 &0xf8)!=0xf8) / 本行有鍵按下tmpcode = (P1 &0 xf8)|0x07;return(sca ncode)+(tmpcode); /返回特征字節(jié)碼,為1的位即對應(yīng)于行和列else scan code = (sca ncode<<1)|0x01
4、; /行掃描碼左移一位return(0); /無鍵按下,返回值為0void ma in (void)uchar key;count = 0;step_i ndex = 0;spco unt = 0;P10 = 0;P11 = 0;P12 = 0;P13 = 0;EA = 1;允許CPU中斷TMOD = 0x11; /設(shè)定時器0和1為16位模式1ET0 = 1;定時器0中斷允許TH0 = 0xFc;TL0 = 0x18;設(shè)定時每隔1ms中斷一次TR0 = 1;/開始計數(shù)stop_flag = 0;turn=0;speedlevel = 20;while(1)key = keysca n();sw
5、itch(key)case 0x09:按鍵#, 正轉(zhuǎn)以 speedlevel = 1 的速度轉(zhuǎn) 1000*0.5MS=0.5Sstop_flag=0;turn = 0;speedlevel =10;goru n();delay(IOOO);break;case 0x0c:/按鍵*,停止 2000*0.5MS=0.5Sstop_flag=1;break;case 0x0a:/按鍵 0,反轉(zhuǎn)以 speedlevel = 1 的速度轉(zhuǎn) 1000*0.5MS=0.5Sstop_flag=0;turn=1;speedlevel =10;goru n();delay(1000);break;case 0x
6、11:按鍵 9, 以-speedlevel 的加速轉(zhuǎn) 1000*0.5MS=0.5Sstop_flag=0;if (speedlevel=1) speedlevel=1;else -speedlevel;goru n();delay(1000);break;case 0x12: / 按鍵 8, 以 +speedlevel 的減速轉(zhuǎn) 1000*0.5MS=0.5S stop_flag=0;+speedlevel;goru n();delay(1000);break;/定時器0中斷處理void timeint(void) interrupt 1TH0=0xFc;TL0=0x18; /設(shè)定時每隔1m
7、s中斷一次coun t+;SPEEDLEVEL越大,延時越長(延時約為spco un t-;if(spcou nt<=0)/ 速度調(diào)整,1MS*SPEEDLEVEL ),/頻率越小,速度越慢spco unt = speedlevel;goru n(); 延時函數(shù),延時為endcount*0.5毫秒void delay (un sig ned int en dco un t)/coun t=0;dowhile(co un t<e ndcoun t);void gorun()if (stop_flag=1)P10 = 0;P11 = 0;P12 = 0;P13 = 0;return;s
8、witch(step_i ndex) _case 0: 0P10 = 1;P11 = 0;P12 = 0;P13 = 0;break;case 1: 0、1P10 = 1;P11 = 1;P12 = 0;P13 = 0;break;case 2: 1P10 = 0;P11 = 1;P12 = 0;P13 = 0;break;case 3: 1、2P10 = 0;P11 = 1;P12 = 1;P13 = 0;break;case 4:2P10 = 0;P11 = 0;P12 = 1;P13 = 0;break;case 5: 2、3P10 = 0;P11 = 0;P12 = 1;P13 = 1
9、; break;case 6: 3P10 = 0;P11 = 0;P12 = 0;P13 = 1;break;case 7: 3、0P10 = 1;P11 = 0;P12 = 0;P13 = 1;if (turn=0)/ 正轉(zhuǎn)step_i ndex+;if (step_i ndex>7) step_i ndex=0; _else反轉(zhuǎn)step_i ndex-;if (stepndex<0) stepndex=7;步進電機(鍵盤控制可調(diào)速加顯示)#in elude <reg51.h>#defi ne uchar un sig ned charstatic un sig ne
10、d int count; 計數(shù)static int step_index;步進索引數(shù),值為 0 7static bit turn; 步進電機轉(zhuǎn)動方向static bit stop_flag;/步進電機停止標志static int speedlevel; /步進電機轉(zhuǎn)速參數(shù),數(shù)值越大速度越大,最小值為1,速度最慢static int spcou nt;/步進電機轉(zhuǎn)速參數(shù)計數(shù)void ddelay(void); 鍵盤掃描延時函數(shù)void delay(unsigned int endcount);延時函數(shù),延時為 endcount*1 毫秒void goru n();步進電機控制步進函數(shù)void D
11、elay400Ms(void);void LCMI ni t(void); LCM 初始化void WriteCommandLCM(unsigned char WCLCM,BuysC); /BuysC 為 0 時忽略忙檢測void Display On eChar(uchar X, uchar Y, uchar DData);void DisplayListChar(uchar X, uchar Y,uchar ListLe ngth, uchar *DData,ucharn);sbit P10=P3A0 ;電機端口定義sbit P1 仁 P3A1 ;sbit P12=P3A2;sbit P1
12、3=P3A3 ;ucharcodespeed= 0x3a,0x39,0x38,0x37,0x36,0x35,0x34,0x33,0x32,0x31,0x30;uchar code stop = "stop"uchar code go = "go:"uchar code back = "back:"uchar code max = "max:8"/*鍵消抖延時函數(shù)*/void ddelay(void)uchar i;for (i=300;i>0;i-);/*鍵掃描函數(shù)*/uchar keysca n(v oid
13、)uchar sca ncode;uchar tmpcode;P1 = 0xf8;/發(fā)全0行掃描碼if (P1 &0 xf8)!=0xf8) / 若有鍵按下ddelay(); /延時去抖動if (P1 &0 xf8)!=0xf8) / 延時后再判斷一次,去除抖動影響sca ncode = 0xfe;while(sca ncode&0 x08)!=0) / 逐行掃描P1 = sca ncode; / 輸出行掃描碼if (P1 &0xf8)!=0xf8) / 本行有鍵按下tmpcode = (P1 &0 xf8)|0x07;return(sca ncode)
14、+(tmpcode); /返回特征字節(jié)碼,為1的位即對應(yīng)于行和列else scan code = (sca ncode<<1)|0x01; /行掃描碼左移一位return(0); /無鍵按下,返回值為0void ma in (void)uchar key;count = 0;step_i ndex = 0;spco unt = 0;P10 = 0;P11 = 0;P12 = 0;P13 = 0;EA = 1;允許CPU中斷TMOD = 0x11; /設(shè)定時器0和1為16位模式1ET0 = 1;定時器0中斷允許TH0 = OxFc;TL0 = 0x18;設(shè)定時每隔1ms中斷一次TR0
15、 = 1;/開始計數(shù)P0=0XFF;P3 &=OXEF; 573 片選LCMInit(); /LCM 初始化Delay400Ms();stop_flag = 0;turn=0;speedlevel = 5;DisplayListChar(0,0,3,go,1); / 每次掃描鍵盤顯示更新一次 uchar code go DisplayOneChar(0,1,0x35); /每次掃描鍵盤顯示更新一次while(1)key = keysca n();switch(key)case 0x09:按鍵#, 正轉(zhuǎn)以 speedlevel = 1 的速度轉(zhuǎn) 1000*0.5MS=0.5Sstop_f
16、lag=0;turn = 0;speedlevel =5;goru n();WriteCommandLCM(0x01,1); 顯示清屏,DisplayListChar(0,0,3,go,0); / 每次掃描鍵盤顯示更新一次 uchar code goDisplayOneChar(0,1,0x35); /每次掃描鍵盤顯示更新一次 delay(1000);break;case 0x0c:/按鍵*,停止 2000*0.5MS=0.5Sstop_flag=1;WriteCommandLCM(0x01,1); 顯示清屏,DisplayListChar(0,0,4,stop,0); /每次掃描鍵盤顯示更新
17、一次 break;case OxOa:/按鍵 0,反轉(zhuǎn)以 speedlevel = 1 的速度轉(zhuǎn) 1000*0.5MS=0.5Sstop_flag=0;turn=1;speedlevel =5;goru n();WriteCommandLCM(0x01,1); 顯示清屏,DisplayListChar(0,0,5,back,0); /每次掃描鍵盤顯示更新一次DisplayOneChar(0,1,0x35); /每次掃描鍵盤顯示更新一次 delay(1000);break;case 0x11:按鍵 9, 以-speedlevel 的加速轉(zhuǎn) 1000*0.5MS=0.5Sstop_flag=0;i
18、f (speedlevel=2) speedlevel=2;else speedlevel-;goru n();if(speedlevel=2) DisplayListChar(0,1,5,max,0);else DisplayO neChar(0,1, speedspeedlevel); /每次掃描鍵盤顯示更新一次delay(1000);break;case 0x12: / 按鍵 8, 以 +speedlevel 的減速轉(zhuǎn) 1000*0.5MS=0.5S stop_flag=0;speedlevel+;goru n();WriteCommandLCM(0x01,1); 顯示清屏,if(tur
19、n=0)DisplayListChar(0,0,3,go,0); / 每次掃描鍵盤顯示更新一次 uchar code go Display On eChar(0,1, speedspeedlevel); /每次掃描鍵盤顯示更新一次else DisplayListChar(0,0,5,back,0); / 每次掃描鍵盤顯示更新一次Display On eChar(0,1,speedspeedlevel); / 每次掃描鍵盤顯示更新一次 delay(1000);break;/定時器0中斷處理void timeint(void) interrupt 1 THO=OxFc;TL0=0x18; /設(shè)定時
20、每隔1ms中斷一次coun t+;spco un t-;if(spcou nt<=0) /速度調(diào)整,SPEEDLEVEL 越大,延時越長(延時約為1MS*SPEEDLEVEL ),/頻率越小,速度越慢spco unt = speedlevel;goru n();void delay(unsigned int endcount) 延時函數(shù),延時為 endcount*0.5 毫秒coun t=0;dowhile(co un t<e ndcoun t);void gorun()if (stop_flag=1)P10 = 0;P11 = 0;P12 = 0;P13 = 0;return;s
21、witch(step_i ndex)case 0: 0P10 = 1;P11 = 0;P12 = 0;P13 = 0;break;case 1: 0、1P10 = 1;P11 = 1;P12 = 0;P13 = 0;break;case 2: 1P10 = 0;P11 = 1;P12 = 0;P13 = 0;break;case 3: 1、2P10 = 0;P11 = 1;P12 = 1;P13 = 0; break;case 4:2P10 = 0;P11 = 0;P12 = 1;P13 = 0;break;case 5: 2、3P10 = 0;P11 = 0;P12 = 1;P13 = 1;
22、break;case 6: 3P10 = 0;P11 = 0;P12 = 0;P13 = 1;break;case 7: 3、0P10 = 1;P11 = 0;P12 = 0;P13 = 1;if (turn=0)/ 正轉(zhuǎn)step_i ndex+; if (step_i ndex>7) step_i ndex=0;else反轉(zhuǎn)stepndex-;if (step_i ndex<0)step_i ndex=7; _步進電機(自動循環(huán)調(diào)速)#in elude <reg51.h> sbit P00=P2A0 ; sbit P01=P2A1 ; sbit P02=P2A2; s
23、bit P03=P2A3 ;static un sig ned int count;計數(shù)static int step_index;步進索引數(shù),值為 0 7static bit turn;步進電機轉(zhuǎn)動方向static bit stop_flag;/步進電機停止標志static int speedlevel; /步進電機轉(zhuǎn)速參數(shù),數(shù)值越大速度越快,最小值為1,速度最慢static int spcou nt;/步進電機轉(zhuǎn)速參數(shù)計數(shù)void delay(unsigned int endcount);延時函數(shù),延時為 endcount*0.5 毫秒void goru n();步進電機控制步進函數(shù)voi
24、d ma in (void)count = 0;step_i ndex = 0;spco unt = 0;stop_flag = 0;P00 = 0;P01 = 0;P02 = 0;P03 = 0;EA = 1;允許CPU中斷TMOD = 0x11; /設(shè)定時器0和1為16位模式1ET0 = 1;定時器0中斷允許TH0 = 0xFE;17TL0 = 0x0C;TR0 = 1;/設(shè)定時每隔0.5ms中斷一次/開始計數(shù)turn = 0;dospeedlevel =4;delay(10000);/以 speedlevel = 4 的速度轉(zhuǎn) 2000*0.5MS=1Sspeedlevel =4;delay(10000);/以 speedlevel = 4 的速度轉(zhuǎn) 2000*0.5MS=1Sstop_flag=1;delay(6000); 停止,2000*0.5MS=3Sstop_flag=0;whil
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五年度建筑工人勞動合同(附創(chuàng)新技術(shù)培訓(xùn)內(nèi)容)
- 二零二五年度國際酒店餐飲業(yè)勞務(wù)供應(yīng)協(xié)議
- 二零二五年度生活垃圾清運與環(huán)保技術(shù)研發(fā)應(yīng)用合同
- 電子商務(wù)平臺代運營服務(wù)協(xié)議
- 采購合同辣椒采購合同
- 音樂課本中的歌曲背后的故事征文
- 專業(yè)保潔服務(wù)合作協(xié)議
- 簡愛人物形象塑造分析:世界名著導(dǎo)讀課程教案
- 人力資源招聘與培訓(xùn)流程說明
- 企業(yè)綠色信用修復(fù)服務(wù)協(xié)議
- 心理健康教育課《在變化中成長》課件
- JJF 1341-2012 鋼筋銹蝕測量儀校準規(guī)范-(高清現(xiàn)行)
- 人教版數(shù)學(xué)五年級下冊 全冊各單元教材解析
- 給水排水管道工程質(zhì)量通病以及防治
- 偏癱臨床路徑流程
- 計算機視覺全套課件
- GB-T 9251-2022 氣瓶水壓試驗方法(高清版)
- 基于單片機的電子廣告牌設(shè)計畢業(yè)設(shè)計論文
- 中國聯(lián)通IMS接口規(guī)范 第三分冊:Sh接口 V1.0
- 判斷抽樣(課堂PPT)
- 通用橫版企業(yè)報價單模板
評論
0/150
提交評論