ATmega128A::StepMotor 실습과제
실습과제1
1상여자 신호 발생 실습
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
char out_data[4]={0x01, 0x02, 0x04, 0x08}; //1상여자
//char out_data[4]={0x03, 0x06, 0x0c, 0x09}; //2상여자
unsigned int i=0,j=0;
DDRA |=0x0F;
while(1)
{
j = (i++)%4;
PORTA = out_data[j];
_delay_ms(2);
}
return 0;
}
실습과제2
2상여자 신호 발생 실습
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
int main()
{
char out_data[4]={0x03, 0x06, 0x0c, 0x09}; //2상여자
//char out_data[4]={0x09,0x0c,0x06,0x03};
int i=0,j=0;
DDRA |=0x0f;
while(1)
{
j=(i--)%4;
PORTA=out_data[j];
_delay_ms(2);
}
return 0;
}
실습과제3
1-2상여자 신호 발생 실습
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
int main()
{
char out_data[8]={0x01, 0x03, 0x02, 0x06, 0x04, 0x0C, 0x08, 0x09}; //1-2상여자
unsigned int i=0,j=0;
DDRA |=0x0F;
while(1){
j = i++%8;
PORTA = out_data[j];
_delay_ms(2);
}
return 0;
}
실습과제4
각도제어 실습
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
//#include "usart.h"
int main()
{
char out_data[4]={0x01,0x02,0x04,0x08};
int i=0,j=0;
DDRA|=0x0f;
while(i<100)
{
j=i++%4;
PORTA=out_data[j];
_delay_ms(2);
}
return 0;
}
실습과제5
하이퍼터미널에서 회전각도, 방향을 입력 받아 스텝모터를 구동시키는 프로그램을 작성하시오.
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include "usart.h"
int gets_num(char *pnum);
int main()
{
char str[20]={0,};
int data=0;
char cw[4]={0x01,0x02,0x04,0x08};
char ccw[4]={0x08,0x04,0x02,0x01};
int i=0,j=0;
unsigned int turn=1;
DDRA|=0x0f;
usart0_init();
usart0_printf("\033[H\033[J");
while(1)
{
usart0_printf("input data :");
usart0_gets(str);
turn = data= gets_num(str);
usart0_printf("%s\n",str);
usart0_printf("%d\n",data);
if(data<0) turn*=-1;
turn = turn/1.8;
while(i<turn)
{
j=i++%4;
if(data>=0) PORTA=cw[j];
else PORTA=ccw[j];
_delay_ms(2);
}
turn=data=0;
i=j=0;
usart0_init();
}
return 0;
}
int gets_num(char *pnum)
{
int i = 0, cnt=0, res = 0;
int dir=1;
if(pnum[i]=='-')
{
dir=-1;
cnt=1;
}
while (pnum[cnt] != '\0') //'\r' xx-->232
{
res = res*10 + (pnum[cnt]-'0');
cnt++;
}
res*=dir;
return res;
}
실습과제6
타이머 인터럽트를 이용한 구동
[main.c]
#include "motor.h"
#include "usart.h"
int main()
{
int pps=0, step=0;
usart0_init();
mtr0_init();
usart0_printf("\033[H\033[J");
while(1)
{
usart0_printf("input pps :");
pps = usart0_getintnum();
usart0_printf("input step :");
step = usart0_getintnum();
//if(step>=0) PORTA=cw[j];
//else PORTA=ccw[j];
mtr0_tc0(pps, step);
usart0_printf("pps:%d, ocr0:%d\n ",pps, OCR0);
}
return 0;
}
[motor.h]
#ifndef _MOTOR_H_
#define _MOTOR_H_
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include "usart.h"
void mtr0_init();
void mtr0_tc0(int pps, int step);
int gets_num(char *pnum);
[motor.c]
#include "motor.h"
static char cw[4]={0x01,0x02,0x04,0x08};
static char ccw[4]={0x08,0x04,0x02,0x01};
int angle=0;
static int i=0;
int cw0=1;
int ocr0=0;
void mtr0_init()
{
DDRA |= 0x0F; //OC0,PB0출력설정
ASSR =0x00;
TCCR0 = 0x1F; //0001_1111 CTC, OC0토글출력,1024분주
OCR0 = 0x07; //
TCNT0 = 0x00;
TIFR &= 0xFD;
}
ISR(TIMER0_COMP_vect)
{
int j=0;
j = (i++)%4;
if(cw0) PORTA=cw[j];
else PORTA=ccw[j];
if(i>=angle)
{
TIMSK &= 0xFD;
i=0;
}
}
void mtr0_tc0(int pps, int step)
{
cli();
angle = step/1.8;
if(!step || !pps){ TIMSK &= 0xFD; } //비활성화
TIMSK |= 0x02;
if(step<0){cw0=0; step=-step;}
else cw0=1;
ocr0 = (7.18125/((double)pps/1000)-1);
if(ocr0<0 || ocr0>256){ TIMSK &= 0xFD; }
OCR0= ocr0;
sei();
}
실습과제7
모터 2대 구동(위치제어)
[main.c]
#include "motor.h"
int main()
{
int step1=0, step2=0;
usart0_init();
mtr0_init();
mtr2_init();
usart0_printf("\033[H\033[J");
while(1)
{
usart0_printf("input step1:");
step1=usart0_getintnum();
usart0_printf("input step2:");
step2=usart0_getintnum();
mtr0_tc0(step1);
mtr2_tc0(step2);
}
return 0;
}
[motor.h]
#ifndef _MOTOR_H_
#define _MOTOR_H_
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include "usart.h"
//실습과제7
void mtr0_init();
void mtr2_init();
void mtr0_tc0(int step);
void mtr2_tc0(int step);
#endif
[motor.c]
#include "motor.h"
int cw0=1;
int cw2=1;
static char cw[4]={0x01, 0x02, 0x04, 0x08};
static char ccw[4]={0x08, 0x04, 0x02, 0x01};
static int i0=0;
static int i2=0;
int angle1, angle2;
void mtr0_init()
{
DDRA |= 0x0F;
ASSR =0x00;
TCCR0 = 0x1F;
OCR0 = 0x22;
TCNT0 = 0x00;
TIFR &= 0xFD;
}
void mtr2_init()
{
DDRB |= 0x0F;
ASSR =0x00;
TCCR2 = 0x1D;
OCR2 = 0x22;
TCNT2 = 0x00;
TIFR &= 0x7F;
}
ISR(TIMER0_COMP_vect)
{
int j=0;
j = (i0++)%4;
if(cw0){PORTA = cw[j];}
else {PORTA = ccw[j];}
if(i0>=angle1)
{
TIMSK &= 0xFD;
i0=0;
}
}
ISR(TIMER2_COMP_vect)
{
int j=0;
j = (i2++)%4;
if(cw2){PORTB = ccw[j];}
else {PORTB = cw[j];}
if(i2>=angle2)
{
TIMSK &= 0x7F; //disable
i2=0;
}
}
void mtr0_tc0(int step)
{
cli();
TIMSK |= 0x02;
if(step<0){cw0=0; step=-step;}
else{cw0=1;}
angle1 = step/1.8;
sei();
}
void mtr2_tc0(int step)
{
cli();
TIMSK |= 0x80;
if(step<0){cw2=0; step=-step;}
else{cw2=1;}
angle2 = step/1.8;
sei();
}
실습과제8
모터 2대 구동(속도제어)
[main.c]
#include "motor.h"
int main()
{
int pps1=0;
int pps2=0;
usart0_init();
mtr0_init();
mtr2_init();
usart0_printf("\033[H\033[J");
while(1)
{
usart0_printf("input pps1:");
pps1=usart0_getintnum();
usart0_printf("input pps2:");
pps2=usart0_getintnum();
mtr0_tc0(pps1);
mtr1_tc2(pps2);
}
return 0;
}
[motor.h]
#ifndef _MOTOR_H_
#define _MOTOR_H_
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include "usart.h"
void mtr0_init();
void mtr2_init();
void mtr0_tc0(int pps);
void mtr1_tc2(int pps);
#endif
[motor.c]
#include "motor.h"
int cw0=1;
int cw2=1;
static char cw[4]={0x01, 0x02, 0x04, 0x08};
static char ccw[4]={0x08, 0x04, 0x02, 0x01};
static int i0=0;
static int i2=0;
int ocr0, ocr2;
void mtr0_init()
{
DDRA |= 0x0F;
ASSR =0x00;
TCCR0 = 0x1F;
OCR0 = 0x22;
TCNT0 = 0x00;
TIFR &= 0xFD;
}
void mtr2_init()
{
DDRB |= 0x0F;
ASSR =0x00;
TCCR2 = 0x1D;
OCR2 = 0x22;
TCNT2 = 0x00;
TIFR &= 0x7F;
}
ISR(TIMER0_COMP_vect)
{
int j=0;
j = (i0++)%4;
if(cw0){PORTA = cw[j];}
else {PORTA = ccw[j];}
}
ISR(TIMER2_COMP_vect)
{
int j=0;
j = (i2++)%4;
if(cw2){PORTB = cw[j];}
else {PORTB = ccw[j];}
}
void mtr0_tc0(int pps)
{
cli();
TIMSK |= 0x02;
if(pps<0){cw0=1; pps=-pps;}
else if(pps==0){TIMSK &= 0xFD;}
else {cw0=0;}
ocr0 = (7.18125/((double)pps/1000)-1);
if(ocr0<0 || ocr0>256){TIMSK &= 0xFD;}
OCR0 = ocr0;
sei();
}
void mtr1_tc2(int pps)
{
cli();
TIMSK |= 0x80;
if(pps<0){cw2=1; pps=-pps;}
else if(pps==0){TIMSK &= 0x7F;}
else {cw2=0;}
ocr2 = (7.18125/((double)pps/1000)-1);
if(ocr2<0 || ocr2>256){TIMSK &= 0x7F;}
OCR2 = ocr2;
sei();
}