最近想做一个51单片机怎么检测增量编码器代替传统电位器的电路
在编程卡住了,先编写检测的程序,用数码管显示脉冲,正反连续旋转几次就卡住了,应该是死机,求助
下面是写的几个程序,求大家帮助
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit d1=P1^0;
sbit d2=P1^7;
sbit j1=P3^2;
sbit j2=P2^0;
sbit dula=P2^6;
sbit wela=P2^7;
uchar num,num1;
uint tt,yy;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f,
0x77,0x7c,0x39,0x5e,0x79,
0x71};
void main()
{
TMOD=0x05;//设置定时器0为方式1计数
TH0=(65536-600)/256;
TL0=(65536-600)%256;//装初值
EX0=1;
ET0=1;
EA=1;//开总中断
ET0=1;
TR0=1;//开外部中断0
IT0=1;//设置为下降沿出触发
wela=1;
P0=0xc0;
wela=0;
dula=1;
dula=0;
num=0;
while(1)
{
if(tt==60)
{
num++;
tt=0;
if(num==16)
num=0;
dula=1;
P0=table[num];
dula=0;
}
if(yy==60)
{
num--;
yy=0;
if(num==0)
num=16;
dula=1;
P0=table[num];
dula=0;
}
}
}
void jimiqi() interrupt 0//外部中断程衶
{
if(j2==1)
tt++;
else
yy++;
}
第二个
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
#define ulog unsigned long int
uchar numdu ,shiwan,wan,qian,bai,shi,ge;
ulog temp;
sbit dula=P2^6;
sbit wela=P2^7;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void xianshi(uchar shiwan,uchar wan,uchar qian,uchar bai,uchar shi,uchar ge);
void init()
void delay(uint z);
void main()
{
init();
shiwan=temp/100000;
wan=temp%100000/10000;
qian=temp%10000/1000;
bai=temp%1000%100;
shi=temp%100/10;
ge=temp%10;
while(1)
{
xianshi(shiwan,wan,qian,bai,shi,ge);
}
}
void delay(uint z)
{
uint x,y; //定义局部变量(只能在本 函数中使用)
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void xianshi(uchar shiwan,uchar wan,uchar qian,uchar bai,uchar shi,uchar ge)
{
dula=1;
P0=table[shiwan];
dula=0;
wela=1;
P0=0xfe;
wela=0;
delay(5);
dula=1;
P0=table[wan];
dula=0;
wela=1;
P0=0xfd;
wela=0;
delay(5);
dula=1;
P0=table[qian];
dula=0;
wela=1;
P0=0xfb;
wela=0;
delay(5);
dula=1;
P0=table[bai];
dula=0;
wela=1;
P0=0xf7;
wela=0;
delay(5);
dula=1;
P0=table[shi];
dula=0;
wela=1;
P0=0xef;
wela=0;
delay(5);
dula=1;
P0=table[ge];
dula=0;
wela=1;
P0=0xdf;
wela=0;
delay(5);
}
void init()
{
numdu=0;
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
|