|
/********************************************************************
* 文件名 : 步進(jìn)電機(jī).c
* 描述 : 用單片機(jī)驅(qū)動(dòng)ULN2003去控制步進(jìn)電機(jī)。
數(shù)碼管旋轉(zhuǎn)的角度會(huì)在數(shù)碼管上進(jìn)行顯示。
* 創(chuàng)建人 : 東流,2012年2月8日
* 版本號 : 1.0
* 杜邦線接法:
P1.3用杜邦線連接到J17的D端。
P1.4用杜邦線連接到J17的C端。
P1.5用杜邦線連接到J17的B端。
P1.6用杜邦線連接到J17的A端。
步進(jìn)電機(jī)接到J18的五個(gè)端口,其中,步進(jìn)電機(jī)的紅線接J18的VCC端。
***********************************************************************/
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar Step = 0;
bit FB_flag = 0;
unsigned char code F_Rotation[8]={0x08,0x18,0x10,0x30,0x20,0x60,0x40,0x48}; //順時(shí)針轉(zhuǎn)表格
unsigned char code B_Rotation[8]={0x48,0x40,0x60,0x20,0x30,0x10,0x18,0x08}; //逆時(shí)針轉(zhuǎn)表格
uchar code table[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar code LED_W[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
/********************************************************************
* 名稱 : Delay_1ms()
* 功能 : 延時(shí)子程序,延時(shí)時(shí)間為 1ms * x
* 輸入 : x (延時(shí)一毫秒的個(gè)數(shù))
* 輸出 : 無
***********************************************************************/
void Delay(uint i)
{
uchar x,j;
for(j=0;j<i;j++)
for(x=0;x<=148;x++);
}
main()
{
uchar i,count=0;
uint j;
uint k = 0;
while(1)
{
for(k=0;k<512;k++)
{
for(i=0;i<8;i++) //因?yàn)橛?路的控制時(shí)序
{
P1 = F_Rotation[ i]; //順時(shí)針轉(zhuǎn)動(dòng)
Delay(2); //改變這個(gè)參數(shù)可以調(diào)整電機(jī)轉(zhuǎn)速
}
j=(uint)((360000/512) * k /1000);
P0 = table[j%10];
P2 = LED_W[0];
Delay(1);
P0 = table[j/10%10];
P2 = LED_W[1];
Delay(1);
P0 = table[j/100%10];
P2 = LED_W[2];
Delay(1);
}
}
} |
|