本帖最后由 yangjing6191519 于 2015-5-8 20:00 编辑
附图一张
http://bbs.elecfans.com/jishu_481183_1_1.html原理图proteus
程序在此keil:
http://bbs.elecfans.com/forum.ph ... ypeid%26typeid%3D93
void main(void)
{ UINT8 M,N,P;
UINT16 hour,min,l;
unsigned long m;
M=P0;//拨码开关从P0口输入
N=M&0xf0;//取P0口前四位作为定时时间的小时
P=M&0x0f;// 取P0口后四位作为定时时间的分钟
switch(N)
{ case 0x00:hour=0;
case 0x10:hour=3600;//将小时化为秒
case 0x20:hour=7200;
case 0x30:hour=10800;
case 0x40:hour=14400;
case 0x50:hour=18000;
case 0x60:hour=21600;
case 0x70:hour=25200;
}
switch(P)
{ case 0x00:min=0;//降分钟化为秒
case 0x01:min=300;
case 0x02:min=600;
case 0x03:min=900;
case 0x04:min=1200;
case 0x05:min=1500;
case 0x06:min=1800;
case 0x07:min=2100;
case 0x08:min=2400;
case 0x09:min=2700;
case 0x0a:min=3000;
case 0x0b:min=3300;
}
m=hour+min; //一共要掩饰的时间秒为单位
delay1s(m);//延时设定的时间
led1=1;
led2=1;
led3=1;
led4=1;
}
void delay1s(unsigned long MS)//延时1s的程序 {UINT8 h,i,j,k;
while(MS--)
{for(h=5;h>0;h--)
for(i=4;i>0;i--)
for(j=116;j>0;j--)
for(k=214;k>0;k--); } }
#include"reg52.h"
#include <intrins.h>
#define DataPort P0,P1
sbit led1=P1^4;
sbit led2=P1^5;
sbit led3=P1^6;
sbit led4=P1^7;
sbit A1 = P1^0; /*步进机A */
sbit B1 = P1^1; /* 步进机A */
sbit C1 = P1^2; /* 步进机A */
sbit D1 = P1^3;
/**************************** 步进机各向分别通电 正转*******************************/
#define Coil_D1_CW() { A1 = 0; B1 = 0; C1 = 0; D1 = 1;}
#define Coil_C1_CW() { A1 = 0; B1 = 0; C1 = 1; D1 = 0;}
#define Coil_B1_CW() { A1 = 0; B1 = 1; C1 = 0; D1 = 0;}
#define Coil_A1_CW() { A1 = 1; B1 = 0; C1 = 0; D1 = 0;}
/**************************** 步进机反转*******************************/
#define Coil_A1_CCW() { A1 = 1; B1 = 0; C1 = 0; D1 = 0;}
#define Coil_B1_CCW() { A1 = 0; B1 = 1; C1 = 0; D1 = 0;}
#define Coil_C1_CCW() { A1 = 0; B1 = 0; C1 = 1; D1 = 0;}
#define Coil_D1_CCW() { A1 = 0; B1 = 0; C1 = 0; D1 = 1;}
#define Coil_Off() { A1 = 0; B1 = 0; C1 = 0; D1 = 0;} /*各向均断电停止转动 */
typedef unsigned char UINT8;
typedef unsigned int UINT16;
void delay1s(unsigned long MS);
void DelayMs( UINT16 Ms )
{
UINT8 i;
while (Ms--)
{
for ( i = 0; i < 114; i++ );
}
}
void main(void)
{ UINT8 M,N,P;
UINT16 hour,min,l;
unsigned long m;
M=P0;
N=M&0xf0;
P=M&0x0f;
switch(N)
{ case 0x00:hour=0;
case 0x10:hour=3600;
case 0x20:hour=7200;
case 0x30:hour=10800;
case 0x40:hour=14400;
case 0x50:hour=18000;
case 0x60:hour=21600;
case 0x70:hour=25200;
}
switch(P)
{ case 0x00:min=0;
case 0x01:min=300;
case 0x02:min=600;
case 0x03:min=900;
case 0x04:min=1200;
case 0x05:min=1500;
case 0x06:min=1800;
case 0x07:min=2100;
case 0x08:min=2400;
case 0x09:min=2700;
case 0x0a:min=3000;
case 0x0b:min=3300;
}
m=hour+min;
led1=0;
led2=0;
led3=0;
led4=0;
delay1s(m);//按输入时间延迟
led1=1;
led2=1;
led3=1;
led4=1;
for( l = 0 ; l< 512; l++ )
{
Coil_D1_CW()
DelayMs(100);
Coil_C1_CW()
DelayMs(100);
Coil_B1_CW()
DelayMs(100);
Coil_A1_CW()
DelayMs(100);
}
Coil_Off();
while(1);
}
void delay1s(unsigned long MS)//1S钟延迟
{UINT8 h,i,j,k;
while(MS--)
{for(h=5;h>0;h--)
for(i=4;i>0;i--)
for(j=116;j>0;j--)
for(k=214;k>0;k--);
}
}
一周热门 更多>