티스토리 뷰


ADMUX Register의 REFS bit를 사용해서 내부 기준 전압 2.56V를 사용한다.

ADC는 128분주비, ADC 데이터 좌측정렬

SW 1을 눌렀을 떄 ADC interrupt가 발생, ADC중지 및 LED 전체 ON

SW 2를 눌렀을 때 ADC 다시 시작하고 LED 전체가 꺼지고, ADC MODE는 단일 Running Mode를 실행

SW 3를 눌렀을 때 Free Running Mode 실행


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <stdlib.h>
#include <delay.h>
#include <mega128_bits.h>
#include <mega128.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "LCD.h"
 
unsigned char status;
unsigned char led;
unsigned char modeSel;
unsigned short int temp;
 
void LCD_Decimal(unsigned char num, short AD_dat)
{
 
             unsigned char Decimal[5];
             Decimal[4= '0'+ AD_dat/10000;
             AD_dat = AD_dat%10000;
             Decimal[3= '0' + AD_dat/1000;
             AD_dat = AD_dat%1000;
             Decimal[2= '0' + AD_dat/100;
             AD_dat = AD_dat%100;
             Decimal[1= '0' + AD_dat/10;
             AD_dat = AD_dat%10;
             Decimal[0= '0' + AD_dat/1;
             
             if(num == 0)
             {
               LCD_Pos(0,10);
               LCD_Data(Decimal[3]);
               LCD_Pos(0,11);
               LCD_Data(Decimal[2]);
               LCD_Pos(0,12);
               LCD_Data(Decimal[1]);
               LCD_Pos(0,13);
               LCD_Data(Decimal[0]);
             }                      
             
             else if(num == 1)
             {
               LCD_Pos(1,10);
               LCD_Data(Decimal[4]);
               LCD_Pos(1,11);
               LCD_Data('.');
               LCD_Pos(1,12);
               LCD_Data(Decimal[3]);
               LCD_Pos(1,13);
               LCD_Data(Decimal[2]);
               LCD_Pos(1,14);
               LCD_Data(Decimal[1]);
               LCD_Pos(1,15);
               LCD_Data(Decimal[0]);
               LCD_Pos(1,16);
               LCD_Data('v');
             }
}
 
void AD_Init(void)
{
             ADMUX |= (1<<REFS1) | (1<<REFS0) | (1<<ADLAR);
             ADCSRA |= (1<<ADEN) | (1<<ADSC) | (1<<ADFR);
}
 
void ADC_Stop(void)
{
           ADCSRA &= 0b01111111;
           status = 0;
}
 
void ADC_Restart(void)
{          
           ADCSRA &= 0b00;
           ADCSRA |= (1<<ADEN) | (1<<ADSC);
           status = 0;
}
void ADC_FreeRunning(void)
{
            ADCSRA |= (1<<ADEN) | (1<<ADSC) | (1<<ADFR);
}
 
interrupt [2void int0_func(){   // INT.0의 인터럽트 벡터는 2
           status = 1;
           led = 0xff;
}
 
interrupt [3void int1_func(){   // INT.1의 인터럽트 벡터는 3
           status = 2;
           led = 0x00;
}
 
interrupt [4void int2_func(){   // INT.2의 인터럽트 벡터는 4
           status = 3;
}
 
interrupt [5void int3_func(){   // INT.3의 인터럽트 벡터는 5
           if(modeSel == 0)
           {
                      modeSel = 1;
                      ADMUX |= 0b00100000;
 
           }else{
                      modeSel = 0;
                      ADMUX &= 0b11011111;                      
           }
}
 
void main(void)
{
             AD_Init();
             EIMSK = 0x0f;
             EICRA = 0x00;
             SREG |= 0x80;
             
             status = 0
             led = 0;
             modeSel = 0;
             temp = 0;
          
             DDRB = 0xff;
             DDRD = 0x00;
             DDRF = 0xff;     // ADC
             
             Port_Init();
             LCD_Init();
             
             LCD_Pos(0,0);
             LCD_STR("Voltage:");
             
             LCD_Pos(1,0);
             LCD_STR("R.Value:");
             
             while(1)
             {         
                      short Voltage;
                      PORTB = ~led;
                      
                      if(status == 1){
                                 ADC_Stop();
                      }
                      else if(status == 2){
                                 ADC_Restart();
                      }
                      else if(status == 3){
                                 ADC_FreeRunning();
                      }          
                      
                      if(modeSel == 0)
                      {
                                 temp = ADCW;
                      }else{
                                 temp = ADCL | (ADCH << 8); // 또는 ADCL + (ADCH*256);
                                 temp = (temp >> 6); 
                      }
                      
                        Voltage = (short)((0.0025*temp)*10000);
                        LCD_Decimal(0, temp);
                        LCD_Decimal(1, Voltage);
                        delay_ms(100); 
                              
 
             }
}
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <stdlib.h>
#include <delay.h>
#include <mega128_bits.h>
#include <mega128.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "LCD.h"
 
#define MAXLEN 16
#define ENTER 13
 
unsigned char status;
unsigned char led;
unsigned char modeSel;
unsigned short int temp;
 
unsigned char mode;
 
unsigned char lastData;
unsigned char insertData;
           
 
 
void Init_USART1(void)
{
             UCSR1A = 0x00;
             UCSR1B = (1<<RXCIE1)|(1<<UDRIE1)|(1<<RXEN1);
             
             UCSR1C = (1<<UCSZ11)|(1<<UCSZ10);
             UCSR1C &= ~(1<<UMSEL0);
             
             UBRR1H = 0x00;
             UBRR1L = 0x07;
             
             SREG = 0x80;
}
 
 
 
void LCD_Decimal(unsigned char num, short AD_dat)
{
 
             unsigned char Decimal[5];
             Decimal[4= '0'+ AD_dat/10000;
             AD_dat = AD_dat%10000;
             Decimal[3= '0' + AD_dat/1000;
             AD_dat = AD_dat%1000;
             Decimal[2= '0' + AD_dat/100;
             AD_dat = AD_dat%100;
             Decimal[1= '0' + AD_dat/10;
             AD_dat = AD_dat%10;
             Decimal[0= '0' + AD_dat/1;
             
             if(num == 0)
             {
               LCD_Pos(0,10);
               LCD_Data(Decimal[3]);
               LCD_Pos(0,11);
               LCD_Data(Decimal[2]);
               LCD_Pos(0,12);
               LCD_Data(Decimal[1]);
               LCD_Pos(0,13);
               LCD_Data(Decimal[0]);
             }                      
             
             else if(num == 1)
             {
               LCD_Pos(1,10);
               LCD_Data(Decimal[4]);
               LCD_Pos(1,11);
               LCD_Data('.');
               LCD_Pos(1,12);
               LCD_Data(Decimal[3]);
               LCD_Pos(1,13);
               LCD_Data(Decimal[2]);
               LCD_Pos(1,14);
               LCD_Data(Decimal[1]);
               LCD_Pos(1,15);
               LCD_Data(Decimal[0]);
               LCD_Pos(1,16);
               LCD_Data('v');
             }
}
 
void AD_Init(void)
{
             ADMUX |= (1<<REFS1) | (1<<REFS0) | (1<<ADLAR);
             ADCSRA |= (1<<ADSC) | (1<<ADFR);
}
 
void ADC_Stop(void)
{
           ADCSRA &= 0b01111111;
           status = 0;
}
 
void ADC_Restart(void)
{          
           ADCSRA &= 0b00;
           ADCSRA |= (1<<ADEN) | (1<<ADSC);
           status = 0;
}
void ADC_FreeRunning(void)
{
            ADCSRA |= (1<<ADEN) | (1<<ADSC) | (1<<ADFR);
}
 
interrupt [2void int0_func(){   // INT.0의 인터럽트 벡터는 2
           status = 1;
           led = 0xff;
}
 
interrupt [3void int1_func(){   // INT.1의 인터럽트 벡터는 3
           status = 2;
           led = 0x00;
}
 
interrupt [4void int2_func(){   // INT.2의 인터럽트 벡터는 4
           status = 3;
}
 
interrupt [5void int3_func(){   // INT.3의 인터럽트 벡터는 5
           if(mode == 0)
           {
                      mode = 0;    
           }
}
 
interrupt [USART1_RXC] void usart1_receive(void)
{
             while(!(UCSR1A & (1<<UDRE1)));
             
             insertData = UDR1;
             
             if(insertData == 'S'){
                      ADCSRA |= (1<<ADEN) | (1<<ADSC) | (1<<ADFR); 
                      UCSR1B = 0x00;
                      UCSR1C = 0x00;
              mode = 1;                  
             }else if(insertData == 'Q')
             {       
                      ADCSRA |= (1<<ADEN) | (1<<ADSC) | (1<<ADFR); 
                      UCSR1B = 0x00;
                      UCSR1C = 0x00;
                      mode = 1
             }SSSS
 
             }
             
             lastData = insertData;
}   
 
void main(void)
{
 
             Init_USART1();
 
             AD_Init();
             EIMSK = 0x0f;
             EICRA = 0x00;
             SREG |= 0x80;
             
             status = 0
             led = 0;
             modeSel = 0;
             temp = 0;
             
             insertData = '\0';
             lastData = '\0'
             
             DDRB = 0xff;
             DDRD = 0x00;
             DDRF = 0xff;     // ADC
             
             Port_Init();
             LCD_Init();
             
             LCD_Pos(0,0);
             LCD_STR("Voltage:");
             
             LCD_Pos(1,0);
             LCD_STR("R.Value:");
             
             mode = 0;
             
             while(1)
             {         
                      short Voltage;
                      PORTB = ~led;                     
                      
                      
                      if(status == 1){
                                 ADC_Stop();
                      }
                      else if(status == 2){
                                 ADC_Restart();
                      }
                      else if(status == 3){
                                 ADC_FreeRunning();
                      }          
                      
                      if(mode == 1)
                      {
                                 UCSR1B = (1<<RXCIE1)|(1<<UDRIE1)|(1<<RXEN1);
             
                                 UCSR1C = (1<<UCSZ11)|(1<<UCSZ10);
                      }
                          
                      
                        temp = ADCL | (ADCH << 8); // 또는 ADCL + (ADCH*256);
                                 temp = (temp >> 6);
                        Voltage = (short)((0.0025*temp)*10000);
                        LCD_Decimal(0, temp);
                        LCD_Decimal(1, Voltage);
                        delay_ms(100); 
                      
             }
}
cs


'프로그래밍 > CodeVision' 카테고리의 다른 글

181002 마이크로프로세서  (0) 2018.10.02
190918 마이크로프로세서  (0) 2018.09.18
180904 마이크로프로세서  (0) 2018.09.04
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2025/01   »
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
글 보관함