📄 t_measure.c
字号:
/*
------------------------------------------------------------------*-
T_Measure.C (v1.00)
------------------------------------------------------------------
LCD display program (Test Version 1.0)
COPYRIGHT
---------
This code is from the book:
PATTERNS FOR TIME-TRIGGERED EMBEDDED SYSTEMS by Michael J. Pont
[Pearson Education, 2001; ISBN: 0-201-33138-1].
This code is copyright (c) 2001 by Michael J. Pont.
--- Modefied by sylva zhu to apply for AVR Microcontroller .
--- Ver 1.0 Sept 25th , 2006 .
-*------------------------------------------------------------------
*/
#include <iom32.h>
#include <inavr.h>
#include <comp_a90.h>
#include <math.h>
#include "T_Measure.h"
#include "T_Lcd.h"
//
//
// Global variables
unsigned char ADCresult_calc_cnt = 0; // variable to count number of temperature calcualtions
unsigned int ADCresult_average = 0; // integer to keep the ADC results for later averaging
float Temperature; // float to calculate the temperature
unsigned int TEMP_HIGHBYTE;
unsigned int TEMP_LOWBYTE;
//
unsigned char temp_hundred;
unsigned char temp_ten;
unsigned char temp_zero;
unsigned char temp_subone;
unsigned char temp_subtwo;
//
//
static void ADC_conv_temp(void);
static void Temp_calculation(unsigned int iADC_value);
//
//------------------------------------------------------------------------------
//
void ADC_Update(void)
{
ADC_conv_temp();
}
/*
********************************************************************************
*
* Function name : ADC_conversion
*
* Returns : None
*
* Parameters : None
*
* Purpose : Run the ADC conversion
*
********************************************************************************
*/
static void ADC_conv_temp(void)
{
unsigned int ADC_temp;
unsigned int ADCresult = 0;
unsigned char i;
for(i=0;i<8;i++) // do the ADC conversion 8 times for better accuracy
{
ADCSRA |= (1<<ADSC); // do single conversion
while(!(ADCSRA & 0x10)); // wait for conversion done, ADIF flag active
ADC_temp = (unsigned int)ADCL; // read out ADCL register
ADC_temp += ((unsigned int)ADCH*256); // read out ADCH register
ADCresult += ADC_temp; // accumulate result (8 samples) for later averaging
}
//ADCresult = ADCresult >> 3; // average the 8 samples
ADCresult=ADCresult/8;
ADCresult_calc_cnt++; // increment the counter that keeps track of how many calulations that have been donet
ADCresult_average += ADCresult; // add the ADCresult to the previous measurements
if(ADCresult_calc_cnt >= 32) // if 32 calculations
{
ADCresult_calc_cnt = 0; // clear the counter
//ADCresult_average = ADCresult_average >> 5; // find the average ADC result for the last 32 times
ADCresult_average=ADCresult_average/32;
Temp_calculation(ADCresult_average); // call the temperature calculation function
ADCresult_average = 0; // clear the ADCresult_average
}
}
/*
********************************************************************************
*
* Function name : Temp_calculation
*
* Returns : None
*
* Parameters : The result in the ADCH/L registers
*
* Purpose : Calculate the corresponding temperature in degree Celsius,
* and place the value in SRAM at location 0x4F0 - 0x4FF
*
********************************************************************************
*/
static void Temp_calculation(unsigned int iADC_value)//ADCresult_average
{
float V_ADC;
//if((ADMUX & 0x1F) == SingleEnded) // check if Single_ended operation
V_ADC = (iADC_value * V_ref)/1024; // calculate the voltage over the NTC
//else if((ADMUX & 0x1F) == Differential) // check if Differential operation
//V_ADC = (iADC_value * V_ref)/512; // calculate the voltage over the NTC
// float temperature
Temperature = Beta/( log(V_ADC/(V_ref-V_ADC))/log(__E) + (Beta/T_amb) ) -T_zero-470; // calculate the temperature
// int TEMP_HIGHBYTE , FLOAT Temperature
TEMP_HIGHBYTE = Temperature; // store the integer to "TEMP_HIGHBYTE"
Temperature = (Temperature - TEMP_HIGHBYTE); // subtract the the integer from Temperature_average ,
//ang get value below littel digital value.
TEMP_LOWBYTE = (Temperature * 100); // multiply Temperature_average by 100 to get the decimal,
// and store it to "TEMP_LOWBYTE"
//************************************** int . int
// TEMP_HITHBYTE . TEMP_LOWBYTE
//Temperature_regulation(); //call the Temperatur_regulation rutine
temp_hundred=(unsigned char)(TEMP_HIGHBYTE%1000/100);
temp_ten=(unsigned char)(TEMP_HIGHBYTE%100/10);
temp_zero=(unsigned char)(TEMP_HIGHBYTE%10);
temp_subone=(unsigned char)(TEMP_LOWBYTE%1000/100);
temp_subtwo=(unsigned char)(TEMP_LOWBYTE%100/10);
//
if(temp_hundred==0)
{
ShowChar(0x2d,' ');
}
else
{
ShowChar(0x2d,temp_hundred+0x30);
}
ShowChar(0x2e,temp_ten+0x30);
ShowChar(0x2f,temp_zero+0x30);
ShowChar(0x30,'.');
ShowChar(0x31,temp_subone+0x30);
ShowChar(0x32,temp_subtwo+0x30);
ShowChar(0x33,' ');
ShowChar(0x34,'D');
ShowChar(0x35,'E');
ShowChar(0x36,'G');
ShowChar(0x37,'R');
ShowChar(0x38,'E');
ShowChar(0x39,'E');
ShowChar(0x3a,' ');
ShowChar(0x3b,' ');
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -