⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ad.c

📁 atmega16单片机AD转换源代码
💻 C
字号:
//ICC-AVR application builder : 2005-6-21 15:56:32
// Target : M16
// Crystal: 8.0000Mhz

#include <iom16v.h>
#include <macros.h>
#include "HD7279.C"
#include "TIMEDELAY.C"
unsigned int adc_rel;
unsigned int adc_old;
unsigned char ad_mux;




void port_init(void)
{
 PORTA = 0xFF;
 DDRA  = 0x00;
 PORTB = 0xFF;
 DDRB  = 0x00;
 PORTC = 0xFF; //m103 output only
 DDRC  = 0x1E;
 PORTD = 0xFF;
 DDRD  = 0x00;
}

//ADC initialisation
// Conversion time: 104uS
void adc_init(void)
{
 PORTA = 0X00;
 PORTA = 0Xff;
 
 ADCSR = 0x00; //disable adc
 ADMUX = ( 0<< REFS0 )|( 0<< REFS1 )|ad_mux & 0x0f; //select adc input 0		  //00000000
 ACSR  = ( 1<< ACD); 			
 ADCSR = (1 << ADEN ) |( 1 << ADSC ) |( 1 << ADIE ) | BIT( ADPS2)| BIT( ADPS1);
}

void bubble_sort(int *x, int n)
{
 int j, k, h, t;
  
 for (h=n-1; h>0; h=k) /*循环到没有比较范围*/
 {
  for (j=0, k=0; j<h; j++) /*每次预置k=0,循环扫描后更新k*/
  {
   if (*(x+j) > *(x+j+1)) /*大的放在后面,小的放到前面*/
   {
    t = *(x+j);
    *(x+j) = *(x+j+1);
    *(x+j+1) = t; /*完成交换*/
    k = j; /*保存最后下沉的位置。这样k后面的都是排序排好了的。*/
   }
  }
 }
}


int  adc()
{
 int result[12];
 long sum = 0;
 char i; 
 for( i = 0; i < 12; i ++ )	  //中值数字滤波
 {
  delay_ms( 1 );
  ADCSRA |= BIT( ADSC );
  while( !( ADCSRA & BIT( ADIF ) ) );
  ADCSRA &= ~BIT( ADIF );
  result[ i ] = ADC;
 }
 bubble_sort( result, 12 );
 for( i = 1; i < 11; i ++ )
  sum += result[ i ];
 return (int)( sum / 10 );
}


#pragma interrupt_handler adc_isr:15
void adc_isr(void)
{
	
 //conversion complete, read value (int) using...
 CLI();
 ADMUX = ( 0<< REFS0 )|( 0 << REFS1 )|ad_mux & 0x0f;
 //adc_rel = ADC & 0X03FF;
 adc_rel=adc();
 
 
 //if( adc_old != adc_rel)
 if (ad_mux==0)
 	{
 	 dis_data(0,3,adc_rel);
	 //write7279(DECODE0+4,0x80+ad_mux); 
 	 adc_old = adc_rel;
 	}
 //else dis_data( 0,4, adc_rel);
 else 
 	{
 	 dis_data(4,7,adc_rel);
	 //write7279(DECODE0+4,0x80+ad_mux); 
 	 adc_old = adc_rel;
 	}
 
 
 if(ad_mux<1)
 	ad_mux++;
 else
 	ad_mux=0;
	
	
ADCSR |= BIT( ADSC); 
 SEI();
 
}



//call this routine to initialise all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 port_init();
 adc_init();

 MCUCR = 0x00;
 GICR  = 0x00;
 TIMSK = 0x00; //timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialised
}

//
void main(void)
{
	int  i;
	ad_mux = 0x00;
	adc_old = 0x0000;
 	init_devices();
 	init_7279();
	//insert your functional code here...
	
	ADCSR |= BIT( ADSC );
	while(1)
	{
//	 dis_data(0,3,1234);
//	 dis_data(4,7,5678);
	}
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -