Vous êtes sur la page 1sur 5

http://codientu.org/threads/giup-e-do-nhiet-do-su-dung-4-lm35-voi-avr-voi.

617/page-4

/***************************************************** This program was produced by the CodeWizardAVR V2.03.4 Standard Automatic Program Generator Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l. http://www.hpinfotech.com Project : Version : Date : 10/31/2012 Author : Company : Comments:

Chip type : ATmega16 Program type : Application Clock frequency : 12.000000 MHz Memory model : Small External RAM size : 0 Data Stack size : 256 *****************************************************/ #include <mega16.h> #include <delay.h> #include <stdio.h> // 1 Wire Bus functions #asm .equ __w1_port=0x18 ;PORTB .equ __w1_bit=7 #endasm #include <1wire.h> // DS1820 Temperature Sensor functions #include <ds18b20.h> // maximum number of DS1820 devices // connected to the 1 Wire bus #define MAX_DS18b20 8 // number of DS1820 devices // connected to the 1 Wire bus unsigned char ds18b20_devices; // DS1820 devices ROM code storage area, // 9 bytes are used for each device // (see the w1_search function description in the help) unsigned char ds18b20_rom_codes[MAX_DS18b20][9];

// Alphanumeric LCD Module functions #asm .equ __lcd_port=0x1B ;PORTA #endasm #include <lcd.h> void lcd_putnum(float so, char x,char y) { long t,c,d; long tp1,tp2,tp3,tp4; float temp1,temp2; t=so/100;// lay phan tram c= (so-100*t)/10;// lay hang chuc d= so-100*t-10*c;// lay hang don vi if(so<0){temp2=so*-1;} else{temp2=so;} temp1=temp2-(t*100+c*10+d); tp1=(unsigned char)(temp1*10); tp2=(temp1*100-tp1*10); //tp3 =(unsigned char)(temp1*1000-tp1*100-tp2*10); //tp4 =(unsigned char)(temp1*10000-tp1*1000-tp2*100-tp3*10); lcd_gotoxy(x,y); lcd_putchar(t+48); lcd_putchar(c+48); lcd_putchar(d+48); lcd_putsf("."); lcd_putchar(tp1+48); lcd_putchar(tp2+48); //lcd_putchar(tp3+48); //lcd_putchar(tp4+48); }

// Declare your global variables here void main(void) { unsigned char i,j; float Temp; char Lcd_Buffer[33]; // Declare your local variables here // Input/Output Ports initialization // Port A 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 PORTA=0x00; DDRA=0x00; // 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 // 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 PORTC=0x00; DDRC=0x00; // Port D 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 PORTD=0x00; DDRD=0x00; // Timer/Counter 0 initialization // Clock source: System Clock // Clock value: Timer 0 Stopped // Mode: Normal top=FFh // OC0 output: Disconnected TCCR0=0x00; TCNT0=0x00; OCR0=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 // INT2: Off MCUCR=0x00; MCUCSR=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; // Determine the number of DS1820 devices // connected to the 1 Wire bus ds18b20_devices=w1_search(0xf0,ds18b20_rom_codes); // LCD module initialization lcd_init(16); ds18b20_devices = w1_search(0xf0,ds18b20_rom_codes); sprintf(Lcd_Buffer,"SO THIET BI\nDS18B20: %u",ds18b20_devices); lcd_puts(Lcd_Buffer); delay_ms(3000); if (ds18b20_devices) { for (i=0;i<ds18b20_devices;i++) { sprintf(Lcd_Buffer," IC DS18b20 #%u\nROM CODE:",i+1); lcd_clear(); lcd_puts(Lcd_Buffer); delay_ms(2000); lcd_clear(); for (j=0;j<8;j++) { sprintf(Lcd_Buffer,"%02X ",ds18b20_rom_codes[i][j]); lcd_puts(Lcd_Buffer); if(j==3) lcd_gotoxy(0,1); }; delay_ms(2000); }; } else while (1); for (i=0;i<ds18b20_devices;) if (!ds18b20_init(&ds18b20_rom_codes[i++][0],20,30,DS18B20_12BIT_RES)) { sprintf(Lcd_Buffer,"Init error for\ndevice #%u",i); lcd_clear(); lcd_puts(Lcd_Buffer); while (1); // Qua trinh khoi tao co loi };

//lcd_clear(); //lcd_gotoxy(0,0); //lcd_putsf("NHIET DO TAI:"); while (1) { j=1; // Cap nhat nhiet do tai cac diem va hien thi len LCD1602 for (i=0;i<ds18b20_devices;i++) { Temp = ds18b20_temperature(&ds18b20_rom_codes[i][0]); // sprintf(Lcd_Buffer,"t%u=%c%i.%u\xdfC",j++,Temp); lcd_clear(); lcd_gotoxy(0,0); lcd_putsf("t"); lcd_putchar(i+49); lcd_putsf("="); lcd_putnum(Temp,4,0); delay_ms(1500); } // lcd_gotoxy(0,0); // lcd_putchar(ds18b20_devices+48); // lcd_gotoxy(0,0); // lcd_putsf("ds18b20"); // Place your code here }; }

Vous aimerez peut-être aussi