반응형

/*

flash memory를 사용하여 전원이 On/off시 count


*/




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
#include <stdio.h>
#include "stm32f4xx.h"
#include "lcd.h"
 
uint32_t FLASH_DATA_SECTOR = 0x080E0000// 섹터 11의 시작 메모리 번지
 
int main(void) {
    volatile unsigned short size = *(unsigned short*) (0x1FFF7A22); // Flash Size in KB
 
    volatile int value = ((int*) (FLASH_DATA_SECTOR))[0];
 
    if (value == 0xffffffff// Initial State: 초기화가 필요함.
        value = 0;
 
    value++;
 
    int ret;
 
    FLASH_Unlock();  // 잠금 해제
    FLASH_ClearFlag(
            FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR
                    | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); //에러 플래그들 초기화함.
    ret = FLASH_EraseSector(FLASH_Sector_11, VoltageRange_3); // 값 저장 전에는 섹터를 초기화해야함.
    ret = FLASH_ProgramWord(FLASH_DATA_SECTOR, value); // 한 바이트씩 저장하는 것은 FLASH_ProgramByte
    FLASH_Lock();  // 잠금
 
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
    lcd_init();
 
    char buf[255];
    sprintf(buf, "%d th boot.", value);
    lcd_printxy(01, buf);
 
    for (;;) {
    }
}
 
cs


반응형

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

[Elevator(ARM)] ADC  (0) 2018.10.25
[Elevator(ARM)] BLE USART  (0) 2018.10.25
[Elevator(ARM)] key Matrix  (0) 2018.10.25
[Elevator(ARM)] DoorLock  (0) 2018.10.25
[Elevator(ARM)] Calculator  (0) 2018.10.25
반응형

// ADC


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
Elevator (ARM)
* ADC 
1. H/W 연결
 - PB0에 가변저항이 연결되어 있음
 /**
  ******************************************************************************
  * @file    main.c
  * @author  Ac6
  * @version V1.0
  * @date    01-December-2013
  * @brief   Default main function.
  ******************************************************************************
*/
 
 
#include "stm32f4xx.h"
#include "stm32f4xx_adc.h"
void ADC_init(){
 
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
    GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN;
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOB, &GPIO_InitStruct);
 
 
    ADC_InitTypeDef ADC_InitStruct;
    ADC_InitStruct.ADC_ContinuousConvMode = DISABLE;
    ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_InitStruct.ADC_ExternalTrigConv = DISABLE;
    ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
    ADC_InitStruct.ADC_NbrOfConversion =1;
    ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b;
    ADC_InitStruct.ADC_ScanConvMode = DISABLE;
    ADC_Init(ADC1,&ADC_InitStruct);
 
    ADC_Cmd(ADC1, ENABLE);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_8,1,ADC_SampleTime_84Cycles);
}
int ADC_Read(){
    ADC_SoftwareStartConv(ADC1);
    while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));
    return ADC_GetConversionValue(ADC1);
}
int main(void)
{
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
    ADC_init();
    int j = 0;
    while(1){
         j = ADC_Read();
    }
}
 
cs


반응형

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

[Elevator(ARM)] Flash memory  (0) 2018.10.26
[Elevator(ARM)] BLE USART  (0) 2018.10.25
[Elevator(ARM)] key Matrix  (0) 2018.10.25
[Elevator(ARM)] DoorLock  (0) 2018.10.25
[Elevator(ARM)] Calculator  (0) 2018.10.25
반응형

//BLE USART


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
/**
  ******************************************************************************
  * @file    main.c
  * @author  Ac6
  * @version V1.0
  * @date    01-December-2013
  * @brief   Default main function.
  ******************************************************************************
*/
 
#include "stm32f4xx.h"
/* Include my libraries here */
//#include "defines.h"
#include "tm_stm32f4_usart.h"
 
int main(void) {
    uint8_t c;
 
    TM_USART_Init(USART2, TM_USART_PinsPack_1, 9600);
    
    TM_USART_Puts(USART2, "Hello world\n");
    
    while (1) {
 
        /* Get character from internal buffer */
        c = TM_USART_Getc(USART2);
        if (c) {
            /* If anything received, put it back to terminal */
            TM_USART_Putc(USART2, c);
        }
    }
}
 
cs


// BLE USART Scanf

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
/**
  ******************************************************************************
  * @file    main.c
  * @author  Ac6
  * @version V1.0
  * @date    01-December-2013
  * @brief   Default main function.
  ******************************************************************************
*/
 
#include "stm32f4xx.h"
/* Include my libraries here */
//#include "defines.h"
#include "tm_stm32f4_usart.h"
 
char inputbuf[255];
int bufsize = 0;
 
char* ReadUartStr()
{
    int c;
    do {
        c = TM_USART_Getc(USART2);
        if (c) {
            if (c=='\n')
            {
                inputbuf[bufsize] = 0;
                bufsize=0;
                return inputbuf;
            }
            inputbuf[bufsize++= c;
        }
    } while (c);
    return 0;
}
 
int main(void) {
    uint8_t c;
 
    TM_USART_Init(USART2, TM_USART_PinsPack_1, 9600);
    TM_USART_Puts(USART2, "Hello world\n");
 
    int input;
    while (1) {
        char* str = ReadUartStr();
        if(str)
        {
            sscanf(str, "%d"&input);
            if (input == 1)
            {
                TM_USART_Puts(USART2, "hi");
            }
            if (input == 2)
            {
                TM_USART_Puts(USART2, "low");
            }
 
            //TM_USART_Puts(USART2, str);
        }
    }
}
 
 
 
 
cs


반응형

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

[Elevator(ARM)] Flash memory  (0) 2018.10.26
[Elevator(ARM)] ADC  (0) 2018.10.25
[Elevator(ARM)] key Matrix  (0) 2018.10.25
[Elevator(ARM)] DoorLock  (0) 2018.10.25
[Elevator(ARM)] Calculator  (0) 2018.10.25
반응형

// 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
 
 
#include "stm32f4xx.h"
 
#define KM_INT_1 0b10001000
#define KM_INT_2 0b10000100
#define KM_INT_3 0b10000010
#define KM_INT_4 0b10001001
#define KM_INT_5 0b01001000
 
void matrix_init()
{
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    GPIO_InitTypeDef GPIO_Initstruct;
    GPIO_Initstruct.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
    GPIO_Initstruct.GPIO_OType = GPIO_OType_PP;
    GPIO_Initstruct.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_Initstruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Initstruct.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_Init(GPIOA, &GPIO_Initstruct);
    GPIO_Initstruct.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_Initstruct.GPIO_Mode = GPIO_Mode_IN;
    GPIO_Initstruct.GPIO_PuPd = GPIO_PuPd_DOWN;
    GPIO_Init(GPIOA, &GPIO_Initstruct);
}
 
void simple_delay(int count)
{
    int i;
    for(i=0; i<count; i++);
}
 
int GetKey()
{
    int i;
    for(i=0; i<4; i++)
    {
        GPIO_ResetBits(GPIOA, 0b1111 << 8);
        GPIO_SetBits(GPIOA, 1 << i << 8);
        simple_delay(5);
        int key = (GPIO_ReadInputData(GPIOA) >> 4& 0b1111;
        if (key != 0)
        {
            return 1 << 4 << i | key;
        }
    }
    return 0;
}
 
int main(void)
{
    matrix_init();
    for(;;)
    {
        int key = GetKey();
 
        if (key == KM_INT_1)
        {
            // 내부 1층이 입력되었습니다.
        }
    }
}
 
 
cs


반응형

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

[Elevator(ARM)] ADC  (0) 2018.10.25
[Elevator(ARM)] BLE USART  (0) 2018.10.25
[Elevator(ARM)] DoorLock  (0) 2018.10.25
[Elevator(ARM)] Calculator  (0) 2018.10.25
[Elevator(ARM)] Coretex-M4 Elevator  (0) 2018.10.18
반응형

/** 도어락 프로젝트

- 도어락 프로그램을 구현하세요.

1. Up버튼을 누른 후 비밀번호를 누르고 다시 Up버튼을 누르면 도어락이 열린다.

 ->100ms 간 buzzer를 켠다.

 -> 도어락이 열리면 LCD에 'Door: Open'

 2. Down 버튼을 누르고 새 비밀번호를 입력하고 Down 버튼을 누르면 비밀번호가 바뀐다.

 -> Buzzer 100ms On/ LCD에 'Password Chaged' 표시.

 3. 열림버튼을 누르면 도어락이 열린다.

 -> Buzzer 100ms On

 4. 닫힘 버튼을 누르면 도어락이 닫힌다.

 -> Buzzer 100ms On

 5. 도어락이 '열린' 상태에서 10초가 지나면 자동으로 닫힌다.

*/


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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/**
 ******************************************************************************
 * @file    main.c
 * @author  Ac6
 * @version V1.0
 * @date    01-December-2013
 * @brief   Default main function.
 ******************************************************************************
 */
 
#include "stm32f4xx.h"
#include "stm32f4xx_adc.h"
#include "lcd.h"
 
#define FND0 0b0011111100000000
#define FND1 0b0000011000000000
#define FND2 0b0101101100000000
#define FND3 0b0100111100000000
#define FND4 0b0110011000000000
#define FND5 0b0110110100000000
#define FND6 0b0111110100000000
#define FND7 0b0010011100000000
#define FND8 0b0111111100000000
#define FND9 0b0110111100000000
 
#define LED1 0b0000000000000001
#define LED2 0b0000000000000010
#define LED3 0b0000000000000100
#define LED4 0b0000000000001000
#define LED5 0b0000000000010000
 
#define KEY_Down 0b10001000 // Down
#define KEY_Up 0b10000100 // Up
#define KEY_OUT_5 0b10000010 // 외부 5층
#define KEY_OUT_4 0b10000001 // 외부 4층
#define KEY_OUT_3 0b01001000 // 외부 3층
#define KEY_OUT_2 0b01000100 // 외부 2층
#define KEY_OUT_1 0b01000010 // 외부 1층
#define KEY_TitleSet 0b01000001 // Title Set
#define KEY_Emergency 0b00101000 // 비상
#define KEY_Open 0b00100100 // 열림
#define KEY_Close 0b00100010 // 닫힘
#define KEY_IN_5 0b00100001 // 내부 5층
#define KEY_IN_4 0b00011000 // 내부 4층
#define KEY_IN_3 0b00010100 // 내부 3층
#define KEY_IN_2 0b00010010 // 내부 2층
#define KEY_IN_1 0b00010001 // 내부 1층
 
#define buzzer_pin GPIO_Pin_1
#define buzzer_port GPIOB
 
#define photo_port GPIOD
#define photo_pin GPIO_Pin_2
 
volatile int mstime = 0;
volatile char str[255];
volatile int count = 0;
volatile int door = 0;
void SysTick_Handler(void) {
    mstime++;
}
void delay(int ms) {
    volatile int future = ms + mstime;
    while (future > mstime)
        ;
}
void buzzer(int a) {
    GPIO_SetBits(GPIOB, GPIO_Pin_1);
    delay(a);
    GPIO_ResetBits(GPIOB, GPIO_Pin_1);
    delay(a);
}
void FND_display(int num) {
 
    GPIO_SetBits(GPIOC,
            GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12
                    | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
    switch (num) {
    case 0:
        GPIO_ResetBits(GPIOC, (0b1111111100000000 & FND0));
        break;
    case 1:
        GPIO_ResetBits(GPIOC, (0b1111111100000000 & FND1));
        break;
    case 2:
        GPIO_ResetBits(GPIOC, (0b1111111100000000 & FND2));
        break;
    case 3:
        GPIO_ResetBits(GPIOC, (0b1111111100000000 & FND3));
        break;
    case 4:
        GPIO_ResetBits(GPIOC, 0b1111111100000000 & FND4);
        break;
    case 5:
        GPIO_ResetBits(GPIOC, 0b1111111100000000 & FND5);
        break;
    case 6:
        GPIO_ResetBits(GPIOC, 0b1111111100000000 & FND6);
        break;
    case 7:
        GPIO_ResetBits(GPIOC, 0b1111111100000000 & FND7);
        break;
    case 8:
        GPIO_ResetBits(GPIOC, 0b1111111100000000 & FND8);
        break;
    case 9:
        GPIO_ResetBits(GPIOC, 0b1111111100000000 & FND9);
        break;
    }
}
void LED(int num) {
    GPIO_SetBits(GPIOC,
    GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4);
    switch (num) {
    case 1:
        GPIO_ResetBits(GPIOC, 0b0000000000011111 & LED1);
        break;
    case 2:
        GPIO_ResetBits(GPIOC, 0b0000000000011111 & LED2);
        break;
    case 3:
        GPIO_ResetBits(GPIOC, 0b0000000000011111 & LED3);
        break;
    case 4:
        GPIO_ResetBits(GPIOC, 0b0000000000011111 & LED4);
        break;
    case 5:
        GPIO_ResetBits(GPIOC, 0b0000000000011111 & LED5);
        break;
    }
}
void getKey_init() {
    GPIO_InitTypeDef GPIO_InitStructure;
 
    RCC_AHB1PeriphClockCmd(
            RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC
                    | RCC_AHB1Periph_GPIOD, ENABLE); // GPIO 클럭 인가
    // Key_Row Output set
    GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10
            | GPIO_Pin_11);
    GPIO_Init(GPIOA, &GPIO_InitStructure);
 
    // KEY_Col Input set
    GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6
            | GPIO_Pin_7);
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // push pull
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
}
char getKey() {
    char key = 0xff;
    GPIO_SetBits(GPIOA, GPIO_Pin_8);
    delay(5);
    if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_4) == 1)
        key = '0';
    else if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5) == 1)
        key = '1';
    else if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6) == 1)
        key = '2';
    else if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_7) == 1)
        key = '3';
    GPIO_ResetBits(GPIOA, GPIO_Pin_8);
    GPIO_SetBits(GPIOA, GPIO_Pin_9);
    delay(5);
    if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_4) == 1)
        key = '4';
    else if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5) == 1)
        key = '5';
    else if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6) == 1)
        key = '6';
    else if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_7) == 1)
        key = '7';
    GPIO_ResetBits(GPIOA, GPIO_Pin_9);
    GPIO_SetBits(GPIOA, GPIO_Pin_10);
    delay(5);
    if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_4) == 1)
        key = '8';
    else if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5) == 1)
        key = '9';
    else if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6) == 1)
        key = 'A';
    else if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_7) == 1)
        key = 'B';
    GPIO_ResetBits(GPIOA, GPIO_Pin_10);
    GPIO_SetBits(GPIOA, GPIO_Pin_11);
    delay(5);
    if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_4) == 1)
        key = 'C';
    else if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5) == 1)
        key = 'D';
    else if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6) == 1)
        key = 'E';
    else if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_7) == 1)
        key = 'F';
    GPIO_ResetBits(GPIOA, GPIO_Pin_11);
    delay(5);
    return key;
}
void ADC_init() {
 
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
    GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN;
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOB, &GPIO_InitStruct);
 
    ADC_InitTypeDef ADC_InitStruct;
    ADC_InitStruct.ADC_ContinuousConvMode = DISABLE;
    ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_InitStruct.ADC_ExternalTrigConv = DISABLE;
    ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
    ADC_InitStruct.ADC_NbrOfConversion = 1;
    ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b;
    ADC_InitStruct.ADC_ScanConvMode = DISABLE;
    ADC_Init(ADC1, &ADC_InitStruct);
 
    ADC_Cmd(ADC1, ENABLE);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_84Cycles);
 
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_AHB1PeriphClockCmd(
            RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC
                    | RCC_AHB1Periph_GPIOD, ENABLE); // GPIO 클럭 인가
 
    GPIO_InitStructure.GPIO_Pin =
    // LED Output set
            (GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4
                    |
                    // FND Output set
                    GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11
                    | 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(GPIOC, &GPIO_InitStructure);
 
}
int ADC_Read() {
    ADC_SoftwareStartConv(ADC1);
    while (!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC))
        ;
    return ADC_GetConversionValue(ADC1);
}
void init() {
    GPIO_InitTypeDef GPIO_InitStructure;
 
    RCC_AHB1PeriphClockCmd(
            RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC
                    | RCC_AHB1Periph_GPIOD, ENABLE); // GPIO 클럭 인가
 
    GPIO_InitStructure.GPIO_Pin =
    // LED Output set
            (GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4
                    |
                    // FND Output set
                    GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11
                    | 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(GPIOC, &GPIO_InitStructure);
 
    // Buzzer Output set
    GPIO_InitStructure.GPIO_Pin = buzzer_pin;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
 
    // LED default set
    GPIO_SetBits(GPIOC,
    GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4);
    // FND default set
    GPIO_SetBits(GPIOC,
            GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12
                    | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
 
    // PD2 Photo Interrupt set
    GPIO_InitStructure.GPIO_Pin = photo_pin;
    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);
}
 
void matrix_init() {
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    GPIO_InitTypeDef GPIO_Initstruct;
    GPIO_Initstruct.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10
            | GPIO_Pin_11;
    GPIO_Initstruct.GPIO_OType = GPIO_OType_PP;
    GPIO_Initstruct.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_Initstruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Initstruct.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_Init(GPIOA, &GPIO_Initstruct);
    GPIO_Initstruct.GPIO_Pin =
    GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_Initstruct.GPIO_Mode = GPIO_Mode_IN;
    GPIO_Initstruct.GPIO_PuPd = GPIO_PuPd_DOWN;
    GPIO_Init(GPIOA, &GPIO_Initstruct);
}
 
void simple_delay(int count) {
    int i;
    for (i = 0; i < count; i++)
        ;
}
 
int GetKey(int prevKey) {
    int i;
    for (i = 0; i < 4; i++) {
        GPIO_ResetBits(GPIOA, 0b1111 << 8);
        GPIO_SetBits(GPIOA, 1 << i << 8);
        simple_delay(40);
        int key = (GPIO_ReadInputData(GPIOA) >> 4& 0b1111;
        if (key != 0) {
            int retKey = 1 << 4 << i | key;
            if (retKey != prevKey)
                return retKey;
        }
    }
    return 0;
}
void motor_init() {
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
    GPIO_InitTypeDef GPIO_Initstruct;
    GPIO_Initstruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_Initstruct.GPIO_OType = GPIO_OType_PP;
    GPIO_Initstruct.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_Initstruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Initstruct.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_Init(GPIOB, &GPIO_Initstruct);
}
void motorForward(int time) {
    GPIO_SetBits(GPIOB, GPIO_Pin_6 | GPIO_Pin_7);
    GPIO_ResetBits(GPIOB, GPIO_Pin_6);
    delay(time);
    GPIO_SetBits(GPIOB, GPIO_Pin_6);
}
void motorBackward(int time) {
    GPIO_SetBits(GPIOB, GPIO_Pin_6 | GPIO_Pin_7);
    GPIO_ResetBits(GPIOB, GPIO_Pin_7);
    delay(time);
    GPIO_SetBits(GPIOB, GPIO_Pin_7);
}
int main(void) {
    init();
    SysTick_Config(53676000 / 1000);
    matrix_init();
    lcd_init();
    motor_init();
 
    int value = 0;
    int Passvalue = 0;
    int mode = 0;
    int password = 0;
    int passchangemode = 0;
    int CorrectPassword = 1;
    int timeflag = 0;
    int timeValue = 0;
    for (;;) {
        int key = GetKey(key);
        if (door == 0) {
            lcd_printxy(00"[Door Close]");
        } else if (door == 1) {
            lcd_printxy(00"[Door Open]");
            if (timeflag == 0) {
                timeValue = mstime;
                timeflag = 1;
            } else if (timeflag == 1) {
                if ((mstime - timeValue) > 10000) {
                    door = 0;
                    buzzer(300);
                    motorBackward(1000);
                }
            }
        }
    if (key == KEY_Open) {
        if (door == 0) {
            door = 1;timeValue = mstime;
            motorForward(1000);
            buzzer(100);
            lcd_printxy(00"                ");
        }
    }
    if (key == KEY_Close) {
        if (door == 1) {
            door = 0;
            motorBackward(1000);
            buzzer(300);
            lcd_printxy(00"[Door Close]");
            lcd_printxy(00"                ");
        }
    }
    if (key == KEY_Up) {
        if (door == 0) {
            mode++;
            if (mode > 1) {
                mode = 0;
                if (password == CorrectPassword) {
                    door = 1;timeValue = mstime;
                    buzzer(100);
                    motorForward(1000);
                }
                value = 0;
                lcd_printxy(00"                ");
                lcd_printxy(01"                ");
            }
        }
    }
    if (key == KEY_Down) {
        if (door == 1) {
            passchangemode++;
            if (passchangemode > 1) {
                passchangemode = 0;
                Passvalue = 0;
                buzzer(100);
                lcd_printxy(01"                ");
                lcd_printxy(01"Password changed");
                lcd_printxy(01"                ");
            }
        }
    }
    if (passchangemode == 1) {
        lcd_printxy(01"Pass:");
        sprintf(str, "%d", Passvalue);
        lcd_printxy(51, str);
        if (key == KEY_IN_5) {
            Passvalue = Passvalue * 10 + 1;
            sprintf(str, "%d", Passvalue);
            lcd_printxy(51, str);
            buzzer(10);
        }
        if (key == KEY_OUT_5) {
            Passvalue = Passvalue * 10 + 2;
            sprintf(str, "%d", Passvalue);
            lcd_printxy(51, str);
            buzzer(10);
        }
        if (key == KEY_IN_4) {
            Passvalue = Passvalue * 10 + 3;
            sprintf(str, "%d", Passvalue);
            lcd_printxy(51, str);
            buzzer(10);
        }
        if (key == KEY_OUT_4) {
            Passvalue = Passvalue * 10 + 4;
            sprintf(str, "%d", Passvalue);
            lcd_printxy(51, str);
            buzzer(10);
        }
        if (key == KEY_IN_3) {
            Passvalue = Passvalue * 10 + 5;
            sprintf(str, "%d", Passvalue);
            lcd_printxy(51, str);
            buzzer(10);
        }
        if (key == KEY_OUT_3) {
            Passvalue = Passvalue * 10 + 6;
            sprintf(str, "%d", Passvalue);
            lcd_printxy(51, str);
            buzzer(10);
        }
        if (key == KEY_IN_2) {
            Passvalue = Passvalue * 10 + 7;
            sprintf(str, "%d", Passvalue);
            lcd_printxy(51, str);
            buzzer(10);
        }
        if (key == KEY_OUT_2) {
            Passvalue = Passvalue * 10 + 8;
            sprintf(str, "%d", Passvalue);
            lcd_printxy(51, str);
            buzzer(10);
        }
        if (key == KEY_IN_1) {
            Passvalue = Passvalue * 10 + 9;
            sprintf(str, "%d", Passvalue);
            lcd_printxy(51, str);
            buzzer(10);
        }
        if (key == KEY_OUT_1) {
            Passvalue = Passvalue * 10 + 0;
            sprintf(str, "%d", Passvalue);
            lcd_printxy(51, str);
            buzzer(10);
        }
        CorrectPassword = Passvalue;
    }
    if (mode == 1) {
        lcd_printxy(01"Pass:");
        if (key == KEY_IN_5) {
            value = value * 10 + 1;
            sprintf(str, "%d", value);
            lcd_printxy(51, str);
            buzzer(10);
        }
        if (key == KEY_OUT_5) {
            value = value * 10 + 2;
            sprintf(str, "%d", value);
            lcd_printxy(51, str);
            buzzer(10);
        }
        if (key == KEY_IN_4) {
            value = value * 10 + 3;
            sprintf(str, "%d", value);
            lcd_printxy(51, str);
            buzzer(10);
        }
        if (key == KEY_OUT_4) {
            value = value * 10 + 4;
            sprintf(str, "%d", value);
            lcd_printxy(51, str);
            buzzer(10);
        }
        if (key == KEY_IN_3) {
            value = value * 10 + 5;
            sprintf(str, "%d", value);
            lcd_printxy(51, str);
            buzzer(10);
        }
        if (key == KEY_OUT_3) {
            value = value * 10 + 6;
            sprintf(str, "%d", value);
            lcd_printxy(51, str);
            buzzer(10);
        }
        if (key == KEY_IN_2) {
            value = value * 10 + 7;
            sprintf(str, "%d", value);
            lcd_printxy(51, str);
            buzzer(10);
        }
        if (key == KEY_OUT_2) {
            value = value * 10 + 8;
            sprintf(str, "%d", value);
            lcd_printxy(51, str);
            buzzer(10);
        }
        if (key == KEY_IN_1) {
            value = value * 10 + 9;
            sprintf(str, "%d", value);
            lcd_printxy(51, str);
            buzzer(10);
        }
        if (key == KEY_OUT_1) {
            value = value * 10 + 0;
            sprintf(str, "%d", value);
            lcd_printxy(51, str);
            buzzer(10);
        }
        password = value;
    }
}
}
 
cs


반응형

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

[Elevator(ARM)] BLE USART  (0) 2018.10.25
[Elevator(ARM)] key Matrix  (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
반응형


/** Calculator project

숫자를 입력받고, +-* / 사칙연산을 입력받고,

다시 숫자를 입력받고, = 혹은 사칙연산자를 입력하면 결과가 LCD액정에 출력된다.

*0으로 나누었을 때는 1초동안 Error 라고 표시하면서 Buzzer소리 출력

열림 -> +

닫힘 -> -

Up   -> *

Down -> /

Title set -> =

*/


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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
 
/**
 ******************************************************************************
 * @file    main.c
 * @author  Ac6
 * @version V1.0
 * @date    01-December-2013
 * @brief   Default main function.
 ******************************************************************************
 */
 
#include "stm32f4xx.h"
#include "stm32f4xx_adc.h"
#include "lcd.h"
 
#define FND0 0b0011111100000000
#define FND1 0b0000011000000000
#define FND2 0b0101101100000000
#define FND3 0b0100111100000000
#define FND4 0b0110011000000000
#define FND5 0b0110110100000000
#define FND6 0b0111110100000000
#define FND7 0b0010011100000000
#define FND8 0b0111111100000000
#define FND9 0b0110111100000000
 
#define LED1 0b0000000000000001
#define LED2 0b0000000000000010
#define LED3 0b0000000000000100
#define LED4 0b0000000000001000
#define LED5 0b0000000000010000
 
#define KEY_Down 0b10001000 // Down
#define KEY_Up 0b10000100 // Up
#define KEY_OUT_5 0b10000010 // 외부 5층
#define KEY_OUT_4 0b10000001 // 외부 4층
#define KEY_OUT_3 0b01001000 // 외부 3층
#define KEY_OUT_2 0b01000100 // 외부 2층
#define KEY_OUT_1 0b01000010 // 외부 1층
#define KEY_TitleSet 0b01000001 // Title Set
#define KEY_Emergency 0b00101000 // 비상
#define KEY_Open 0b00100100 // 열림
#define KEY_Close 0b00100010 // 닫힘
#define KEY_IN_5 0b00100001 // 내부 5층
#define KEY_IN_4 0b00011000 // 내부 4층
#define KEY_IN_3 0b00010100 // 내부 3층
#define KEY_IN_2 0b00010010 // 내부 2층
#define KEY_IN_1 0b00010001 // 내부 1층
 
#define buzzer_pin GPIO_Pin_1
#define buzzer_port GPIOB
 
#define photo_port GPIOD
#define photo_pin GPIO_Pin_2
 
volatile int mstime = 0;
volatile char str[255];
volatile int count =0;
void SysTick_Handler(void) {
    mstime++;
}
void delay(int ms) {
    volatile int future = ms + mstime;
    while (future > mstime)
        ;
}
void buzzer() {
    GPIO_SetBits(GPIOB, GPIO_Pin_1);
    delay(500);
    GPIO_ResetBits(GPIOB, GPIO_Pin_1);
    delay(500);
}
void FND_display(int num) {
 
    GPIO_SetBits(GPIOC,
            GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12
                    | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
    switch (num) {
    case 0:
        GPIO_ResetBits(GPIOC, (0b1111111100000000 & FND0));
        break;
    case 1:
        GPIO_ResetBits(GPIOC, (0b1111111100000000 & FND1));
        break;
    case 2:
        GPIO_ResetBits(GPIOC, (0b1111111100000000 & FND2));
        break;
    case 3:
        GPIO_ResetBits(GPIOC, (0b1111111100000000 & FND3));
        break;
    case 4:
        GPIO_ResetBits(GPIOC, 0b1111111100000000 & FND4);
        break;
    case 5:
        GPIO_ResetBits(GPIOC, 0b1111111100000000 & FND5);
        break;
    case 6:
        GPIO_ResetBits(GPIOC, 0b1111111100000000 & FND6);
        break;
    case 7:
        GPIO_ResetBits(GPIOC, 0b1111111100000000 & FND7);
        break;
    case 8:
        GPIO_ResetBits(GPIOC, 0b1111111100000000 & FND8);
        break;
    case 9:
        GPIO_ResetBits(GPIOC, 0b1111111100000000 & FND9);
        break;
    }
}
void LED(int num) {
    GPIO_SetBits(GPIOC,
    GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4);
    switch (num) {
    case 1:
        GPIO_ResetBits(GPIOC, 0b0000000000011111 & LED1);
        break;
    case 2:
        GPIO_ResetBits(GPIOC, 0b0000000000011111 & LED2);
        break;
    case 3:
        GPIO_ResetBits(GPIOC, 0b0000000000011111 & LED3);
        break;
    case 4:
        GPIO_ResetBits(GPIOC, 0b0000000000011111 & LED4);
        break;
    case 5:
        GPIO_ResetBits(GPIOC, 0b0000000000011111 & LED5);
        break;
    }
}
void getKey_init() {
    GPIO_InitTypeDef GPIO_InitStructure;
 
    RCC_AHB1PeriphClockCmd(
            RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC
                    | RCC_AHB1Periph_GPIOD, ENABLE); // GPIO 클럭 인가
    // Key_Row Output set
    GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10
            | GPIO_Pin_11);
    GPIO_Init(GPIOA, &GPIO_InitStructure);
 
    // KEY_Col Input set
    GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6
            | GPIO_Pin_7);
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // push pull
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
}
char getKey() {
    char key = 0xff;
    GPIO_SetBits(GPIOA, GPIO_Pin_8);
    delay(5);
    if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_4) == 1)
        key = '0';
    else if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5) == 1)
        key = '1';
    else if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6) == 1)
        key = '2';
    else if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_7) == 1)
        key = '3';
    GPIO_ResetBits(GPIOA, GPIO_Pin_8);
    GPIO_SetBits(GPIOA, GPIO_Pin_9);
    delay(5);
    if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_4) == 1)
        key = '4';
    else if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5) == 1)
        key = '5';
    else if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6) == 1)
        key = '6';
    else if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_7) == 1)
        key = '7';
    GPIO_ResetBits(GPIOA, GPIO_Pin_9);
    GPIO_SetBits(GPIOA, GPIO_Pin_10);
    delay(5);
    if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_4) == 1)
        key = '8';
    else if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5) == 1)
        key = '9';
    else if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6) == 1)
        key = 'A';
    else if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_7) == 1)
        key = 'B';
    GPIO_ResetBits(GPIOA, GPIO_Pin_10);
    GPIO_SetBits(GPIOA, GPIO_Pin_11);
    delay(5);
    if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_4) == 1)
        key = 'C';
    else if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5) == 1)
        key = 'D';
    else if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6) == 1)
        key = 'E';
    else if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_7) == 1)
        key = 'F';
    GPIO_ResetBits(GPIOA, GPIO_Pin_11);
    delay(5);
    return key;
}
void ADC_init() {
 
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
    GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN;
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOB, &GPIO_InitStruct);
 
    ADC_InitTypeDef ADC_InitStruct;
    ADC_InitStruct.ADC_ContinuousConvMode = DISABLE;
    ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_InitStruct.ADC_ExternalTrigConv = DISABLE;
    ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
    ADC_InitStruct.ADC_NbrOfConversion = 1;
    ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b;
    ADC_InitStruct.ADC_ScanConvMode = DISABLE;
    ADC_Init(ADC1, &ADC_InitStruct);
 
    ADC_Cmd(ADC1, ENABLE);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_84Cycles);
 
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_AHB1PeriphClockCmd(
            RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC
                    | RCC_AHB1Periph_GPIOD, ENABLE); // GPIO 클럭 인가
 
    GPIO_InitStructure.GPIO_Pin =
    // LED Output set
            (GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4
                    |
                    // FND Output set
                    GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11
                    | 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(GPIOC, &GPIO_InitStructure);
 
}
int ADC_Read() {
    ADC_SoftwareStartConv(ADC1);
    while (!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC))
        ;
    return ADC_GetConversionValue(ADC1);
}
void init() {
    GPIO_InitTypeDef GPIO_InitStructure;
 
    RCC_AHB1PeriphClockCmd(
            RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC
                    | RCC_AHB1Periph_GPIOD, ENABLE); // GPIO 클럭 인가
 
    GPIO_InitStructure.GPIO_Pin =
    // LED Output set
            (GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4
                    |
                    // FND Output set
                    GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11
                    | 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(GPIOC, &GPIO_InitStructure);
 
    // Buzzer Output set
    GPIO_InitStructure.GPIO_Pin = buzzer_pin;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
 
    // LED default set
    GPIO_SetBits(GPIOC,
    GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4);
    // FND default set
    GPIO_SetBits(GPIOC,
            GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12
                    | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
 
    // PD2 Photo Interrupt set
    GPIO_InitStructure.GPIO_Pin = photo_pin;
    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);
}
 
void matrix_init() {
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    GPIO_InitTypeDef GPIO_Initstruct;
    GPIO_Initstruct.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10
            | GPIO_Pin_11;
    GPIO_Initstruct.GPIO_OType = GPIO_OType_PP;
    GPIO_Initstruct.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_Initstruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Initstruct.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_Init(GPIOA, &GPIO_Initstruct);
    GPIO_Initstruct.GPIO_Pin =
    GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_Initstruct.GPIO_Mode = GPIO_Mode_IN;
    GPIO_Initstruct.GPIO_PuPd = GPIO_PuPd_DOWN;
    GPIO_Init(GPIOA, &GPIO_Initstruct);
}
 
void simple_delay(int count) {
    int i;
    for (i = 0; i < count; i++)
        ;
}
 
int GetKey(int prevKey) {
    int i;
    for (i = 0; i < 4; i++) {
        GPIO_ResetBits(GPIOA, 0b1111 << 8);
        GPIO_SetBits(GPIOA, 1 << i << 8);
        simple_delay(40);
        int key = (GPIO_ReadInputData(GPIOA) >> 4& 0b1111;
        if (key != 0) {
            int retKey= 1 << 4 << i | key;
            if(retKey != prevKey)
                return retKey;
        }
    }
    return 0;
}
 
int main(void) {
    init();
    SysTick_Config(53676000 / 1000);
    matrix_init();
    lcd_init();
    int j = 0;
    int value =0;
    int result=0;
    int data1=0;
    int error =0;
    char yun;
    lcd_printxy(0,0,"Calculator!");
 
    for (;;) {
        int key = GetKey(key);
        GPIO_ResetBits(GPIOB, GPIO_Pin_1);
        if (key == KEY_IN_5){
            lcd_printxy(0,1,"                ");
            value = value*10 +1;sprintf(str,"%d",value);lcd_printxy(0,1,str);delay(1000);
        }
        if (key == KEY_OUT_5){
            lcd_printxy(0,1,"                ");
            value = value*10+2;sprintf(str,"%d",value);lcd_printxy(0,1,str);delay(1000);}
        if (key == KEY_IN_4){
            lcd_printxy(0,1,"                ");
            value = value*10+3;sprintf(str,"%d",value);lcd_printxy(0,1,str);delay(1000);}
        if (key == KEY_OUT_4){
            lcd_printxy(0,1,"                ");
            value = value*10+4;sprintf(str,"%d",value);lcd_printxy(0,1,str);delay(1000);}
        if (key == KEY_IN_3){
            lcd_printxy(0,1,"                ");
            value = value*10+5;sprintf(str,"%d",value);lcd_printxy(0,1,str);delay(1000);}
        if (key == KEY_OUT_3){
            lcd_printxy(0,1,"                ");
            value = value*10+6;sprintf(str,"%d",value);lcd_printxy(0,1,str);delay(1000);}
        if (key == KEY_IN_2){
            lcd_printxy(0,1,"                ");
            value = value*10+7;sprintf(str,"%d",value);lcd_printxy(0,1,str);delay(1000);}
        if (key == KEY_OUT_2){
            lcd_printxy(0,1,"                ");
            value = value*10+8;sprintf(str,"%d",value);lcd_printxy(0,1,str);delay(1000);}
        if (key == KEY_IN_1){
            lcd_printxy(0,1,"                ");
            value = value*10+9;sprintf(str,"%d",value);lcd_printxy(0,1,str);delay(1000);}
        if (key == KEY_OUT_1){
            lcd_printxy(0,1,"                ");
            value = value*10;sprintf(str,"%d",value);lcd_printxy(0,1,str);delay(1000);}
        if (key==KEY_Open){
            lcd_printxy(0,1,"                ");
            lcd_printxy(0,1,"+");delay(1000);
            data1= value;
            value =0;
            yun = '+';
        }
        if(key == KEY_Close){
            lcd_printxy(0,1,"                ");
            lcd_printxy(0,1,"-");delay(1000);
            data1= value;
            value =0;
            yun='-';
        }
        if(key == KEY_Up){
            lcd_printxy(0,1,"                ");
            lcd_printxy(0,1,"*");delay(1000);
            data1= value;
            value =0;
            yun='*';
        }
        if(key == KEY_Down){
            lcd_printxy(0,1,"                ");
            lcd_printxy(0,1,"/");delay(1000);
            data1= value;
            value =0;
            if (value==0){
                error=1;
            }
            else {
                yun='/';
            }
        }
        if(key == KEY_TitleSet){
        lcd_printxy(0,1,"                ");
            if(error==1) {
                lcd_printxy(0,1,"Error");
                GPIO_SetBits(GPIOB, GPIO_Pin_1);
                delay(1000);
                error=0;
            }
        lcd_printxy(0,1,"                ");
        switch(yun){
        case '+':
            result = data1+value;
            break;
        case '-':
            result = data1-value;
            break;
        case '*':
            result = data1*value;
            break;
        case '/':
            result = data1/value;
            break;
        }
        sprintf(str,"%d",result);lcd_printxy(0,1,str);delay(1000);
        data1 = result;
        }
 
    }
}
 
cs


반응형

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

[Elevator(ARM)] key Matrix  (0) 2018.10.25
[Elevator(ARM)] DoorLock  (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
반응형

/**

동작 원리 : FND를 이용하여 층수를 표시하며, 내부와 외부로 구분, Motor의 정회전은 문 열림, 역회전은 문 닫힘, Door에 물체 감지 기능이 있으며, 무게 초과 확인 기능이 있다.

 

개발 환경 : System Workbench for STM32

 

주요 기능 : LED GPIO, Key Matrix, FND, Buzzer, char LCD, B/T Module, Serial 통신, DC motor

*/

/*

Header File

1.attributes.h

2.lcd.h

3.tm_stm32f4_gpio.c

4.tm_stm32f4_gpio.h

5.tm_stm32f4_usart.c

6.tm_stm32f4_usart.h


attributes.h

lcd.h

tm_stm32f4_gpio.c

tm_stm32f4_gpio.h

tm_stm32f4_usart.c

tm_stm32f4_usart.h


*.h파일은 inc에 copy

*.c파일은 src에 copy


//설계 가이드

엘레베이터 프로그램 설계(ARM)-학생자습용.pdf


*/




반응형

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

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

Arm trainning kit


1. 키와 몸무게를 이력받아 BMI를 계산하는 프로그램을 작성하시오


캐릭터 LCD에 메뉴를 표시한다.

상태1 

[1. INPUT WEIGHT]


상태2

[2. INPUT HEINGT]


상태3

[3. CALC BMI]


0번 버튼 : 상

1번 버튼 : 하

2번 버튼 : 선택

3번 버튼 : 취소

------------------------------------------------------------


1. 상/하 버튼을 누르면 상태가 바뀜. 상태1 > 상태2 > 상태3 


2. 1번 상태에서 버튼을 누르면 

 상태1

 [50] +-

 상/하 버튼을 입력하여 몸무게 증감. 

 [선택]을 누르면 몸무게가 셋팅 되고 1번 상태로 돌아감

 [취소]을 누르면 취소되고 1로 돌아감


 상태2

  상/하 버튼을 입력하여 키 증감. 

 [선택]을 누르면 키가 셋팅 되고 1번 상태로 돌아감

 [취소]을 누르면 취소되고 1로 돌아감


 상태3

 Your BMI is 

 BMI Calc = w/(t^2) (단위, t = m)

 >= 30 비만, 25>= 과체중

 < 18.5 저체중

 Over weight

 Nomal weight

 Under weignt

 [취소]을 누르면 1로 돌아감 // 해당 기능 구현 필요


// LCD.H


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
#include "stm32f4xx.h"
 
void lcd_delay_us(u32 nCount) {
 int i,tmp;
 for(; nCount !=0; nCount--)
    {
       for(i=0; i<10; i++) tmp += i;
    }
}
 
void lcd_delay_ms(u32 nCount)
{
    //delay(nCount);
   for(; nCount !=0; nCount--)
      lcd_delay_us(1000);
}
 
void lcd_command(unsigned char command); // write a command(instruction) to text
void lcd_data(unsigned char data); // display a character on text LCD
void lcd_init(void); // initialize text LCD module
void lcd_string(unsigned char command, char *string);
 
//CLCD Control Port set
#define CLCD_PORT GPIOE //CLCD CONTROL PORT SET
#define Rs(x) CLCD_PORT->ODR=((CLCD_PORT->ODR&~(GPIO_Pin_8))|(x<<8)) //CLCD Rs : PE8
#define E(x) CLCD_PORT->ODR=((CLCD_PORT->ODR&~(GPIO_Pin_10))|(x<<10)) //CLCD E : PE10
#define CLCD_data(x) CLCD_PORT->ODR=((CLCD_PORT->ODR&(~(GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15)))|(x<<12 )) //CLCD Data bus : PE12~PE15
 
void lcd_init(void) { // initialize text LCD module (4BIT)
   int Cont;
 
   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
 
   GPIO_InitTypeDef GPIO_PORTE; //GPIO 설정관련 구조체 선언
   GPIO_PORTE.GPIO_Speed = GPIO_Speed_50MHz;
   GPIO_PORTE.GPIO_Mode = GPIO_Mode_OUT;
   GPIO_PORTE.GPIO_OType = GPIO_OType_PP;
   GPIO_PORTE.GPIO_Pin = GPIO_Pin_All;
   GPIO_Init(CLCD_PORT, &GPIO_PORTE);  //PORTE 설정 메모리번지를 구조체 포인터 지시
   lcd_delay_ms(30); //30ms
   E(0), Rs(0); CLCD_data(0x30); //Functon set
   lcd_delay_ms(6); //4.1ms
   E(1);
   for(Cont=0;Cont<2;Cont++) { //Functon set loop
      E(0);
      lcd_delay_us(150); // 100us
      E(1);
   }
   lcd_delay_us(50); //50
 
   CLCD_data(0x20);
   E(1);
   lcd_delay_us(150); //150
   E(0);
   lcd_delay_us(50); //50
   lcd_command(0x28); // function set(4 bit, 2 line, 5x7 dot)
   lcd_command(0x0C); // display control(display ON, cursor OFF)
   lcd_command(0x06); // entry mode set(increment, not shift)
   lcd_command(0x01); // clear display
   lcd_delay_ms(10); //4
}
 
void lcd_command(unsigned char command) { // write a command(instruction) to text
//상위 4BIT 전송
   Rs(0),E(0);
   lcd_delay_us(1); //tas : Min 40ns
   CLCD_data(command>>4);
   E(1);
   lcd_delay_us(1);   //PW_eh min 230ns
   E(0);
   lcd_delay_us(50);   //100
//  하위 4BIT 전송
   CLCD_data((command&0x0F)); // output command
   E(1); lcd_delay_us(1); //1
   E(0); lcd_delay_us(50); //50
}
void lcd_data(unsigned char data) { // display a character on text LCD //상위 4BIT 전송
   E(0), Rs(1); CLCD_data(data>>4); // output data
   E(1); lcd_delay_us(1); // 1
   E(0); lcd_delay_us(50); //50
//하위 4BIT 전송
   CLCD_data((data&0x0F));
   E(1); lcd_delay_us(1);  //   1
   E(0); lcd_delay_us(50); //50
}
 
void lcd_string(unsigned char command, char *string)  { // display a string on LCD
   lcd_command(command); // start position of string
   while(*string != '\0') { // display string
      lcd_data(*string); string++;
   }
}
 
void lcd_gotoxy(int x, int y)
{
   lcd_command(0x80 | (0x40 * y) | x);
}
 
void lcd_clear()
{
    lcd_command(0x01); // clear display
    lcd_delay_ms(10); //4
 
}
 
void lcd_printxy(int x, int y, char *string)  { // display a string on LCD
   lcd_gotoxy(x, y);
   while(*string != '\0') { // display string
      lcd_data(*string);
      string++;
   }
}
 
cs



// Main Code


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
 
 #include "stm32f4xx.h"
#include "lcd.h"
#include "stdio.h"
 
 
int msTime = 0;
void SysTick_Handler() {
    msTime++;
}
void delay(int ms) {
    int future = ms + msTime;
    while (future > msTime)
        ;
}
void init() {
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2
            | GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
}
 
int prev_pin_0 = 1;
int pin_0 = 1;
int prev_pin_1 = 1;
int pin_1 = 1;
int prev_pin_2 = 1;
int pin_2 = 1;
int prev_pin_3 = 1;
int pin_3 = 1;
char buf[255];
 
void getBtn() {
    prev_pin_0 = pin_0;
    pin_0 = GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_0);
    prev_pin_1 = pin_1;
    pin_1 = GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_1);
    prev_pin_2 = pin_2;
    pin_2 = GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_2);
    prev_pin_3 = pin_3;
    pin_3 = GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_3);
}
 
int InputWeightMenu(int CurrentWeight) {
    int iWeight = CurrentWeight;
    while (1) {
        lcd_clear();
        lcd_printxy(00"[In Your Weight]");
        while (1) {
            sprintf(buf, "[Weight:%d kg]+-", iWeight);
            lcd_printxy(01, buf);
 
            getBtn();
            // 키 입력시 몸무게 증가
            if (pin_0 == 0 && prev_pin_0 == 1) {
                iWeight++;
                if (iWeight > 300)
                    iWeight = 0;
                break;
            }
            // 키 입력시 몸무게 감소
            if (pin_1 == 0 && prev_pin_1 == 1) {
                iWeight--;
                if (iWeight < 0)
                    iWeight = 0;
                break;
            }
            if (pin_2 == 0 && prev_pin_2 == 1) {
                lcd_clear();
                lcd_printxy(00"[Input Weight]");
                sprintf(buf, "[Weight:%d kg]+-", iWeight);
                lcd_printxy(01, buf);
                return iWeight;
            }
            if (pin_3 == 0 && prev_pin_3 == 1) {
                lcd_clear();
                lcd_printxy(00"[Input Weight]");
                sprintf(buf, "[Weight:%d kg]+-", CurrentWeight);
                lcd_printxy(01, buf);
                return CurrentWeight;
            }
        }
    }
}
double InputHeightMenu(volatile double CurrentHeight) {
    double iHeight = CurrentHeight;
    while (1) {
        lcd_clear();
        lcd_printxy(00"[In Your Height]");
        while (1) {
 
            sprintf(buf, "[Height:%.2f m]+-", iHeight);
            lcd_printxy(01, buf);
 
            getBtn();
            // 키 입력시 키 증가
            if (pin_0 == 0 && prev_pin_0 == 1) {
                iHeight += 0.01;
                break;
            }
            // 키 입력시 키 감소
            if (pin_1 == 0 && prev_pin_1 == 1) {
                iHeight -= 0.01;
                break;
            }
            if (pin_2 == 0 && prev_pin_2 == 1) {
                lcd_clear();
                lcd_printxy(00"[Input Height]");
                sprintf(buf, "[Height:%.2f m]+-", iHeight);
                lcd_printxy(01, buf);
                return iHeight;
            }
            if (pin_3 == 0 && prev_pin_3 == 1) {
                lcd_clear();
                lcd_printxy(00"[Input Height]");
                sprintf(buf, "[Height:%.2f m]+-", CurrentHeight);
                lcd_printxy(01, buf);
                return CurrentHeight;
            }
        }
    }
}
 
int main(void) {
    init();
    lcd_init();
    SysTick_Config(SystemCoreClock / 1000 / 3.125);
    int iCurrentMenu = 0;
    int iWeight = 50;
    double iHeight = 1.60;
    for (;;) {
        lcd_clear();
        if (iCurrentMenu == 0) {
            lcd_printxy(00"[Input Weight]");
            sprintf(buf, "[Weight:%d kg]", iWeight);
            lcd_printxy(01, buf);
        }
        if (iCurrentMenu == 1) {
            lcd_printxy(00"[Input Height]");
            sprintf(buf, "[Height:%.2f m]", iHeight);
            lcd_printxy(01, buf);
        }
        if (iCurrentMenu == 2) {
            lcd_printxy(00"[Calc BMI]");
            double bmi = iWeight/(iHeight*iHeight);
            sprintf(buf, "[BMI:%.2f]", bmi);
            lcd_printxy(0,1,buf);
        }
 
        while (1) {
            getBtn();
 
            if (pin_0 == 0 && prev_pin_0 == 1) {
                iCurrentMenu = (iCurrentMenu + 1) % 3// 다음 메뉴
                break;
            }
            if (pin_2 == 0 && prev_pin_2 == 1) {
                if (iCurrentMenu == 0)
                    iWeight = InputWeightMenu(iWeight);
                if (iCurrentMenu == 1)
                    iHeight = InputHeightMenu(iHeight);
                if (iCurrentMenu == 2){
                    lcd_clear();
                    double bmi = iWeight/(iHeight*iHeight);
                    sprintf(buf, "[BMI : %.2f]", bmi);
                    lcd_printxy(00, buf);
                    if(bmi>= 30)
                    lcd_printxy(0,1,"Obesity");
                    else if(bmi>=25)
                        lcd_printxy(0,1,"OVER Weight");
                    else if(bmi>=18.5)
                        lcd_printxy(0,1,"Nomal Weight");
                    else if(bmi <18.5)
                        lcd_printxy(0,1,"Under Weight");
                }
            }
        }
    }
}
 
 
// Sprintf(buf,"%f", 15.5);
//소숫점 표기를 위해 프로젝트
//properties -> C/C++ build -> setting -> MCU GCC Linker -> miscellaneous -> -u _printf_float 추가
 
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] Coretex-M4 Stop_Watch  (0) 2018.10.18
[MTX32F407-M4] Cortex-M4 LED제어, SW제어, FND 제어  (0) 2018.10.15
반응형

// 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--&amp;gt;=0);
}
void delay_us(int time){
    while(time--&amp;gt;=0)
        delay_time(1);
}
void delay_ms(int time){
    while(time--&amp;gt;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, &amp;amp;GPIO_InitStructure);
 
    GPIO_InitStructure.GPIO_Pin = 0b1111111100000000;
    GPIO_Init(GPIOD, &amp;amp;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, &amp;amp;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&amp;lt;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&amp;amp;&amp;amp; flag ==0){
            GPIO_SetBits(GPIOB, GPIO_Pin_5);
            num++;
            flag=1;
        }
        else if((GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_0)==1&amp;amp;&amp;amp; flag==1){
            GPIO_ResetBits(GPIOB, GPIO_Pin_5);
            flag=0;
        }
 
        // GPIOD 이력시 GPIOB bit set, num--;
        if((GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_1)==0&amp;amp;&amp;amp; flag2 ==0){
            GPIO_SetBits(GPIOB, GPIO_Pin_6);
            num--;
            flag2=1;
        }
        else if ((GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_1)==1)&amp;amp;&amp;amp; flag2 ==1){
            GPIO_ResetBits(GPIOB, GPIO_Pin_6);
            flag2=0;
        }
        if ((GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_2)==0)&amp;amp;&amp;amp; flag3 ==0){
 
            flag3=1;
        }
        else if((GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_2)==1)&amp;amp;&amp;amp;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

+ Recent posts