Hard Ware/Atmega 128!!

여러개의 ADC 사용할때 주의 사항!!!

달려가보자 2011. 9. 14. 19:25
여러개의 ADC를 사용할때에는 MUX를 미리 설정하고 그다음에 함수를 불러들려서 값을 

읽어야 한다 !!.

ex)
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include <util/delay.h>

#define  F_CPU               16000000L
#define  TRUE 1
#define  FALSE 0

void ADC_START(unsigned int *g_adcData0)
{
unsigned int ADC_DataH=0,ADC_DataL = 0;
//cCount++;
ADMUX = 0x00;
ADCSRA = 0xc3;
*g_adcData0 = 0;
while(!(ADCSRA & (1<<ADIF)));
ADC_DataL = ADCL & 0xff;

ADC_DataH = ADCH & 0x03;

//printf("L : %d\n",ADC_DataL);
*g_adcData0 = (ADC_DataH <<8) | ADC_DataL;
//*g_adcData = (*g_adcData >> 6);
//*g_adcData = ADC_DataH;
    ADMUX = (1<<MUX2);
ADCSRA |= (1<<4);
}
void ADC_START1(unsigned int *g_adcData1)
{
unsigned int ADC_DataH=0,ADC_DataL = 0;
ADCSRA = 0xc3;
*g_adcData1 = 0;
while(!(ADCSRA & (1<<ADIF)));
ADC_DataL = ADCL & 0xff;
ADC_DataH = ADCH & 0x03;

*g_adcData1= (ADC_DataH <<8) | ADC_DataL;

ADMUX = (1<<MUX1);
ADCSRA |= (1<<4);

}
int ADC_START2(unsigned int *g_adcData2)
{
unsigned int ADC_DataH=0,ADC_DataL = 0;

ADCSRA = 0xc3;
*g_adcData2 = 0;
while(!(ADCSRA & (1<<ADIF)));

ADC_DataL = ADCL & 0xff;
ADC_DataH = ADCH & 0x03;


*g_adcData2 = (ADC_DataH <<8) | ADC_DataL;


ADMUX = (1<<MUX1)|(1<<MUX0);
ADCSRA |= (1<<4);

}
int ADC_START3(unsigned int *g_adcData3)
{
unsigned int ADC_DataH=0,ADC_DataL = 0;

ADCSRA = 0xc3;
*g_adcData3 = 0;
while(!(ADCSRA & (1<<ADIF)));
ADC_DataL = ADCL & 0xff;
ADC_DataH = ADCH & 0x03;

*g_adcData3 = (ADC_DataH <<8) | ADC_DataL;

ADMUX = 0x00;
ADCSRA |= (1<<4);
}
int INIT_PIN()
{
DDRF = 0x00;
DDRE = 0x02;
DDRB = 0xff;
DDRD = 0x08;
PORTF = 0x00;
PORTE = 0x02;
PORTD = 0x08;
return TRUE;
}

int INIT_USART0(int baud0)
{
int ubrr0 = F_CPU/16/baud0-1; // baud rate setting
UCSR0A = 0x00;
UCSR0B = 0x18;
UCSR0C = 0x06;
UBRR0H = (unsigned char)(ubrr0 >> 8); //int value  shift 8 right 
UBRR0L = (unsigned char)ubrr0; //low data setting

return TRUE;
}
int INIT_USART1(int baud1)
{
int ubrr1 = F_CPU/16/baud1-1; // baud rate setting
UCSR1A = 0x00;
UCSR1B = 0x18;
UCSR1C = 0x06;
UBRR1H = (unsigned char)(ubrr1 >> 8); //int value  shift 8 right 
UBRR1L = (unsigned char)ubrr1; //low data setting

return TRUE;
}
int USART0_Putch(char Usdata0)
{
while(!(UCSR0A & (1<<5)));
UDR0 = Usdata0;

return TRUE;
}
int USART1_Putch(unsigned char Usdata1, FILE *stream )
{
if( Usdata1 == '\n' ) USART1_Putch( '\r',stream);
while(!(UCSR1A & (1<<5)));
UDR1 = Usdata1;

return TRUE;
}

int Sleep_mode(void)
{
MCUCR = (1<<5) | (1<SM0);
return TRUE;
}



int main()
{
FILE usart1 = FDEV_SETUP_STREAM( USART1_Putch,NULL,_FDEV_SETUP_RW );
  stdin = stdout = &usart1;
  int i = 0;
unsigned int ADC0_Data=0,ADC1_Data = 0, ADC2_Data = 0, ADC3_Data = 0; 
if(INIT_USART0(9600)) printf("USART SET OK\n"); 
if(INIT_USART1(9600)) printf("USART SET OK\n"); 
if(INIT_PIN()) printf("PIN SET OK\n");
//if(INIT_INTERRUPT()) printf("INTERRUPT SET OK\n");
if(Sleep_mode()) printf("Sleep Mode SET OK\n");
for(;;)
{
ADC_START(&ADC0_Data);
_delay_us(10);
ADC_START1(&ADC1_Data);
_delay_us(10);
ADC_START2(&ADC2_Data);
_delay_us(10);
ADC_START3(&ADC3_Data);
_delay_us(10);
printf("ADC : %d , ADC1 :%d , ADC2 : %d, ADC3 : %d\n",ADC0_Data,ADC1_Data,ADC2_Data,ADC3_Data);
_delay_ms(100);
}
return 0;
}

사이에 딜레이를 주는 이유는 ADC를 한번하고 다음 ADC까지의 25주기의 클럭 주기가 필요하기
때문이다!!.