ADC interrupt
Posted: 29 Jan 2010 08:29
I want to read voltage at AN0->AN3 then display on PORTC and PORTD
adc_data[] stores AD conversion result
This is my program
unsigned int adc_data[4];
unsigned char analog_selected=0;
unsigned char adc_flag=0;
void interrupt()
{ if(PIR1.ADIF==1)
adc_flag=1;
{if((analog_selected==0) && (adc_flag==1))
{adc_data[0]=(ADRESH << 8) | ADRESL; // read dadta from channel 0
analog_selected++; // increase channel index
adc_flag=0;
ADCON0=0b11001001; // select channel 1
}
if((analog_selected==1)&&(adc_flag==1))
{adc_data[1]=(ADRESH << 8) | ADRESL; // read dadta from channel 1
analog_selected++; // increase channel index
adc_flag=0;
ADCON0=0b11010001; // select channel 2;
}
if((analog_selected==2)&&(adc_flag==1))
{adc_data[2]=(ADRESH << 8) | ADRESL; // read dadta from channel 2
analog_selected++; // increase channel index
adc_flag=0;
ADCON0=0b11011001; // select channel 3;
}
if((analog_selected==3)&&(adc_flag==1))
{adc_data[3]=(ADRESH << 8) | ADRESL; // read dadta from channel 3
analog_selected=0;
adc_flag=0; // return channel 0
ADCON0=0b11000001; // select channel 0;
}
Delay_Cyc(3); //wait acquisition time
ADCON0.F2=1; //start conversion again
PIR1.ADIF=0; // clear AD interrupt flag
}
}
void main()
{unsigned int i=0,temp;
TRISA=0xFF;
TRISE=0x0F;
TRISB=0xFF;
TRISD=0;
TRISC=0;
PORTD=0;
PORTC=0;
ADCON1=0x82; // AN0->AN4 selected as analog input
ADCON0=0b11000001; // Configue analog mode
INTCON.GIE = 1; //Enable global interrupt
INTCON.PEIE = 1; //Enable peripheral interrupt
PIE1.ADIE = 1; //Enable ADC interrupt
Delay_us(20); //wait for acquisition time
ADCON0.F2=1; //start conversion
while(1)
{ temp=adc_data[i]; // read data from channel i
i++;
if(i==4) i=0;
PORTD = temp; // Send lower 8 bits to PORTD
PORTC = temp >> 2; // Send 2 most significant bits to RB7, RB6
delay_ms(1000);
}
}
But it doesn't run. Please hepl me. Thank so much
adc_data[] stores AD conversion result
This is my program
unsigned int adc_data[4];
unsigned char analog_selected=0;
unsigned char adc_flag=0;
void interrupt()
{ if(PIR1.ADIF==1)
adc_flag=1;
{if((analog_selected==0) && (adc_flag==1))
{adc_data[0]=(ADRESH << 8) | ADRESL; // read dadta from channel 0
analog_selected++; // increase channel index
adc_flag=0;
ADCON0=0b11001001; // select channel 1
}
if((analog_selected==1)&&(adc_flag==1))
{adc_data[1]=(ADRESH << 8) | ADRESL; // read dadta from channel 1
analog_selected++; // increase channel index
adc_flag=0;
ADCON0=0b11010001; // select channel 2;
}
if((analog_selected==2)&&(adc_flag==1))
{adc_data[2]=(ADRESH << 8) | ADRESL; // read dadta from channel 2
analog_selected++; // increase channel index
adc_flag=0;
ADCON0=0b11011001; // select channel 3;
}
if((analog_selected==3)&&(adc_flag==1))
{adc_data[3]=(ADRESH << 8) | ADRESL; // read dadta from channel 3
analog_selected=0;
adc_flag=0; // return channel 0
ADCON0=0b11000001; // select channel 0;
}
Delay_Cyc(3); //wait acquisition time
ADCON0.F2=1; //start conversion again
PIR1.ADIF=0; // clear AD interrupt flag
}
}
void main()
{unsigned int i=0,temp;
TRISA=0xFF;
TRISE=0x0F;
TRISB=0xFF;
TRISD=0;
TRISC=0;
PORTD=0;
PORTC=0;
ADCON1=0x82; // AN0->AN4 selected as analog input
ADCON0=0b11000001; // Configue analog mode
INTCON.GIE = 1; //Enable global interrupt
INTCON.PEIE = 1; //Enable peripheral interrupt
PIE1.ADIE = 1; //Enable ADC interrupt
Delay_us(20); //wait for acquisition time
ADCON0.F2=1; //start conversion
while(1)
{ temp=adc_data[i]; // read data from channel i
i++;
if(i==4) i=0;
PORTD = temp; // Send lower 8 bits to PORTD
PORTC = temp >> 2; // Send 2 most significant bits to RB7, RB6
delay_ms(1000);
}
}
But it doesn't run. Please hepl me. Thank so much