pt100热敏电阻+AD824S放大并由ATmega8单片机主控的测温系统仿真原理图如下
ATmega8单片机源程序如下:
/*****************************************************
This program was produced by the
CodeWizardAVR V2.03.4 Standard
Automatic Program Generator
?Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.
Project :
Version :
Date : 2011-4-16
Author :
Company :
Comments:
Chip type : ATmega8
Program type : Application
Clock frequency : 8.000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 256
*****************************************************/
#include //#include #include #include // Alphanumeric LCD Module functions #asm .equ __lcd_port=0x18 ;PORTB #endasm #include #include #define ADC_VREF_TYPE 0xC0 #define MCPCS PORTD.0 #define MCPSCK PORTD.1 #define MCPDATA PIND.2 #define A 3.9083e-3 #define B -5.775e-7 #define C -4.183e-12 unsigned long read_spi(void); float CalTem(float PT100R) { double fT,fR,fT0; char i=0; fR=PT100R; fT0=(fR/100-1)/A; if ((fR>=80.31)&&(fR<100)) //-30~0 度 { for(i=0;i<50;i++) { fT=fT0+(fR-100*(1+A*fT0+B*fT0-100*C*fT0*fT0+C*fT0*fT0*fT0))/ (100*(A+2*B*fT0-300*C*fT0*fT0+4*C*fT0*fT0*fT0)); if(fabs(fT-fT0)<0.001) break; else fT0=fT; } } else if(fR>=100&&fR<=390.481) // 0~850 度 { for (i=0;i<50;i++) { fT=fT0+(fR-100*(1+A*fT0+B*fT0*fT0))/(100*(A+2*B*fT0)); if (fabs(fT-fT0)<0.001) break; else fT0=fT; } } else fT=-1000.0; return fT; }; unsigned long read_mcp(void) { long a[]={0,0,0,0,0}; long x=0; char i=0; char k=5; // 数组大小 -1 for (i=0;i<5;i++) { a[i]=read_spi(); // 连续3次读出数据 delay_us(5); } //中值滤波 while (k>0) { for (i=0;(i<(k-1));i++) // 从低到高排序 { if (a[i]>a[i+1]) { x=a[i+1]; a[i+1]=a[i]; a[i]=x; }; }; k--; }; return a[2]; // 舍弃最大数据和最小数据。 } unsigned long read_spi(void) { volatile char i=0; volatile long int result=0,x=0; MCPCS=0;// CS 先一个100us 低电平脉冲 delay_us(100); MCPCS=1; delay_ms(80); // 高电平等待80ms 等待转换完成 MCPCS=0; // 置 CS 低电平 开始发生 sck 脉冲 for (i=0; i<24;i++) // 24 位数据 { MCPSCK=0; // sck 脉冲下降沿 delay_us(1); // 等5us 等待稳定 //result=result<<1; x=MCPDATA; // 读出一位 while (MCPDATA!=x) // 抖动处理 2次读出电平相同说明数据稳定 { delay_us(1); x=MCPDATA; }; result<<=1; result|=x;//(x<<(23-i)); delay_us(5); MCPSCK=1; // 发送sck 上升沿 delay_us(10); }; MCPCS=1; // cs=1 return result>>6; } // Read the AD conversion result unsigned int read_adc(unsigned char adc_input) { ADMUX=adc_input | (ADC_VREF_TYPE & 0xff); // Delay needed for the stabilization of the ADC input voltage delay_us(10); // Start the AD conversion ADCSRA|=0x40; // Wait for the AD conversion to complete while ((ADCSRA & 0x10)==0); ADCSRA|=0x10; return ADCW; } // 校准温度计查表 没20度一个校准, // -50 -30 -10 10 30 50 70 90 110 130 150 const float CAL_Tem[]={4.7 ,4.65,4.65,4.6,4.6 ,4.55,4.55,4.50,4.45,4.45,4.45}; const int ADCSTEP[]={ 1 ,96 ,189 ,282, 374,466 ,557 ,648,738,827 ,916}; float CalcuTem(int ADC) // 温度校准计算 没有使用 { int i=0; float r; for (i=0; i<10;i++) { if ((ADC }; r=(ADC-ADCSTEP[i]); r=r/CAL_Tem[i]; r=r-50;///CAL_Tem[i]-50+i*20+; r=r+i*20.0; return r; } volatile char stradc[15]=" "; void main(void) { // Declare your local variables here volatile long int MCPADC=0; volatile unsigned int adc=0; volatile float fadc=0; // Input/Output Ports initialization // Port B initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTB=0x00; DDRB=0x00; // Port C initialization // Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTC=0x00; DDRC=0x00; // Port D initialization // Func7=out Func6=out Func5=out Func4=out Func3=out Func2=int Func1=out Func0=out // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=1 PORTD=0x07; DDRD=0xFB; // Timer/Counter 0 initialization // Clock source: System Clock // Clock value: Timer 0 Stopped TCCR0=0x00; TCNT0=0x00; // Timer/Counter 1 initialization // Clock source: System Clock // Clock value: Timer 1 Stopped // Mode: Normal top=FFFFh // OC1A output: Discon. // OC1B output: Discon. // Noise Canceler: Off // Input Capture on Falling Edge // Timer 1 Overflow Interrupt: Off // Input Capture Interrupt: Off // Compare A Match Interrupt: Off // Compare B Match Interrupt: Off TCCR1A=0x00; TCCR1B=0x00; TCNT1H=0x00; TCNT1L=0x00; ICR1H=0x00; ICR1L=0x00; OCR1AH=0x00; OCR1AL=0x00; OCR1BH=0x00; OCR1BL=0x00; // Timer/Counter 2 initialization // Clock source: System Clock // Clock value: Timer 2 Stopped // Mode: Normal top=FFh // OC2 output: Disconnected ASSR=0x00; TCCR2=0x00; TCNT2=0x00; OCR2=0x00; // External Interrupt(s) initialization // INT0: Off // INT1: Off MCUCR=0x00; // Timer(s)/Counter(s) Interrupt(s) initialization TIMSK=0x00; // Analog Comparator initialization // Analog Comparator: Off // Analog Comparator Input Capture by Timer/Counter 1: Off ACSR=0x80; SFIOR=0x00; // ADC initialization // ADC Clock frequency: 125.000 kHz // ADC Voltage Reference: Int., cap. on AREF ADMUX=ADC_VREF_TYPE & 0xff; ADCSRA=0x86; // LCD module initialization lcd_init(16); while (1) { MCPADC=0; ……………………
上一篇:AVR16 AVR128的DS18B20温度传感器protues仿真与源码
下一篇:MEGA2560主控自行车骑行监测器
推荐阅读最新更新时间:2024-11-02 20:03