现象是这样的旋转编码器时转的慢没有事!转的快单片机就死机了!大家帮我看看是什么问题!
单片机源程序如下:
- #include <STC12C5A60S2.H>
- typedef unsigned int uint;
- typedef unsigned char uchar;
- sbit wela = P1^1;
- sbit dula = P1^2;
- sbit LED=P1^0;
- sbit AA=P3^7; //编码加
- sbit BB=P3^6; //编码减
- uint flage,status,current_bmb,counter = 0,
- p,dataa;
- float PA;
- static uint m,tm;
- unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
- 0x07,0x7f,0x6f};
- void delay(unsigned char i)
- {
- unsigned char j,k;
- for(j=i;j>0;j--)
- for(k=110;k>0;k--);
- }
- void datakae()
- {
- status=BB; //一直赋值!
- while(0==AA)//当AA条件允许进入主循环!并且BB上次的值保持为临近状态!
- {
- current_bmb=BB;
- flage=1;
- }
- if(1==flage)
- {
- flage=0;
- if((0==current_bmb)&&(1==status))
- {
- tm++;
- if(tm>=1000)
- {
- tm=1000;
- }
- }
- if((0==status)&&(1==current_bmb))
- {
- tm--;
- if(tm<=10)
- {
- tm=10;
- }
- }
- }
- }
- void display()
- {
- P0=table[dataa%10];//个
- dula=1;
- dula=0;
- P2=0xfe;
- wela=1;
- wela=0;
- delay(2);
- P0=table[dataa%100/10]; //十
- dula=1;
- dula=0;
- P2=0xfd;
- wela=1;
- wela=0;
- delay(2);
- P0=table[dataa%1000/100];
- dula=1;
- dula=0;
- P2=0xfb; // 百
- wela=1;
- wela=0;
- delay(2);
-
- P0=table[dataa%10000/1000];
- dula=1;
- dula=0;
- P2=0xf7;//千
- wela=1;
- wela=0;
- delay(2);
- }
- void huansuan()
- {
- m=10000/tm;
- dataa=tm;
- }
- //定时器T1初始化
- void initial()
- {
- LED=0;
- P1M1 = 0x00;
- P1M0 = 0xFF;
- counter=0;
- EA = 1;
- TR0 = 1;
- TMOD = 0x01;
- TH0 = (65535-1000)/256; //1ms
- TL0 = (65535-1000)%256;
- ET0 = 1;
- }
- //主程序
- void main()
- {
- tm=250;
- initial();
- while(1)
- {
- huansuan();
- datakae();
- display();
- LED=0;
- }
- }
- void timer0_int()interrupt 1 using 0
- {
- TH0 = (65535-1000)/256;//1ms产生一次中断
- TL0 = (65535-1000)%256;
- counter++;
- if(counter==m)
- {
- LED=1;
- for(p=0;p<150;p++);
- counter=0;
- } else LED=0;
-
- }
复制代码
|