[소스] ADC 인터럽트 방식 소스
아래는 가속도 센서 X,Y,Z 축 각각을 g값으로 변환하여 보여주는 소스입니다.
참고 바랍니다.
//ICC-AVR application builder : 2011-01-21 오전 11:40:49
// Target : M128
// Crystal: 16.000Mhz
#include <iom128v.h>
#include <macros.h>
#include <stdio.h>
#define DONE 1
#define NO 0
#define Xaxis 0
#define Yaxis 1
#define Zaxis 2
volatile int value1, value2, value3, adcstatus, flag, adc_flag;
int putchar(char c)
{
while (((UCSR0A>>UDRE0)&0x01) == 0) ; // UDRE, data register empty
UDR0 = c;
return c;
}
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
PORTE = 0x00;
DDRE = 0x00;
PORTF = 0x00;
DDRF = 0x00;
PORTG = 0x00;
DDRG = 0x00;
}
void uart0_init(void)
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x06;
UBRR0L = 0x67; //set baud rate lo
UBRR0H = 0x00; //set baud rate hi
UCSR0B = 0x98;
}
void delay(int data)
{
int i,j;
for(i=0;i<667;i++){
for(j=0;j<data;j++){
}
}
}
//ADC initialize
// Conversion time: 104uS
void adc_init(void)
{
ADCSRA = 0x00; //disable adc
ADMUX = 0x00; //select adc input 0
ACSR = 0x80;
ADCSRA = 0x0F;
}
#pragma interrupt_handler adc_isr:iv_ADC
void adc_isr(void)
{
//conversion complete, read value (int) using...
if(adc_flag == Xaxis){
value1=ADCL; //Read 8 low bits first (important)
value1|=(int)ADCH << 8; //read 2 high bits and shift into top byte
value1 &= 0x03ff;
adcstatus=DONE;
}
if(adc_flag == Yaxis){
value2=ADCL; //Read 8 low bits first (important)
value2|=(int)ADCH << 8; //read 2 high bits and shift into top byte
value2 &= 0x03ff;
adcstatus=DONE;
}
if(adc_flag == Zaxis){
value3=ADCL; //Read 8 low bits first (important)
value3|=(int)ADCH << 8; //read 2 high bits and shift into top byte
value3 &= 0x03ff;
adcstatus=DONE;
}
flag=1;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
XDIV = 0x00; //xtal divider
XMCRA = 0x00; //external memory
port_init();
adc_init();
uart0_init();
MCUCR = 0x00;
EICRA = 0x00; //extended ext ints
EICRB = 0x00; //extended ext ints
EIMSK = 0x00;
TIMSK = 0x00; //timer interrupt sources
ETIMSK = 0x00; //extended timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//
void main(void)
{
float accel1, accel2, accel3, volt1, volt2, volt3;
init_devices();
//insert your functional code here...
ADCSRA &= 0x7f;
ADMUX=0x00;
ADCSRA |= 0xc0;
while(1){
adc_flag=Xaxis;
ADMUX=0x00;
ADCSRA |= 0x40;
while(flag == 0);
flag = 0;
delay(10);
if(adcstatus == DONE){
volt1 = ((float)value1)*5/1024;
accel1 = (volt1-1.65)/0.8;
adcstatus = NO;
}
adc_flag=Yaxis;
ADMUX=0x02;
ADCSRA |= 0x40;
while(flag == 0);
flag = 0;
delay(10);
if(adcstatus == DONE){
volt2 = ((float)value2)*5/1024;
accel2 = (volt2-1.65)/0.8;
adcstatus = NO;
delay(10);
}
adc_flag=Zaxis;
ADMUX=0x04;
ADCSRA |= 0x40;
while(flag == 0);
flag = 0;
delay(10);
if(adcstatus == DONE){
volt3 = ((float)value3)*5/1024;
accel3 = (volt3-2.45)/0.8;
adcstatus = NO;
delay(10);
}
printf("X : %.2fg, Y : %.2fg, Z : %.2fg\\r\\n",accel1,accel2,accel3);
delay(10);
}
}
