ULN2003是高耐压、大电流达林顿阵列,由7个硅NPN达林顿管组成。ULN2003灌电流可达500mA,并且能够在关态时承受50v的电压,输出还可以在高负载电流并行运行。本例使用ULN2003驱动步进电机,在运行过程中,按下K1将使步进电机正转3圈,按下K2时反转3圈,在转动过程中按下K3时可使步进电机停止转动。
Proteus仿真截图:
Atmel Studio6.2 编译通过截图:
程序清单:
/*
* GccApplication36.c
*
* Created: 2014-12-18 20:42:55
* Author: Administrator
*/
#define F_CPU 4000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <stdint.h>
//正转序列 A->AB->B->BC->C->CD->D->DA
const uint8_t FFW[] = {0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09};
//反转序列 A->AD->D->CD->C->BC->B->AB
const uint8_t REV[] = {0x01,0x09,0x08,0x0c,0x04,0x06,0x02,0x03};
#define K1_DOWN() ((PIND & _BV(PD0))== 0x00)
#define K2_DOWN() ((PIND & _BV(PD1))== 0x00)
#define KX_DOWN() (PIND != 0xFF)
void STEP_MOTOR_RUN(uint8_t Direction,uint8_t n)
{
uint8_t i,j;
for(i=0;i<n;i++)
{
for(j=0;j<8;j++)
{
if(KX_DOWN())return;
if(Direction == 0)
PORTB = FFW[j];
else
PORTB = REV[j];
_delay_ms(200);
}
}
PORTB = 0x01;
}
int main(void)
{
uint8_t r=3;
DDRB = 0xFF;PORTB = FFW[0];
DDRC = 0xFF;PORTC=0xFF;
DDRD = 0x00;PORTD = 0xFF;
while(1)
{
//TODO:: Please write your application code
if(K1_DOWN())
{
while(K1_DOWN());
PORTC = 0xFE;
STEP_MOTOR_RUN(0,r);
}
if(K2_DOWN())
{
while(K2_DOWN());
PORTC = 0xFD;
STEP_MOTOR_RUN(1,r);
}
PORTC = 0xFB;
}
}
|