|
這是我的代碼:
如下:改改
#include<reg52.h>
#include "intrins.h"
#include "delay.h"
#include"Drive_parallel.h"
#define uint8 unsigned char //宏定義
#define uint unsigned int //宏定義
#define uchar unsigned char //宏定義
#define DataPort P0 //數(shù)據(jù)口
sbit bianmaqiZ=P1^3; //編碼器Z線
sbit fuwei=P3^5; //復(fù)位口
sbit BMQshuruB=P3^2; //輸入口接編碼器B線
sbit DIR=P3^3; //比較口接編碼器A線
int i,count;
long bianma;//編碼
uchar code line1_data[]={" 編碼器測(cè)試 "};
uchar code line2_data[]={"計(jì)數(shù): "};
///////////////////////////////////////
void lcd_pos(uchar X,uchar Y)
{
uchar pos;
if (X==1) {X=0x80;}
else if (X==2) {X=0x90;}
else if (X==3) {X=0x88;}
else if (X==4) {X=0x98;}
pos = X+Y ;
lcd_wcmd(pos); //顯示地址
}
///////////////////////////////////////
void display() // 開(kāi)機(jī)頁(yè)面
{
lcd_clr();
lcd_pos(1,0);
for(i=0;i<16;i++)
{
lcd_wdat(line1_data[i]);
delay_ms(1);
}
lcd_pos(3,0);
for(i=0;i<16;i++)
{
lcd_wdat(line2_data[i]);
delay_ms(1);
}
}
void bianma_display() //顯示計(jì)數(shù)
{
lcd_pos(3,4);
lcd_wdat(bianma/10000%10+0x30);
lcd_wdat(bianma/1000%10+0x30);
lcd_wdat(bianma/100%10+0x30);
lcd_wdat(bianma/10%10+0x30);
lcd_wdat(bianma%10+0x30);
}
////////////////////////////////////////////
void main()
{
lcd_init();//初始化
lcd_clr(); //清屏
display(); //開(kāi)機(jī)頁(yè)面
bianma=0;
EA=1; //開(kāi)總中斷
EX0=1; //外部中斷0打開(kāi)
IT0=1; //等于0為電平觸發(fā)方式,低電平有效,等于1為脈沖觸發(fā)方式,下降沿有效.
while(1)
{
if(!fuwei||bianma>999000||bianma<0)
{
bianma=0;bianmaqiZ=0;
bianma_display();
}
bianma_display();
}
}
/*中斷函數(shù),編碼器AB進(jìn)入單片機(jī)時(shí)要通過(guò)高速光耦隔離,
INT0與INT1口10K上拉電阻接+5V*/
void int0() interrupt 0
{
EX0=0;//外部中斷0關(guān)閉//等于0為電平觸發(fā)方式,低電平有效,等于1為脈沖觸發(fā)方式,下降沿有效,
if(DIR){count--; }//中斷計(jì)數(shù)---正轉(zhuǎn)時(shí),A為下降沿,B為高電平
else count++;
if(!BMQshuruB)bianma++; //中斷計(jì)數(shù)---反轉(zhuǎn)時(shí),A為下降沿,B為低電平
EX0=1;//外部中斷0打開(kāi)
}
|
|