Code:
//bài viết này tặng cho nhưng bạn mới học AVR ,chúc các bạn thành công //LAP TRINH : DO NHIET DO PHONG //TH : PHAN THE BINH //CN : DIEU KHIEN TU DONG //NHT : 30/12/2007 //-------------------------------------- #include <mega8515.h> #include <ds18b20.h> #define load PORTC.5 #define data PORTC.4 //hien thi qua ic595 voi 4 led 7 thanh #define clk PORTC.6 //-------------------------------------- #asm .equ __w1_port=0x1b .equ __w1_bit=0 #endasm //--------------------------------------- #define MAX_DEVICES 8 unsignedunsigned char byte); //--------------------------------------- void main(void) { char rom_code[MAX_DEVICES][9]; //--------------------------------------- void hien_thi (void); void dich( int temp; unsigned char i,devices,sodem; //---------Khoi tao cong vao ra---------- DDRC=0xff; //--------Khoi tao giao tiep i2c--------- devices=w1_search(0xf0,rom_code); //--khai bao do phan giai 12bit ,nhiet do cach bao TL=10,TH=40 do-- for (i=0;i<devices;) if (!ds18b20_init(&rom_code[i++][0],10,40,DS18B20_12BIT_RES)) { while (1); //Dung he thong neu thiet bi loi }; //-----------hien thi nhiet do---------------------- while (1) { while (sodem==temp){ //kiem tra nhiet do co thay doi khong for (i=0;i<devices;i++) { temp=ds18b20_temperature(&rom_code[i][0]); //neu nhiet do khong thay doi thi khong day du lieu ra 595 }; }; //neu nhiet do thay doi thi day du lieu ra 595 sodem=temp; hien_thi(); }; } //------------------------------------------------- void hien_thi (void) { unsigned char ht,dv,p,q,sodem,n_7seg,led_7seg; unsigned char byte,a,b; dv=sodem % 10; p=sodem / 10; ht=p % 10; q=p / 10; //------------------------------------------------- n_7seg=dv; switch (n_7seg) { case 0: led_7seg=40; break; case 1: led_7seg=252; break; case 2: led_7seg=25; break; case 3: led_7seg=88; break; case 4: led_7seg=204; break; case 5: led_7seg=74; break; case 6: led_7seg=10; break; case 7: led_7seg=248; break; case 8: led_7seg=8; break; case 9: led_7seg=72; break; } a=led_7seg; n_7seg=ht; switch (n_7seg) { case 0: led_7seg=40; break; case 1: led_7seg=252; break; case 2: led_7seg=25; break; case 3: led_7seg=88; break; case 4: led_7seg=204; break; case 5: led_7seg=74; break; case 6: led_7seg=10; break; case 7: led_7seg=248; break; case 8: led_7seg=8; break; case 9: led_7seg=72; break; } b=led_7seg; //------------------------------------------------ byte=43; dich(byte); byte=201; dich(byte); byte=a; dich(byte); byte=b; dich(byte); } //------------------------------------------------ void dich(unsigned char byte) { unsigned char k; for(k=0;k<8;k++) { data=(byte>>k)&0x01; clk=0; clk=1; } load=0; load=1; } //ket thuc chuong trinh //------------------------------------------------ //------------------------------------------------
Comment