📄 rainwiper v10.c
字号:
/********************************
名称:雨刷控制器
日期:2007-06-23
作者:Mavinger
版本:
V1.0:2007-07-8 ,创建此版本
V2.0:2006-11-5 ,修改外部晶振为11.0592M
硬件:
1.控制单片机: 12C5052AD
2.步进电机驱动器: L293D
********************************/
#include "../code/STC12C5410AD.h"
//#include "../code/STC89C51RC.h"
#include <INTRINS.h>
#include <math.h>
#include <stdio.h>
//数据类型定义
typedef unsigned char byte;
typedef unsigned int word;
typedef unsigned long dword;
//24M
//#define Timer_H 0xFF //0.1ms
//#define Timer_L 0x38
#define Timer_H 0xFF //0.01ms
#define Timer_L 0xEC
#define RunDegree 360
#define HighSpeed 35
#define LowSpeed 55
#define FordRun 1
#define BackRun 0
//电机驱动控制
//input
sbit Run_Mode =P3^5;
sbit Stop_Mode =P3^4;
sbit Speed_Mode =P3^3;
//output
sbit Step_EN1 =P1^7;
sbit Step_EN2 =P1^6;
sbit Out_A0 =P1^3;
sbit Out_A1 =P1^2;
sbit Out_B0 =P1^1;
sbit Out_B1 =P1^0;
bit MotorDir;
bit Runflag,Stopflag;
word stepsave,Degreesave;
byte np;
const byte forward[]=
{0xB8,0xFA,0x72,0xF6,0xB4,0xF5,0x71,0xF9};
const byte backward[]=
{0xF9,0x71,0xF5,0xB4,0xF6,0x72,0xFA,0xB8};
void Initial(void)
{
//AUXR=AUXR & 0x7F; //T0x12=0; 系统时钟12分频,即 T=12/6=2us
AUXR=0x00;
P1M0=0; //P1M0=0,P1M1=0,P1口为准双向口
P1M1=0;
P3M0=0;
P3M1=0;
EA=1;
}
//1ms*N 延时
void Delay1MS(word delay1ms)
{
word cnt;
byte cnt1;
for(cnt=0;cnt<delay1ms;cnt++)
{
for(cnt1=0;cnt1<200;cnt1++)
_nop_();
}
}
void MotorRst(void)
{
Step_EN1=1;
Step_EN2=1;
Out_A0=0;
Out_A1=0;
Out_B0=0;
Out_B1=0;
}
void MotorSet(void)
{
P1=0xB8;
Delay1MS(1);
}
void StepRun(byte StepDir,word Speednum) // One step run
{
if(StepDir & 0x01)
{
MotorDir=1;
if(np==0)
np=7;
else
np--;
P1=forward[np];
Delay1MS(Speednum);
}
else
{
MotorDir=0;
if(np==0)
np=7;
else
np--;
P1=backward[np];
Delay1MS(Speednum);
}
}
void MotorTurn(byte StepDir,word Speednum,word Degree) //方向,速度,度数
{
word degreecnt;
word stepcnt;
degreecnt=Degree/0.9;
Degreesave=degreecnt;
for(stepcnt=0;stepcnt<degreecnt;stepcnt++)
{
StepRun(StepDir,Speednum);
}
}
main()
{
Initial();
Run_Mode=1;
Stop_Mode=1;
Speed_Mode=1;
stepsave=0;
Degreesave=0;
if(Stop_Mode)
{
while(Stop_Mode)
{
StepRun(BackRun,LowSpeed);
}
}
while(1)
{
if(!Run_Mode)
{
Runflag=1;
if(!Speed_Mode)
{
MotorTurn(FordRun,LowSpeed,RunDegree); //正转
Delay1MS(200);
MotorTurn(BackRun,LowSpeed,RunDegree); //反转
MotorRst();
}
else
{
MotorTurn(FordRun,HighSpeed,RunDegree); //正转
Delay1MS(200);
MotorTurn(BackRun,HighSpeed,RunDegree); //反转
MotorRst();
}
}
else
{
MotorSet();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -