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

📄 scon2

📁 M8制作的舵机控制器
💻
字号:
#include <iom8v.h>
#include <macros.h>

#define Uchar unsigned char
#define Uint unsigned int


#define nop() asm("nop")
#define _nop_() asm("nop")



#define PB PORTB
#define PC PORTC
#define PD PORTD
 


#define Forward PB|=0x01; PB&=~0x02;
#define Backward PB|=0x02; PB&=~0x01;
#define Stop PB|=0x03;


void Delay (Uint time)
{
while(time)
{
time--;
}
}



void port_init(void)
{
 PORTB = 0x00;
 DDRB  = 0xFF;
 PORTC = 0x00; //m103 output only
 DDRC  = 0x00;
 PORTD = 0x00;
 DDRD  = 0xFF;
}




//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 port_init();
 adc_init();

 MCUCR = 0x00;
 GICR  = 0x00;
 TIMSK = 0x00; //timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}


void adc_init(void)
{
 ADCSR = 0x00; //disable adc
 ADMUX = 0x00; //select adc input 0
 ACSR  = 0x80;
 ADCSR = 0x86;
}

Uint ReadADC (Uchar channel)
{
Uint int1,int2;
ADMUX&=~0x1F;
ADMUX|=channel;
ADCSRA|=0x40;
while(~ADCSRA&0x10){}
ADCSRA|=0x10;
int2=ADCL;
int1=ADCH;
int1<<=8;
return (int1+int2);
}





main()
{
Uint a,b,c,d;
Uint a1,b1,c1,d1;
Uchar barke1,barke2;//强力刹车,由全速运行转换到爬行需要强力刹车即长时间反向制动
init_devices();


while(1)
{
a1=ReadADC (0);
b1=ReadADC (1);
if((a1>a+2)||(a1<a+2))a=a1;
if((b1>b+2)||(b1<b)+2)b=b1;

switch(c)
{
case 1:Forward break;
case 2:Backward break;
case 3:Stop break;
}

c=3;
if(a>(b+2))
{c=1 ;d=a-b;
if(d<60) //爬行状态,运转-反向制动-普通制动
 {
 d1=d<<1;
 if(!barke1)Delay (200+d1);//制动时正向马上关闭
 Backward 
 // if(barke1)Delay (100);//长时间反向制动,消除转子惯性
  Delay (125-d1);//正常爬行的制动时间
 Stop
if(barke1)barke1--;

 }
 else barke1=5;
}

if(a<(b-2))
{c=2 ;d=b-a;
if(d<60)  //爬行状态,运转-反向制动-普通制动
 {
 d1=d<<1;
 if(!barke1)Delay (200+d1);
 Forward  
 // if(barke1)Delay (100);//长时间反向制动
  Delay (125-d1);//正常爬行的制动时间        
 Stop
 if(barke1)barke1--;
 }
 else barke1=5;
}


}

}

⌨️ 快捷键说明

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