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

📄 to控制电机修改版.txt

📁 PWM控制电机C程序-已通过实验测试 PWM控制精确控制电机的转动.
💻 TXT
字号:
//功能:利用pwm控制led光暗及峰鳴器音量大小
 
//硬件电路图设计:  PB3与蜂鸣器连接
 //             PB3与LED灯相连(低电位灯亮)
//               PD0 -----> K1(按键开关1)
 //              PD1 -----> K2(按键开关2)
 //              PD2 -----> K3(按键开关3)
               //PD  
//ICC程序:
//ICC-AVR application builder : 2005-4-18 12:46:03 
// Target : M16 
// Crystal: 4.0000Mhz 

#include <iom16v.h> 
#include <macros.h> 

#define uchar unsigned char
#define uint unsigned int

void port_init(void);
void timer0_init(void);
void init_devices(void);



uchar key,OCR0_V;
#define motor_en1 PORTB|=0x10
#define motor_uen1 PORTB&=~0x10

#define key1 (PIND&0x04) //前进
#define key2 (PIND&0x08) //后退
#define key3 (PIND&0x10) //加速
#define key4 (PIND&0x20) //减速

#define key5 (PIND&0x40) //全速

#define key6 (PIND&0x80) //停止



void delay_ms(uint k)//k取2000就是2秒
{
uint i,j;
for(i=0;i<k;i++)
  { 
    for(j=0;j<1140;j++)
	;
   }
}
void port_init(void) 
{ 
 PORTA = 0x00; 
 DDRA  = 0x00; 
 PORTB = 0x00; 
 DDRB  = 0x18; 
 PORTC = 0x00; //m103 output only 
 DDRC  = 0x00; 
 PORTD = 0x00; 
 DDRD  = 0x00; 
} 

// WGM: PWM Phase correct
// desired value: 1KHz
// actual value:  0.980KHz (-2.0%)
void timer0_init(void) 
{ 
 TCCR0 = 0x00; //stop 
 TCNT0 = 0x01; //set count 
 OCR0  = 0xff;  //set compare 
 TCCR0 = 0x62; //start timer ; 相位修正, 8分頻
} 

//call this routine to initialize all peripherals 
void init_devices(void) 
{ 
 //stop errant interrupts until set up 
 CLI(); //disable all interrupts 
 port_init(); 
 timer0_init(); 
 MCUCR = 0x00; 
 GICR  = 0x00; 
 TIMSK = 0x00; //timer interrupt sources 
 SEI(); //re-enable interrupts 
 //all peripherals are now initialized 
}


uchar scan_key()               //扫描键
{
  uchar  n;
  n=0;
 while(key1==0)
   {delay_ms(5);
	    while(key1==0)
	    {n=1;
	    while(key1==0);
	    }
	}
  while(key2==0)
   {delay_ms(5);
	    while(key2==0)
	    {n=2;
	    while(key2==0);
	    }
	}
 while(key3==0)
   {delay_ms(5);
	    while(key3==0)
	    {n=3;
	    while(key3==0);
	    }
	}
 while(key4==0)
   {delay_ms(5);
	    while(key4==0)
	    {n=4;
	    while(key4==0);
	    }
	}
 while(key5==0)
   {delay_ms(5);
	    while(key5==0)
	    {n=5;
	    while(key5==0);
	    }
	}

return n;	
}


void for_ward()  //以设定速度前进
 {  motor_uen1 ;
   OCR0_V  = 0x50; 
       OCR0 = OCR0_V;  
    }
    
void back_ward()   //---还没做,以设定速度后退
 { motor_en1; 
   OCR0  = 0x80; 
 }
void speed_subtract()  //减速
 { 
	
      OCR0_V -= 10;
      OCR0 = OCR0_V;
 }
void speed_add()  //加速
 {
	  
      OCR0_V += 10;
	  OCR0 = OCR0_V;
	
 }
void motor_stop(void) //停止  
 {   
	 motor_en1;
  	 OCR0_V = 0xFF;
     OCR0 = OCR0_V; 
	
 }
void motor_fast(void) //全速
 {   
	 OCR0_V = 0xFF;
     OCR0 = OCR0_V;  
	
 }
 
// 
void main(void) 
{  
  init_devices(); 

 while(1)
 {
   key = scan_key();
   switch(key)
   {case 1:for_ward(); 
           break;
    case 2:back_ward(); 
	       break; 
    case 3:speed_add(); 
		   break; 
    case 4:speed_subtract();
	       break;                             
    case 5:motor_stop(); 
	       break;
    case 6:motor_fast(); 
	       break;

	}
   
 }; 
} 

    

⌨️ 快捷键说明

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