熱門: 51單片機(jī) | 24小時(shí)必答區(qū) | 單片機(jī)教程 | 單片機(jī)DIY制作 | STM32 | Cortex M3 | 模數(shù)電子 | 電子DIY制作 | 音響/功放 | 拆機(jī)樂園 | Arduino | 嵌入式OS | 程序設(shè)計(jì)
![]() |
發(fā)布時(shí)間: 2018-9-27 18:22
正文摘要:遇到兩個(gè)問題 1.擴(kuò)展口的PA口無法使用,我設(shè)定的程序是當(dāng)P2.0口為低電平時(shí)將PA口循環(huán)輸出信號(hào)經(jīng)過光耦和驅(qū)動(dòng)電路使得電機(jī)轉(zhuǎn)動(dòng),但是無法實(shí)現(xiàn)。已經(jīng)定義了這些不知道缺少了什么; #include<reg51.h ... |
sxhwdz 發(fā)表于 2018-9-28 07:22 謝謝 這個(gè)問題解決了 |
地址鎖存器74S373的輸入端順序接反了,CPU的 P0.0~P0.7 依次連接了 373的 D7~D0,所以8255的地址全錯(cuò)了。把連線換過來,或者把地址定義改一下: #define PA XBYTE[0x0000] #define PB XBYTE[0x0080] #define PC XBYTE[0x0040] #define COM XBYTE[0x000C] 這樣也是可以的 |
#include<reg51.h> #include<absacc.h> #include<intrins.h> #define uchar unsigned char #define uint unsigned int #define PA XBYTE[0x0000] #define PB XBYTE[0x0001] #define PC XBYTE[0x0002] #define COM XBYTE[0x0003] //PA、PB、PC端口及命令端口地址定義 #define motor1 PA #define motor2 PB sbit RS = P3^5; //液晶引腳定義 sbit RW = P3^3; sbit EN = P3^4; sbit Xqj=P2^0; //電機(jī)正轉(zhuǎn),X軸前進(jìn)按鈕 sbit Yqj=P2^1; //電機(jī)正轉(zhuǎn),Y軸前進(jìn)按鈕 sbit Xht=P2^2; //電機(jī)反轉(zhuǎn),X軸后退按鈕 sbit Yht=P2^3; //電機(jī)反轉(zhuǎn),Y軸后退按鈕 sbit jiting=P2^4;//急停按鈕 sbit dula=P2^6; sbit wela=P2^7; bit flag1=0; //x軸正反轉(zhuǎn)標(biāo)志位 bit flag2=0; //Y軸正反轉(zhuǎn)標(biāo)志位 uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0xf8,0x80,0x90};//共陽極數(shù)碼管編碼 uchar code a1[]={0xf2,0xfa,0xf8,0xf9,0xf1,0xf5,0xf4,0xf6}; //八拍正轉(zhuǎn) uchar code a2[]={0xf6,0xf4,0xf5,0xf1,0xf9,0xf8,0xfa,0xf2}; //八拍反轉(zhuǎn) uchar temp1=0000,temp2=0000,temp3=0000,temp4=0000; void delayms(uchar x)//延時(shí)程序 { uchar c,j; for(c=x;c>0;c--) for(j=123;j>0;j--); } /***********LED寫入命令函數(shù)***********/ void write_com(uchar com) { //while(LCD_Check_Busy()); //忙則等待 RS=0; P1=com; _nop_(); RW=0; EN=1; _nop_(); EN=0; _nop_(); } /**********LED寫入數(shù)據(jù)函數(shù)**********/ void write_dat(uchar chr) { //while(LCD_Check_Busy()); //忙則等待 RS=1; P1= chr; _nop_(); RW=0; EN=1; _nop_(); EN=0; _nop_(); } void write_shuzi(uchar dat) { uchar t; t=dat+'0'; //while(LCD_Check_Busy()); //忙則等待 RS=1; RW=0; P1=t; _nop_(); EN=1; _nop_(); EN=0; } void GotoXY(uchar x,uchar y) { if(y==0) //第一行 write_com(0x80|x); //寫地址 if(y==1) //第二行 write_com(0x80|(x+0x40)); } /*****LED初始化函數(shù)******/ void LCD_Init() { RW=0; dula=0; //關(guān)閉數(shù)碼管顯示 wela=0; //關(guān)閉數(shù)碼管顯示 write_com(0x38); /*顯示模式設(shè)置*/ delayms(5); write_com(0x06);/*顯示光標(biāo)移動(dòng)設(shè)置*/ delayms(5); write_com(0x0C); /*顯示開及光標(biāo)設(shè)置*/ write_com(0x01); /*顯示清屏*/ } void dispose() { uchar a,b,bai1,shi1,ge1,bai2,shi2,ge2; a=(temp1-temp3)/8*75/36*5; b=(temp2-temp4)/8*75/36*5; bai1=a%1000/100; shi1=a%100/10; ge1=a%10; bai2=b%1000/100; shi2=b%100/10; ge2=b%10; write_shuzi(bai1); GotoXY(7,1); write_shuzi(shi1); GotoXY(8,1); write_shuzi(ge1); GotoXY(9,1); write_shuzi(bai2); GotoXY(7,2); write_shuzi(shi2); GotoXY(8,2); write_shuzi(ge2); GotoXY(9,2); write_dat('X'); GotoXY(4,1); write_dat(':'); GotoXY(5,1); write_dat('Y'); GotoXY(4,2); write_dat(':'); GotoXY(5,2); if(a<0) { write_dat('-'); GotoXY(6,1); } if(b<0) { write_dat('-'); GotoXY(6,2); } } void key() { if(Xqj==0) { delayms(5); if(Xqj==0) { flag1=1; while(Xqj==0); } } if(Xht==0) { delayms(5); if(Xht==0) { flag1=~flag1; while(Xht==0); } } if(Yqj==0) { delayms(5); if(Yqj==0) { flag2=1; while(Yqj==0); } } if(Yht==0) { delayms(5); if(Yht==0) { flag2=~flag2; while(Yht==0); } } } void qudong() { if(flag1==1) { uchar i; for(i=0;i<8;i++) { if(jiting==0) {break;} PA=a1[i]; temp1++; } i=0; } if(flag2==1) { uchar j; for(j=0;j<8;j++) { if(jiting==0) {break;} PB=a1[j]; temp2++; } j=0; //八拍正轉(zhuǎn)子程序 } if(flag1==0) { uchar k; for(k=0;k<8;k++) { if(jiting==0) {break;} if(Xht==0) {PA=a2[k];} temp3++; } k=0; } if(flag2==0) { uchar u; for(u=0;u<8;u++) { if(jiting==1) {break;} if(Yht==1) {PB=a2[u];} temp4++; } //八拍反轉(zhuǎn)轉(zhuǎn)子程序 u=0; } } void main() { COM=0x80;//方式0,PA口與PB口均為輸出 Xqj=1; Yqj=1; Xht=1; Yht=1; jiting=1; while(1) { key(); dispose(); qudong(); } } 這是程序 |
本帖最后由 LonelyLiar 于 2018-9-27 18:25 編輯 還有LCD總是顯示busy,無法顯示.. |
Powered by 單片機(jī)教程網(wǎng)