您的位置 首页 国产IC

ATMEGA16 多通道AD收集

//ADEN置位即启动ADC,否则ADC功能关闭。在转换过程中关闭ADC将立即中止正在进行的转换。同时ADEN清零会将ADIE(AD结束中断使能)清除。…

//ADEN置位即发动ADC ,不然ADC功用封闭。在转化过程中封闭ADC将当即间断正在进行的转化。一起ADEN清零会将ADIE(AD完毕中止使能)铲除。别的作业在接连形式下假如没有对ADEN清零则AD采样一向运转。

#include “main.h”

#define HOT_MUX 4
#define MIX_MUX 5
#define POWER_MUX 7
#define AD_PORT PORTA
#define AD_DIR DDRA

//收集次数
#define HOT_SAMP_CNT 4
#define MIX_SAMP_CNT 4
#define POWER_SAMP_CNT 4

unsigned char AD_READY = 0;
volatile unsigned int buf[8];

volatile unsigned int AD_hot_result = 0;
volatile unsigned int AD_mix_result = 0;
volatile unsigned int AD_power_result = 0;

void ADC_Init(void)
{
AD_PORT &= ~(1<AD_DIR &= ~(1< ADMUX = (1<ACSR |= 0x80; //制止模仿比较器
SFIOR = 0x00; //接连转化形式
ADCSRA = (1<}

#pragma interrupt_handler ADC_ISR:15
void ADC_ISR(void)
{
unsigned int temp = 0;
static unsigned int sum = 0;
static unsigned char cnt = 0;
static unsigned char AD_status = 0;

if (cnt == 0) //切换通道时第一个数据有必要放弃
{
cnt = 1;
ADCSR|=(1}

temp = ADCL;
temp |= (unsigned int)ADCH<<8;
sum += temp;

switch(AD_status)
{
case 0:

if (++cnt > HOT_SAMP_CNT)
{
AD_status++;
ADMUX = (1<AD_hot_result = sum / HOT_SAMP_CNT;
cnt = 0;
sum = 0;
}
break;
case 1:

if (++cnt > MIX_SAMP_CNT)
{
AD_status++;
ADMUX = (1<AD_mix_result = sum / MIX_SAMP_CNT;
cnt = 0;
sum = 0;
}
break;
case 2: //电源电压
if (++cnt > POWER_SAMP_CNT)
{
AD_status = 1;
ADMUX = (1<AD_power_result = sum / POWER_SAMP_CNT;
cnt = 0;
sum = 0;
AD_READY = 1;

ADCSR &= ~(1}
break;
}
ADCSR|=(1<}

unsigned int Res_Matrixing_Temper(unsigned int temp)
{
float Rt = 0;
float T_inv = 0;
float temper = 0;

Rt = (float)50000*temp/(1024-temp);
T_inv = log(Rt/50000)/3950 + 1/298.15;
temper = (1/T_inv -273.15)*10 + 0.5;

return ((unsigned int)temper);
}

void get_AD_result(void)
{
unsigned int t1 = 0;
float T_inv = 0;
float Rt = 0;
float temp = 0;
if (AD_READY == 1)
{
AD_READY = 0;

t1 = Res_Matrixing_Temper(AD_hot_result);

buf[0] = t1/100;
buf[1] = t1/10%10;
buf[2] = t1%10;

t1 = Res_Matrixing_Temper(AD_mix_result);
buf[3] = t1/100;
buf[4] = t1/10%10;
buf[5] = t1%10;

}
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/bandaoti/ic/273253.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部