- #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
|