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

📄 scon2._c

📁 M8制作的舵机控制器
💻 _C
字号:
#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 Forward0 PB|=0x01; PB&=~0x02;
#define Backward0 PB|=0x02; PB&=~0x01;
#define Stop0 PB|=0x03;

#define Forward1 PC|=0x10; PC&=~0x20;
#define Backward1 PC|=0x20; PC&=~0x10;
#define Stop1 PC|=0x30;


#define IN0 PIND&0x04
#define IN1 PIND&0x08
#define OF2 TIFR&0x40
#define COF2 TIFR|=0x40

//原理;外部上升中断-定时器开-0。4毫秒后比较中断-清零定时器-外部下降中断-读出计数器-PWM输出

Uint time1=127,time2=127;//定时器数据



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



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




//TIMER1 initialize - prescale:8
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 1Hz
// actual value: Out of range
void timer1_init(void)
{
 TCCR1B = 0x00; //stop
 TCNT1 = 0x00 /*INVALID SETTING*/; //setup
 OCR1A = 100;
 OCR1B = 0x00 /*INVALID SETTING*/;
 ICR1  = 0x00 /*INVALID SETTING*/;
 TCCR1A = 0x00;
// TCCR1B = 0x03; //start Timer
}



//TIMER2 initialize - prescale:8
// WGM: Normal
// desired value: 1Hz
// actual value: Out of range
void timer2_init(void)
{
 TCCR2 = 0x00; //stop
 ASSR  = 0x00; //set async mode
 TCNT2 = 0x00 /*INVALID SETTING*/; //setup
 OCR2  = 100;
 //TCCR2 = 0x03; //start
}



//ADC initialize
// Conversion time: 104uS
void adc_init(void)
{
 ADCSR = 0x00; //disable adc
 ADMUX = 0x00; //select adc input 0
 ACSR  = 0x80;
 ADCSR = 0x86;
}

#pragma interrupt_handler int0_isr:2
void int0_isr(void)
{
 //external interupt on INT0
 switch(IN0)
 {
 case 0://下降触发
 TCCR1B = 0x00; //stop
 time1=TCNT1&0x01ff;
 time1-=40;
 if(time1>250)time1=250;
 TCNT1=0;
 break;
 case 0x04://上升触发
 TCCR1B = 0x03; //start Timer
 }
}

#pragma interrupt_handler int1_isr:3
void int1_isr(void)
{
 //external interupt on INT1
  switch(IN1)
 {
 case 0://下降触发
 TCCR2 = 0x00; //stop
 if(OF2){time2=TCNT2+0xff;COF2;}//定时器2扩展使用
 else
 time2=TCNT2;
 time2-=120;
 if(time2>250)time2=250;
 TCNT2=0;
 
 break;
 case 0x08://上升触发
 TCCR2 = 0x03; //start
 }
 
}

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

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





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()
{
Uchar dst0,dst1;//距离定点的数值
Uint stopt0,stopt1;//刹车时间
Uint adc0,adc1;//舵机电位
Uint pos0,pos1;//舵机电位
Uint temp0,temp1;//舵机电位
Uchar barke0,barke1;//强力刹车,由全速运行转换到爬行需要强力刹车即长时间反向制动
Uchar action0,action1;//电机动作

init_devices();


while(1)
{
adc0=ReadADC (0);
adc1=ReadADC (1);
if((adc0>temp0+1)||(adc0<temp0-1))temp0=adc0;
if((adc1>temp1+1)||(adc1<temp1-1))temp1=adc1;

pos0=temp0>>2;
pos1=temp1>>2;

switch(action0)
{
case 1:Forward0 break;
case 2:Backward0 break;
case 3:Stop0 break;
}

action0=3;
if(pos0>(time1+2))
{action0=1 ;dst0=pos0-time1;
if(dst0<15) //爬行状态,运转-反向制动-普通制动
 {
 stopt0=dst0<<3;
 if(!barke0)Delay (200+stopt0);//制动时正向马上关闭
 Backward0 
  Delay (125-stopt0);//正常爬行的制动时间
 Stop0
if(barke0)barke0--;

 }
 else barke0=100;
}

if(pos0<(time1-2))
{action0=2 ;dst0=time1-pos0;
if(dst0<15)  //爬行状态,运转-反向制动-普通制动
 {
 stopt0=dst0<<3;
 if(!barke0)Delay (200+stopt0);
 Forward0  
  Delay (125-stopt0);//正常爬行的制动时间        
 Stop0
 if(barke0)barke0--;
 }
 else barke0=100;
}

///////////////////////////////////////////////////////////////////
switch(action1)
{
case 1:Forward1 break;
case 2:Backward1 break;
case 3:Stop1 break;
}

action1=3;
if(pos1>(time2+2))
{action1=1 ;dst1=pos1-time2;
if(dst1<15) //爬行状态,运转-反向制动-普通制动
 {
 stopt1=dst1<<3;
 if(!barke1)Delay (200+stopt1);//制动时正向马上关闭
 Backward1 
  Delay (125-stopt1);//正常爬行的制动时间
 Stop1
if(barke1)barke1--;

 }
 else barke1=100;
}

if(pos1<(time2-2))
{action1=2 ;dst1=time2-pos1;
if(dst1<15)  //爬行状态,运转-反向制动-普通制动
 {
 stopt1=dst1<<3;
 if(!barke1)Delay (200+stopt1);
 Forward1  
  Delay (125-stopt1);//正常爬行的制动时间        
 Stop1
 if(barke1)barke1--;
 }
 else barke1=100;
}

}

}

⌨️ 快捷键说明

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