machtest.DSN
Đây là mạch test
em đang lập trình AVR quét ADC 8 kênh để dò dường sao nó chạy chậm quá
em dùng codevisionAVR
Đây là mạch test
em đang lập trình AVR quét ADC 8 kênh để dò dường sao nó chạy chậm quá
/************************************************** ***
This program was produced by the
CodeWizardAVR V2.03.4 Standard
Automatic Program Generator
© Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date : 1/1/2007
Author :
Company :
Comments:
Chip type : ATmega16
Program type : Application
Clock frequency : 4.000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 256
************************************************** ***/
#include <mega16.h>
#include <delay.h>
int TAM[8]={0,0,0,0,0,0,0,0};
eeprom int luu_ADC[8]={100,100,100,100,100,100,100,100};
int ADC[8]={100,100,100,100,100,100,100,100};
int L[8] ={0,0,0,0,0,0,0,0};
int w[8] ={1,2,4,8,16,32,64,128};
int sensor = 0B00000000;
#define FIRST_ADC_INPUT 0
#define LAST_ADC_INPUT 7
unsigned char adc_data[LAST_ADC_INPUT-FIRST_ADC_INPUT+1];
#define ADC_VREF_TYPE 0x20
// ADC interrupt service routine
// with auto input scanning
interrupt [ADC_INT] void adc_isr(void)
{
static unsigned char input_index=0;
adc_data[input_index]=ADCH;
if (++input_index > (LAST_ADC_INPUT-FIRST_ADC_INPUT))
input_index=0;
ADMUX=(FIRST_ADC_INPUT | (ADC_VREF_TYPE & 0xff))+input_index;
delay_us(10);
ADCSRA|=0x40;
}
void doc_c_bien()
{
L[0]=adc_data[0];
L[1]=adc_data[1];
L[2]=adc_data[2];
L[3]=adc_data[3];
L[4]=adc_data[4];
L[5]=adc_data[5];
L[6]=adc_data[6];
L[7]=adc_data[7];
}
void TB_ADC(int so_lan)
{
int i=0,j=0,n=0;
int max_cb[8]={0,0,0,0,0,0,0,0};
int min_cb[8]={255,255,255,255,255,255,255,255};
int MAX[8] ={0,0,0,0,0,0,0,0};
int KQ_MAX[8]={0,0,0,0,0,0,0,0};
for(n=0;n<so_lan;n++)
{
for(j=0;j<100;j++)
{
doc_c_bien();
for(i=0;i<8;i++)
{
if(max_cb[0]<L[0])
{max_cb[0]=L[0];}
if(min_cb[0]>L[0])
{min_cb[0]=L[0];}
if(max_cb[1]<L[1])
{max_cb[1]=L[1];}
if(min_cb[1]>L[1])
{min_cb[1]=L[1];}
if(max_cb[2]<L[2])
{max_cb[2]=L[2];}
if(min_cb[2]>L[2])
{min_cb[2]=L[2];}
if(max_cb[3]<L[3])
{max_cb[3]=L[3];}
if(min_cb[3]>L[3])
{min_cb[3]=L[3];}
if(max_cb[4]<L[4])
{max_cb[4]=L[4];}
if(min_cb[4]>L[4])
{min_cb[4]=L[4];}
if(max_cb[5]<L[5])
{max_cb[5]=L[5];}
if(min_cb[5]>L[5])
{min_cb[5]=L[5];}
if(max_cb[6]<L[6])
{max_cb[6]=L[6];}
if(min_cb[6]>L[6])
{min_cb[6]=L[6];}
if(max_cb[7]<L[7])
{max_cb[7]=L[7];}
if(min_cb[7]>L[7])
{min_cb[7]=L[7];}
}
}
MAX[0] = max_cb[0]+ MAX[0];
MAX[1] = max_cb[1]+ MAX[1];
MAX[2] = max_cb[2]+ MAX[2];
MAX[3] = max_cb[3]+ MAX[3];
MAX[4] = max_cb[4]+ MAX[4];
MAX[5] = max_cb[5]+ MAX[5];
MAX[6] = max_cb[6]+ MAX[6];
MAX[7] = max_cb[7]+ MAX[7];
}
for(i=0;i<8;i++)
{
KQ_MAX[i] = MAX[i]/n;
MAX[i]=0;
}
for(i=0;i<8;i++)
{
TAM[i]=(KQ_MAX[i] + min_cb[i])/2; //Approximation ADC
}
}
void cam_bien()// Thoi gian de doc duoc ham nay la 276us
{
sensor=0B00000000;
L[0]=adc_data[0];
L[1]=adc_data[1];
L[2]=adc_data[2];
L[3]=adc_data[3];
L[4]=adc_data[4];
L[5]=adc_data[5];
L[6]=adc_data[6];
L[7]=adc_data[7];
if(L[0]<=luu_ADC[0])
{
PORTC.0 = 1 ;
}
else PORTC.0 = 0;
if(L[1]<=luu_ADC[1])
{
PORTC.1 = 1 ;
}
else PORTC.1 = 0;
if(L[2]<=luu_ADC[2])
{
PORTC.2 = 1 ;
}
else PORTC.2 = 0;
if(L[3]<=luu_ADC[3])
{
PORTC.3 = 1 ;
}
else PORTC.3 = 0;
if(L[4]<=luu_ADC[4])
{
PORTC.4 = 1 ;
}
else PORTC.4 = 0;
if(L[5]<=luu_ADC[5])
{
PORTC.5 = 1 ;
}
else PORTC.5 = 0;
if(L[7]<=luu_ADC[7])
{
PORTC.7 = 1 ;
}
else PORTC.7 = 0;
if(L[6]<=luu_ADC[0])
{
PORTC.6 = 1 ;
}
else PORTC.6 = 0;
}
void luu_CB()
{
int i=0;
for(i=0;i<8;i++)
{
luu_ADC[i]=ADC[i];
}
}
void main(void)
{
PORTA=0x00;
DDRA=0x00;
PORTB=0x00;
DDRB=0x00;
PORTC=0xff;
DDRC=0xFF;
PORTD=0x00;
DDRD=0x00;
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
MCUCR=0x00;
MCUCSR=0x00;
TIMSK=0x00;
ACSR=0x80;
SFIOR=0x00;
ADMUX=FIRST_ADC_INPUT | (ADC_VREF_TYPE & 0xff);
ADCSRA=0xCA;
luu_CB();
// Global enable interrupts
#asm("sei")
while (1)
{
TB_ADC(10);
doc_c_bien();
cam_bien();
};
}
This program was produced by the
CodeWizardAVR V2.03.4 Standard
Automatic Program Generator
© Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date : 1/1/2007
Author :
Company :
Comments:
Chip type : ATmega16
Program type : Application
Clock frequency : 4.000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 256
************************************************** ***/
#include <mega16.h>
#include <delay.h>
int TAM[8]={0,0,0,0,0,0,0,0};
eeprom int luu_ADC[8]={100,100,100,100,100,100,100,100};
int ADC[8]={100,100,100,100,100,100,100,100};
int L[8] ={0,0,0,0,0,0,0,0};
int w[8] ={1,2,4,8,16,32,64,128};
int sensor = 0B00000000;
#define FIRST_ADC_INPUT 0
#define LAST_ADC_INPUT 7
unsigned char adc_data[LAST_ADC_INPUT-FIRST_ADC_INPUT+1];
#define ADC_VREF_TYPE 0x20
// ADC interrupt service routine
// with auto input scanning
interrupt [ADC_INT] void adc_isr(void)
{
static unsigned char input_index=0;
adc_data[input_index]=ADCH;
if (++input_index > (LAST_ADC_INPUT-FIRST_ADC_INPUT))
input_index=0;
ADMUX=(FIRST_ADC_INPUT | (ADC_VREF_TYPE & 0xff))+input_index;
delay_us(10);
ADCSRA|=0x40;
}
void doc_c_bien()
{
L[0]=adc_data[0];
L[1]=adc_data[1];
L[2]=adc_data[2];
L[3]=adc_data[3];
L[4]=adc_data[4];
L[5]=adc_data[5];
L[6]=adc_data[6];
L[7]=adc_data[7];
}
void TB_ADC(int so_lan)
{
int i=0,j=0,n=0;
int max_cb[8]={0,0,0,0,0,0,0,0};
int min_cb[8]={255,255,255,255,255,255,255,255};
int MAX[8] ={0,0,0,0,0,0,0,0};
int KQ_MAX[8]={0,0,0,0,0,0,0,0};
for(n=0;n<so_lan;n++)
{
for(j=0;j<100;j++)
{
doc_c_bien();
for(i=0;i<8;i++)
{
if(max_cb[0]<L[0])
{max_cb[0]=L[0];}
if(min_cb[0]>L[0])
{min_cb[0]=L[0];}
if(max_cb[1]<L[1])
{max_cb[1]=L[1];}
if(min_cb[1]>L[1])
{min_cb[1]=L[1];}
if(max_cb[2]<L[2])
{max_cb[2]=L[2];}
if(min_cb[2]>L[2])
{min_cb[2]=L[2];}
if(max_cb[3]<L[3])
{max_cb[3]=L[3];}
if(min_cb[3]>L[3])
{min_cb[3]=L[3];}
if(max_cb[4]<L[4])
{max_cb[4]=L[4];}
if(min_cb[4]>L[4])
{min_cb[4]=L[4];}
if(max_cb[5]<L[5])
{max_cb[5]=L[5];}
if(min_cb[5]>L[5])
{min_cb[5]=L[5];}
if(max_cb[6]<L[6])
{max_cb[6]=L[6];}
if(min_cb[6]>L[6])
{min_cb[6]=L[6];}
if(max_cb[7]<L[7])
{max_cb[7]=L[7];}
if(min_cb[7]>L[7])
{min_cb[7]=L[7];}
}
}
MAX[0] = max_cb[0]+ MAX[0];
MAX[1] = max_cb[1]+ MAX[1];
MAX[2] = max_cb[2]+ MAX[2];
MAX[3] = max_cb[3]+ MAX[3];
MAX[4] = max_cb[4]+ MAX[4];
MAX[5] = max_cb[5]+ MAX[5];
MAX[6] = max_cb[6]+ MAX[6];
MAX[7] = max_cb[7]+ MAX[7];
}
for(i=0;i<8;i++)
{
KQ_MAX[i] = MAX[i]/n;
MAX[i]=0;
}
for(i=0;i<8;i++)
{
TAM[i]=(KQ_MAX[i] + min_cb[i])/2; //Approximation ADC
}
}
void cam_bien()// Thoi gian de doc duoc ham nay la 276us
{
sensor=0B00000000;
L[0]=adc_data[0];
L[1]=adc_data[1];
L[2]=adc_data[2];
L[3]=adc_data[3];
L[4]=adc_data[4];
L[5]=adc_data[5];
L[6]=adc_data[6];
L[7]=adc_data[7];
if(L[0]<=luu_ADC[0])
{
PORTC.0 = 1 ;
}
else PORTC.0 = 0;
if(L[1]<=luu_ADC[1])
{
PORTC.1 = 1 ;
}
else PORTC.1 = 0;
if(L[2]<=luu_ADC[2])
{
PORTC.2 = 1 ;
}
else PORTC.2 = 0;
if(L[3]<=luu_ADC[3])
{
PORTC.3 = 1 ;
}
else PORTC.3 = 0;
if(L[4]<=luu_ADC[4])
{
PORTC.4 = 1 ;
}
else PORTC.4 = 0;
if(L[5]<=luu_ADC[5])
{
PORTC.5 = 1 ;
}
else PORTC.5 = 0;
if(L[7]<=luu_ADC[7])
{
PORTC.7 = 1 ;
}
else PORTC.7 = 0;
if(L[6]<=luu_ADC[0])
{
PORTC.6 = 1 ;
}
else PORTC.6 = 0;
}
void luu_CB()
{
int i=0;
for(i=0;i<8;i++)
{
luu_ADC[i]=ADC[i];
}
}
void main(void)
{
PORTA=0x00;
DDRA=0x00;
PORTB=0x00;
DDRB=0x00;
PORTC=0xff;
DDRC=0xFF;
PORTD=0x00;
DDRD=0x00;
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
MCUCR=0x00;
MCUCSR=0x00;
TIMSK=0x00;
ACSR=0x80;
SFIOR=0x00;
ADMUX=FIRST_ADC_INPUT | (ADC_VREF_TYPE & 0xff);
ADCSRA=0xCA;
luu_CB();
// Global enable interrupts
#asm("sei")
while (1)
{
TB_ADC(10);
doc_c_bien();
cam_bien();
};
}
Comment