반응형

j-kit-128을 이용하여 전체적인 시스템을 구현중입니다.

현재까지

1.led
2.buzzer(PWM적용 필요)
3.fnd
4.timer
5.switch(Ext interrupt 구현 필요)
6.serial통신

//미구현
7.adc_dimmer
8.cds
9.i2c
10. 추가로 wifi모듈을 달아서 wifi 통신도 해 볼 예정입니다.

미구현 항목들은 기능에 대한 숙지가 부족하여 좀더 공부 할 필요가 있을 것 같습니다.
j-kit-128을 이용하여 복습하고, 해당 키트를 이용하여 공부하시는 분들에게 도움이 되고자? 만들게 되었습니다.

기능 구현 및 개선이 되는대로 업로드 하겠습니다.
저도 부족한 점이 많기에 많은 가르침 부탁드립니다.


=======================================================================================

추가적으로 기능 구현한 내용


1. CDS (adc값 읽기)

2. extSwitch (외부 키 인터럽트)


// 추가 구현 예정

void init_pwmDimmer();//pwm dimmer

void init_i2c();//i2c 온도센서

void init_pwmMotor(); //pwm motor

void init_pwmBuzzer(); //pwm buzzer

void init_INTSerial(); // uart인터럽트


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
#include <avr/io.h>
#define F_CPU 16000000UL
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdio.h>
 
#define FND_NUM0 0x3f
#define FND_NUM1 0x06
#define FND_NUM2 0x5b
#define FND_NUM3 0x4f
#define FND_NUM4 0x66
#define FND_NUM5 0x6d
#define FND_NUM6 0x7d
#define FND_NUM7 0x27
#define FND_NUM8 0x7f
#define FND_NUM9 0x6f
 
#define FND_SEL1 0x01
#define FND_SEL2 0x02
#define FND_SEL3 0x04
#define FND_SEL4 0x08
 
unsigned int fnd[10]= {FND_NUM0,FND_NUM1,FND_NUM2,FND_NUM3,FND_NUM4,FND_NUM5,FND_NUM6,FND_NUM7,FND_NUM8,FND_NUM9};
unsigned int fnd_sel[4= {FND_SEL1,FND_SEL2,FND_SEL3,FND_SEL4};
unsigned int count=0, sec=0;
 
char textBuff[100];
unsigned int extCount=0;
 
void init_led();
void init_buzzer();
void init_fnd();
void init_switch();
void init_timer();
void init_serial();
void init_adc();
void init_extSwitch();
 
/* 미구현
void init_pwmDimmer();//pwm dimmer
void init_i2c();//i2c 온도센서
void init_pwmMotor(); //pwm motor
void init_pwmBuzzer(); //pwm buzzer
void init_INTSerial(); // uart인터럽트
*/
 
void fnd_display(int num);
void SendByte(char data);
void SendLine(char *string);
char ReceiveByte();
uint16_t read_adc(uint8_t channel);
 
void test_led();
void test_buzzer();
void test_fnd();
void test_switch();
void test_serial();
void test_timer();
void test_adc();
void test_extSwitch();
 
int main(void)
{
    init_led();
    init_buzzer();
    init_fnd();
    init_switch();
    init_timer();
    init_serial();
    init_adc();
    init_extSwitch();
    sei();
    while (1)
    {
        //test_led();
        //test_buzzer();
        //test_fnd();
        //test_switch();
        //test_timer();
        //test_serial();
        //test_adc();
        //test_extSwitch();
    }
}
 
void init_led(){
    DDRA = 0xff;
    PORTA = 0x00;
}
void test_led(){
    PORTA = 0x00;
    _delay_ms(1000);
    PORTA = 0xff;
    _delay_ms(1000);
}
void init_buzzer(){
    DDRB = 0x10;
}
void test_buzzer(){
    PORTB = 0x10;
    _delay_ms(10);
    PORTB = 0x10;
    _delay_ms(10);
}
void init_fnd(){
    DDRC = 0xff;
    DDRG = 0x00;
}
void test_fnd(){
    fnd_display(1234);
}
void fnd_display(int num){
    int fnd1,fnd2,fnd3,fnd4;
    fnd1 = num%10;
    fnd2 = (num/10)%10;
    fnd3 = (num/100)%10;
    fnd4 = num/1000;
 
    PORTC = fnd[fnd1];
    PORTG = fnd_sel[0];
    _delay_ms(1);
    PORTC = fnd[fnd2];
    PORTG = fnd_sel[1];
    _delay_ms(1);
    PORTC = fnd[fnd3];
    PORTG = fnd_sel[2];
    _delay_ms(1);
    PORTC = fnd[fnd4];
    PORTG = fnd_sel[3];
    _delay_ms(1);
}
void init_switch(){
    // pull-up
    DDRE = 0x00;
    PORTE = 0x30;
}
void test_switch(){
    init_led();
    if((PINE & 0x20)== 0x00)
    PORTA = 0xff;
    if((PINE & 0x10)== 0x00)
    PORTA = 0x00;
}
void init_timer(){
    TIMSK = 0x01// R/W 선택 TIMER 0 사용
    TCCR0 = 0x04// 분주비 64
    TCNT0 =256-5// 0에서 시작 255가되어 256이 되면 OVF가 되어 인터럽트 구문을 실행한다.
    /*
    Timer set
    1/16000000 = 0.0000000625
    64분주
    0.0000000625* 64 = 0.000004
    0부터 250회 돌면 0.000004 *250 = 0.001
    OVF 발생 count 증가
    1000번 발생하면 1초 가 된다.
    */
}
void test_timer(){
    init_fnd();
    init_serial();
    fnd_display(sec);
    sprintf(textBuff,"%d\r\n",sec);
    SendLine(textBuff);
}
void init_serial(long BaudRate){
    UBRR0H = 0;
    switch(BaudRate)
    {
        case 115200:
        UBRR0L = 8;
        break;
        
        case 57600:
        UBRR0L = 16;
        break;
        
        case 38400:
        UBRR0L = 25;
        break;
        
        case 19200:
        UBRR0L = 51;
        break;
        
        case 14400:
        UBRR0L = 68;
        break;
        
        case 9600:
        UBRR0L = 103;
        break;
        
        // Default 115200
        default:
        UBRR0L = 8;
        break;
    }
    UCSR0A = 0x00;
    UCSR0B = 0x18;
    UCSR0C = 0x06;  // 8 bit
}
void SendByte(char data)
{
    while((UCSR0A & 0x20== 0x00);
    UDR0 = data;
}
/*** Function for sending line ***/
void SendLine(char *string)
{
    while(*string != '\0')
    {
        SendByte(*string);
        string++;
    }
}
 
char ReceiveByte(){
    while(!(UCSR0A & (1<<RXC0)));
    return UDR0;
}
void test_serial(){
    SendByte('A');
    _delay_ms(10);
    SendLine("Hello World");
    
    if((ReceiveByte() == 'a'&& (ReceiveByte() =='b')){
        SendByte('o');
        SendByte('k');
    }
}
void init_adc(){
        ADCSRA |= ((1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)); //16Mhz/128 = 125Khz
        ADMUX |= (1<<REFS0);       //AVCC(5V)
        ADCSRA |= (1<<ADEN);      //ADC 인에이블
}
uint16_t read_adc(uint8_t channel){
    ADMUX &= 0xF0;
    ADMUX |= channel;
    
    ADCSRA |= (1<<ADSC);      //변환 시작
    while(ADCSRA&(1<<ADSC));//변환 완료되기를 기다림.
    
    return ADCW;  //ADC값 반환
}
void test_adc(){
    init_adc();
    init_fnd();
    uint16_t adcValue =0;
    adcValue = read_adc(0);
    sprintf(textBuff,"adcValue : %d\r\n",adcValue);
    SendLine(textBuff);
    fnd_display(adcValue);
    _delay_ms(100);
}
void init_extSwitch(){
    DDRE = 0x00;
    PORTE = 0xf0;
    EICRB =0x0a;
    
    EIMSK = 0x30// INT4, INT5 사용
}
void test_extSwitch(){
    init_fnd();
    fnd_display(extCount);
}
 
ISR(INT4_vect){
    extCount++;
}
ISR(INT5_vect){
    extCount--;
}
ISR(TIMER0_OVF_vect){
    TCNT0 =256-(256-5);
    count++;
    if (count >=1000){
        sec++; count=0;
    }
}
cs



jkit_190122.zip


반응형

'Project > j-kit-128-1실습' 카테고리의 다른 글

[j-kit-128] jkit 128 기능구현하기  (0) 2018.12.10
Sonar_GPS - C#Window Form  (0) 2018.10.21
Sonar_GPS  (0) 2018.10.11
Serial 통신_ 문자열 전송  (0) 2018.10.01
Serial 통신_Uart0_문자 1개  (0) 2018.10.01
반응형

j-kit-128을 이용하여 전체적인 시스템을 구현중입니다.

현재까지

1.led
2.buzzer(PWM적용 필요)
3.fnd
4.timer
5.switch(Ext interrupt 구현 필요)
6.serial통신

//미구현
7.adc_dimmer
8.cds
9.i2c
10. 추가로 wifi모듈을 달아서 wifi 통신도 해 볼 예정입니다.

미구현 항목들은 기능에 대한 숙지가 부족하여 좀더 공부 할 필요가 있을 것 같습니다.
j-kit-128을 이용하여 복습하고, 해당 키트를 이용하여 공부하시는 분들에게 도움이 되고자? 만들게 되었습니다.

기능 구현 및 개선이 되는대로 업로드 하겠습니다.
저도 부족한 점이 많기에 많은 가르침 부탁드립니다.

- datasheet 및 회로도 참고 자료 : http://www.devicemart.co.kr/1059759

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
#include <avr/io.h>
#define F_CPU 16000000UL
#include <util/delay.h>
#include <avr/interrupt.h>
 
#define FND_NUM0 0x3f
#define FND_NUM1 0x06
#define FND_NUM2 0x5b
#define FND_NUM3 0x4f
#define FND_NUM4 0x66
#define FND_NUM5 0x6d
#define FND_NUM6 0x7d
#define FND_NUM7 0x27
#define FND_NUM8 0x7f
#define FND_NUM9 0x6f
 
#define FND_SEL1 0x01
#define FND_SEL2 0x02
#define FND_SEL3 0x04
#define FND_SEL4 0x08
 
unsigned int fnd[10]= {FND_NUM0,FND_NUM1,FND_NUM2,FND_NUM3,FND_NUM4,FND_NUM5,FND_NUM6,FND_NUM7,FND_NUM8,FND_NUM9};
unsigned int fnd_sel[4= {FND_SEL1,FND_SEL2,FND_SEL3,FND_SEL4};
unsigned int count=0, sec=0;
 
void init_led();
void init_buzzer();
void init_fnd();
void init_switch();
void init_timer();
void init_serial();
 
/* 미구현
void init_adc();
void init_cds();
void init_i2c();
*/
 
void fnd_display(int num);
void SendByte(char data);
void SendLine(char *string);
char ReceiveByte();
 
void test_led();
void test_buzzer();
void test_fnd();
void test_switch();
void test_serial();
void test_timer();
 
int main(void)
{
    init_led();
    init_buzzer();
    init_fnd();
    init_switch();
    init_timer();
    init_serial();
    sei();
    while (1)
    {
        //test_led();
        //test_buzzer();
        //test_fnd();
        //test_switch();
        //test_timer();
        //test_serial();
        
    }
}
 
void init_led(){
    DDRA = 0xff;
    PORTA = 0x00;
}
void test_led(){
    PORTA = 0x00;
    _delay_ms(1000);
    PORTA = 0xff;
    _delay_ms(1000);
}
void init_buzzer(){
    // PWM 제어 필요
    DDRB = 0x10;
}
void test_buzzer(){
    PORTB = 0x10;
    _delay_ms(10);
    PORTB = 0x10;
    _delay_ms(10);
}
void init_fnd(){
    DDRC = 0xff;
    DDRG = 0x00;
}
void test_fnd(){
    fnd_display(1234);
}
void fnd_display(int num){
    int fnd1,fnd2,fnd3,fnd4;
    fnd1 = num%10;
    fnd2 = (num/10)%10;
    fnd3 = (num/100)%10;
    fnd4 = num/1000;
 
    PORTC = fnd[fnd1];
    PORTG = fnd_sel[0];
    _delay_ms(1);
    PORTC = fnd[fnd2];
    PORTG = fnd_sel[1];
    _delay_ms(1);
    PORTC = fnd[fnd3];
    PORTG = fnd_sel[2];
    _delay_ms(1);
    PORTC = fnd[fnd4];
    PORTG = fnd_sel[3];
    _delay_ms(1);
}
void init_switch(){
    // pull-up
    DDRE = 0x00;
    PORTE = 0x30;
}
void test_switch(){
    init_led();
    if((PINE & 0x20)== 0x00)
    PORTA = 0xff;
    if((PINE & 0x10)== 0x00)
    PORTA = 0x00;
}
void init_timer(){
    TIMSK = 0x01// R/W 선택 TIMER 0 사용
    TCCR0 = 0x04// 분주비 64
    TCNT0 =256-5// 0에서 시작 255가되어 256이 되면 OVF가 되어 인터럽트 구문을 실행한다.
    /*
    Timer set
    1/16000000 = 0.0000000625
    64분주
    0.0000000625* 64 = 0.000004
    0부터 250회 돌면 0.000004 *250 = 0.001
    OVF 발생 count 증가
    1000번 발생하면 1초 가 된다.
    */
}
void test_timer(){
    init_fnd();
    fnd_display(sec);
}
void init_serial(long BaudRate){
    UBRR0H = 0;
    switch(BaudRate)
    {
        case 115200:
        UBRR0L = 8;
        break;
        
        case 57600:
        UBRR0L = 16;
        break;
        
        case 38400:
        UBRR0L = 25;
        break;
        
        case 19200:
        UBRR0L = 51;
        break;
        
        case 14400:
        UBRR0L = 68;
        break;
        
        case 9600:
        UBRR0L = 103;
        break;
        
        // Default 115200
        default:
        UBRR0L = 8;
        break;
    }
    UCSR0A = 0x00;
    UCSR0B = 0x18;
    UCSR0C = 0x06;  // 8 bit
}
 
void SendByte(char data)
{
    while((UCSR0A & 0x20== 0x00);
    UDR0 = data;
}
 
/*** Function for sending line ***/
void SendLine(char *string)
{
    while(*string != '\0')
    {
        SendByte(*string);
        string++;
    }
}
 
char ReceiveByte(){
    while(!(UCSR0A & (1<<RXC0)));
    return UDR0;
}
 
void test_serial(){
    SendByte('A');
    _delay_ms(10);
    SendLine("Hello World");
    
    if((ReceiveByte() == 'a'&& (ReceiveByte() =='b')){
        SendByte('o');
        SendByte('k');
    }
}
 
ISR(TIMER0_OVF_vect){
    TCNT0 =256-(256-5);
    count++;
    if (count >=1000){
        sec++; count=0;
    }
}
 
cs


반응형

'Project > j-kit-128-1실습' 카테고리의 다른 글

jkit-128-1 추가 기능 구현  (0) 2019.01.22
Sonar_GPS - C#Window Form  (0) 2018.10.21
Sonar_GPS  (0) 2018.10.11
Serial 통신_ 문자열 전송  (0) 2018.10.01
Serial 통신_Uart0_문자 1개  (0) 2018.10.01
반응형

* 인터럽트 : 방해하다, 훼방놓다의 뜻, 긴급하거나 불규칙적인 사건의 처리, 외부와의 인터페이스, 폴링(polling)으로 처리하기에는 프로세싱 타임 낭비

HOW 

 - 현재 수행중인 일을 잠시 중단하고 급한 일을 처리

 - 일이 끝나면 본래의 일을 다시 이어서 수행

 - 이때, 급한 일을 해결하는 작업을 인터럽트 서비스 루틴이라 하는데, 각 인터럽트마다 고유의 인터럽트 서비스 루틴 존재

 - 인터럽트가 여러 개 동시에 걸리면 우선 순위에 따라 조치

* 인터럽트 서비스 루틴

 - 인터럽트가 발생하면 프로세서는 현재 수행중인 프로그램을 멈추고 상태 레지스터와 PC 등을 스택에 잠시 저장 후 인터럽트 서비스 루틴으로 점프, 인터럽트 서비스 루틴을 실행한 후에는 이전의 프로그램으로 복귀하여 정상적인 절차를 실행한다.


* 인터럽트 스위치로 1/100 스톱워치 만들기

 - 메인

1. 초기설정

2. state 변수 값 설정 : stop

3. 인터럽트 활성화

4. state 검사 후 stop 상태시 복사된 10초, 1초, 1/10초, 1/100초 변수를 디스플레이하고, go 상태면 현재의 10초, 1초, 1/10초, 1/100초 변수를 디스플레이
 - 인터럽트 INT4
1. state 값이 stop시 go 변경
2. state go 일시 stop으로 변경 후 이때 시간 값 복사 저장
 - 인터럽트 INT5
1. 변수 및 복사된 시간 값 0으로 초기화
2. state 값 stop으로 초기화

#include <avr/io.h> 

#include <avr/interrupt.h>

#define F_CPU 16000000UL

#define __DELAY_BACKWARD_COMPATIBLE__

#include <util/delay.h>

#define STOP 0

#define GO 1

volatile int cur_time = 0;

volatile int stop_time = 0;

volatile int state = STOP; 

unsigned char digit[]= {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7c, 0x07, 0x7f, 0x67};

unsigned char fnd_sel[4] = {0x01, 0x02, 0x04, 0x08};

SIGNAL(SIG_INTERRUPT4) 

if (state == STOP)

 state = GO; 

  else 

state = STOP; 

  stop_time = cur_time; 

SIGNAL(SIG_INTERRUPT5) 

{

state = STOP; 

cur_time = 0; stop_time = 0; 

}

void init( ) 

{

DDRC = 0xff; // FND Data 

DDRG = 0x0f; // FND Select 

DDRE = 0xcf; // INT4, 5

PORTC = digit[0]; PORTG = 0x0f; EICRB = 0x0a; //falling edge 

EIMSK = 0x30; //interrupt en 

sei(); 

}


void display_fnd(int count) // 수행시간 = 약 10ms 

int i, fnd[4]; 

fnd[3] = (count/1000)%10; // 천 자리

fnd[2] = (count/100)%10; // 백 자리

fnd[1] = (count/10)%10; // 십 자리 

fnd[0] = count%10; // 일 자리 

for (i=0; i<4; i++)

{

PORTC = digit[fnd[i]];

PORTG = fnd_sel[i];

_delay_ms(2+i%2); 

}

}


int main()

{

init( );

while(1)

if (state == STOP)

display_fnd(stop_time);

else display_fnd(cur_time);

cur_time++;

}

}

반응형

'Project > j-kit-128-1실습' 카테고리의 다른 글

FND_Display with Timer/Counter  (0) 2018.10.01
LED 제어  (0) 2016.02.26
FND 실습  (0) 2016.02.26
FND 실습 1/100초 스탑워치, 24시간 디지털 시계만들기  (0) 2016.02.26
회로도 참고  (0) 2016.02.26

+ Recent posts