반응형

piano.pdf

lcd.h

piano.zip

 

// Piano 만들기 시작

1. Power ON/OFF 기능 구현

2. Key Matrix 입력시 스피커 출력 음계 구현

 

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * piano-0911.cpp
 *
 * Created: 2018-09-11 오전 11:27:56
 * Author : USER
 */ 
 
#include <avr/io.h>
#define F_CPU 16000000
#define __DELAY_BACKWARD_COMPATIBLE__
#include <util/delay.h>
#include <stdio.h>
#include "lcd.h"
#include <stdlib.h>
 
//MODE 전원
#define PLAYMODE 1
#define OFFMODE 0
 
//음계
#define DO 261.63
#define RE 293.66
#define MI 329.63
#define FA 349.23
#define SOL 391.99
#define RA 440.0
#define SI 493.88
 
// Key Matrix 함수
unsigned char getKey(){
    unsigned char key = 0,result =0;
    for (int i=0; i<3;i++){
        if (i==0)PORTD = 0b11111110;
        if (i==1)PORTD = 0b11111101;
        if (i==2)PORTD = 0b11111011;
    
        _delay_ms(5);
    
        key = (~PIND & 0xf0);
        if (key){
            return key|(PORTD & 0x0f);
        }
    }
    return 0;
}
 
// 음계 출력 Buzzer(주파수, 출력횟수)
void Buzzer(double hz,int count){
    for (int i=0;i<count; i++){
        PORTB = 0xff;
        _delay_ms(((double)1000/hz)/2);
        PORTB = 0x00;
        _delay_ms(((double)1000/hz)/2);
    }
}
 
// 음악 저장
int school[25]= {SOL,SOL,RA,RA,SOL,SOL,MI, SOL,SOL,MI,MI,RE,SOL,SOL,RA,RA,SOL,SOL,MI,SOL,MI,RE,MI,DO};
int plane [25]=    {MI,RE,DO,RE,MI,MI,MI,RE,RE,RE,MI,MI,MI,MI,RE,DO,RE,MI,MI,MI,RE,RE,MI,RE,DO};
    
int main(void)
{
    DDRA= 0xff// LED 출력 설정
    DDRB = 0xff// 스피커 출력 설정 
    DDRD = 0x0f// key Matrix 입출력 설정
    
    PORTA = 0xff// LED OFF
    PORTD = 0xff// Key Matrix 초기값
    
    int mode =0;
    
    char str[16];
    lcd_init();
    lcd_clear();
    /* Replace with your application code */
    
    while (1
    {        
        if (mode == PLAYMODE){
            lcd_putsf(0,0,(unsigned char *)"  PIANO START!  ");
            lcd_putsf(0,1,(unsigned char *)"    POWER ON!   "); // 꺼짐 > 켜짐 상태 변경 후 3초 뒤 사라짐.
            
        
            //itoa(getKey(), str, 2); // char 문자열을 2진수로 변환 itoa함수 stdlib.h헤더파일에 위치함.
            //lcd_putsf(0,0,(unsigned char *)str); // LCD 출력
            //lcd_putsf(0,1,(unsigned char *)"  ");
            if (getKey() == 0b00011110){
                Buzzer(DO,50);
            }
            if (getKey()==0b00101110){
                Buzzer(RE,50);
            }
            if (getKey() == 0b1001110){
                Buzzer(MI,50);
            }
            if (getKey() == 0b00011101){
                Buzzer(FA,50);
            }
            if (getKey() == 0b00101101){
                Buzzer(SOL,50);
            }
            if (getKey() == 0b01001101){
                Buzzer(RA,50);
            }
            if(getKey()==0b00011011){
                Buzzer(SI,50);
            }
        
        }
        else if (mode == OFFMODE){
            // 켜짐 > 꺼짐
            lcd_putsf(0,0,(unsigned char *)"  PIANO START!  ");
            lcd_putsf(0,1,(unsigned char *)"   POWER OFF!   ");
            //1초 후
            lcd_putsf(0,0,(unsigned char *)"    GOOD BYE    ");
            // 1초 후 공백
            lcd_putsf(0,0,(unsigned char *)"                ");
            lcd_putsf(0,0,(unsigned char *)"                ");
        }
    }
}
 
 
cs

 

 

//앞 뒤 납땜

 

 

 

 

 

// LCD출력중..

/*

모드별 LCD ON/OFF 문구 출력

키입력시 건반모양 출력 구현 필요

스위치(건반) 입력시 스피커 출력 타이머 카운터로 구현 필요

*/

 

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * piano-0911.cpp
 *
 * Created: 2018-09-11 오전 11:27:56
 * Author : USER
 */ 
 
#include <avr/io.h>
#define F_CPU 16000000
#define __DELAY_BACKWARD_COMPATIBLE__
#include <util/delay.h>
#include <stdio.h>
#include "lcd.h"
#include <stdlib.h>
 
//MODE 전원
#define PLAYMODE 1
#define OFFMODE 0
 
//음계
#define DO 261.63
#define RE 293.66
#define MI 329.63
#define FA 349.23
#define SOL 391.99
#define RA 440.0
#define SI 493.88
 
 
// Key Matrix 함수
unsigned char getKey(){
    unsigned char key = 0,result =0;
    for (int i=0; i<3;i++){
        if (i==0)PORTD = 0b11111110;
        if (i==1)PORTD = 0b11111101;
        if (i==2)PORTD = 0b11111011;
    
        _delay_ms(5);
    
        key = (~PIND & 0xf0);
        if (key){
            return key|(PORTD & 0x0f);
        }
    }
    return 0;
}
 
// 음계 출력 Buzzer(주파수, 출력횟수)
void Buzzer(double hz,int count){
    for (int i=0;i<count; i++){
        PORTB = 0xff;
        _delay_ms(((double)1000/hz)/2);
        PORTB = 0x00;
        _delay_ms(((double)1000/hz)/2);
    }
}
 
// 음악 저장
int school[25]= {SOL,SOL,RA,RA,SOL,SOL,MI, SOL,SOL,MI,MI,RE,SOL,SOL,RA,RA,SOL,SOL,MI,SOL,MI,RE,MI,DO};
int plane [25]=    {MI,RE,DO,RE,MI,MI,MI,RE,RE,RE,MI,MI,MI,MI,RE,DO,RE,MI,MI,MI,RE,RE,MI,RE,DO};
    
int main(void)
{
    DDRA= 0xff// LED 출력 설정
    DDRB = 0xff// 스피커 출력 설정 
    DDRD = 0x0f// key Matrix 입출력 설정
    
    PORTA = 0xff// LED OFF
    PORTD = 0xff// Key Matrix 초기값
 
    int mode =OFFMODE , modeFlag =1, playmodeFlag=0;
    int keyFlag =0;
    
    char str[16];
    lcd_init();
    lcd_clear();
    /* Replace with your application code */
    
    while (1
    {        
        // SW8 00101011
        // SW9 01001011
        if (getKey() == 0b01001011){
            if (mode == PLAYMODE){
                mode =OFFMODE; modeFlag=0;
            }
            else if (mode == OFFMODE){
                mode =PLAYMODE;
                playmodeFlag =0;
            }
            keyFlag=1;
        }
        else if (getKey()== 0x00) keyFlag=0;
        
        if (mode == PLAYMODE){
            if (playmodeFlag ==0){
            lcd_putsf(0,0,(unsigned char *)"  PIANO START!  ");
            lcd_putsf(0,1,(unsigned char *)"    POWER ON!   "); // 꺼짐 > 켜짐 상태 변경 후 3초 뒤 사라짐.
            _delay_ms(3000);
            lcd_putsf(0,1,(unsigned char *)"                ");
            playmodeFlag =1;
            }
            else if(playmodeFlag ==1){
                lcd_putsf(0,0,(unsigned char *)"  PIANO START!  ");
            }
            //itoa(getKey(), str, 2); // char 문자열을 2진수로 변환 itoa함수 stdlib.h헤더파일에 위치함.
            //lcd_putsf(0,0,(unsigned char *)str); // LCD 출력
            //lcd_putsf(0,1,(unsigned char *)"  ");
            if (getKey() == 0b00011110){
                Buzzer(DO,50);
            }
            else if (getKey()==0b00101110){
                Buzzer(RE,50);
            }
            else if (getKey() == 0b1001110){
                Buzzer(MI,50);
            }
            else if (getKey() == 0b00011101){
                Buzzer(FA,50);
            }
            else if (getKey() == 0b00101101){
                Buzzer(SOL,50);
            }
            else if (getKey() == 0b01001101){
                Buzzer(RA,50);
            }
            else if(getKey()==0b00011011){
                Buzzer(SI,50);
            }
        }
        else if (mode == OFFMODE){
            if(modeFlag ==0){
                lcd_putsf(0,0,(unsigned char *)"                ");
                lcd_putsf(0,1,(unsigned char *)"                ");
                lcd_putsf(0,0,(unsigned char *)"  PIANO SYSTEM  ");
                lcd_putsf(0,1,(unsigned char *)"   POWER OFF!   ");
                _delay_ms(1000);
                lcd_putsf(0,0,(unsigned char *)"    GOOD BYE    ");
                lcd_putsf(0,1,(unsigned char *)"   POWER OFF!   ");
                _delay_ms(1000);
                modeFlag=1;
            }
            else if (modeFlag==1){
                lcd_putsf(0,0,(unsigned char *)"                ");
                lcd_putsf(0,1,(unsigned char *)"                ");    
            }
        }
    }
}
 
 
cs

 

 

 

반응형

+ Recent posts