반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107 |
/*
* avr_motor_0910.cpp
*
* Created: 2018-09-10 오후 2:07:45
* Author : USER
*/
#include <avr/io.h>
#define F_CPU 16000000
#include <util/delay.h>
#include "lcd.h"
#include <stdio.h>
void DC_Motor30(){
PORTB = 0x40;
_delay_us(30);
PORTB = 0x20;
_delay_us(70);
}
void DC_Motor50(){
PORTB = 0x40;
_delay_us(50);
PORTB = 0x20;
_delay_us(50);
}
void DC_Motor70(){
PORTB = 0x40;
_delay_us(70);
PORTB = 0x20;
_delay_us(30);
}
void Step_MotorFW(){
PORTB = 0x01;
_delay_ms(20);
PORTB = 0x02;
_delay_ms(20);
PORTB = 0x04;
_delay_ms(20);
PORTB = 0x08;
_delay_ms(20);
}
void Step_MotorBW(){
PORTB = 0x08;
_delay_ms(20);
PORTB = 0x04;
_delay_ms(20);
PORTB = 0x02;
_delay_ms(20);
PORTB = 0x01;
_delay_ms(20);
}
void io_init(void)
{
MCUCR|=(1<<SRE) | (1<<SRW10);
XMCRA=(1<<SRL2) | (0<<SRL1) | (0<<SRL0) | (1<<SRW01) | (1<<SRW00) | (1<<SRW11);
XMCRB |= 0x00;
}
int main(void)
{
/* Replace with your application code */
DDRB = 0x0f;
DDRE = 0x00; // 스위치
PORTE = 0x70;
PORTB = 0x01;
io_init();
lcd_init();
char temp[16];
int degree =0,flag =0, current_degree=0;;
while (1)
{
if (((PINE & 0x10)==0x00)&& flag ==0){
if(degree >=15) degree = degree-15;
flag =1;
}
else if (((PINE & 0x20)==0x00)&& flag ==0){
if(degree <360) degree = degree+15;
flag=1;
}
else if(PINE == 0xff){
flag=0;
}
// 스텝모터 동작(미완성)
if (((PINE & 0x40) == 0x00)&& flag ==0){
if (current_degree < degree)
PORTB= PORTB <<1;
_delay_ms(20);
if (PORTB ==0x10){ PORTB = 0x01;
_delay_ms(20);
}
}
flag=1;
}
else if (PINE ==0xff) flag =0;
sprintf(temp,"> %d ", degree);
lcd_putsf(0,0," Input Degree ");
lcd_putsf(0,1,temp);
}
}
|
cs |
// 여러 방법이 있지만, 여기서는
void Step_MotorFW(){
PORTB = 0x01;
_delay_ms(20);
PORTB = 0x02;
_delay_ms(20);
PORTB = 0x04;
_delay_ms(20);
PORTB = 0x08;
_delay_ms(20);
}
// 4차례를 순서대로 이동시켜야 스텝모터가 동작 한다.
반응형
'Study > 8-bit MCUs' 카테고리의 다른 글
[NPAVR Board] AVR - Atmega128(과제 프로젝트) (0) | 2018.09.10 |
---|---|
[NPAVR Board] AVR - Atmega128(서보 모터) (0) | 2018.09.10 |
[NPAVR Board] AVR - Atmega128 (시계만들기) (0) | 2018.09.10 |
[NPAVR Board] AVR - Atemga128(Timer/Counter0)- OVF (0) | 2018.09.10 |
[NPAVR Board] AVR - Atmega128 (Dot Matrix) (0) | 2018.09.04 |