📄 main.c
字号:
/****************************************Copyright (c)**************************************************
** 广州周立功单片机发展有限公司
** 研 究 所
** 产品一部
**
** http://www.zlgmcu.com
**
** 文 件 名: main.c
** 最后修改日期: 2004年11月15日
** 描 述: 用户应用程序
** 版 本: V5.0
**********************************************************************************************************/
#include <reg52.h>
#include "D12Config.h"
#include "Descriptor.h"
#include "Chap_9.h"
#include "D12Driver.h"
sbit MCU_LED0 = P1^2; //LED
sbit MCU_LED1 = P1^3;
sbit P1_4=P1^4; //P1.4输出方向电平信号
sbit P1_5=P1^5; //P1.5输出步进脉冲信号
sbit P2_0=P2^0; //P3.4输出脱机信号
unsigned int flag,th0,tl0,step,step1,step2,j,k,TIMER,count=0,EvTimer=0;
void main()
{ INT32U cnt,i;
EpBuf[0]=0;
for(cnt = 0; cnt <= 0xffff; cnt++) //延时
{
MCU_LED0 = 0; //D2和D3点亮
MCU_LED1 = 0;
}
MCU_LED0 = 1; //D2和D3灭
MCU_LED1 = 1;
P0 = 0xFF; //初始化I/O口
P1 = 0xFF;
P2 = 0xFF;
P3 = 0xFF;
if (Init_D12()!=0) //初始化D12
return; //如果初始化不成功,返回
EX0 = 1; //开外部中断0
PX0 = 0; //设置外部中断0中断优先级
EA = 1; //开总中断
TMOD=0x01; //T0采用方式1进行定时
ET0=1; //开定时器中断
TR0=0; //关定时器
while(1)
{
usbserve(); //处理USB事件
if(bEPPflags.bits.configuration)
{ if(bEPPflags.bits.ep2_rxdone==1) //端点2收到数据
{ DISABLE();
bEPPflags.bits.ep2_rxdone=0; //清除端点2收到数据标志
ENABLE();
flag=EpBuf[0]; //转动方向
th0=EpBuf[1]; //定时器T0高字节
tl0=EpBuf[2]; //定时器T0低字节
TIMER=EpBuf[3]; //定时次数
step1=EpBuf[4]; //步距数高字节
step2=EpBuf[5]; //步距数低字节
step=(step1<<8)+step2; //步距数
switch(flag)
{case 0: {P2_0=0;EpBuf[4]=0;EpBuf[5]=0;}break; //停止
case 1: {P2_0=1; //使能
P1_4=0; //正转
for(cnt = 0; cnt <= 0xff; cnt++); //延时
j=0; //判别已转步数
for(i=0;i<2*step;i++)
{TH0=th0;TL0=tl0;TR0=1; //启动定时器
while(!EvTimer); //等待定时器中断,直到EvTimer=1
EvTimer=0; //清除中断标志
P1_5=!P1_5; //输出信号跳变
if(P1_5)j++; //转动步数增加
TR0=0; //禁止定时器
}
P2_0=0; //停止转动后禁止使能
k=j>>8; //高字节
EpBuf[4]=k;
EpBuf[5]=j-256*k;}break;//低字节
case 2:{P2_0=1; //使能
P1_4=1; //反转
for(cnt = 0; cnt <= 0xff; cnt++); //延时
j=0; //判别已转步数
for(i=0;i<2*step;i++)
{TH0=th0;TL0=tl0;TR0=1; //启动定时器
while(!EvTimer); //等待定时器中断,直到EvTimer=1
EvTimer=0; //清除中断标志
P1_5=!P1_5; //输出信号跳变
if(P1_5)j++; //转动步数增加
TR0=0; //禁止定时器
}
P2_0=0; //停止转动后禁止使能
k=j>>8; //高字节
EpBuf[4]=k;
EpBuf[5]=j-256*k;}break;//低字节
}
D12_WriteEndpoint(5,6,EpBuf); //通过端点2发送5个字节给主机
}
}
}
}
void TIMER0(void) interrupt 1 //定时中断处理程序
{TH0=th0;
TL0=tl0;
count++;
if(count>=TIMER)
{count=0;
EvTimer=1;}//设置定时器中断
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -