细分问题难啊,那位能给个AVR用C语言控制步进电机的程序啊,谢谢
细分问题难啊,那位能给个AVR用C语言控制步进电机的程序啊,谢谢 if (i==0){
PORTC=0b00100001;
}
else if (i==1)
{
PORTC=0b00100000;
}
else if (i==2)
{
PORTC=0b00100010;
}
else if (i==3)
{
PORTC=0b00000010;
}
else if (i==4)
{
PORTC=0b00010010;
}
else if (i==5)
{
PORTC=0b00010000;
}
else if (i==6)
{
PORTC=0b00010001;
}
else if (i==7)
{
PORTC=0b00000001;
}
四线两项 电机. PORTC的 0,1,4,5 控制 能细说吗,最好给个例子,为什么不用switch语句啊, 显然不能细分。 /*****************************************************
Chip type : ATmega16
Program type : Application
Clock frequency : 16.000000 MHz
Memory model : Small
External SRAM size: 0
Data Stack size : 256
*****************************************************/
#include <mega16.h>
const unsigned char SIN={
0 ,13 ,25 ,37 ,50 ,62 ,74 ,86 ,98 ,109 ,120 ,131 ,142 ,152
,162 ,171 ,180 ,189 ,197 ,205 ,212 ,219 ,225
,231 ,236 ,240 ,244 ,247 ,250 ,252 ,254 ,255
,255 ,255 ,254 ,252 ,250 ,247 ,244 ,240 ,236
,231 ,225 ,219 ,212 ,205 ,197 ,189 ,180 ,171
,162 ,152 ,142 ,131 ,120 ,109 ,98 ,86 ,74 ,62 ,50 ,37 ,25
,13 ,0
};
unsigned char DEG=0;
unsigned char DEGSTEP=0;
unsigned char ENABLE=0;// External Interrupt 0 service routine //没有编程
interrupt void ext_int0_isr(void) //收到每步中断执行
{
unsigned i=0,j=0;
i=DEG;
if (i<65)
{
PORTD.0=1; // A方向 置1
}else
{
i=i-64;
PORTD.0=0; // A方向 置 0
};
j=DEG+32;
if (j>127) j=j-128;
if (j<65)
{
PORTD.1=1; // B方向 置 1
}else
{
j=j-64;
PORTD.1=0; // B方向 置 0
};
PORTA=SIN;
PORTB=SIN ;
if (PINC.0==1) {
DEG=DEG+DEGSTEP; }
else {
if (DEG<DEGSTEP)
DEG=128-DEG;
DEG=DEG-DEGSTEP;}
if (DEG>127) DEG=DEG-128 ;
}
void main(void)
{
// Port A initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTA=0x00;
DDRA=0xFF;
// Port B initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTB=0x00;
DDRB=0xFF;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=P
PORTC=0x01;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=Out Func0=Out
// State7=P State6=P State5=P State4=T State3=T State2=T State1=0 State0=0
PORTD=0xE0;
DDRD=0x03;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Rising Edge
// INT1: Off
// INT2: Off
GICR|=0x40;
MCUCR=0x03;
MCUCSR=0x00;
GIFR=0x40;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
DEGSTEP=PIND&0B11100000;
DEGSTEP=DEGSTEP>>5;
m=0x00;
if (DEGSTEP==0b00000010) m=16; //2细分
if (DEGSTEP==0b00000100) m=8; //4细分
if (DEGSTEP==0b00000101) m=4; //8细分
if (DEGSTEP==0b00000110) m=2; //16细分
if (DEGSTEP==0b00000111) m=1;//32细分
if (m==0) m=1;
DEGSTEP=m;
#asm("sei")
// Global enable interrupts
while (1)
{
};
} 楼主外围用的什么芯片? 外部使用 8位的da,再去控制斩波器+ 功率驱动
页:
[1]