반응형

 

// 서보모터 구현하기

Timer/Counter와 Delay를 이용하여 구현

 

1. Timer/Counter를 이용하여 구현

 

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
 
/*
 * 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>
#include <avr/interrupt.h>
 
 
 
void io_init(void)
{
    MCUCR|=(1<<SRE) | (1<<SRW10);
    XMCRA=(1<<SRL2) | (0<<SRL1) | (0<<SRL0) | (1<<SRW01) | (1<<SRW00) | (1<<SRW11);
    XMCRB |= 0x00;
}
 
unsigned int tCount =0;
unsigned int servoCount =0;
 
ISR (TIMER0_OVF_vect){ // 100us
    TCNT0 = 256 -50;
    tCount ++;    
    if ( tCount>= 0 && tCount<=7) PORTB = 0xff;
    else if (tCount>7 && tCount <230) PORTB = 0x00;
    else tCount=0;
}
int main(void)
{
    /* Replace with your application code */
    DDRB = 0xff;
    DDRE = 0x00// 스위치
 
    PORTE = 0x70;
    PORTB = 0x01;
    
    io_init();
    lcd_init();
    
    int flag =0;
    TIMSK = 0x01;
    TCCR0 = 0x03;
    TCNT0 = 256 -50;
    sei();
    while (1
    {    
    
    
    }
}
 
 
cs

 

 

2. delay함수를 이용하여 구현

 

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
 
/*
 * 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>
#include <avr/interrupt.h>
 
 
 
void io_init(void)
{
    MCUCR|=(1<<SRE) | (1<<SRW10);
    XMCRA=(1<<SRL2) | (0<<SRL1) | (0<<SRL0) | (1<<SRW01) | (1<<SRW00) | (1<<SRW11);
    XMCRB |= 0x00;
}
 
unsigned int tCount =0;
unsigned int servoCount =0;
 
 
int main(void)
{
    /* Replace with your application code */
    DDRB = 0xff;
    DDRE = 0x00// 스위치
 
    PORTE = 0x70;
    PORTB = 0x01;
    
    io_init();
    lcd_init();
    
    int flag =0;
    while (1
    {    
    
        
        
        if (((PINE & 0x10)== 0x00)&& flag ==0){
            
            for (int i=0; i<100;i++){
                PORTB = 0xff;
                _delay_us(700);
                PORTB = 0x00;
                _delay_us(19300);
            }
            flag =1;
        }
        else if (((PINE & 0x20)==0x00)&& flag==0){
                    for (int i = 0; i<100;i++){
                        PORTB = 0xff;
                        _delay_us(2300);
                        PORTB = 0x00;
                        _delay_us(17700);
                    }
            flag=1;
        }
        else if (((PINE &0x40)== 0x00)&& flag==0){
            for (int i = 0; i<100;i++){
                PORTB = 0xff;
                _delay_us(1500);
                PORTB = 0x00;
                _delay_us(18500);
            }
            flag =1;
        }
        else if (PINE==0xff) flag=0;
    
    }
}
 
 
cs

 

반응형

+ Recent posts