标题:
一个自己写的程序 编码器转动没反应
[打印本页]
作者:
1120320067
时间:
2016-5-1 17:36
标题:
一个自己写的程序 编码器转动没反应
大神请指教 本程序测得是增量式编码器位置 说白了就是计数A B脉冲 但是烧进去之后示数一直是0 转动编码器不会有反应 前后改了几次实在是改不出来了 求大神支招
#include <REG51.H> // 寄存器头文件包含
#include <INTRINS.H> // 空操作函数,移位函数头文件包含
#define uchar unsigned char
#define uint unsigned int
unsigned char counter;
unsigned char b;
sbit PINA = P1^0;//csn信号white
sbit PINB = P1^1;//这个是时钟blue
sbit PIND = P1^2;//这个是数据green
sbit LED1 = P2^7;//LED显示位选控制 千位
sbit LED2 = P2^6;//LED显示位选控制 百位
sbit LED3 = P2^5;//LED显示位选控制 十位
sbit LED4 = P2^4;//LED显示位选控制 个位
uchar led_buf[4]; /*定义LED显示缓冲区,将带显示数值放入该区域*/
/*------------------------------------------------
数码管段码表
------------------------------------------------*/
uchar code disp_buff[]= {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};
/*------------------------------------------------
延时子程序
------------------------------------------------*/
void delay(unsigned int cnt)
{
while(--cnt);
}
/*void delay(uint t)
{
uint i;
while(t--)
{
for(i=0;i<125;i++){}
}
}*/
/*------------------------------------------------
取编码器位置值子程序
------------------------------------------------*/
long GetSSI(void)
{
static bit Curr_encoder_b; //定义一个变量来储存当前B信号
static bit Last_encoder_b; //定义一个变量来储存上次B脚信号
static bit updata= 0;
if( PINA && PINB) //编码器无转动退出
{
updata = 0;
return;
}
Last_encoder_b = PINB; //记录B信号
while(!PINA) //等待A由低变高
{
Curr_encoder_b = PINB; //记录等待期间的B信号(指当前B信号)
updata = 1; }
if(updata)
{
updata = 0 ;
if( (Last_encoder_b == 0)&&(Curr_encoder_b== 1) ) //B从0到1为正转
{
if(counter == 255)
return;
counter++; //正转计数加
}
else if( (Last_encoder_b == 1)&&(Curr_encoder_b == 0) ) //B从1到0为反转
{
if(counter == 0)
return;
counter--; //反转计数减
}
}
}
//LED 共阳
void main()
{
//设置定时器模式
TMOD = 0x01; //设置定时器模式
TL0 = (65636-50000)/256; //设置定时初值
TH0 = (65636-50000)%256; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时;
//启动定时器2
counter = 0;
while(1)
{
b=GetSSI();//取绝对值编码器位置数据
led_buf[3]=b/1000;//千位
led_buf[2]=(b%1000)/100;//百位
led_buf[1]=(b%100)/10;//十位
led_buf[0]=b%10; //个位
//将数据的各位数字放入指定缓冲区
P0 = disp_buff[led_buf[3]]; //取显示数据,千位段码
LED1 = 0; //点亮LED1(千位)
delay(200); //延时一小段时间
LED1 = 1; //熄灭LED1
P0 = disp_buff[led_buf[2]]; //取显示数据,段码
LED2 = 0; //点亮LED2(第二位百位)
delay(200); //延时一小段时间
LED2 = 1; //熄灭LED2
P0 = disp_buff[led_buf[1]]; //取显示数据,段码
LED3 = 0; //点亮LED3(第三位十位)
delay(200); //延时一小段时间
LED3 = 1; //熄灭LED3
P0 = disp_buff[led_buf[0]]; //取显示数据,段码
LED4 = 0; //点亮LED4(第四位)
delay(200); //延时一小段时间
LED4 = 1; //熄灭LED4
// delay(300);
}
}
作者:
jwjjwj123
时间:
2016-8-11 18:38
楼主问题解决了吗,我也正要
作者:
jubaolun
时间:
2016-8-11 19:02
这是我的代码:
如下:改改
#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 //数据口
sbit bianmaqiZ=P1^3; //编码器Z线
sbit fuwei=P3^5; //复位口
sbit BMQshuruB=P3^2; //输入口接编码器B线
sbit DIR=P3^3; //比较口接编码器A线
int i,count;
long bianma;//编码
uchar code line1_data[]={" 编码器测试 "};
uchar code line2_data[]={"计数: "};
///////////////////////////////////////
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() // 开机页面
{
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() //显示计数
{
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(); //开机页面
bianma=0;
EA=1; //开总中断
EX0=1; //外部中断0打开
IT0=1; //等于0为电平触发方式,低电平有效,等于1为脉冲触发方式,下降沿有效.
while(1)
{
if(!fuwei||bianma>999000||bianma<0)
{
bianma=0;bianmaqiZ=0;
bianma_display();
}
bianma_display();
}
}
/*中断函数,编码器AB进入单片机时要通过高速光耦隔离,
INT0与INT1口10K上拉电阻接+5V*/
void int0() interrupt 0
{
EX0=0;//外部中断0关闭//等于0为电平触发方式,低电平有效,等于1为脉冲触发方式,下降沿有效,
if(DIR){count--; }//中断计数---正转时,A为下降沿,B为高电平
else count++;
if(!BMQshuruB)bianma++; //中断计数---反转时,A为下降沿,B为低电平
EX0=1;//外部中断0打开
}
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1