⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stepping_motor.txt

📁 /*用mega32三个口线驱动软驱电机. PA2接LB1833 ENA12 脚 PA1接LB1833 IN1 脚 PA0接LB1833 IN2 脚. *****************
💻 TXT
字号:
/*用mega32三个口线驱动软驱电机. 
PA2接LB1833 ENA1\2 脚; 
PA1接LB1833 IN1 脚; 
PA0接LB1833 IN2 脚. 
*************************************************** /
//ICC-AVR application 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 ); 
  } 

} 
/****************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -