pt100熱敏電阻+AD824S放大并由ATmega8單片機主控的測溫系統仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
0.jpg (145.46 KB, 下載次數: 104)
下載附件
2018-1-12 02:06 上傳
0.png (16.87 KB, 下載次數: 123)
下載附件
2018-1-12 02:07 上傳
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 <mega8.h>
- //#include <delay.h>
- #include <stdlib.h>
- #include <string.h>
- // Alphanumeric LCD Module functions
- #asm
- .equ __lcd_port=0x18 ;PORTB
- #endasm
- #include <lcd.h>
- #include <delay.h>
- #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<ADCSTEP[i+1])&&(ADC>=ADCSTEP[i])) break;
- };
- 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]="\0";
- 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;
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
ourdev_632978S7VPZ0.rar
(28.62 KB, 下載次數: 159)
2018-1-11 16:58 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|