程序如下,因为我接了一个脚踏开关,踩一下计数,并且数码管显示,所以7219各位可以忽略,只关注电机部分就可以了。
#include<reg52.h>
sbit PUL = P1^0;
sbit DIR = P1^1;
sbit Max7219_pinCLK = P1^6;
sbit Max7219_pinCS = P1^5;
sbit Max7219_pinDIN = P1^4;
sbit KEY = P1^3;
unsigned int puls;
void Delay_xms(unsigned int x)
{
unsigned int i,j;
for(i=0;i<x;i++)
for(j=0;j<112;j++);
}
void Write_Max7219_byte(unsigned char DATA)
{
unsigned char i;
Max7219_pinCS=0;
for(i=8;i>=1;i--)
{
Max7219_pinCLK=0;
Max7219_pinDIN=DATA&0x80;
DATA=DATA<<1;
Max7219_pinCLK=1;
}
}
void Write_Max7219(unsigned char address,unsigned char dat)
{
Max7219_pinCS=0;
Write_Max7219_byte(address);
Write_Max7219_byte(dat);
Max7219_pinCS=1;
}
void Init_MAX7219(void)
{
Write_Max7219(0x09, 0xFF);
Write_Max7219(0x0a, 0x03);
Write_Max7219(0x0b, 0x07);
Write_Max7219(0x0c, 0x01);
Write_Max7219(0x0f, 0x00);
}
void StepMotorRun();
void Delay200us();
void DesplayLed();
unsigned char LED[8] = {0,0,0,0,0,0,0,0};
unsigned char code LedChar[] = { //数码管显示字符转换表
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,0x8,0x9
};
unsigned long sec=0;
bit flag = 1;
void main()
{
DIR = 1;
Delay_xms(50);
Init_MAX7219();
Delay_xms(2000);
Write_Max7219(0x0f, 0x00); //显示测试:1;测试结束,正常显示:0
while(1)
{
DesplayLed();
if(KEY != flag)
{
flag = KEY;
Delay_xms(10);
if(KEY == 0)
{
sec++;
StepMotorRun();
}
}
}
}
void StepMotorRun()
{
puls = 1600;
while(puls--)
{
PUL = 0;
Delay200us();
PUL = 1;
Delay200us();
}
}
void Delay200us() //@12.000MHz
{
unsigned char i, j;
i = 3;
j = 82;
do
{
while (--j);
} while (--i);
}
void DesplayLed()
{
unsigned char j;
LED[0] = LedChar[sec%10];
LED[1] = LedChar[sec/10%10];
LED[2] = LedChar[sec/100%10];
LED[3] = LedChar[sec/1000%10];
LED[4] = LedChar[sec/10000%10];
LED[5] = LedChar[sec/100000%10];
LED[6] = LedChar[sec/1000000%10];
LED[7] = LedChar[sec/10000000%10];
for(j=7;j>=1;j--)
{
if(LED[j] == 0)
LED[j] = 0xF;
else
break;
}
Write_Max7219(1,LED[0]);
Write_Max7219(2,LED[1]);
Write_Max7219(3,LED[2]);
Write_Max7219(4,LED[3]);
Write_Max7219(5,LED[4]);
Write_Max7219(6,LED[5]);
Write_Max7219(7,LED[6]);
Write_Max7219(8,LED[7]);
}
|