ATmaga8单片机Pt100温度计源程序+Proteus仿真设计
目录
1、项目功能
2、仿真图
3、程序
资料下载地址:ATmaga8单片机Pt100温度计源程序+Proteus仿真设计
1、项目功能
|
2、仿真图
3、程序
#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; return fT0; }; 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;