adc.c

来自「单片机中的ADC模块」· C语言 代码 · 共 85 行

C
85
字号

#include <iom128v.h>
#include <macros.h>
#include "stdtypes.h"
#include "adc.h"


#define ADC_END    ADCSRA&0x10

int adc0_value;
int adc1_value;
UNS8 channel;
UNS8 adc_couter;
UNS8 adc_flag;

//ADC initialize
// Conversion time: 416uS
void adc_init(void)
{

 ADCSRA = 0x00; //disable adc
 ADMUX = 0x40; //select adc input 0
 ACSR  = 0x80;
 ADCSRA = 0x07;//00101111 分频128 不是连续转换 中断使能
 adc0_value=0;
 adc1_value=0;
 channel=0;
 adc_couter=0;
 adc_flag=0;
}


int read_adc_value(void)
{  
   int value;
   //CLI();
   if(ADC_END)
   {
 
   ADCSRA=ADCSRA&(1<<ADIF);
   value=ADCL;            //Read 8 low bits first (important)
   value|=(int)ADCH << 8; //read 2 high bits and shift into top byte
   
   //SEI(); //re-enable interrupts
   }
   return value;
}

void system_protect(void)
{
 /*改变采集通道*/ 
if(adc_couter==1)
{
 if(ADC0==channel)
   {
   ADMUX=0x41;//select adc input 1 
   channel=1;
   }
 else if (ADC1==channel)
   {
   ADMUX = 0x40; //select adc input 0
   channel=0;
   }  
   adc_couter=0; 
   ADCSRA|=(1<<ADEN)|(1<<ADSC);//开始转换  
}
else
{
      adc_couter++;
      
	if(ADC0==channel)
      {
	  adc0_value=read_adc_value();
	  }
	else if (ADC1==channel)
      {
	  adc1_value=read_adc_value();
	  }
}

 
}


⌨️ 快捷键说明

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