ADConverter
#include <avr/io.h>
#include <util/delay.h>
#define FND0 0xFE //FND0
#define FND1 0xFD //FND1
#define FND2 0xFB //FND2
#define FND3 0xF7 //FND3
void setup(void);
void setupADC(void);
void fndDisplay( unsigned char data, unsigned char select);
int num[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x27, 0x7F, 0x6F};
int main(void)
{
int adcValue = 0;
setup();
setupADC();
while(1){
//ADC 변환된 값 읽어오기
// adc 변환 다 되었는지 확인 ADIF (xxx1 xxxx)
while( (ADCSRA & 0x10) == 0 )
;
// ADC 데이타 레지스터(ADCL, ADCH)에서 데이타를 읽어온다.
adcValue = ADCL;
adcValue = (ADCH << 8) | adcValue; // 16비트로 만들어줘야함.
// adch 8 비트 쉬프트 시켜서 or연산을 통해 adcl 과 합친다.
fndDisplay( num[adcValue / 1000], FND0 );
fndDisplay( num[adcValue / 100 % 10], FND1 );
fndDisplay( num[adcValue / 10 % 10], FND2 );
fndDisplay( num[adcValue % 10], FND3 );
//_delay_ms(800);
}
return 0;
}
void setupADC(void)
{ // 좌정렬(xx1x xxxx)
ADMUX = 0xC0; // 1100 0000 <- 내부 2.56W (11xx xxxx) 우정렬(xx0x xxxx) 0채널(xxx0 0000)
ADCSR = 0xA6; // <- ADEN(1xxx xxx) AD컨버터 on, ADFR(xx1x xxxx) 프리러닝 on, ADPS2~0(xxxx x110) 분주비 64
//시작안함 | 인터럽트 사용 안함 | free running 사용 안함. (xooo oxxx)
// 64 분주비 xxxx xx110
// 1000 0110
ADCSRA |= 0x40; // 시작하기. 0100 0000
// 그냥 쓰면 위에 설정 초기화 되니까 |
}
void setup(void)
{
// fnd 설정한다.
// pe0~&, data, pg3-0 ee
DDRE = 0xFF;
DDRG = 0x0F;
}
void fndDisplay( unsigned char data, unsigned char select)
{
PORTE = data;
PORTG = select;
_delay_ms(5);
}
'AVR' 카테고리의 다른 글
config (0) | 2015.04.14 |
---|---|
exUART (0) | 2015.04.14 |
PDF - AVR MCU (ATmega128) (0) | 2015.04.13 |
interruptClock (0) | 2015.04.13 |
interruptTimer (0) | 2015.04.13 |
interruptStopWatch (0) | 2015.04.13 |
Interrupt (0) | 2015.04.10 |
switch (0) | 2015.04.10 |