/************************************** thermo.c Digital Thermometer -55C to +125C AVR 90S2313 & DS1820 Copyright 2001 by Wichit Sirichote **************************************/ #include <90s2313.h> #include #include // 1 Wire Bus functions #asm .equ __w1_port=0x12 .equ __w1_bit=6 #endasm #include <1wire.h> // DS1820 temperature sensor functions #include // Declare your global variables here unsigned char tick,i,digit; unsigned char heat[4]; unsigned int xtimer1; int temp,T,X1,X2,X3,X4,X5; // signed number for negative and positve temperature manipulating char key; unsigned char convert[10] = {0x3F,0x06,0xdb,0xcf,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; #define xtal 4000000L //selbst eigefügt #define segment PORTB #define LED_digit PORTD // converts 16-bit data in T to display buffer for both negative and positive reading void heatToBuffer() { if(T<0){ heat[3] = 0x40; // if negative, put - heat[0] = 0x39; // C T = 34 + abs(T); // get only amplitude (+34 selbst eingefügt) heat[1] = convert[T%10]; heat[2] = convert[T/10]; if (heat[2] == 0x3f) heat[2] = 0; // off msd } else { heat[0] = 0x39; // C heat[3] = convert[T/100]; temp = T%100; heat[1] = convert[temp%10]; heat[2] = convert[temp/10]; // off msd if (heat[3] == 0x3f) { heat[3] = 0; if(heat[2] == 0x3f) heat[2] = 0; } } } LPF() // performs five-point moving average { X5=X4; X4=X3; X3=X2; X2=X1; X1= T; T = (X1+X2+X3+X4+X5)/5; } read_temp() { if(++xtimer1 >=5) { xtimer1 = 0; segment = 0xff; T = (ds1820_temperature_10(0)/10) LPF(); // enter filter heatToBuffer(); // convert it } } // Timer 0 overflow interrupt service routine // timer interrupt every 1/15 sec provides foreground task to be run periodically. interrupt [TIM0_OVF] void timer0_ovf_isr(void) { switch (++tick){ case 15: tick = 0; read_temp(); //second_task(); } } void scanLED() /* scan 4-digit LED and 4-key switch, if key pressed key = 0-3 else key = -1 */ // adapted from 89C2051 project if needs scan key, find one bit input port { char i; digit = 0x20; key = -1; for( i = 0; i < 4; i++) /* 4-DIGIT scanning */ { LED_digit = ~digit; /* send complement[digit] */ segment = ~heat[i]; /* send complement[segment] */ delay_ms(1); /* delay a while */ segment = 0xff; /* off LED */ // if ((PORTD & 0x10) == 0) /* if key pressed P3.4 became low */ // key = i; /* save key position to key variable */ digit>>=1; /* next digit */ } } void main(void) { DDRB=0xFF; PORTB=0x00; DDRD=0x7F; PORTD=0x00; // Timer/Counter 0 initialization // Clock source: System Clock // Clock value: 3.906 kHz // Mode: Output Compare // OC0 output: Disconnected TCCR0=0x05; TCNT0=0x00; // Timer(s)/Counter(s) Interrupt(s) initialization TIMSK=0x02; // Analog Comparator initialization // Analog Comparator: Off // Analog Comparator Input Capture by Timer/Counter 1: Off ACSR=0x80; // 1 Wire Bus initialization w1_init(); // Global enable interrupts #asm("sei") // 1 Wire Bus initialization w1_init(); T = 44; heatToBuffer(); while (1) { scanLED(); // run background task forever } }