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

📄 adc.c

📁 单片机中的ADC模块
💻 C
字号:

#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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -