STC15W408AS单片机按键控制2步进电机
- 主控芯片:
STC15W408AS
- 采用TB6600驱动
主程序代码:
#include
sbit CLK = P1^0;
sbit ENA = P1^1;
sbit DIR = P1^2;
sbit CLK1 = P1^3;
sbit ENA1 = P1^4;
sbit DIR1 = P1^5;
sbit UP = P3^2;
sbit DWN = P3^3;
sbit LFT = P3^4;
sbit RHT = P3^5;
sbit SET = P3^6;
sbit RST = P3^7;
unsigned int Tspeed[10]={30,40,60,80,100,200,400,600,800,1000};
bit CY_status=0;
unsigned int CYCLE=20;
unsigned int cy_count=0;
unsigned PWM_count=0;
void Init_time();
void Key_scan();
void main()
{
ENA=0;
CLK=0;
DIR=0;
ENA1=0;
CLK1=0;
DIR1=0;
Init_time();
while(1)
{
Key_scan();
}
}
void Init_time()
{
TMOD |= 0x01;
TH0=(65536-CYCLE)/256;
TL0=(65536-CYCLE)%256;
IE = 0x82;
TR0=1;
}
void Enter_Timer0() interrupt 1
{
TH0=(65536-CYCLE)/256;
TL0=(65536-CYCLE)%256;
CLK=~CLK;
CLK1=~CLK1;
PWM_count++;
if(PWM_count==(2*cy_count)&&CY_status)
{
PWM_count=0;
TR0=0;
ENA=0;
ENA1=0;
}
}
void Key_scan()
{
static char i=0;
if(UP==0)
{
if(UP==0)
{
DIR=1;
ENA=1;
TR0=1;
while(!UP);
ENA=0;
TR0=0;
CY_status=0;
}
}
if(DWN==0)
{
if(DWN==0)
{
DIR=0;
ENA=1;
TR0=1;
while(!DWN);
ENA=0;
TR0=0;
CY_status=0;
}
}
if(LFT==0)
{
if(LFT==0)
{
DIR1=1;
ENA1=1;
TR0=1;
while(!LFT);
ENA1=0;
TR0=0;
CY_status=0;
}
}
if(RHT==0)
{
if(RHT==0)
{
DIR1=0;
ENA1=1;
TR0=1;
while(!RHT);
ENA1=0;
TR0=0;
CY_status=0;
}
}
if(SET==0)
{
if(SET==0)
{
while(!SET);
i--;
if(i<1)
i=0;
CYCLE=Tspeed[i];
}
}
if(RST==0)
{
if(RST==0)
{
while(!RST);
i++;
if(i>10)
i=10;
CYCLE=Tspeed[i];
}
}
}