반응형
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
/*
 * avr-0910.cpp
 *
 * Created: 2018-09-10 오전 9:20:31
 * Author : USER
 */ 
 
#include <avr/io.h>
#define F_CPU 16000000
#include <util/delay.h>
#include "lcd.h"
#include <avr/interrupt.h>
#include <stdio.h>
 
void io_init(void)
{
    MCUCR|=(1<<SRE) | (1<<SRW10);
    XMCRA=(1<<SRL2) | (0<<SRL1) | (0<<SRL0) | (1<<SRW01) | (1<<SRW00) | (1<<SRW11);
    XMCRB |= 0x00;
}
unsigned int count =0, sec=0, min=0,hour=0;
unsigned int mode =0, ampm =0;
unsigned int flag=0;;
 
ISR(TIMER0_OVF_vect){
    TCNT0 =6;
    count++;
    if (count >1000){
        sec++; count=0;
        if(sec>=60){
            min++; sec=0;
            if (min>=60){
                hour++; min=0;
                if(hour>12){
                    hour=0;
                    if(ampm == 0) ampm =1;
                    else if(ampm ==1) ampm=0;
                    count =0;
                }
            }
        }
    }
}
 
int main(void)
{
    /* Replace with your application code */
    io_init();
    lcd_init();
    
    DDRE = 0x8f// 스위치 
    PORTE = 0x70; // 추후 확인 필요
    
    // 타이머 인터럽트 레지스터
    TIMSK = 0x01// R/W 선택 TIMER 0 사용
    TCCR0 =0x04// 분주비 64
    TCNT0 =6// 0에서 시작 255가되어 256이 되면 OVF가 되어 인터럽트 구문을 실행한다.
    
    /*
    1/16000000 = 0.0000000625
    분주비가 64
    0.0000000625 *64 = 0.000004 // TCNT0가 1올라가는 속도.
    ISR이 발생하는 시간 = 0.000004
    TCNT 250 회로 OVF 발생시 걸리는 시간 0.001
    500번이 OVF 인터럽트가 발생하면 1초가 된다.
    */
    
    sei();
    char temp[16];
    
    while (1
    {
        //input
        if (((PINE && 0x10)== 0x00&& flag ==0){
            mode++;
            flag=1;
            if (mode >=2)
            mode=0;
        }
        else if (PINE ==0xff) flag=0;    
        
        //display
        switch(mode){
            case 0:
            mode =1;
            sprintf(temp,"%d  :  %d  :  %d  %s",hour,min,sec,(ampm=0)?"AM":"PM");
            lcd_putsf(0,0,"  Normal Mode  ");
            lcd_putsf(0,1,temp);
        
            case 1:
            mode =0;
            sprintf(temp,"%d  :  %d  :  %d  %s",hour,min,sec,(ampm=0)?"AM":"PM");
            if (sec%2 ==0){
                temp[2]=temp[3= ' ';
            }
            lcd_putsf(0,0,"  Modify Mode  ");
            lcd_putsf(0,1,temp);
        }
        
 
    }
}
cs

 

 

// 스위치 DDRE 설정 다시 확인 후 시계 만들기 완성 소스

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
/*
 * avr-0910.cpp
 *
 * Created: 2018-09-10 오전 9:20:31
 * Author : USER
 */ 
 
#include <avr/io.h>
#define F_CPU 16000000
#include <util/delay.h>
#include "lcd.h"
#include <avr/interrupt.h>
#include <stdio.h>
#define LED_SEL1 (*(volatile unsigned char *)0xA000)
#define LED_SEL2 (*(volatile unsigned char *)0xB000)
#define FND (*(volatile unsigned char *)0x8000)
 
void io_init(void)
{
    MCUCR|=(1<<SRE) | (1<<SRW10);
    XMCRA=(1<<SRL2) | (0<<SRL1) | (0<<SRL0) | (1<<SRW01) | (1<<SRW00) | (1<<SRW11);
    XMCRB |= 0x00;
}
unsigned int count =0, sec=0, min=0,hour=0;
unsigned int mode =0, ampm =0;
unsigned int flag=0;
unsigned int saveSec=0, saveMin=0,saveHour=0;
 
ISR(TIMER0_OVF_vect){
    TCNT0 =6;
    count++;
    if (count >1000){
        sec++; count=0;
        if(sec>=60){
            min++; sec=0;
            if (min>=60){
                hour++; min=0;
                if(hour>12){
                    hour=0;
                    if(ampm == 0) ampm =1;
                    else if(ampm ==1) ampm=0;
                    count =0;
                }
            }
        }
    }
}
 
int main(void)
{
    /* Replace with your application code */
    io_init();
    lcd_init();
    
    DDRE = 0x8f// 스위치 
    PORTE = 0xff
    
    // 타이머 인터럽트 레지스터
    TIMSK = 0x01// R/W 선택 TIMER 0 사용
    TCCR0 =0x04// 분주비 64
    TCNT0 =6// 6에서 시작 255가되어 256이 되면 OVF가 되어 인터럽트 구문을 실행한다.
    
    /*
    1/16000000 = 0.0000000625
    분주비가 64
    0.0000000625 *64 = 0.000004 // TCNT0가 1올라가는 속도.
    ISR이 발생하는 시간 = 0.000004
    TCNT 250 회로 OVF 발생시 걸리는 시간 0.001
    500번이 OVF 인터럽트가 발생하면 1초가 된다.
    */
    
    mode =0;
    sei();
    char temp[16];
    int sel =0;
    while (1
    {
        
        //input
        if (((PINE & 0x10)== 0x00&& flag ==0 ){    
            sel=0;
            mode++;
            if(mode >=2)mode =0;
            flag=1;
        }    
        // 숫자 증가 input
        else if (((PINE & 0x20)== 0x00)&& flag==0 && mode==1 ){
            if (sel==0){hour++;
                if (hour >=60) hour =0;
            }
            else if (sel ==1){ min++;
                if(min>=60) min=0;
            }
            else if(sel ==2){ sec++;
                if(sec>=60) sec=0;
            }
            else if(sel==3){
                if (ampm ==0) ampm =1;
                else if (ampm ==1)  ampm=0;
            }
            flag=1;
        }
        // 숫자 선택 셀 증가
        else if (((PINE & 0x40)== 0x00)&& flag==0 && mode==1 ){
            sel++;
            if (sel >=4) sel=0;
            flag=1;
        }
        else if (PINE == 0xff){ flag=0;    
        }
        
        //display
        switch(mode){
            case 0:
            sprintf(temp," %2d :%2d :%2d %2s",hour,min,sec,(ampm==0)?"AM":"PM");
            lcd_putsf(0,0,"  Normal Mode  ");
            lcd_putsf(0,1,temp);
                break;
            case 1:
            sprintf(temp," %2d :%2d :%2d %2s",hour,min,sec,(ampm==0)?"AM":"PM");    
            if (sec%2 ==0){
                if(sel==0)temp[1= temp[2=' ';
                else if(sel==1)temp[5= temp[6=' ';
                else if(sel==2)temp[9= temp[10=' ';
                else if (sel==3)temp[12= temp[13=' ';
                else sel =0;
            }
            lcd_putsf(0,0,"  Modify Mode  ");
            lcd_putsf(0,1,temp);
                break;
        }
    }
}
cs

 

 

 

 

/*

// SW4, SW5, SW6

SW4 입력시 모드가 바뀌며, Nomal Mode , Modify Mode가 있다.

Modify Mode 에서는 SW5 과 SW6을 입력하여 시간과 AM/PM을 변경 할 수 있다.

*/

 

 

반응형
반응형
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
/*
 * avr-0910.cpp
 *
 * Created: 2018-09-10 오전 9:20:31
 * Author : USER
 */ 
 
#include <avr/io.h>
#define F_CPU 16000000
#include <util/delay.h>
#include "lcd.h"
#include <avr/interrupt.h>
#include <stdio.h>
 
void io_init(void)
{
    MCUCR|=(1<<SRE) | (1<<SRW10);
    XMCRA=(1<<SRL2) | (0<<SRL1) | (0<<SRL0) | (1<<SRW01) | (1<<SRW00) | (1<<SRW11);
    XMCRB |= 0x00;
}
unsigned int count =0, sec=0;
 
ISR(TIMER0_OVF_vect){
    TCNT0 =256-25;
    count++;
    if (count >625){
        sec++; count=0;
    }
}
int main(void)
{
    /* Replace with your application code */
    io_init();
    lcd_init();
    
    // 타이머 인터럽트 레지스터
    TIMSK = 0x01// R/W 선택 TIMER 0 사용
    TCCR0 =0x07// 분주비 1024
    TCNT0 =256-25// 0에서 시작 255가되어 256이 되면 OVF가 되어 인터럽트 구문을 실행한다.
    
    /*
    1/16000000 = 0.0000000625
    분주비가 1024
    0.0000000625 *1024 = 0.000064 // TCNT0가 1올라가는 속도.
    ISR이 발생하는 시간 = 0.016384
    약 61번이 OVF 인터럽트가 발생하면 1초가 된다.
    */
    
    sei();
    char temp[16];
    
    
    while (1
    {
        sprintf(temp,"%d",sec);
        lcd_putsf(0,0,temp);        
    }
}
cs

 

// 해당 소스는 정확한 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
54
55
56
57
58
59
/*
 * avr-0910.cpp
 *
 * Created: 2018-09-10 오전 9:20:31
 * Author : USER
 */ 
 
#include <avr/io.h>
#define F_CPU 16000000
#include <util/delay.h>
#include "lcd.h"
#include <avr/interrupt.h>
#include <stdio.h>
 
void io_init(void)
{
    MCUCR|=(1<<SRE) | (1<<SRW10);
    XMCRA=(1<<SRL2) | (0<<SRL1) | (0<<SRL0) | (1<<SRW01) | (1<<SRW00) | (1<<SRW11);
    XMCRB |= 0x00;
}
unsigned int count =0, sec=0;
 
ISR(TIMER0_OVF_vect){
    TCNT0 =256-5;
    count++;
    if (count >500){
        sec++; count=0;
    }
}
int main(void)
{
    /* Replace with your application code */
    io_init();
    lcd_init();
    
    // 타이머 인터럽트 레지스터
    TIMSK = 0x01// R/W 선택 TIMER 0 사용
    TCCR0 =0x04// 분주비 64
    TCNT0 =256-5// 0에서 시작 255가되어 256이 되면 OVF가 되어 인터럽트 구문을 실행한다.
    
    /*
    1/16000000 = 0.0000000625
    분주비가 64
    0.0000000625 *64 = 0.000004 // TCNT0가 1올라가는 속도.
    ISR이 발생하는 시간 = 0.000004
    TCNT 250 회로 OVF 발생시 걸리는 시간 0.001
    500번이 OVF 인터럽트가 발생하면 1초가 된다.
    */
    
    sei();
    char temp[16];
    
    
    while (1
    {
        sprintf(temp,"%d",sec);
        lcd_putsf(0,0,temp);        
    }
}
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
/*
 * GccApplication1.cpp
 *
 * Created: 2018-09-04 오전 11:21:22
 * Author : USER
 */ 
 
#include <avr/io.h>
#define F_CPU 16000000
#include <util/delay.h>
#include "lcd.h"
#include <stdio.h>
#include <stdlib.h>
 
#define LED_SEL1 (*(volatile unsigned char *)0xA000)
#define LED_SEL2 (*(volatile unsigned char *)0xB000)
#define FND (*(volatile unsigned char *)0x8000)
#define DOTMATRIX (*(volatile unsigned char *)0x9000)
 
void DotMatrix(char rdata, char addr)
{
    char data = 0;
    DOTMATRIX = 0x00;
    for (int i=0; i<8; i++)
    {
        data = 0;
        if (rdata & 0x80) data |= 0x01;
        DOTMATRIX = data;
        DOTMATRIX = 0x04;
        DOTMATRIX = 0x00;
        rdata <<= 1;
    }
    DOTMATRIX = (addr << 4| 0x88;
}
 
 
void io_init(void)
{
    MCUCR|=(1<<SRE) | (1<<SRW10);
    XMCRA=(1<<SRL2) | (0<<SRL1) | (0<<SRL0) | (1<<SRW01) | (1<<SRW00) | (1<<SRW11);
    XMCRB |= 0x00;
}
 
void fndController (char loc , char number){
    FND = (number <<3)|loc;
}
void fndDisplay (unsigned long long number){
    fndController(5,number%10);_delay_ms(1);
    fndController(4,(number%100)/10);_delay_ms(1);
    fndController(3,(number%1000)/100);_delay_ms(1);
    fndController(2,(number%10000)/1000);_delay_ms(1);
    fndController(1,(number%100000)/10000);_delay_ms(1);
    fndController(0,number/100000);_delay_ms(1);
    
}
 
int keyMatrix(){
    unsigned char key =0, ret_val =0;
    for (int i =0; i<4;i++){
        if(i==0) PORTD = 0b11101111;
        if(i==1) PORTD = 0b11011111;
        if(i==2) PORTD = 0b10111111;
        if(i==3) PORTD = 0b01111111;
        _delay_ms(5);
        
        key = ~PINF &0xf0;
        if(key){
            ret_val = key|(PORTD >>4);
            return ret_val;
            }
    }
    return 0;
}
int main(void)
{
    io_init();
    unsigned char data = 0b00000001;
    while (1
    {
        
        DotMatrix(0b01100110,0);
        _delay_ms(1);
        DotMatrix(0b10011001,1);
        _delay_ms(1);
        DotMatrix(0b10000001,2);
        _delay_ms(1);
        DotMatrix(0b10000001,3);
        _delay_ms(1);
        DotMatrix(0b01100110,4);
        _delay_ms(1);
        DotMatrix(0b00100100,5);
        _delay_ms(1);
        DotMatrix(0b00011000,6);
        _delay_ms(1);
        
    }
}
 
 
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
/*
 * GccApplication1.cpp
 *
 * Created: 2018-09-04 오전 11:21:22
 * Author : USER
 */ 
 
#include <avr/io.h>
#define F_CPU 16000000
#include <util/delay.h>
#include "lcd.h"
#include <stdio.h>
#include <stdlib.h>
 
#define LED_SEL1 (*(volatile unsigned char *)0xA000)
#define LED_SEL2 (*(volatile unsigned char *)0xB000)
#define FND (*(volatile unsigned char *)0x8000)
 
void io_init(void)
{
    MCUCR|=(1<<SRE) | (1<<SRW10);
    XMCRA=(1<<SRL2) | (0<<SRL1) | (0<<SRL0) | (1<<SRW01) | (1<<SRW00) | (1<<SRW11);
    XMCRB |= 0x00;
}
 
void fndController (char loc , char number){
    FND = (number <<3)|loc;
}
void fndDisplay (unsigned long long number){
    fndController(5,number%10);_delay_ms(1);
    fndController(4,(number%100)/10);_delay_ms(1);
    fndController(3,(number%1000)/100);_delay_ms(1);
    fndController(2,(number%10000)/1000);_delay_ms(1);
    fndController(1,(number%100000)/10000);_delay_ms(1);
    fndController(0,number/100000);_delay_ms(1);
    
}
 
int keyMatrix(){
    unsigned char key =0, ret_val =0;
    for (int i =0; i<4;i++){
        if(i==0) PORTD = 0b11101111;
        if(i==1) PORTD = 0b11011111;
        if(i==2) PORTD = 0b10111111;
        if(i==3) PORTD = 0b01111111;
        _delay_ms(5);
        
        key = ~PINF &0xf0;
        if(key){
            ret_val = key|(PORTD >>4);
            return ret_val;
            }
    }
    return 0;
}
int main(void)
{
    io_init();
    lcd_init();
    lcd_clear();
    
    DDRF = 0x0f;
    DDRD = 0xf0;
    
    PORTF = 0x0f;
    PORTD = 0x00;
    char str[16];
    
    while (1
    {    
        lcd_clear();
        itoa (keyMatrix(),str,2);
        lcd_putsf(0,0,str);_delay_ms(5);
        lcd_clear();
        
    }
}
 
 
cs

 

// 키를 입력할 경우 키값을 반환한다.

 

 

2. keyMatrix - 알파벳 출력

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
#include <avr/io.h>
#define F_CPU 16000000
#include <util/delay.h>
#include <stdio.h>
#include <stdlib.h>
#include "lcd.h"
 
void io_init(void)
{
    MCUCR|=(1<<SRE) | (1<<SRW10);
    XMCRA=(1<<SRL2) | (0<<SRL1) | (0<<SRL0) | (1<<SRW01) | (1<<SRW00) | (1<<SRW11);
    XMCRB |= 0x00;
}
 
unsigned int getKey()
{
    unsigned char key = 0, ret_val = 0;
    for (int i=0; i<4; i++)
    {
        PORTD = ~(0b00010000 << i);
        _delay_ms(5);        
        key = ~PINF & 0xf0;
        if (key) 
        {
            ret_val = key | (PORTD >> 4);                    
            return ret_val;
        }        
    }
    return 0;
}
void inputChar(char array[16], char data, int *count)
{
    array[*count] = data;
    array[*count + 1= 0;
    *count = *count + 1;
}
 
int main(void)
{
    io_init();
    lcd_init();
    lcd_clear();    
    
    DDRF = 0x00;        
    DDRD = 0xff;
    PORTF = 0xff;
    PORTD = 0x00;
    char str[16];
    int count = 0;
    while (1
    {    
        if (getKey() == 0b00011110) inputChar(str, 'a'&count);
        if (getKey() == 0b00011101) inputChar(str, 'b'&count);
        if (getKey() == 0b00011011) inputChar(str, 'c'&count);
        if (getKey() == 0b00010111) inputChar(str, 'd'&count);
        
        lcd_putsf(0,0,str);
        _delay_ms(10);
    }
}
 
cs

 

// 키를 누를 경우 알파벳 출력. 

반응형
반응형

 

 

Analog to Digital Converter

ADC 레지스터

ADMUX

ADCSRA

 

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
/*
 * GccApplication1.cpp
 *
 * Created: 2018-09-04 오전 11:21:22
 * Author : USER
 */ 
 
#include <avr/io.h>
#define F_CPU 16000000
#include <util/delay.h>
#include "lcd.h"
#include <stdio.h>
#include <stdlib.h>
 
#define LED_SEL1 (*(volatile unsigned char *)0xA000)
#define LED_SEL2 (*(volatile unsigned char *)0xB000)
#define FND (*(volatile unsigned char *)0x8000)
 
void io_init(void)
{
    MCUCR|=(1<<SRE) | (1<<SRW10);
    XMCRA=(1<<SRL2) | (0<<SRL1) | (0<<SRL0) | (1<<SRW01) | (1<<SRW00) | (1<<SRW11);
    XMCRB |= 0x00;
}
 
void fndController (char loc , char number){
    FND = (number <<3)|loc;
}
void fndDisplay (unsigned long long number){
    fndController(5,number%10);_delay_ms(1);
    fndController(4,(number%100)/10);_delay_ms(1);
    fndController(3,(number%1000)/100);_delay_ms(1);
    fndController(2,(number%10000)/1000);_delay_ms(1);
    fndController(1,(number%100000)/10000);_delay_ms(1);
    fndController(0,number/100000);_delay_ms(1);
    
}
 
 
int main(void)
{
    io_init();
    lcd_init();
    lcd_clear();
    
    ADCSRA = 0xe7;
    ADMUX = 0x01;
    
    char str[16];
    
    while (1
    {    
        unsigned int sum = ADC ;
        itoa(sum,str,10);
        lcd_putsf(0,0,str);
        _delay_ms(1);
    }
}
 
 
cs

 

// 가변저항을 돌리면 LCD 에 값이 출력된다.

 

http://tackbro.tistory.com/entry/AVR-%EA%B0%80%EB%B3%80%EC%A0%80%ED%95%AD%EC%9C%BC%EB%A1%9C-atmega-128%EC%97%90%EC%84%9C-ADC-%EC%82%AC%EC%9A%A9-%EC%98%88%EC%A0%9C

 

 

반응형
반응형
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
#include <avr/io.h>
#define F_CPU 16000000
#include <util/delay.h>
#include "lcd.h"
#include <stdio.h>
#include <stdlib.h>
 
#define LED_SEL1 (*(volatile unsigned char *)0xA000)
#define LED_SEL2 (*(volatile unsigned char *)0xB000)
#define FND (*(volatile unsigned char *)0x8000)
 
void io_init(void)
{
    MCUCR|=(1<<SRE) | (1<<SRW10);
    XMCRA=(1<<SRL2) | (0<<SRL1) | (0<<SRL0) | (1<<SRW01) | (1<<SRW00) | (1<<SRW11);
    XMCRB |= 0x00;
}
 
void fndController (char loc , char number){
    FND = (number <<3)|loc;
}
void fndDisplay (unsigned long long number){
    fndController(5,number%10);_delay_ms(1);
    fndController(4,(number%100)/10);_delay_ms(1);
    fndController(3,(number%1000)/100);_delay_ms(1);
    fndController(2,(number%10000)/1000);_delay_ms(1);
    fndController(1,(number%100000)/10000);_delay_ms(1);
    fndController(0,number/100000);_delay_ms(1);
    
}
 
 
int main(void)
{
    io_init();
    lcd_init();
    lcd_clear();
    
    DDRE = 0x00;
    PORTE = 0xff;
    
    int count=0;
    char str[16];
    
    while (1
    {    
        itoa(PINE,str,2);
        sprintf(str,"C:%d",count);
    }
}
 
cs

 

 

 

// 스위치를 입력할 경우 LCD 2진수가 출력된다.

//디버깅에 용이하다

반응형
반응형
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
/*
 * GccApplication1.cpp
 *
 * Created: 2018-09-04 오전 11:21:22
 * Author : USER
 */ 
 
#include <avr/io.h>
#define F_CPU 16000000
#include <util/delay.h>
#include "lcd.h"
 
#define LED_SEL1 (*(volatile unsigned char *)0xA000)
#define LED_SEL2 (*(volatile unsigned char *)0xB000)
#define FND (*(volatile unsigned char *)0x8000)
 
void io_init(void)
{
    MCUCR|=(1<<SRE) | (1<<SRW10);
    XMCRA=(1<<SRL2) | (0<<SRL1) | (0<<SRL0) | (1<<SRW01) | (1<<SRW00) | (1<<SRW11);
    XMCRB |= 0x00;
}
 
void fndController (char loc , char number){
    FND = (number <<3)|loc;
}
void fndDisplay (unsigned long long number){
    fndController(5,number%10);_delay_ms(1);
    fndController(4,(number%100)/10);_delay_ms(1);
    fndController(3,(number%1000)/100);_delay_ms(1);
    fndController(2,(number%10000)/1000);_delay_ms(1);
    fndController(1,(number%100000)/10000);_delay_ms(1);
    fndController(0,number/100000);_delay_ms(1);
    
}
 
 
int main(void)
{
    io_init();
    lcd_init();
    lcd_clear();
    
    int flag = 0;
    while (1
    {    
        if (flag == 0){
            for (int i = 0;i<16;i++){
            lcd_putsf(0,0,"             ");
            lcd_putsf(i,0,"A");
            _delay_ms(100);
            
                if (i==15){flag =1; lcd_putsf(15,0,"   ");
                }
            }
        }
            
        else if (flag == 1){
            for (int i = 15; i>=0;i--){
                lcd_putsf(0,1,"                     ");
                lcd_putsf(i,1,"A");    
                _delay_ms(100);
                if (i==0){flag=0;lcd_putsf(0,1,"   ");}
            }
        }
    }
}
 
 
cs

lcd.h

 

 

 

 

반응형
반응형

AVR

io레지스터

DDRA = 0b1111 1111; //출력설정

PORTA = 0b1111 1111; // 포트를 출력한다.

DDRA = 0b0000 0000; // 입력설정

PORTA = 0b11111111; // Software pull-up

//

if(PINA == 0b0111 1111) // PIN 레지스터는 Read-Only 읽기만 가능.

{

PORTA = 0b0000 0000; // PORT 레지스터읽기 쓰기 둘다 가능하다.

}

 

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
/*
 * avr_test.cpp
 *
 * Created: 2018-09-03 오전 10:56:46
 * Author : USER
 */ 
#include <avr/io.h>
#define F_CPU 16000000
#include <util/delay.h>
 
#define LED_SEL1 (*(volatile unsigned char *)0xA000)
#define LED_SEL2 (*(volatile unsigned char *)0xB000)
#define FND (*(volatile unsigned char *)0x8000)
 
 
void io_init(void)
{
    MCUCR|=(1<<SRE) | (1<<SRW10);
    XMCRA=(1<<SRL2) | (0<<SRL1) | (0<<SRL0) | (1<<SRW01) | (1<<SRW00) | (1<<SRW11);
    XMCRB |= 0x00;
}
 
void fndController (char loc , char number){
    FND = (number <<3)|loc;
}
void fndDisplay (unsigned long long number){
    fndController(5,number%10);_delay_ms(1);
    fndController(4,(number%100)/10);_delay_ms(1);
    fndController(3,(number%1000)/100);_delay_ms(1);
    fndController(2,(number%10000)/1000);_delay_ms(1);
    fndController(1,(number%100000)/10000);_delay_ms(1);
    fndController(0,number/100000);_delay_ms(1);
    
}
int main(void)
{
    io_init();
    DDRE = 0x00;
    PORTE = 0xff;
    //int count =0;
    
    bool state = false;
    int randnum =0, dispnum =0;
    while (1)
    {
        
        /* //눌렀을 때 증가하는 소스
        if (PINE == 0b11101111 && state ==false){ count ++;state =true;}
        else if (PINE ==0b11111111&& state==true){ state =false;}
        fndDisplay(count);
        */
        // 0~100 사이의 랜덤수 출력하는 소스
        randnum++;
        if (randnum>100) randnum=0;
        if (PINE == 0b11101111 && state ==false){  dispnum= randnum;state =true;}
        else if (PINE ==0b11111111&& state==true){ state =false;}
        fndDisplay(dispnum);
        
    }
}
 
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
/*
 * avr_test.cpp
 *
 * Created: 2018-09-03 오전 10:56:46
 * Author : USER
 */ 
#include <avr/io.h>
#define F_CPU 16000000
#include <util/delay.h>
 
#define LED_SEL1 (*(volatile unsigned char *)0xA000)
#define LED_SEL2 (*(volatile unsigned char *)0xB000)
#define FND (*(volatile unsigned char *)0x8000)
 
 
void io_init(void)
{
    MCUCR|=(1<<SRE) | (1<<SRW10);
    XMCRA=(1<<SRL2) | (0<<SRL1) | (0<<SRL0) | (1<<SRW01) | (1<<SRW00) | (1<<SRW11);
    XMCRB |= 0x00;
}
 
void fndController (char loc , char number){
    FND = (number <<3)|loc;
}
int main(void)
{
    io_init();
 
    int count =0;
 
    int num2 =0;
    int num3 =0;
    int num4 =0;
    int num5 =0;
    int num6 =0;
    
    while (1)
    {
        
        count++;
        if (count %10 == 0){
        num6 ++;
        count =0;
        }
        if (num6 >=10){
            num5 ++;
            num6=0;
        }
        if (num5>=10){ num4 ++;
            num5=0;
            
        }
        if (num4>=10){
            num3++;
            num4=0;
            
            
        }
        if ( num3>=10){
            num2++;
            num3=0;
            
        }
        
        for (int i =0; i<166;i++){
            
        fndController(5,count);
        _delay_ms(1);
        fndController(4,num6);
        _delay_ms(1);
        fndController(3,num5);
        _delay_ms(1);
        fndController(2,num4);
        _delay_ms(1);
        fndController(1,num3);
        _delay_ms(1);
        fndController(0,num2);
        _delay_ms(1);
            
        }
    
    }
}
 
cs

 

// 1ms 6번 출력함으로 1초를 만들기위해 166회 for 문을 돌리면 1초에 근접한 시간이 만들어 진다.

//  6*166 = 996 ms

 

 

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
/*
 * avr_test.cpp
 *
 * Created: 2018-09-03 오전 10:56:46
 * Author : USER
 */ 
#include <avr/io.h>
#define F_CPU 16000000
#include <util/delay.h>
 
#define LED_SEL1 (*(volatile unsigned char *)0xA000)
#define LED_SEL2 (*(volatile unsigned char *)0xB000)
#define FND (*(volatile unsigned char *)0x8000)
 
 
void io_init(void)
{
    MCUCR|=(1<<SRE) | (1<<SRW10);
    XMCRA=(1<<SRL2) | (0<<SRL1) | (0<<SRL0) | (1<<SRW01) | (1<<SRW00) | (1<<SRW11);
    XMCRB |= 0x00;
}
 
void fndController (char loc , char number){
    FND = (number <<3)|loc;
}
void fndDisplay (unsigned long long number){
    fndController(0,number%10);_delay_ms(1);
    fndController(1,(number%100)/10);_delay_ms(1);
    fndController(2,(number%1000)/100);_delay_ms(1);
    fndController(3,(number%10000)/1000);_delay_ms(1);
    fndController(4,(number%100000)/10000);_delay_ms(1);
    fndController(5,number/100000);_delay_ms(1);
    
}
int main(void)
{
    io_init();
    DDRE = 0x00;
    PORTE = 0xff;
    
    while (1)
    {
        if (PINE == 0b11101111) fndDisplay(999999);
        else fndDisplay(0);
        
    
    }
}
 
cs

// FND 출력 함수를 만들어 놓으면 나중에 확인할 때 유용하다.

// SW4입력시 999999를 출력한다.

 

 

 

반응형

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

[NPAVR Board] AVR - Atmega128 (Character LCD)  (0) 2018.09.04
[NPAVR Board] AVR -Atmega128 (FND, Switch)  (0) 2018.09.03
Atmgea8 FND 시계  (0) 2018.02.21
B/T LED 부품 선정 및 견적서  (1) 2017.04.10
B/T LED Control circuit  (0) 2017.04.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
#include <avr/io.h>
#include <util/delay.h>
#define F_CPU 16000000UL
#include <avr/interrupt.h>
 
#define OFF 0
#define ON 1
volatile unsigned char digit_c[10= {0x000x270x090x030x260x120x100x060x000x02}; // 0 ~ 10
volatile unsigned char digit_b[10= {0x040x04, ~0x04, ~0x04, ~0x04, ~0x04, ~0x040x04, ~0x04, ~0x04}; // 0 ~ 10
volatile unsigned char fnd_sel[4= {0x100x200x400x80}; // fnd_sel c1, c2, c3, c4
 
volatile unsigned int msec, sec,min,hour = 0// time
volatile unsigned int play = ON; // play ON/OFF
 
 
 
int main(){
 
 
 DDRB = 0x04;
 DDRC = 0xff;
 DDRD = 0xf0;
 
 
 while(1){
  
  if ((PIND & 0x01== 0x00){ 
  play = ON; continue;}
  if ((PIND & 0x02== 0x00){
  play = OFF; continue;}
    
  if(play == ON){
  
  msec++
  if(msec>40){msec=0; sec++;}
  if(sec>59){sec=0; min++;}
  if(min>59){min=0; hour++;}
  if(hour>23){hour=0;}
 
  PORTC = digit_c[hour/10]; PORTB = digit_b[hour/10]; PORTD = fnd_sel[3];_delay_ms(3);
  PORTC = digit_c[hour%10]; PORTB = digit_b[hour%10]; PORTD = fnd_sel[2];_delay_ms(3);
  PORTC = digit_c[min/10]; PORTB = digit_b[min/10]; PORTD = fnd_sel[1];_delay_ms(3);
  PORTC = digit_c[min%10]; PORTB = digit_b[min%10]; PORTD = fnd_sel[0];_delay_ms(3);
  }
 
  if(play == OFF){
 
  cli();
  GICR = 0xc0;
  MCUCR = 0x0a;
  GIFR = 0x00;
  sei();
  
  PORTC = digit_c[hour/10]; PORTB = digit_b[hour/10]; PORTD = fnd_sel[3]; _delay_ms(3);
  PORTC = digit_c[hour%10]; PORTB = digit_b[hour%10]; PORTD = fnd_sel[2]; _delay_ms(3);
  PORTC = digit_c[min/10]; PORTB = digit_b[min/10]; PORTD = fnd_sel[1]; _delay_ms(3);
  PORTC = digit_c[min%10]; PORTB = digit_b[min%10]; PORTD = fnd_sel[0]; _delay_ms(3);
 
  }
 }
 
}
 
ISR(INT0_vect){
 if(min <= 59){
 min++;
 }
 if(min >59){
 min = 0; hour++;
 }
}
ISR(INT1_vect){
 if(hour <= 23){hour++;
 }
 if(hour >23){
 hour =0;
 }
}
 
 
/*
SW 1,2,3,4,
SW 1: Play Mode
SW 2: STOP Setting Mode
SW 3: Hour ++;
SW 4: Min ++;
 
스위치 4개로 시간을 설정/재생 할 수 있다.
 
시간측정을해보니 약 5~10초정도 오차가 있다. 몇달 몇년으로 따지면 엄청난 오차.
 
차후엔 ds3231 rtc모듈을 이용하여 만들어 볼 예정.
*/
 

// 그전에 Timer/Counter로 먼저 만들어 봐야 할 것 같다. 

 
cs

 

 

http://cafe.naver.com/carroty/291041

 

반응형

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

[NPAVR Board] AVR -Atmega128 (FND, Switch)  (0) 2018.09.03
[NPAVR Board] AVR -Atmega128 (FND)  (0) 2018.09.03
B/T LED 부품 선정 및 견적서  (1) 2017.04.10
B/T LED Control circuit  (0) 2017.04.10
led 제어 현재 상황  (0) 2016.02.26

+ Recent posts