AVR驱动软驱电机程序
时代变了,软驱没有用了,但是里面的电机是比较好玩的,拿出来研究研究 。【AVR驱动软驱电机程序】
CODE:
/*用mega32三个口线驱动软驱电机.
PA2接LB1833 ENA12 脚;
PA1接LB1833 IN1 脚;
PA0接LB1833 IN2 脚.
*************************************************** /
//ICC-AVRapplication builder : 2005-5-20 10:40:30
// Target : M32
// Crystal: 3.6864Mhz
#include
#include
unsigned char np;
//步进电机运行数据表
const unsigned char motortb[]={0x05,0x07,0x06,0x04,0x05,0x07,0x06,0x04};
void delay ( unsigned char t);// 每步延时的子程序
void a_step( unsigned char d, unsigned char t );//步进电机走一步d=0 正转d=1 反转
void port_init( void )
{
PORTA = 0x00;
DDRA = 0xFF;
PORTB = 0xFF;
DDRB = 0x00;
PORTC = 0xFF;
DDRC = 0x00;
PORTD = 0xFF;
DDRD = 0x00;
}
//call this routine to initialise all peripherals
void init_devices( void )
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialised
}
void delay ( unsigned char t )// 每步延时的子程序
{
unsigned char i;
unsigned int j;
for (i = 0 ; i < t ; i++ )
{
for ( j = 0 ;j < 800 ; j++ )
;
}
}
void a_step ( unsigned char d, unsigned char t) //步进电机走一步d=0 正转d=1 反转 t // 越大走得越慢
{
if ( d & 0x01 )
{
if ( np == 0 )
{
np = 7;
}
else
{
np--;
}
}
else
{
if ( np == 7 )
{
np = 0;
}
else
{
np++;
}
}
PORTA = motortb[np];
delay(t);
}
void a_turn (unsigned char d, unsigned char t)// 步进电机走一圈
{
unsigned char i;
for ( i = 0 ; i < 96 ; i++ )
{
a_step ( d, t );
}
}
void main ( void )
{
np = 4;
while (1)
{
a_turn ( 1, 1 );
}
}
推荐阅读
- 2018年台IC设计产值创新高,今年聚焦面板驱动IC、AI、5G
- 你知道LED显示屏行驱动设计将要面对的挑战吗?
- 关于LED驱动器可靠性,你知道它为什么低吗?
- 如何运用电容降压原理来完成LED电源驱动电路设计
- 为什么 LED 灯具需要用恒流电源来驱动?
- LED驱动电源参数你真的了解吗?
- LED 驱动器的应用方法?
- 单片式LED驱动器应该如何应用
- 多路LED驱动电源技术的详细解读
- LED 驱动电源RMS值测试