- #include "regx51.h"
- unsigned char a,b,c,d;
- unsigned char code disaplay []= {0x3f, 0x06, 0x5b, 0x4f, 0x66,0x6d, 0x7d, 0x07, 0x7f, 0x6f} ;
- long counter=0; //旋轉編碼 器的脈沖
- long position=0; //旋轉 編碼器的當前位置
- long degree=0;
- void main()
- TMOD=0x06;
- TH0=0xff;
- TL0=0xff;
- TH1= (65536-1000)/256;
- TL1= (65536-1000)%256;
- IE=0x0f;
- EA = 1; //開總中斷
- r=0;
- while(1) //主程序循環顯示位置
- {
- a=degree%10; //計算參數的個十百千位
- b= (degree/10)%10; .
- c= (degree/100)%10;
- d= (degree/1000)%10;
- p2_3=0;//顯示position值
- p0=diaplay[a];
- p2_3=1;
- p2_2=0;
- p0=diaplay[b];
- p2_ 2=1;
- p2_ 1=0;
- p0=diaplay[c];
- p2_1=1;
- p2_0=0;
- p0=diaplay[d];
- p2_0=1;
- }
- }
- void int0() interrupt 0 //A 相中斷
- if(p2_7)position++; //判斷2_7 為高則正向,為低則反向
- else position;
- counte++ ; //累計脈沖
- }
- void int1()
- interrupt 1 //Z相中斷
- {
- position=0; //使位置參數置零
- void tl_ () interrupt 2 //定時0. ls刷新一次轉速和角
- //度參數
- THO= (65536-1000) /256;
- TL0= (65536-1000) %256;
- r=600*counter/ 300; //假設編碼器每轉300個脈沖
- degree=1. 2*position; //把脈沖數轉換為 角度數
- if(! degree) //角度為負時加360度轉為正
- degree+=360}
- void t0_ () interrupt 3 //計數器T0中斷顯示速度
- {
- unsigned char i
- i=100;
- a=r%10;
- b=(r/10)%10;
- c=(r/100)%10;
- d= (r/1000)%10;
- while(i)
- {
- p2_ 3=0;
- p0=diaplay[a];
- p2_ 3=1;
- p2_ 2=0;
- p0=diaplay[b];
- p2_ 2=1;
- p2_ 1=0;
- p0=diap1ay[c];
- p2_ 1=1;
- p2_ _0=0;
- p0=diaplay[d];
- p2_ 0=1;
- i-- ;
- }
- }
復制代碼
*** WARNING C318 IN LINE 1 OF ..\main.c: can't open file 'regx51.h'
*** ERROR C244 IN LINE 8 OF ..\main.c: 'TMOD': can't initialize, bad type or class
*** ERROR C132 IN LINE 8 OF ..\main.c: 'TMOD': not in formal parameter list
|