반응형

// Arm Trainning kit

// Timer 이용

// Stop Watch 만들기



// 회로도


0.1sec_Stop_Watch.c

Down_Stop_Watch.c

Stop_Watch.c

Stop_Watch2.c

Timer Set 방법.c


반응형

'Study > 32-bit MCUs' 카테고리의 다른 글

[Elevator(ARM)] DoorLock  (0) 2018.10.25
[Elevator(ARM)] Calculator  (0) 2018.10.25
[Elevator(ARM)] Coretex-M4 Elevator  (0) 2018.10.18
[MTX32F407-M4] BMI Calculator  (0) 2018.10.18
[MTX32F407-M4] Cortex-M4 LED제어, SW제어, FND 제어  (0) 2018.10.15
반응형

// Cortex-M4를 이용한 LED, Switch, Fnd 제어

// SW PD0를 누를경우 Num 증가 

// SW PD1을 누를경우 Num 감소








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
152
153
154
155
156
157
158
159
/**
  ******************************************************************************
  * @file    main.c
  * @author  Ac6
  * @version V1.0
  * @date    01-December-2013
  * @brief   Default main function.
  ******************************************************************************
*/
 
 
#include "stm32f4xx.h"
            
void delay_time(int time){
    while(time-->=0);
}
void delay_us(int time){
    while(time-->=0)
        delay_time(1);
}
void delay_ms(int time){
    while(time-->0)
        delay_us(1000);
}
 
unsigned int fnd[10=
    {0b0011111100000000,0b0000011000000000,0b0101101100000000,0b0100111100000000,
     0b0110011000000000,0b0110110100000000,0b0111110100000000,0b0010011100000000,
     0b0111111100000000,0b0110011100000000};
 
unsigned int fnd_sel[4]=
    {0b1000000000000000,0b0100000000000000,0b0010000000000000,0b0001000000000000};
 
void display_fnd(int num){
 
    int num1=0,num2=0,num3=0,num4=0;
 
    num1 = num%10;
    num2 = (num/10)%10;
    num3 = (num/100)%10;
    num4 = (num/1000)%10;
 
    GPIO_ResetBits(GPIOB,fnd_sel[3]);
    GPIO_SetBits(GPIOD, fnd[num1]);
    GPIO_SetBits(GPIOB,fnd_sel[3]);
    GPIO_ResetBits(GPIOD,fnd[num1]);
 
    GPIO_ResetBits(GPIOB,fnd_sel[2]);
    GPIO_SetBits(GPIOD, fnd[num2]);
    GPIO_SetBits(GPIOB,fnd_sel[2]);
    GPIO_ResetBits(GPIOD,fnd[num2]);
 
    GPIO_ResetBits(GPIOB,fnd_sel[1]);
    GPIO_SetBits(GPIOD, fnd[num3]);
    GPIO_SetBits(GPIOB,fnd_sel[1]);
    GPIO_ResetBits(GPIOD,fnd[num3]);
 
    GPIO_ResetBits(GPIOB,fnd_sel[0]);
    GPIO_SetBits(GPIOD, fnd[num4]);
    GPIO_SetBits(GPIOB,fnd_sel[0]);
    GPIO_ResetBits(GPIOD,fnd[num4]);
}
 
int main(void)
{
 
    GPIO_InitTypeDef GPIO_InitStructure;
 
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOD,ENABLE); // GPIO 클럭 인가
 
    // GPIOB set
    GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|
            GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // push pull
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
 
    GPIO_Init(GPIOB, &GPIO_InitStructure);
 
    GPIO_InitStructure.GPIO_Pin = 0b1111111100000000;
    GPIO_Init(GPIOD, &GPIO_InitStructure);
 
    //GPIOD set
    GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3);
    // equal to 0b00001111
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // push pull
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
 
    GPIO_Init(GPIOD, &GPIO_InitStructure);
 
    //GPIO_ResetBits(GPIOB, 0b1111000000000000);
    //GPIO_SetBits(GPIOD, 0b1111111100000000);
    delay_ms(500);
 
    GPIO_SetBits(GPIOB, GPIO_Pin_5);
    delay_ms(500);
    GPIO_ResetBits(GPIOB, GPIO_Pin_5);
    delay_ms(500);
 
    GPIO_SetBits(GPIOB, GPIO_Pin_6);
    GPIO_ResetBits(GPIOB, GPIO_Pin_6);
 
 
    GPIO_SetBits(GPIOB, GPIO_Pin_7 | GPIO_Pin_8); // 특정비트 1
    //GPIO_ResetBits(GPIOB, GPIO_Pin_7 | GPIO_Pin_8); // 틀정비트 0
    // setBit : 특정 (1로 표시된) 비트를 1로 바꿈
    // ResetBit : 1로 표시된 비트를 0으로 바꿈
    // WriteBit
    int num =1234;
    int flag = 0, flag2=0, flag3=0;
 
 
    for(;;)
    {
        /*
        // FND display 1234 출력
        for (int i =0;i<4; i++){
            GPIO_ResetBits(GPIOB,fnd_sel[i]);
            GPIO_SetBits(GPIOD, fnd[i]);
            GPIO_SetBits(GPIOB,fnd_sel[i]);
            GPIO_ResetBits(GPIOD,fnd[i]);
        }*/
 
        display_fnd(num); //FND display(num)
 
        // GPIOD 입력시 GPIOB bit set, num++;
        if((GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_0)==0&& flag ==0){
            GPIO_SetBits(GPIOB, GPIO_Pin_5);
            num++;
            flag=1;
        }
        else if((GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_0)==1&& flag==1){
            GPIO_ResetBits(GPIOB, GPIO_Pin_5);
            flag=0;
        }
 
        // GPIOD 이력시 GPIOB bit set, num--;
        if((GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_1)==0&& flag2 ==0){
            GPIO_SetBits(GPIOB, GPIO_Pin_6);
            num--;
            flag2=1;
        }
        else if ((GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_1)==1)&& flag2 ==1){
            GPIO_ResetBits(GPIOB, GPIO_Pin_6);
            flag2=0;
        }
        if ((GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_2)==0)&& flag3 ==0){
 
            flag3=1;
        }
        else if((GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_2)==1)&&flag ==1){
            flag3=0;
        }
    }
}
 
cs


반응형

'Study > 32-bit MCUs' 카테고리의 다른 글

[Elevator(ARM)] DoorLock  (0) 2018.10.25
[Elevator(ARM)] Calculator  (0) 2018.10.25
[Elevator(ARM)] Coretex-M4 Elevator  (0) 2018.10.18
[MTX32F407-M4] BMI Calculator  (0) 2018.10.18
[MTX32F407-M4] Coretex-M4 Stop_Watch  (0) 2018.10.18
반응형
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
 
<html>
 
<body>
    <canvas id="myCanvas" width="300" height="300" style="border:1px solid black">
    </canvas>
    <button onclick="addbody();"></button>
    <img src="heart.png" id="heart">
    <div style="border:1px solid black " id="block1"> 현재 점수 : 0점 입니다</div>
    <div style="border:1px solid black " id="block2"> 기록 <br></div>
    <script>
        var x = 0, y = 0;
        var vx = 1, vy = 0;
        var SnakeX = new Array(100);
        var SnakeY = new Array(100);
        var tall, n;
        var isLive = false;
        var appleCount =0;
        var appleX = 5, appleY = 10;
        var boomX =10, boomY = 20;
        var keyFlag = false;
        var point = new Array(100);
        var gameCount =0;
 
        function draw() {
            var canvas = document.getElementById("myCanvas");
            var ctx = canvas.getContext("2d");
 
            var img = document.getElementById("heart");
            ctx.beginPath();
            ctx.clearRect(00, canvas.width, canvas.height);
 
 
            ctx.fillStyle = "red";
            for (var i=0; i<n; i++)
            {
                ctx.fillRect(SnakeX[i] * 10, SnakeY[i] * 101010);
            }
            ctx.fillStyle = "blue";
            ctx.fillRect(appleX * 10, appleY * 101010);
            ctx.drawImage(img, appleX * 10, appleY * 101010);
            ctx.fillStyle = "black";
            ctx.fillRect(boomX * 10, boomY * 101010);
        }
 
        function collision(x, y)
        {
            for(var i=0; i<n; i++)
            {
                if(x == SnakeX[i] && y == SnakeY[i])
                    return true;
                if(x <0 || x>30||y<0||y>30)
                    return true;
            }
            return false;
        }
 
        function move() {
            if(!isLive)
                return;
            x = x + vx;
            y = y + vy;
            keyFlag=false;
            if (collision(x, y))
            {
                gameCount++;
                point[gameCount] = appleCount;
                document.getElementById("block2").innerHTML += gameCount +'번 째 기록 : '+ point[gameCount] +'점 입니다<br>';
                alert('충돌했네요!! 죽었어요');
                isLive = false;
                return;
            }
 
            if (x == appleX && y == appleY)
            {
                var isInbody = true;
                while(isInbody)
                {
                    appleX =Math.floor(Math.random()*21);
                    appleY = Math.floor(Math.random()*21);
                    isInbody = false;
 
                    for (var i =0;i<tall; i++){
                        if((appleX == SnakeX[i] && appleY == SnakeY[i]) || (appleX == boomX && appleY == boomY)){
                            isInbody = true;
                        }
                    }
                }
                tall = tall + 5;
                appleCount ++;
                document.getElementById("block1").innerHTML = '현재 점수 : '+ appleCount +'점 입니다';
            }
            if (x == boomX && y == boomY){
                gameCount++;
                point[gameCount] = appleCount;
                document.getElementById("block2").innerHTML += gameCount +'번 째 기록 : '+ point[gameCount] +'점 입니다<br>';
                alert('충돌했네요!! 죽었어요');
                isLive = false;
                return;
            }
            /*
                         tall 5, n 5
            SnakeX = [a, b, c, d, e]
                     [b  c  d  e]  n--*/
            if(n >= tall)
            {
                for(var i=0; i<n-1; i++)
                {
                    SnakeX[i]=SnakeX[i+1];
                    SnakeY[i]=SnakeY[i+1];
                }    
                n--;
            }
 
            SnakeX[n] = x;
            SnakeY[n] = y;
            n++;
 
            draw();
        }
        function addbody()
        {
            tall = tall + 5;
        }
        function onkeydown(evt)
        {
            //alert(evt.keyCode);
            if(evt.keyCode == 37&&keyFlag == false){
                if (vx ==1 && vy ==0){
                }
                else {
                vx = -1;
                vy = 0;
                keyFlag=true;
                }    
            }
            if(evt.keyCode == 38&&keyFlag == false){
                if (vx ==0 && vy==1){
                }
                else {
                vy = -1;
                vx = 0;
                keyFlag=true;
                }
            }
            if(evt.keyCode == 39&&keyFlag == false){
                if (vx==-1 && vy ==0){}
                else {
                vx = 1;
                vy = 0;
                keyFlag=true;
                }
            }
            if(evt.keyCode == 40&&keyFlag == false){
                if(vx==0 && vy ==-1){}
                else{
                vx = 0;
                vy = 1;
                keyFlag=true;
                }
            }
            if(evt.keyCode == 32&&keyFlag == false){
                if (isLive == false){
                isLive = true;
                x=0,y=0;
                tall =5;
                n=0;
                vx = 1, vy = 0;
                appleCount=0;
                appleX =Math.floor(Math.random()*21);
                appleY =Math.floor(Math.random()*21);
                document.getElementById("block1").innerHTML = '현재 점수 : '+ appleCount +'점 입니다';
                keyFlag=true;
                }
            }
            draw();
        }
 
        tall = 5;
        n = 0;
 
        draw();
 
        setInterval(move,80);
 
        document.addEventListener("keydown"onkeydown);
        
    </script>
 
    
</body>
 
</html>
cs

html-0921-jhs.zip



 

반응형

'Study > html' 카테고리의 다른 글

HTML - Array, updownGame  (0) 2018.09.20
HTML 실습  (0) 2018.09.14
반응형
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
<html>
    <head> 
        <meta charset ="utf-8"> <!--문자 코드셋 정의 -->
    </head>
<body>
 
        <input id="age" type="text"/> <!-- input text box-->
        <button onclick="btnclick()">button!</button><!-- button -->
 
    <div style="border:1px solid black " id="block1"> ...</div>
    <script>
 
        var p=10;
        var q=10;
        var r="문자열";
        
        function btnclick(){
            var a = (parseInt(document.getElementById("age").value)); 
            console.log(a);
            if(isNaN(a)){
                console.log("숫자가 아닙니다.");
            }
            else {
                document.getElementById("block1").innerHTML += '입력된 값은'+ a +'입니다<br>';
            }
        }
    </script>
</body>
</html>
cs

 

 

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
<html>
    <head> 
        <meta charset ="utf-8"> <!--문자 코드셋 정의 -->
    </head>
<body>
 
        <input id="age" type="text"/> <!-- input text box-->
        <input id="password" type="password"/> <!-- input password box-->
        <input id="date" type="date"/> <!-- input date box-->
        <input id="email" type="email"/> <!-- input email box-->
        <button onclick="btnclick()">button!</button><!-- button -->
 
    <div style="position:relative; border:1px solid black; border-radius : 10px; padding:5px; box-shadow: 10px 10px 5px silver " id="block1" onmousemove ="movemouse()"> 눌러보세요</div>
    <img id = "img1"></body>
    <script>
        var a = [10"안녕하세요"20"하이"];
        var b= new Array(10);
 
        function btnclick(){ // btn func
            for (var i =0; i<a.length;i++){
                a[i]=a[i]+1;
                console.log(a[i]);
            }
            for (var i = 0; i<b.length;i++){
                console.log("B index="+i+":"+b[i]);
            }
            if(document.getElementById("age").value == "1")
                document.getElementById("block1").innerHTML += a[0];
 
            a.push(document.getElementById("age").value);
        }
        function movemouse(){ // mouse hover func
            document.getElementById("block1").style.top = (Math.random()*500)+"px";
        }
        var i =0;
        var mytop =0;
        var velocity =0;
        setInterval (function() { // block1 move
            i++;
            document.getElementById("block1").style.top = (mytop*5+"px";
            mytop = mytop +velocity;
            if(mytop >100){
                mytop =100;
                velocity = -velocity*0.8;
            }
            else {
                velocity = velocity +0.5;
            }
            console.log("Hi"+i);
        }, 50);
    
    </script>
</body>
</html>
cs

 

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
<html>
    <head> 
        <meta charset ="utf-8"> <!--문자 코드셋 정의 -->
    </head>
<body>
    <input id="number" type="text" onkeypress = "if (event.keyCode ==13) btnclick();">
    <button onclick="btnclick()"> 확인! </button><!-- button --><br><br>
    <div style="position:relative; border:1px solid black; border-radius : 10px; padding:5px; box-shadow: 10px 10px 5px silver  "id="block1"> 1부터 500까지 숫자를 입력하세요. </div><br><br>
    <img id = "img" >
 
</body>
    <script>
        var result = Math.round(Math.random()*500);
        var count =0;
            function btnclick(){
                count ++;
            var a = (parseInt(document.getElementById("number").value)); 
            if(document.getElementById("number").value==result){
                document.getElementById("img").src = "3.jpg";
                document.getElementById("block1").innerHTML += '<br>값은'+ a +'입니다, '+ count +' 번 만에 맞추셨네요';
            }
            else if(document.getElementById("number").value >result && document.getElementById("number").value <=500){ 
                document.getElementById("block1").innerHTML += '<br>값은'+ a +'보다 작습니다';
                document.getElementById("img").src = "2.jpg";
            }
            else if(document.getElementById("number").value < result){
                document.getElementById("img").src = "1.jpg";
                document.getElementById("block1").innerHTML += '<br>값은'+ a +'보다 큽니다';
            }
            else if(document.getElementById("number").value >500){
                count --;
                alert("1에서 500사이의 숫자를 입력해 주세요.");
            }
        }
    </script>
 
</body>
</html>
cs

 

 

 

html-0920(updown).zip

 

반응형

'Study > html' 카테고리의 다른 글

HTML,JSP - Snake game  (0) 2018.09.27
HTML 실습  (0) 2018.09.14
반응형

 

// HTML 실습

font : 폰트 색상

br : 줄바꿈

a href : 링크

H1, H2, H3 : 소중대 제목

img src : 이미지

<img width ="100" height="100" src="cat.jpg">

<img width ="100%" height="100" src="cat.jpg"> // 100%로 화면에 꽉차게 넣을 수 있다.

주석 : <!-- 주석 다는 방법-->

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<html>
    <meta charset='UTF-8'>
        <body>
            HTML : <font color ="blue">Hyper-text Markup Language<br>
            <font color="black">
            helo world<br>
 
            <a href="http://google.co.kr"> http://google.co.kr</a><br>
            <a href ="second.html"> Move_SecondPage!<br></a>
            <font color="red"> FontColor =RED
            <font color="black">
        <H1>
            H1 Tag            
        </H1>
        <h2>
            H2 Tag
        </h2>
        <h3>
            H3 Tag
        </h3>
    <img src="cat.jpg">
    </body>
</html>
 
cs

 

 

 

 

 ******참고 링크******

https://www.w3schools.com/

 

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
<html>
    <meta charset='UTF-8'>
        <body style = "background-color: #000000; font-size: 20px; color: white">
            HTML : <font color ="blue">Hyper-text Markup Language<br>
            
            helo world<br>
 
            <a href="http://google.co.kr"> http://google.co.kr</a><br>
            <a href ="second.html"> Move_SecondPage!<br></a>
            <font color="red"> FontColor =RED
            <font color="pink">
        <H1>
            H1 Tag            
        </H1>
        <h2>
            H2 Tag
        </h2>
        <h3>
            H3 Tag
        </h3>
    <img width ="100" height="100" src="cat.jpg">
    <div style="background-image: ('cat.jpg')"></div>
    <div style="color: red"> next line move </div>
    <div> next line move </div>
    <span style="color:blue"> I don't want to next line move </span>
    <span style="color: green"> I don't want to next line move </span>
    </body>
</html>
 
cs

 

 

반응형

'Study > html' 카테고리의 다른 글

HTML,JSP - Snake game  (0) 2018.09.27
HTML - Array, updownGame  (0) 2018.09.20
반응형

// 어떤 자연수 X의 세 자연수의 합으로 만들어 진다.

오름차순으로 중복 없이 조합되는 수를 출력하여라

 

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
// prob.04.cpp: 콘솔 응용 프로그램의 진입점을 정의합니다.
//
 
#include "stdafx.h"
#include <stdio.h>
 
 
int main()
{
    int count1 = 0int count2 = 1int count3 = 2;
    int num = 0
    int num1[100= { 0 }; int num2[100= { 0 }; int num3[100= { 0 };
    
    printf("입력받을 자연수 N의 개수 : ");
    scanf_s("%d"&num);
    
    int x[100= { 0 };
    for (int i = 0; i < num; i++) {
        printf("\n자연수 X를 입력 해 주세요 : ");
        scanf_s("%d"&x[i]);
    }
 
    for (int i = 0; i < num; i++) {
        printf("\n자연수 X %d : %d",i, x[i]);
    }
    
    for (int l = 0; l < num;l++ ) {
        if (x[l] < 4)printf("\nNULL");
        else {
            for (int i = 1; i < x[l]; i++) {
                count1 = i;
                for (int j = i + 1; j < x[l]; j++) {
                    count2 = j;
                    for (int k = j + 1; k < x[l]; k++) {
                        count3 = k;
                        if ((count1 + count2 + count3) == x[l]) {
                            printf("\ncount1 = %d  ", count1);
                            printf("count2 = %d  ", count2);
                            printf("count3 = %d  ", count3);
                        }
                    }
                }
            }
        }
    }
    return 0;
}
 
 
cs

 

 

 

 

반응형

'Study > C' 카테고리의 다른 글

Sprintf에 예제와 사용 예  (0) 2019.01.04
itoa, atoi 함수에 대하여  (0) 2019.01.04
C언어 - 자판기  (0) 2018.09.14
C언어 - 포인터 이해  (0) 2018.09.07
C언어 - EOF  (0) 2018.09.07
반응형

 

// 자판기

어떤 동전 가격 종류 상관 없이 계산하려 한다.

최적의 해를 구하라

예) 동전종류 50원, 100원, 500원, 1000원

  자판기 600원

 > 500원 1개,100원1개

 

 

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
// jhs0914.cpp: 콘솔 응용 프로그램의 진입점을 정의합니다.
//
 
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    printf("동전의 종류는 몇개 인가요?  : ");
    int cointype = 0;
    scanf_s("%d"&cointype);
    printf("동전의 종류 : %d", cointype);
    int cointypeSave[100= { 0 };
 
 
    for (int i = 0; i < cointype; i++) {
        printf("\n%d번째 동전 가격 : ",i+1);
        scanf_s("%d",&cointypeSave[i]);
    }
 
    for (int i = 0; i < cointype; i++) {
        printf("\n%d번째 동전 : %d",i,cointypeSave[i]);
    }
 
    printf("\n자판기에는 얼마가 들어 있나요?  : ");
    int coinpay = 0;
    scanf_s("%d"&coinpay);
    
    int count[100= { 0 };
    int count1 = 0;
    
 
        for (int i = cointype-1; i >=0; i--) {
            count1 = 0;
            while ((coinpay - cointypeSave[i]) >= 0) {
                coinpay = coinpay - cointypeSave[i];
                count1++;
                count[i] = count1;
            }
            
        }
    for (int i = 0; i < cointype; i++) {
        if (count[i] == 0){}
        else 
            printf("\n %d원 짜리 동전 %d개", cointypeSave[i], count[i]);
    }
 
    
    return 0;
}
 
 
cs

 

 

 

반응형

'Study > C' 카테고리의 다른 글

itoa, atoi 함수에 대하여  (0) 2019.01.04
C언어 - 자연수의 조합  (0) 2018.09.14
C언어 - 포인터 이해  (0) 2018.09.07
C언어 - EOF  (0) 2018.09.07
C언어 - 학생 Table(총점, 평균, 석차)  (0) 2018.09.07
반응형

 

 

// Piano 만들기

1. Record mode, Listen mode 기능 구현

2. Exam mode 버그..

 

 

 

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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
 * myfunc.h
 *
 * Created: 2018-09-12 오전 11:14:38
 *  Author: USER
 */ 
 
 
#ifndef MYFUNC_H_
#define MYFUNC_H_
 
#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>
#include <avr/interrupt.h>
 
#define POWER_ON_MODE 0
#define POWER_OFF_MODE 1
 
#define NOMAL_MODE 2
#define MENU_SELECT_MODE 3
 
#define RECORD_MODE 4
#define LISTEN_MODE 5
#define EXAM_SELECT_MODE 6
 
#define EXAM1_MODE 7
#define EXAM2_MODE 8
#define EXAM3_MODE 9
 
#define POWER_ON_DISPLAY 0
#define POWER_ON_UNDISPLAY 1
 
#define POWER_OFF_DISPLAY 0
#define POWER_OFF_UNDISPLAY 1
 
//메뉴
#define MENU_RECORD_MODE 0
#define MENU_LISTEN_MODE 1
#define MENU_EXAM_MODE 2
#define MENU_RETURN_MODE 3
 
//EXAM
#define EXAM1 0
#define EXAM2 1
#define EXAM3 2
#define EXAMRETURN 3
 
//음계
#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
#define NO 0
 
#define DOUP 277.18
#define REUP 311.12
#define FAUP 369.99
#define SOLUP 415.30
#define RAUP 466.16
 
//KEY값
#define K_MENU  0b00101011
#define K_POWER 0b01001011
#define K_DO 0b00011110
#define K_RE 0b00101110
#define K_MI 0b1001110
#define K_FA 0b00011101
#define K_SOL 0b00101101
#define K_RA 0b01001101
#define K_SI 0b00011011
 
#define LED_DO 0b11111110
#define LED_RE 0b11111101
#define LED_MI 0b11111011
#define LED_FA 0b11110111
#define LED_SOL 0b11101111
#define LED_RA 0b11011111
#define LED_SI 0b10111111
 
//전역 변수
unsigned int count =0;
unsigned int mode =0,state =0;
unsigned int menuSelectCount =0;
unsigned int ExamSelectCount =0;
unsigned int timeCount =0,sec =0;
unsigned int PowerOnDisplayState =0,PowerOffDisplayState=0;
unsigned int key=0;
 
unsigned int tCount1 = 0, tCount2 = 0;
unsigned int msec = 0, rotSec = 0;
unsigned int note[100= {0,};
unsigned int note_time[100= {0,};
unsigned int n_index = 0, n_flag = 0;
 
void port_init(){
    TCCR1A =0x40;
    TCCR1B = 0x09;
    
    TIMSK = 0xc0// timer2 enable
    TCCR2 = 0x03// 64분주
    TCNT2 = 256-(256-6); // 0.001s = 1ms
}
 
void powerOn()
{
    if (PowerOnDisplayState == POWER_ON_DISPLAY){
        lcd_putsf(0,0,(unsigned char *)"  PIANO START!  ");
        lcd_putsf(0,1,(unsigned char *)"    POWER ON!   ");
        _delay_ms(3000);
        PowerOnDisplayState =1;
    }
    else if(PowerOnDisplayState==POWER_ON_UNDISPLAY){
        lcd_putsf(0,0,(unsigned char *)"  PIANO START!  ");
        lcd_putsf(0,1,(unsigned char *)"                ");
        mode = NOMAL_MODE;
    }
}
 
void powerOff()
{
    if(PowerOffDisplayState == POWER_OFF_DISPLAY){
        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);
        PowerOffDisplayState =POWER_OFF_UNDISPLAY;
    }
    else if(PowerOffDisplayState == POWER_OFF_UNDISPLAY){
        lcd_putsf(0,0,(unsigned char *)"                ");
        lcd_putsf(0,1,(unsigned char *)"                ");
    }
}
 
 
// Key Matrix 함수
unsigned char getKey(){
    unsigned char key = 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);
    }
}
 
//CTC 모드로 소리 출력
void freqSet(int freq, int delayTime){
    if (freq == 0) OCR1A = 0;
    else OCR1A = 8000000 /freq;
    _delay_ms(delayTime);
}
 
void freq(int freq){
    if (freq == 0) OCR1A = 0;
    else OCR1A = 8000000 /freq;
}
 
// 음악 저장
int doremi[10= {DO,RE,MI,FA,SOL,RA,SI,(DO*2)};
int school[100]= {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 [100]= {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 dog[100]    = {RA,RA,RA,SOL,RA,SOL,FA,2*FA,2*FA,2*MI,RA,2*RE,
                2*RE,2*RE,2*DO,RAUP,RA,SOL,FA,FA,SOL,SOL,2*DO,
                RA,RA,RA,SOL,RA,SOL,FA,2*FA,2*FA,2*MI,RA,2*RE,
                2*RE,2*RE,2*DO,RAUP,RA,SOL,FA,RE,FA,MI,FA,SOL,FA,
                RA,RAUP,RA,2*RE,2*MI,2*FA,2*MI,RA,2*RE,2*RE,2*DO,RAUP,RA,
                SOL,RA,RAUP,2*DO,2*FA,2*MI,2*RE,2*FA,2*FA,2*FA,2*MI,2*RE,2*DO,2*DO,
                2*FA,2*MI,2*FA,2*MI,2*RE,2*RE,2*RE,2*RE,2*DO,2*RE,2*DO,RA,
                FA,FA,SOL,SOL,2*DO,RA,FA,RE,FA,MI,FA,SOL,FA};
int dogtime[100]= {250,500,250,375,125,500,2000,250,750,500,500,2000,
                250,750,500,500,750,250,1000,1000,250,750,2000,
                250,500,250,375,125,500,2000,250,750,500,500,2000,
                250,750,500,500,750,250,1000,250,750,375,500,2000,
                375,125,375,1125,375,125,375,1125,250,750,500,500,2000,
                250,500,250,1000,500,500,1000,250,500,250,500,500,1500,500,
                1000,375,125,375,2125,250,500,250,375,125,500,2000,
                250,375,500,500,500,500,1000,250,750,375,125,500,2000};
int snow[500={RE,SI,RA,SI,2*RE,RA,NO,RE,RE,MI,SOL,SOL,MI,RE,NO,SOL,RA,
                SI,RA,SI,2*MI,2*RE,SI,NO,SI,SI,2*DO,SI,RA,SOL,SI,RA,NO,RE,SI,RA,SI,2*RE,RA,NO,RE,RE,
                MI,SOL,SOL,MI,RE,NO,SOL,RA,SI,RA,SI,2*MI,2*RE,SI,NO,SI,SI,2*DO,SI,RA,SOL,SOL,NO,
                NO,MI,SOL,2*RE,2*DO,SI,RA,RA,SOL,FAUP,SOL,MI,SOL,
                2*RE,2*MI,2*RE,2*DO,SI,RA,SI,NO,SOL,SOL,2*MI,2*FAUP,2*SOL,2*FAUP,2*MI,2*DOUP,2*RE,
                NO,2*MI,(2*FA),2*MI,2*RE,2*DO,SI,SOL,SOL,NO,MI,SOL,2*MI,2*REUP,2*MI,2*FAUP,SI,SI,
                2*SOL,2*FAUP,2*SOL,2*FAUP,2*SOL,2*FAUP,2*MI,2*RE,2*MI,2*SI,2*SI,NO,2*FAUP,2*SOL,2*RA,2*FAUP,2*MI,2*RE,2*RE,NO,SI,2*DO,2*RE,
                2*RE,2*DO,2*DO,2*MI,2*RE,SI,RA,SOL,NO,MI,SOL,2*RE,2*DO,2*DO,2*MI,2*MI,2*REUP,2*MI,2*FAUP,2*SOL,2*FAUP,2*SOL,2*FAUP,2*SOL,2*FAUP,2*MI,2*RE,
                2*MI,2*SI,2*SI,NO,2*FAUP,2*SOL,2*RA,2*FAUP,2*MI,2*MI,2*RE,2*RE,2*RA,2*SOL,SI,2*DO,2*RE,2*RE,2*DO,2*DO,2*RE,SI,RA,SOL,SOL,
                NO,SI,2*RE,2*RE,2*DO,DO,RE,2*MI,2*SOL,2*FAUP,FAUP,2*MI,2*FAUP,2*SOL,3*SOL,NO,RE};
int snowtime[500]={250,250,250,250,125,625,250,125,125,250,125,250,125,750,250,125,125,
                250,250,250,125,185,185,250,125,125,250,125,250,125,125,625,250,250,250,250,250,125,625,250,125,125,
                250,125,250,250,750,250,125,125,250,250,250,125,250,375,250,125,125,250,125,250,125,750,500,
                500,250,250,1250,250,250,250,250,250,250,750,250,250,
                750,125,375,125,250,125,1250,500,250,250,750,250,250,250,250,125,125,
                500,250,250,250,250,250,250,250,125,625,500,250,250,625,125,125,625,250,250,
                250,250,250,125,375,250,250,250,250,125,625,125,250,250,250,125,125,250,125,625,500,250,125,125,
                250,250,250,125,250,125,125,125,125,125,125,250,250,250,250,125,375,250,250,250,250,250,250,125,375,250,250,250,
                };
void snowplay(){
    for(int i =0; i<500;i++){
        freqSet(snow[i],snowtime[i]);
        freqSet(0,10);
    }mode = EXAM_SELECT_MODE;
}
void dogPlay(){
    for (int i= 0;i<100;i++){
        freqSet(dog[i],dogtime[i]);
        freqSet(0,10);
    }
    mode = EXAM_SELECT_MODE;
}
void schoolPlay(){
    for (int i= 0;i<25;i++){
        freqSet(school[i],500);
        freqSet(0,200);
    }
    mode = EXAM_SELECT_MODE;
}
void doremiPlay(){
    for (int i= 0;i<8;i++){
        freqSet(doremi[i],500);
        freqSet(0,200);
    }
    mode = EXAM_SELECT_MODE;
}
void key_init(){
    lcd_putsf(0,0,(unsigned char *)"  PIANO START!  ");
    lcd_putsf(0,1,(unsigned char *)"                ");
    char keyPos = 25;
    char strConv[16];
    
    if (key == K_DO){ PORTA = LED_DO; freq(DO); keyPos =1;}
    else if (key==K_RE){ PORTA = LED_RE; freq(RE); keyPos=3;}
    else if (key == K_MI){ PORTA = LED_MI; freq(MI); keyPos =5;}
    else if (key == K_FA){ PORTA = LED_FA; freq(FA); keyPos =7;}
    else if (key == K_SOL){ PORTA = LED_SOL; freq(SOL); keyPos=9;}
    else if (key == K_RA){ PORTA = LED_RA; freq(RA); keyPos=11;}
    else if(key ==K_SI){ PORTA = LED_SI; freq(SI); keyPos =13;}
    else
    {
        OCR1A=0;
        PORTA = 0xff;
        sprintf(strConv,"                ");
        lcd_putsf(0,1,(unsigned char *)strConv);
    }
    
    sprintf(strConv,"                ");
    strConv[keyPos] = 0xff;
    lcd_putsf(0,1,(unsigned char *)strConv);
    
}
void recordDisplay(){
    lcd_putsf(0,0,(unsigned char *)"  Recording..   ");
    lcd_putsf(0,1,(unsigned char *)"                ");
}
void ListenDisplay(){
    lcd_putsf(0,0,(unsigned char *)"  Listening..   ");
    lcd_putsf(0,1,(unsigned char *)"                ");
}
 
 
void keyPowerOff(){
    if (key == K_POWER){ // POWER KEY 입력시 OFF 모드로 이동한다.
        mode = POWER_OFF_MODE;
        PowerOffDisplayState = POWER_OFF_DISPLAY;
        key =0;
        _delay_ms(100);
    }
}
void keyPowerOn(){
    if (key == K_POWER){
        mode = POWER_ON_MODE;
        PowerOnDisplayState = POWER_ON_DISPLAY;
        key =0;
        _delay_ms(100);
    }
}
 
 
void listen(){
    for (int i=1; i<n_index; i++)
    {
        freq(note[i]);
        _delay_ms(note_time[i] * 25);
    }
    freq(0);
    mode = MENU_SELECT_MODE; 
}
#endif /* MYFUNC_H_ */
cs

 

 

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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* piano-0911.cpp
*
* Created: 2018-09-11 오전 11:27:56
* Author : USER
*/
 
 
#include "myfunc.h"
 
ISR(TIMER2_OVF_vect){
    TCNT2 = 6;
    count++// 0.001초마다 1회증가
    timeCount++;
    if(count >2000){
        menuSelectCount++;
        ExamSelectCount++;
        count =0;
        if(menuSelectCount>3) menuSelectCount=0;
        if(ExamSelectCount>3) ExamSelectCount=0;
    }
    if(timeCount >1000){ // 1초
        sec++;
        timeCount=0;
    }
}
int main(void)
{
    DDRA= 0xff// LED 출력 설정
    DDRB = 0xff// 스피커 출력 설정
    DDRD = 0x0f// key Matrix 입출력 설정
    
    PORTA = 0xff// LED OFF
    PORTD = 0xff// Key Matrix 초기값
 
    char str[16];
    
    
    sei();
    port_init();
    lcd_init();
    /* Replace with your application code */
    
    
    while (1)
    {
        key = getKey();
        
        if (mode == POWER_ON_MODE){
            powerOn();
        }
        else if(mode ==POWER_OFF_MODE){
            powerOff();
            if (key == K_POWER){
                mode = POWER_ON_MODE;
                PowerOnDisplayState = POWER_ON_DISPLAY;
                key =0;
                _delay_ms(100);
            }
        }
        else if(mode == NOMAL_MODE){
            key_init();
            if (key == K_POWER){ // POWER KEY 입력시 OFF 모드로 이동한다.
                mode = POWER_OFF_MODE;
                PowerOffDisplayState = POWER_OFF_DISPLAY;
                key =0;
                _delay_ms(100);
            }
            if (key == K_MENU){
                mode = MENU_SELECT_MODE;
                menuSelectCount =0;
                key=0;
                _delay_ms(100);
            }
        }
        else if(mode == MENU_SELECT_MODE){
            lcd_putsf(0,0,(unsigned char *)"    M E N U     ");
            if (menuSelectCount == MENU_RECORD_MODE){
                lcd_putsf(0,1,(unsigned char *)"1. Recording    ");
                if (key == K_MENU){
                    mode = RECORD_MODE;
                    key=0;
                    _delay_ms(100);
                }
            }
            else if(menuSelectCount == MENU_LISTEN_MODE){
                lcd_putsf(0,1,(unsigned char *)"2. Listening    ");
                if (key == K_MENU){
                    mode = LISTEN_MODE;
                    key=0;
                    _delay_ms(100);
                }
            }
            else if(menuSelectCount == MENU_EXAM_MODE){
                lcd_putsf(0,1,(unsigned char *)"3. Example      ");
                if (key == K_MENU){
                    mode = EXAM_SELECT_MODE;
                    ExamSelectCount =0;
                    key=0;
                    _delay_ms(100);
                }
            }
            else if(menuSelectCount == MENU_RETURN_MODE){
                lcd_putsf(0,1,(unsigned char *)"4. Return       ");
                if (key == K_MENU){
                    mode = NOMAL_MODE;
                    key=0;
                    _delay_ms(100);
                }
            }
            if (key == K_POWER){ // POWER KEY 입력시 OFF 모드로 이동한다.
                mode = POWER_OFF_MODE;
                PowerOffDisplayState = POWER_OFF_DISPLAY;
                key =0;
                _delay_ms(100);
            }
        }
        else if(mode == RECORD_MODE){
            recordDisplay();
            if (key == K_DO && n_flag==0){ note_time[n_index] = msec; note[n_index + 1= DO;  msec =0; n_flag=1; n_index++; freq(DO); }
            else if(key == K_RE && n_flag == 0) { note_time[n_index] = msec; note[n_index + 1= RE; msec = 0; n_flag = 1; n_index++; freq(RE); }
            else if(key == K_MI && n_flag == 0) { note_time[n_index] = msec; note[n_index + 1= MI; msec = 0; n_flag = 1; n_index++; freq(MI); }
            else if(key == K_FA && n_flag == 0) { note_time[n_index] = msec; note[n_index + 1= FA; msec = 0; n_flag = 1; n_index++; freq(FA); }
            else if(key == K_SOL && n_flag == 0) { note_time[n_index] = msec; note[n_index + 1= SOL; msec = 0; n_flag = 1; n_index++; freq(SOL); }
            else if(key == K_RA && n_flag == 0) { note_time[n_index] = msec; note[n_index + 1= RA; msec = 0; n_flag = 1; n_index++; freq(RA); }
            else if(key == K_SI && n_flag == 0) { note_time[n_index] = msec; note[n_index + 1= SI; msec = 0; n_flag = 1; n_index++; freq(SI); }
            else if (key == 0 && n_flag == 1){ note_time[n_index] = msec; note[n_index + 1= NO; msec = 0; n_flag = 0; n_index++; freq(0); }
            if (key == K_MENU){
                mode = MENU_SELECT_MODE;
                key=0;
                _delay_ms(100);
            }
            
            if (key == K_POWER){ // POWER KEY 입력시 OFF 모드로 이동한다.
                mode = POWER_OFF_MODE;
                PowerOffDisplayState = POWER_OFF_DISPLAY;
                key =0;
                _delay_ms(100);
            }
        }
        else if(mode == LISTEN_MODE){
            ListenDisplay();
            listen();
            if (key == K_POWER){ // POWER KEY 입력시 OFF 모드로 이동한다.
                mode = POWER_OFF_MODE;
                PowerOffDisplayState = POWER_OFF_DISPLAY;
                key =0;
                _delay_ms(100);
            }
        }
        else if(mode == EXAM_SELECT_MODE){
            lcd_putsf(0,0,(unsigned char *)"   -Example-    ");
            if(ExamSelectCount == EXAM1){
                lcd_putsf(0,1,(unsigned char *)"1. Example 1    ");
            }
            else if(ExamSelectCount == EXAM2){
                lcd_putsf(0,1,(unsigned char *)"2. Example 2    ");
            }
            else if(ExamSelectCount == EXAM3){
                lcd_putsf(0,1,(unsigned char *)"3. Example 3    ");
            }
            else if(ExamSelectCount == EXAMRETURN){
                lcd_putsf(0,1,(unsigned char *)"4. Return       ");
            }
            if (key == K_POWER){ // POWER KEY 입력시 OFF 모드로 이동한다.
                mode = POWER_OFF_MODE;
                PowerOffDisplayState = POWER_OFF_DISPLAY;
                key =0;
                _delay_ms(100);
            }
            if (key == K_MENU){
                if (ExamSelectCount == EXAM1)mode = EXAM1_MODE;
                else if(ExamSelectCount == EXAM2)mode = EXAM2_MODE;
                else if(ExamSelectCount == EXAM3)mode = EXAM3_MODE;
                else if(ExamSelectCount == EXAMRETURN)mode = MENU_SELECT_MODE;
                key=0;
                _delay_ms(100);
            }
        }
        else if(mode ==EXAM1_MODE){
            doremiPlay();
        }
        else if(mode == EXAM2_MODE){
            schoolPlay();
        }
        else if(mode == EXAM3_MODE){
            dogPlay();
        }
        
    }
}
 
 
cs

 

 

 

lcd.h

 

반응형
반응형

 

// Piano 만들기

1. Menu Rotate, Exam Rotate 기능 구현 (Timer/Counter)

2. Record, listen mode 구현 필요

3. 구조적 변경 필요..

 

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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * piano-0911.cpp
 *
 * Created: 2018-09-11 오전 11:27:56
 * Author : USER
 */ 
 
// last copy
 
#include "myfunc.h"
ISR(TIMER2_OVF_vect){
    TCNT2 = 6;
    count++// 0.001초마다 1회증가
    if(count >2000){
        menuMode++;
        Exammode++;
        count=0;
        if(menuMode >3) menuMode=0;
        if(Exammode >4) Exammode=0;
    }
}
 
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 menu =0, menuFlag=0,menuKeyflag=0;
    int menuModeKeyflag=0;
    
    int keyFlag =0;
    char str[16];
    int key =0;
    
    TCCR1A =0x40;
    TCCR1B = 0x09;
    
    lcd_init();
    lcd_clear();
    
    
    TIMSK = 0xc0// timer2 enable
    TCCR2 = 0x03// 64분주
    TCNT2 = 256-(256-6); // 0.001s = 1ms
    sei();
    /* Replace with your application code */
    
    while (1
    {        
        key = getKey();
        // SW8 00101011
        // SW9 01001011
        if (key == 0b01001011){
            if (mode == PLAYMODE){
                mode =OFFMODE; modeFlag=0;
            }
            else if (mode == OFFMODE){
                mode =PLAYMODE;
                playmodeFlag =0;
            }
            keyFlag=1;
        }
        else if (key== 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){
                if((key == 0b00101011)&&(menuKeyflag==0)){ //SW8 키입력시 동작
                    menuFlag++;
                    menuKeyflag=1;
                    TCNT2=6;
                    if (menuFlag >2) menuFlag =0;
                }
                else if ((key == 0b00101011) && (menuMode == RECORDMENU)){
                    lcd_putsf(0,0,(unsigned char *)"  Recording..   ");
                    lcd_putsf(0,1,(unsigned char *)"                ");
                    
                }
                else if ((key == 0b00101011) && (menuMode == LISTENMENU)){
                    lcd_putsf(0,0,(unsigned char *)"  Listening..   ");
                    lcd_putsf(0,1,(unsigned char *)"                ");
                                
                }
                else if ((key == 0b00101011) && (menuMode == EXAMPLEMENU)){
                    lcd_putsf(0,0,(unsigned char *)"   -Example-    ");
                    if (Exammode == EXAM1)lcd_putsf(0,1,(unsigned char *)"1. Example 1    ");
                    else if(Exammode == EXAM2)lcd_putsf(0,1,(unsigned char *)"2. Example 2    ");    
                    else if(Exammode == EXAM3)lcd_putsf(0,1,(unsigned char *)"3. Example 3    ");    
                    else if(Exammode == 4)lcd_putsf(0,1,(unsigned char *)"4. Return       ");
                }
                else if ((key == 0b00101011) && (menuMode == RETRUNMENU)){
                    
                    menuFlag =0;
                    _delay_ms(100);
                }
                else if(key == 0x00){ 
                    menuKeyflag =0;
                }
 
                if (menuFlag==1){
                    if (menuMode ==RECORDMENU){
                        lcd_putsf(0,0,(unsigned char *)"    M E N U     ");
                        lcd_putsf(0,1,(unsigned char *)"1. Recording    ");
                    }
                    else if(menuMode ==LISTENMENU){
                        lcd_putsf(0,0,(unsigned char *)"    M E N U     ");
                        lcd_putsf(0,1,(unsigned char *)"2. Listening    ");
                    }
                    else if(menuMode == EXAMPLEMENU){
                        lcd_putsf(0,0,(unsigned char *)"    M E N U     ");
                        lcd_putsf(0,1,(unsigned char *)"3. Example      ");
                    }
                    else if(menuMode == RETRUNMENU){
                        lcd_putsf(0,0,(unsigned char *)"    M E N U     ");
                        lcd_putsf(0,1,(unsigned char *)"4. Return       ");
                    }
 
                }
                else if (menuFlag==0) {
                    lcd_putsf(0,0,(unsigned char *)"  PIANO START!  ");
                    lcd_putsf(0,1,(unsigned char *)"                ");
                
                    if (key == 0b00011110){
                        PORTA = 0b11111110;
                        sprintf(str,"                ");
                        str[1]=255;
                        lcd_putsf(0,1,(unsigned char *)str);
                        freq(DO);
                    }
                    else if (key==0b00101110){
                        PORTA = 0b11111101;
                        sprintf(str,"                ");
                        str[3]=255;
                        lcd_putsf(0,1,(unsigned char *)str);
                        freq(RE);
                    }
                    else if (key == 0b1001110){
                        PORTA = 0b11111011;
                        sprintf(str,"                ");
                        str[5]=255;
                        lcd_putsf(0,1,(unsigned char *)str);
                        freq(MI);
                    }
                    else if (key == 0b00011101){
                        PORTA = 0b11110111;
                        sprintf(str,"                ");
                        str[7]=255;
                        lcd_putsf(0,1,(unsigned char *)str);
                        freq(FA);
                    }
                    else if (key == 0b00101101){
                        PORTA = 0b11101111;
                        sprintf(str,"                ");
                        str[9]=255;
                        lcd_putsf(0,1,(unsigned char *)str);
                        freq(SOL);
                    }
                    else if (key == 0b01001101){
                        PORTA = 0b11011111;
                        sprintf(str,"                ");
                        str[11]=255;
                        lcd_putsf(0,1,(unsigned char *)str);
                        freq(RA);
                    }
                    else if(key ==0b00011011){
                        PORTA = 0b10111111;
                        sprintf(str,"                ");
                        str[13]=255;
                        lcd_putsf(0,1,(unsigned char *)str);
                        freq(SI);
                    }
                    else 
                    {
                        OCR1A=0;
                        PORTA = 0xff;
                        sprintf(str,"                ");
                        lcd_putsf(0,1,(unsigned char *)str);
                    }
                }
            }
            //itoa(getKey(), str, 2); // char 문자열을 2진수로 변환 itoa함수 stdlib.h헤더파일에 위치함.
            //lcd_putsf(0,0,(unsigned char *)str); // LCD 출력
            //lcd_putsf(0,1,(unsigned char *)"  ");
            
        }
        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 *)"                ");    
                lcd_clear();
            }
        }
    }
}
 
 
cs

//디버깅 중 코드

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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* piano-0911.cpp
*
* Created: 2018-09-11 오전 11:27:56
* Author : USER
*/
 
 
#include "myfunc.h"
#define SELECTMENU 0
#define DISSELECTMENU 1
#define POWER_ON_DISPLAY 0
#define POWER_ON_UNDISPLAY 1
#define POWER_OFF_DISPLAY 0
#define POWER_OFF_UNDISPLAY 1
 
ISR(TIMER2_OVF_vect){
    TCNT2 = 6;
    count++// 0.001초마다 1회증가
    if(count >2000){
        menuMode++;
        Exammode++;
        count=0;
        if(menuMode >3) menuMode=0;
        if(Exammode >4) Exammode=0;
    }
}
 
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=1;
    int menu =0, menuFlag=0,menuKeyflag=0;
    int menuModeKeyflag=0;
    
    int keyFlag =0;
    char str[16];
    int key =0;
    
    TCCR1A =0x40;
    TCCR1B = 0x09;
    
    lcd_init();
    lcd_clear();
    
    
    TIMSK = 0xc0// timer2 enable
    TCCR2 = 0x03// 64분주
    TCNT2 = 256-(256-6); // 0.001s = 1ms
    sei();
    /* Replace with your application code */
    
    int realMode = 255;
    int realExMode =255;
    int realmenuFlag =255;
    int realmenuSelect =255;
    
    while (1)
    {
        key = getKey();
        if(key == K_POWER){
            if (mode == PLAYMODE){
                mode =OFFMODE; modeFlag=0;
            }
            else if (mode == OFFMODE){
                mode =PLAYMODE;
                playmodeFlag =0;
            }
            keyFlag=1;
        }
        else if (key== 0x00) keyFlag=0;
        
        if(mode == PLAYMODE){
            if (playmodeFlag ==POWER_ON_DISPLAY){
                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 =POWER_ON_UNDISPLAY;
            }
            else if(playmodeFlag ==POWER_ON_UNDISPLAY){
                if((key == 0b00101011)&&(menuKeyflag==0)){ //SW8 키입력시 동작
                    menuFlag++;
                    menuKeyflag=1;
                    TCNT2=6;
                    if (menuFlag >2) menuFlag =0;
                    key = 0;
                    _delay_ms(100);
                }
                else if(key == 0x00){
                    menuKeyflag =0;
                }
                if (menuFlag == 1){//
                    if (realMode == 255)
                    {
                        if (menuMode == RECORDMENU){
                            lcd_putsf(0,0,(unsigned char *)"    M E N U     ");
                            lcd_putsf(0,1,(unsigned char *)"1. Recording    ");
                        }
                        else if(menuMode == LISTENMENU){
                            lcd_putsf(0,0,(unsigned char *)"    M E N U     ");
                            lcd_putsf(0,1,(unsigned char *)"2. Listening    ");
                        }
                        else if(menuMode == EXAMPLEMENU){
                            lcd_putsf(0,0,(unsigned char *)"    M E N U     ");
                            lcd_putsf(0,1,(unsigned char *)"3. Example      ");
                        }
                        else if(menuMode == RETRUNMENU){
                            lcd_putsf(0,0,(unsigned char *)"    M E N U     ");
                            lcd_putsf(0,1,(unsigned char *)"4. Return       ");
                        }
                        if (key == K_MENU)
                        {
                            realMode = menuMode;
                            key = 0;
                            _delay_ms(100);
                        }
                    }
                    else if (realMode == RECORDMENU)
                    {
                        lcd_putsf(0,0,(unsigned char *)"  Recording..   ");
                        lcd_putsf(0,1,(unsigned char *)"                ");
                    }
                    else if (realMode == LISTENMENU){
                        lcd_putsf(0,0,(unsigned char *)"  Listening..   ");
                        lcd_putsf(0,1,(unsigned char *)"                ");
                    }
                    else if (realMode == EXAMPLEMENU)
                    {
                        if (realExMode==255){
                            if (Exammode == EXAM1){
                                lcd_putsf(0,0,(unsigned char *)"   -Example-    ");
                                lcd_putsf(0,1,(unsigned char *)"1. Example 1    ");
                            }
                            else if (Exammode == EXAM2){
                                lcd_putsf(0,0,(unsigned char *)"   -Example-    ");
                                lcd_putsf(0,1,(unsigned char *)"2. Example 2    ");
                            }
                            else if (Exammode == EXAM3){
                                lcd_putsf(0,0,(unsigned char *)"   -Example-    ");
                                lcd_putsf(0,1,(unsigned char *)"3. Example 3    ");
                            }
                            else if (Exammode == EXAMRETURN){
                                lcd_putsf(0,0,(unsigned char *)"   -Example-    ");
                                lcd_putsf(0,1,(unsigned char *)"4. Return       ");
                            }
                            if (key == K_MENU)
                            {
                                realExMode = Exammode;
                            }
                        }
                        else if(realExMode == EXAM1){
                            for (int i = 0; i<5; i++){
                                freqSet(doremi[i],500);
                            }
                            OCR1A=0;
                            realExMode = 255;
                        }
                        else if(realExMode == EXAM2){
                            for (int i =0; i<25;i++){
                                freqSet(school[i],500);
                            }
                            OCR1A=0;
                            realExMode = 255;
                        }
                        else if(realExMode == EXAM3){
                            for (int i = 0;i<25; i++){
                                freqSet(plane[i],500);
                            }
                            OCR1A=0;
                            realExMode = 255;
                        }
                        else if(realExMode == EXAMRETURN){
                            realMode =255;
                            realExMode =255;
                        }
                    }
                    else if(realMode == RETRUNMENU){
                        menuMode= PLAYMODE;
                        realMode =255;
                    }
                }
                else if(menuFlag ==0){
                    lcd_putsf(0,0,(unsigned char *)"  PIANO START!  ");
                    lcd_putsf(0,1,(unsigned char *)"                ");
                }
            }
        }
        else if (mode == OFFMODE){
            if(modeFlag ==POWER_OFF_DISPLAY){ 
                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=POWER_OFF_UNDISPLAY;
            }
            else if (modeFlag==POWER_OFF_UNDISPLAY){
                lcd_putsf(0,0,(unsigned char *)"                ");
                lcd_putsf(0,1,(unsigned char *)"                ");
                lcd_clear();
            }
        }
    }
}
 
 
cs

 

 

// 구조 변경 후

모드별로 구조를 변경 함.

 

 

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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * piano-0912.cpp
 *
 * Created: 2018-09-12 오전 9:34:15
 * Author : USER
 */ 
 
#include "myfunc.h"
 
 
 
int main(void)
{
    DDRA= 0xff// LED 출력 설정
    DDRB = 0xff// 스피커 출력 설정
    DDRD = 0x0f// key Matrix 입출력 설정
    
    PORTA = 0xff// LED OFF
    PORTD = 0xff// Key Matrix 초기값
 
    char str[16];
    int key=0;
    TCCR1A =0x40;
    TCCR1B = 0x09;
    
    TIMSK = 0xc0// timer2 enable
    TCCR2 = 0x03// 64분주
    TCNT2 = 256-(256-6); // 0.001s = 1ms
    sei();
    
    lcd_init();
    /* Replace with your application code */
    int PowerOnDisplayState =0,PowerOffDisplayState=0;
    
    while (1
    {
        key = getKey();
        
        if (mode == POWER_ON_MODE){
            if (PowerOnDisplayState == POWER_ON_DISPLAY){
                lcd_putsf(0,0,(unsigned char *)"  PIANO START!  ");
                lcd_putsf(0,1,(unsigned char *)"    POWER ON!   ");
                _delay_ms(300);
                PowerOnDisplayState =1;
            }
            else if(PowerOnDisplayState==POWER_ON_UNDISPLAY){
                lcd_putsf(0,0,(unsigned char *)"  PIANO START!  ");
                lcd_putsf(0,1,(unsigned char *)"                ");
                mode = NOMAL_MODE;
            }
        }
        else if(mode ==POWER_OFF_MODE){
            if(PowerOffDisplayState == POWER_OFF_DISPLAY){
                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);
                PowerOffDisplayState =POWER_OFF_UNDISPLAY;
            }
            else if(PowerOffDisplayState == POWER_OFF_UNDISPLAY){
                lcd_putsf(0,0,(unsigned char *)"                ");
                lcd_putsf(0,1,(unsigned char *)"                ");
            }
            if (key == K_POWER){
                mode = POWER_ON_MODE;
                key =0;
                _delay_ms(100);
            }
        }
        else if(mode == NOMAL_MODE){
            lcd_putsf(0,0,(unsigned char *)    "   NOMAL MODE   ");
            lcd_putsf(0,1,(unsigned char *)"                ");
            
            if (key == 0b00011110){
                PORTA = 0b11111110;
                sprintf(str,"                ");
                str[1]=255;
                lcd_putsf(0,1,(unsigned char *)str);
                freq(DO);
            }
            else if (key==0b00101110){
                PORTA = 0b11111101;
                sprintf(str,"                ");
                str[3]=255;
                lcd_putsf(0,1,(unsigned char *)str);
                freq(RE);
            }
            else if (key == 0b1001110){
                PORTA = 0b11111011;
                sprintf(str,"                ");
                str[5]=255;
                lcd_putsf(0,1,(unsigned char *)str);
                freq(MI);
            }
            else if (key == 0b00011101){
                PORTA = 0b11110111;
                sprintf(str,"                ");
                str[7]=255;
                lcd_putsf(0,1,(unsigned char *)str);
                freq(FA);
            }
            else if (key == 0b00101101){
                PORTA = 0b11101111;
                sprintf(str,"                ");
                str[9]=255;
                lcd_putsf(0,1,(unsigned char *)str);
                freq(SOL);
            }
            else if (key == 0b01001101){
                PORTA = 0b11011111;
                sprintf(str,"                ");
                str[11]=255;
                lcd_putsf(0,1,(unsigned char *)str);
                freq(RA);
            }
            else if(key ==0b00011011){
                PORTA = 0b10111111;
                sprintf(str,"                ");
                str[13]=255;
                lcd_putsf(0,1,(unsigned char *)str);
                freq(SI);
            }
            else
            {
                OCR1A=0;
                PORTA = 0xff;
                sprintf(str,"                ");
                lcd_putsf(0,1,(unsigned char *)str);
            }
            if (key == K_POWER){ // POWER KEY 입력시 OFF 모드로 이동한다.
                mode = POWER_OFF_MODE;
                PowerOnDisplayState = POWER_ON_DISPLAY;
                key =0;
                _delay_ms(100);
            }
            if (key == K_MENU){
                mode = MENU_SELECT_MODE;
                menuSelectCount =0;
                key=0;
                _delay_ms(100);
            }
        }
        else if(mode == MENU_SELECT_MODE){
            lcd_putsf(0,0,(unsigned char *)"    M E N U     ");
            if (menuSelectCount == MENU_RECORD_MODE){
                lcd_putsf(0,1,(unsigned char *)"1. Recording    ");
                if (key == K_MENU){
                    mode = RECORD_MODE;
                    key=0;
                    _delay_ms(100);
                }
            }
            else if(menuSelectCount == MENU_LISTEN_MODE){
                lcd_putsf(0,1,(unsigned char *)"2. Listening    ");
                if (key == K_MENU){
                    mode = LISTEN_MODE;
                    key=0;
                    _delay_ms(100);
                }
            }
            else if(menuSelectCount == MENU_EXAM_MODE){
                lcd_putsf(0,1,(unsigned char *)"3. Example      ");
                if (key == K_MENU){
                    mode = EXAM_SELECT_MODE;
                    ExamSelectCount =0;
                    key=0;
                    _delay_ms(100);
                }
            }
            else if(menuSelectCount == MENU_RETURN_MODE){
                lcd_putsf(0,1,(unsigned char *)"4. Return       ");
                if (key == K_MENU){
                    mode = NOMAL_MODE;
                    key=0;
                    _delay_ms(100);
                }
            }
        }
        else if(mode == RECORD_MODE){
            lcd_putsf(0,0,(unsigned char *)"  Recording..   ");
            lcd_putsf(0,1,(unsigned char *)"                ");
        }
        else if(mode == LISTEN_MODE){
            lcd_putsf(0,0,(unsigned char *)"  Listening..   ");
            lcd_putsf(0,1,(unsigned char *)"                ");
        }
        
    }
}
 
 
cs

 

 

반응형
반응형

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