📄 motor.c
字号:
#include <REG52.h>
int inportb(unsigned int port);
void acquisition_ball(void);
void outportb(unsigned int port,unsigned char val);
void motor_driver(unsigned char PWM);
void acquisition_encode(void);
//#define ADDRESSH_RECODE 0x0FFF
//#define ADDRESS_CONTROL 0xF73F
//#define ADDRESS_COUNTER0 0xF70F // 8253 计数器0读写地址
//#define ADDRESS_COUNTER1 0xF71F // 8253 计数器1读写地址
#define ADDRESS_BALL 0x2FF8
#define ADDRESS_RESET_RECODE 0x0FFF // endpoint LOAD of 74LS193 connent /Y0
unsigned char idata location_motor,carry;
unsigned char idata location_ball;
bit flag_encode;
void init_special_interrupts(void)
{
//中断0作为Z相的归零信息,中断1为进位或退位来驱动
IT0 = 0; //下降沿触发
//EX0 = 1; //外部中断0允许位
PX0 = 1; //外部中断0设定为高优先级
IT1 = 0; //下降沿触发
EX1 = 1; //外部中断1允许位
PX1 = 0; //外部中断1设定为低优先级
}
//I/O口初始化程序
void init_port()
{
P0 = 0xFF;
P1 = 0xFF;
P2 = 0xFF;
P3 = 0xEF;
flag_encode=0;
}
void main()
{
unsigned char PWM;
// unsigned char i=0,j=0;
carry=0x0A;
//circle=0x00;
init_special_interrupts();
init_port();
outportb(ADDRESS_RESET_RECODE,0x00);
EA=1 ;
acquisition_ball();
PWM=50; //21< PWM<95
motor_driver(PWM);
while(1)
{
acquisition_encode();
//if((j>200)&&(i<30)) carry++;
//if((j<30)&&(i>200)) carry--;
//location_motor=circle*1000+(carry<<8)+ i;
//j=i;
}
while(1);
}
////////////////////////////////////////////////////////////////////////////////////
/* 电机PWM信号输出,PWM值越大,占空比越大 */
//////////////////////////////////////////////////////////////////////////////
void motor_driver(unsigned char PWM)
{
//PWM=100-PWM;
outportb(0x473F,0X34) ; //选择计数器0,工作模式为2,向计数器中送入0X0064数据,设置方波周期为100个脉冲
outportb(0x470F,100) ;
outportb(0x470F,0X00) ;
outportb(0x473F,0X72) ; //选择计数器1,工作模式为1,向计数器中送入0X0032数据,设置方波脉冲宽度为50个脉冲
outportb(0x471F,PWM) ;
outportb(0x471F,0X00) ;
P1_6=0; //定义正反转
P1_7=1; //定义正反转
}
void acquisition_ball(void)
{ unsigned char i;
outportb(0x2FF8,0);
while(!P3_0);
P1_1=1; //AD0809读允许端有效
P1_0=0; //IDT7205读允许端有效进行读数
P1_0=1;
P1_1=0;
i=inportb(0x6FFF);
}
////////////////////////////////////////////////////////
/* 采集光电编码器两路,并送到IDT7205,从P0口读进保存在变量I里 */
////////////////////////////////////////////////////////////////////////
void acquisition_encode(void)
{ unsigned int i;
P1_4=0;
P1_5=1;
P1_5=0;
P1_0=0;
P1_0=1;
P1_4=1;
i=inportb(0x6FFF);
i=i|(carry<<8);
location_motor=i;
}
void carry_isr() interrupt 2
{
EA=0;
//P3_5=1;
if(P3_5==flag_encode) carry++;
else carry--;
P3_4=~P3_4;
flag_encode=~flag_encode;
EA=1 ;
}
int inportb(unsigned int port)
{
int c;
c = *((unsigned char xdata *) port) ;
return c;
}
void outportb(unsigned int port,unsigned char val)
{
*((unsigned char xdata *) port) = val;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -