Giám sát, điều khiển thiết bị điện qua cổng parallel dùng wireles

MỤC LỤC Phần 1: CỔNG SONG SONG . . 3 1.1 Cấu trúc cổng song song: . 3 1.2 Định dạng các thanh ghi: . 4 1.3 Giao tiếp hai máy tính dùng cổng song song 4 Phần 2: GIỚI THIỆU PSOC VÀ MODUL WIRELESS USB7 2.1 Giới thiệu chung về PSOC và chip Cyp29566: 7 2.2 Sơ lược cấu trúc phần cứng: 8 2.2.1 Tổng quan cấu trúc PSOC: 8 2.2.2 Cấu trúc CPU . 9 2.2.3 Bộ tạo tần số: . . 9 2.2.4 Mạch RESET: 10 2.2.5 Đầu vào và ra của tín hiệu số: 10 2.2.6 Drive Mode: . 11 2.2.7 Truy cập các khối số lập trình được: . 12 2.2.8 Các đường input toàn cục (Global input line): . 12 2.2.9 Bộ chọn kênh đầu vào: 12 2.2.10 Khối số khả trình: . . 13 2.2.11 Tín hiệu CLK . 14 2.2.12 Tín hiệu input . 14 2.2.13 Tín hiệu output 14 2.2.14 Multiplexer . 14 2.2.15 Mạch logic 15 2.2.16 Output drivers . 15 2.2.17 Điều khiển ngắt 15 2.2.18 Không gian địa chỉ . 16 2.3 Module WirelessUSB LR 2.4-GHz DSSS Radio SoC CYWUSB6935: 17 2.3.1 Các đặc điểm của CYWUSB6935: . 17 2.3.2 Các ứng dụng: 17 2.3.3 Sơ đồ khối của CYWUSB6935: 17 2.3.4 Chức năng tổng quát của CYWUSB6935: 18 2.3.6 Các thanh ghi của CYWUSB6935: 21 Phần 3: SƠ ĐỒ MẠCH VÀ LưU ĐỒ THUẬT TOÁN . 25 3.1 Cấu hình phần cứng trên PSOC Cyp29566 25 3.1.1 Khối giao tiếp SPI Master . 25 3.1.2 Module LCD 16x2 27 3.1.3. Module I2C: . 29 3.1.4. Giới thiêu DS1307 31 3.2. Sơ đồ mạch và nguyên lý hoạt động: . 34 3.2.1 Mạch điều khiển thiết bị: 34 3.2.2 Mạch giao tiếp máy tính: 35 3.3 Lưu đồ thuật toán: 36 3.3.1 Chương trình chính: . . 36 3.3.2 Chương trình cài đặt giờ: . 38 3.3.3 Chương trình hiển thị menu: . . 39 3.3.4. Chương trình hiển thị và cài đặt trên PSOC: 40 3.3.5. Chương trình xử lý dữ liệu từ máy tính: . 41 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS 2 Phần 4: CHưƠNG TRÌNH GIAO TIẾP CỔNG SONG SONG TRÊN MÁY TÍNH 42 4.1 Giới thiệu chương trình giao tiếp máy tính với module wireless. 42 4.2 Các module chính của chương trình: . . 43 4.2.1 Hàm set và clear 1 bit: . 43 4.2.2 Hàm đọc trạng thái của 1 bit: . 43 4.2.3 Hàm gửi 1 byte lên bus SPI: . 44 4.2.4 Hàm nhận đọc 1 byte trên bus SPI: . 44 4.2.5 Hàm ghi đata vào 1 thanh ghi của module wireless: 44 4.2.6 Hàm đọc data từ 1 thanh ghi của module wireless: . 45 4.2.7 Hàm khởi động module wireless: . 45 4.2.8 Hàm chọn chế độ truyền cho module 6935: . 46 4.2.9 Phát 1 byte dùng module wireless: 46 4.2.10 Nhận 1 byte: . 46 4.2.11 Sự kiện cho nút ‘Bắt đâu’: 46 4.2.12 Sự kiện cho nút ‘Cài đặt’: . 46 4.2.13 Sự kiện cho nút ‘Cập nhật thời gian’: . 47 4.2.14 Sự kiện cho nút ‘Nhận dữ liệu’: . 47 4.2.15 Sự kiện cho timer2 . 47 4.2.16 Sự kiện cho các nút điều khiển trực tiếp thiết bị: . 47 Phần 5: PHỤ LỤC . 48 5.1 Chương trình diều khiển ở VDK . 48 5.2 Chương trình VB: . 64

pdf80 trang | Chia sẻ: lvcdongnoi | Lượt xem: 2356 | Lượt tải: 1download
Bạn đang xem trước 20 trang tài liệu Giám sát, điều khiển thiết bị điện qua cổng parallel dùng wireles, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
ule wireless: tương tự như quá trình ghi, nhưng ở đây, bit MSB của địa chỉ cần đọc để ở mức 0. Private Function Read_WUSB_REG(ByVal reg As Integer) As Integer Dim value As Integer ClrBit(data, SCK) ClrBit(data, nSS) SendByteSPI(reg) value = readbyteSPI() setbit(data, nSS) ClrBit(data, SCK) Return value End Function 4.2.7 Hàm khởi động module wireless: Để khởi tạo module wireless, trước tiên la phải set bit nPD (bit power down) lên 1 để module này hoạt động. Quá trình khởi động của module này có thời gian để bộ dao động của nó ổn định, trong thời gian này không nên ghi dữ liệu tới nó. Do đó ta cần trì hoãn 1 thời gian dùng hàm System.Threading.Thread.Sleep(1). Sau khi khởi động ta cần Reset module, set bit nSS (không chọn truyền SPI, chỉ mỗi khi cần truyền dữ liệu ta mới clear bit này). Tiếp theo ghi giá trị 41H đến 2 thanh ghi REG_CLOCK_MANUAL và REG_CLOCK_ENABLE để module được hoạt động đúng, và ghi 07H tới thanh ghi REG_SERDES_CTL để chọn số bit cần đẻ kêt thúc 1 chuỗi bit.(ở đây chọn 7 bit) Private Sub WUSB_Init() setbit(data, nPD) System.Threading.Thread.Sleep(1) ClrBit(data, nRESET) System.Threading.Thread.Sleep(1) setbit(data, nRESET) setbit(data, nSS) System.Threading.Thread.Sleep(1) Write_WUSB_REG(&H33, &H41) 'REG_CLOCK_MANUAL = 0x33 Write_WUSB_REG(&H32, &H41) 'REG_CLOCK_ENABLE 0x32 Write_WUSB_REG(&H6, &H7) 'REG_SERDES_CTL 0x06 Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 46 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS End Sub 4.2.8 Hàm chọn chế độ truyền cho module 6935: Module wireless có thể hoạt động ở chế độ truyền hoặc chế độ nhận tùy thuộc vào bit điều khiển tương ứng trong thanh ghi REG_CONTROL Private Sub Set_Transmit_Mode() 'Set transmit mode Write_WUSB_REG(&H3, &H40) 'REG_CONTROL 0x03; (chọn chế độ truyền, và bộ khuêch đại bên trong chip) Write_WUSB_REG(&H6, &HF) 'Set he so khuech dai cong suat lon nhat Write_WUSB_REG(&H23, &H7) 'REG_PA 0x23;mPA_BIAS 0x07 'set so bit valid trong thanh ghi REG_SERDES, bit =1->valid, bit = 0 invalid Write_WUSB_REG(&H10, &HFF) 'REG_VALID_TX 0x10 End Sub 4.2.9 Phát 1 byte dùng module wireless: Để phát 1 byte, trước hết ta phải set module ở chế độ truyền. sau đó ghi dữ liệu cần phát đến thanh ghi: REG_TX_DATA ở địa chỉ 0x0F. Sau khi ghi dữ liệu đến thanh ghi nay, ta có thể kiểm tra việc dữ liệu đã truyền xong hay chưa thông qua việc đọc thanh ghi REG_TX_INT_STAT (0x0E), kiểm tra bit 0 (bit Empty). Vd: truyền byte 0xFE: Write_WUSB_REG(&HF, &HFE); 4.2.10 Nhận 1 byte: Để nhận 1 byte, trước hết ta phải set module ở chế độ nhận, sau đó kiểm tra thanh ghi trạng thái nhận REG_RX_INT_STAT (0x08), nếu bit 0 (bit Full A, set lên 1 khi dữ liệu đã nhận đủ) được set lên 1 thì ta đọc giá trị nhận được trong thanh ghi REG_RX_DATA_A (0x09). (Giá trị nhận được lưu trong 2 kênh A và B, ở đây ta đọc giá trị ở kênh A) Vd: If (Read_WUSB_REG(&H8) And 1) Then receive(0) = Read_WUSB_REG(&H9) 4.2.11 Sự kiện cho nút ‘Bắt đâu’: Khởi động module Wireless Chọn chế độ truyền 4.2.12 Sự kiện cho nút ‘Cài đặt’: Gửi lệnh FFH: lệnh để báo hiệu cho VDK biết đó là gửi chuỗi dữ liệu hẹn giờ. Gửi byte cấu hình đầu tiên (byte để nhận biết thiết bị nào được hẹn giờ, và ở chế độ nào) Byte này có khuôn dạng như sau: Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 47 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS - Bit 0,2,4,6: Bit nào được set lên 1 -> thiết bị tương ứng (1,2,3,4) được hẹn giờ. Dữ liệu cho các bit này được lấy từ các checkbox thiết bị. - Bit 1,3,5,7: Bit set lên 1 tương ứng với hẹn giờ on cho thiết bị. các bit này được set hay clear dựa vào Listbox mode. Gửi 24 byte hẹn giờ cho 4 thiết bị (mỗi thiết bị 6 byte). Dữ liệu này được chọn từ các Listbox ở mục „Giờ‟, „Phút‟, „Giây‟, „Ngày‟. Vì dữ liệu ghi tới DS1307 ở board mạch VDK ở dạng BCD, dữ liệu hẹn giờ được so sánh ở dạng số BCD nên trước khi truyền dữ liệu, chúng phải được chuyển đổi sang BCD. Sử dụng hàm: Private Function ConverttoBCD(ByVal deci As Integer) As Integer Return ((deci \ 10) * 16 + (deci Mod 10)) End Function 4.2.13 Sự kiện cho nút ‘Cập nhật thời gian’: Gửi byte lệnh F5H tới VDK để báo hiệu dữ liệu tiếp theo được gửi là dữ liệu thời gian hệ thống. Lấy giờ của hệ thống, chuyển đổi qua BCD sau đó gửi 6 byte thời gian hệ thống xuống VDK. 4.2.14 Sự kiện cho nút ‘Nhận dữ liệu’: Gửi byte lệnh F6H, lệnh yêu cầu VDK gửi dữ liệu lên máy tính. Chuyển module wireless sang chế độ nhận, Chờ đọc đủ 25 byte được gửi từ VDK Nếu sau thời gian là 3s mà không có dữ liệu hoặc dữ liệu không đủ thì tự động kết thúc việc đọc module wireless. Việc định giờ này được thiết lập nhờ Timer1. Chuyển module wireless sang chế độ truyền trở lại. 4.2.15 Sự kiện cho timer2. Sau 1s thì sự kiện Timer2_Tick được gọi để yêu cầu truyền dữ liệu cập nhật trạng thái của thiết bị. Quá trình này giống như sự kiện cho nút „nhận dữ liệu‟, điểm khác là byte lệnh đầu tiên là F4H, và chỉ yêu cầu truyền 1byte từ VDK. Do timer2_tick tự động được gọi sau mỗi 1s do đó tất cả các hàm truyền dữ liệu từ PC trước hết phải disable timer2 để tránh xung đột. 4.2.16 Sự kiện cho các nút điều khiển trực tiếp thiết bị: Điều khiển bật thiết bị 1,2,3,4: lần lượt là các byte FEH, FDH, FCH, FBH. Điều khiển tắt thiết bị 1,2,3,4 lần lượt là các byte FAH, F9H, F8H, F7H Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 48 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS Phần 5: PHỤ LỤC 5.1 Chƣơng trình diều khiển ở VDK #include #include "PSoCAPI.h" #include "function.h" #include #include "PSoCGPIOINT.h" const unsigned char months[]={0x31,0x28,0x31,0x30,0x31,0x30,0x31,0x31,0x30,0x3 1,0x30,0x31}; const char* const menuchinh[]={"Menu chinh","1.Cai Dat","2.Xoa cai dat","3.Hen gio daily","4.Thong tin N1"}; const char* const menuthongtin[]={"Thong tin nhom 1","1.N.Ngoc Nhan","2.D.Van Khanh","3.L.Van Sanh","4.D.H.Cam Le","5.N.T.H.Oanh","6.N.Trong Tri","7.N.Viet Trung","8.N.Duc Khoa","9.S.Thongsoul"}; const char* const menucaidat[]={"Cai Dat Thiet Bi","1.Thiet bi 1","2.Thiet bi 2","3.Thiet bi 3","4.Thiet bi 4"}; const char* const menuthietbi[]={"Thiet bi ","1.Dat gio On","2.Dat gio Off","3.On","4.Off","5.Trang Thai"}; unsigned char mode,j,i,thiet_bi,mode_kt,bWUSB; unsigned char flashBuf[25]; unsigned char time[8],i2cBuf[9],key_flag,key,field,day; unsigned char addr=0; void display_time() { I2Cm_bWriteBytes(0x68,&addr,1,I2Cm_NoStop); //gui byte dia chi dau tien cua vung RAM can doc I2Cm_fReadBytes(0x68,time,7,I2Cm_RepStart); //DOc 7 byte lien tiep display(); } void display() { LCD_1_Position(1,9); LCD_1_PrCString(":"); //di chuyen con tro LCD den hang 1,cot 9,in ki tu ":" LCD_1_Position(1,12);LCD_1_PrCString(":"); LCD_1_Position(0,9); LCD_1_PrCString("/"); Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 49 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS LCD_1_Position(0,12);LCD_1_PrCString("/"); LCD_1_Position(1,13);LCD_1_PrHexByte(time[0]); LCD_1_Position(1,10);LCD_1_PrHexByte(time[1]); LCD_1_Position(1,7);LCD_1_PrHexByte(time[2]); LCD_1_Position(0,7); LCD_1_PrHexByte(time[4]); LCD_1_Position(0,10);LCD_1_PrHexByte(time[5]); LCD_1_Position(0,13);LCD_1_PrHexByte(time[6]); } void delay(){ unsigned char k; for(k=0;k<100;k++); } char get_key() { check_key(); if(key_flag==1) { for(i=0;i<100;i++) { check_key(); if(key_flag==1) i=0; } return 1; } return 0; } void check_key(){ key_flag=1; if(!(PRT0DR&0x80))key=1; else if(!(PRT0DR&0x20)) key=2; else if(!(PRT0DR&0x08)) key=3; else if(!(PRT0DR&0x02)) key=4; else key_flag=0; } //ham gui 1 byte du lieu toi dia chi add cua DS1307 //2 ham nay ko dung trong chuong trinh, chi de test void send_data(BYTE add,BYTE data){ I2Cm_fSendStart(0x68,I2Cm_WRITE); I2Cm_fWrite(add); I2Cm_fWrite(data); I2Cm_SendStop(); Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 50 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS } BYTE read_data(BYTE add){ BYTE data; I2Cm_fSendStart(0x68,I2Cm_WRITE); I2Cm_fWrite(add); I2Cm_fSendRepeatStart(0x68,I2Cm_READ); data=I2Cm_bRead(I2Cm_NAKslave); I2Cm_SendStop(); return data; } ////////////////////////////////////////////////////////// // ///Ham hien thi menu: ///n: so muc chon lua cua menu, //menu_thiet_bi:bien dung de xac dinh neu la menu thiet bi /////thi them chi so chi thiet bi thu menu_thiet_bi vao dong hien thi void hien_thi_menu(const char* const menu[],BYTE n,BYTE menu_thiet_bi){ j=1; //j: bien dung de xac dinh muc can hien thi LCD_1_Control(LCD_1_DISP_CLEAR_HOME); LCD_1_Position(0,0); LCD_1_PrCString(menu[0]);//muc chinh cua menu if(menu_thiet_bi){ LCD_1_Position(0,9); LCD_1_PrHexByte(menu_thiet_bi); } LCD_1_Position(1,1); LCD_1_PrCString(menu[1]);//muc 1 cua menu while(!get_key()); while(key==2||key==3){ if(key==2){ j++; if(j==n) j=1; LCD_1_Control(LCD_1_DISP_CLEAR_HOME); LCD_1_PrCString(menu[0]); LCD_1_Position(1,1); LCD_1_PrCString(menu[j]); if(menu_thiet_bi){ LCD_1_Position(0,9); LCD_1_PrHexByte(menu_thiet_bi); Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 51 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS } } else { j--; if(j==0) j=n-1; LCD_1_Control(LCD_1_DISP_CLEAR_HOME); LCD_1_PrCString(menu[0]); LCD_1_Position(1,1); LCD_1_PrCString(menu[j]); if(menu_thiet_bi){ LCD_1_Position(0,9); LCD_1_PrHexByte(menu_thiet_bi); } } while(!get_key()); } mode=j; ///bien mode de biet duoc ta da chon muc nao } void set_date(){ field=5;//bien de xac dinh dang o vi tri gio,phut,giay,ngay,thang,nam //field=5 -> giay,4->phut,...... LCD_1_Control(LCD_1_DISP_CLEAR_HOME); I2Cm_bWriteBytes(0x68,&addr,1,I2Cm_NoStop); //ham I2Cm_bWriteBytes: 0x68:dia chi cua DS1307 (device address), //addr=0:write dia chi o nho dau tien trong DS1307 I2Cm_fReadBytes(0x68,time,7,I2Cm_RepStart); //Doc lien tiep 7 byte cua DS1307 LCD_1_Control(LCD_1_DISP_CLEAR_HOME); display() ; LCD_1_Position(1,14); LCD_1_Control(LCD_1_CURSOR_BLINK); do{ while(!get_key()); //dung phim thu 4,(phim SET) de thay doi field if(key==4&&field>=0) change_field(); else if(key==2) inc_field(); else if(key==3) dec_field(); time[7]=0x10; Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 52 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS ///byte dieu khien DS1307-> 0x10->tao xung ra tso 1Hz if(field==0xff){//neu nhan qua het cac field- >field=-1->thoat LCD_1_Control(LCD_1_CURSOR_OFF); break; } } while(1); } void change_field(){ field--; if(field!=0xff){ LCD_1_Position(field/3,8+3*(field%3)); //field/3 = 0:hien thi dong 0,=1 hien thi dong 1 //8+3*(field%3)) :vi tri cua gio,phut,giay,ngay,thang,nam LCD_1_Control(LCD_1_CURSOR_BLINK); } } void inc_field(){ ///ham inc_field(): // tang gia tri cua field(gio,phut,giay,ngay,thang,nam) hien thoi //tuy vao ta dang o field nao ma ham se xu ly de tang dung gia tri //(vi du:ko the tang giay>=60.hoac <0) switch (field) { case 5: time[0]++;//tang giay if((time[0]%16)>9) time[0]+=6;//hieu chinh so BCD if(time[0]>=0x60) time[0]=0; break; case 4:time[1]++;//tang phut if((time[1]%16)>9) time[1]+=6;//hieu chinh so BCD if(time[1]>=0x60) time[1]=0; break; case 3:time[2]++;//tang gio if((time[2]%16)>9) time[2]+=6;//hieu chinh so BCD Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 53 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS if(time[2]>=0x24) time[2]=0; break; case 2:time[6]++;//tang nam if((time[6]%16)>9) time[6]+=6;//hieu chinh so BCD if(time[6]>0x99) time[6]=0; break; case 1:time[5]++;//tang thang if((time[5]%16)>9) time[5]+=6;//hieu chinh so BCD if(time[5]>0x12) time[5]=1; break; case 0:time[4]++;//tang ngay if((time[4]%16)>9) time[4]+=6;//hieu chinh so BCD day=months[time[5]-1];//mang months:chua so ngay trong 1 thang if(((time[6]/16+time[6]%16)%4==0)&&(time[5]==2)) //neu nam nhuan va thang 2->so ngay 29 day++; if(time[4]>day) time[4]=1; break; } LCD_1_Control(LCD_1_DISP_CLEAR_HOME); display(); LCD_1_Position(field/3,8+3*(field%3)); LCD_1_Control(LCD_1_CURSOR_BLINK); } void dec_field(){ switch (field) { case 5: time[0]--; if((time[0]%16)==0x0f) time[0]-=6; if(time[0]==0xf9) time[0]=0x59; break; case 4:time[1]--; Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 54 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS if((time[1]%16)==0x0f) time[1]-=6; if(time[1]==0xf9) time[1]=0x59; break; case 3:time[2]--; if((time[2]%16)==0x0f) time[2]-=6; if(time[2]==0xf9) time[2]=0x23; break; case 2:time[6]--; if((time[6]%16)==0x0f) time[6]-=6; if(time[6]==0xf9) time[6]=0x99; break; case 1:time[5]--; if((time[5]%16)==0x0f) time[5]-=6; if(time[5]==0xf9) time[5]=0x12; break; case 0:time[4]--; if((time[4]%16)==0x0f) time[4]-=6; day=months[time[5]-1]; if(((time[6]/16+time[6]%16)%4==0)&&(time[5]==2)) day++; if(time[4]==0xf9) time[4]=day; break; } LCD_1_Control(LCD_1_DISP_CLEAR_HOME); display(); LCD_1_Position(field/3,8+3*(field%3)); LCD_1_Control(LCD_1_CURSOR_BLINK); } ///////////////////////////////////////////////// void write_flash(){ Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 55 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS //xem them FLASH_WRITE_STRUCT fwStruct; fwStruct.wARG_BlockId = 511;//512 block, moi block 64Byte,->ghi toi block cuoi cung fwStruct.pARG_FlashBuffer = flashBuf;//dia chi vung dem chua du lieu can ghi fwStruct.cARG_Temperature = 25; bFlashWriteBlock(&fwStruct); } void read_flash(){ FLASH_READ_STRUCT frStruct; frStruct.wARG_BlockId = 511; frStruct.pARG_FlashBuffer = flashBuf; frStruct.wARG_ReadCount = 25;//doc 25 byte FlashReadBlock(&frStruct); } //truoc ham kiem tra hen gio phai doc 1307 void kiem_tra_hen_gio(BYTE mode_kt){ ///mode_kt =0:hen gio co dinh,=1:hen gio daily BYTE ii,jj=0; if(flashBuf[0]&0x0f){ ////////////Thiet bi 1///////////// if(flashBuf[0]&0b00000001){ for(ii=0;ii<6;ii++) { if(ii==3){ ii++;jj=1;} //so sanh 6 byte, bo qua byte "day of week" cua mang time[] if(time[ii]!=flashBuf[ii+1-jj]) break; } if(ii==6||((mode_kt)&&(ii>=3))){ if(flashBuf[0]&0x10) turnOn(1); else turnOff(1); } } ///////////////Thiet bi 2/////////////// jj=0; if(flashBuf[0]&0b00000010){ for(ii=0;ii<6;ii++) { if(ii==3){ ii++;jj=1;} if(time[ii]!=flashBuf[ii+7-jj]) break; Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 56 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS } if(ii==6||((mode_kt)&&(ii>=3))){ if(flashBuf[0]&0x20) turnOn(2); else turnOff(2); } } /////////////////Thiet bi 3/////////////// jj=0; if(flashBuf[0]&0b00000100){ for(ii=0;ii<6;ii++) { if(ii==3){ ii++;jj=1;} if(time[ii]!=flashBuf[ii+13-jj]) break; } if(ii==6||((mode_kt)&&(ii>=3))){ if(flashBuf[0]&0x40) turnOn(3); else turnOff(3); } } /////////////////Thiet bi 4/////////////// jj=0; if(flashBuf[0]&0b00001000){ for(ii=0;ii<6;ii++) { if(ii==3){ ii++;jj=1;} if(time[ii]!=flashBuf[ii+19-jj]) break; } if(ii==6||((mode_kt)&&(ii>=3))){ if(flashBuf[0]&0x80) turnOn(4); else turnOff(4); } } } } void cai_dat_thiet_bi(unsigned char tb,unsigned char m){ //m:mode neu m=1:hen gio bat //m=2:hen gio tat //m=3:bat //m=4:tat //m=5:xem trang thai switch (m){ case 1: flashBuf[0]|=(0x11<<(tb-1)); Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 57 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS //flashBuf[0]= // x x x x x x x x // | | | | | | | | // 4On/Off 3On/Off 2On/Off 1On/Off 4Set3Set2Set1Set set_date(); for(i=0;i<3;i++) flashBuf[6*(tb-1)+i+1]=time[i]; //bo qua byte chi ngay trong tuan(week day) for(i=4;i<7;i++) flashBuf[6*(tb-1)+i]=time[i]; write_flash(); break; case 2: flashBuf[0]&=~(0x10<<(tb-1)); flashBuf[0]|=(0x01<<(tb-1)); set_date(); for(i=0;i<3;i++) flashBuf[6*(tb-1)+i+1]=time[i]; for(i=4;i<7;i++) flashBuf[6*(tb-1)+i]=time[i]; write_flash(); break; case 3: turnOn(tb);break; case 4: turnOff(tb);break; case 5: xem_trang_thai(tb);break; } } void turnOn(BYTE tbi){ PRT0DR&=~(0b00000001<<(2*tbi-2)); } void turnOff(BYTE tbi){ PRT0DR|=(0b00000001<<(2*tbi-2)); } //ham xem trang thai hen gio cua thiet bi //ham nay se doc flashBuf[0] truoc de xac dinh tb nao co hen gio va mode hen gio ///sau do se doc tiep gia tri hen gio va hien thi len LCD void xem_trang_thai(BYTE tb){ LCD_1_Control(LCD_1_DISP_CLEAR_HOME); //neu co cai dat if((flashBuf[0]&0x0f)&(0x01<<(tb-1))){ Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 58 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS for(i=0;i<3;i++) time[i]=flashBuf[6*(tb-1)+i+1]; for(i=4;i<7;i++) time[i]=flashBuf[6*(tb-1)+i]; LCD_1_Control(LCD_1_DISP_CLEAR_HOME); display(); LCD_1_Position(0,0); if(flashBuf[0]&(0x10<<(tb-1))) LCD_1_PrCString("T.On"); else LCD_1_PrCString("T.Off"); LCD_1_Position(0,4); LCD_1_PrHexByte(tb); } else { LCD_1_Position(0,1); LCD_1_PrCString("Chua cai dat"); } while(!get_key()); } //ham xoa cai dat, de xoa cai dat,chi can xoa byte flash[0] void xoa_cai_dat(){ flashBuf[0]=0; write_flash(); LCD_1_Control(LCD_1_DISP_CLEAR_HOME); LCD_1_Position(0,0); LCD_1_PrCString("Nhan phim bat ki"); LCD_1_Position(1,1); LCD_1_PrCString("de tiep tuc..."); while(!get_key()); } /////////////////////////////////////////// void radio_init(){ PRT1DR|=0b01000000;//nPD chan nPD=1-> set high power cho module 6935 delay(); delay(); //cho cho bo dao dong on dinh PRT1DR&=0b11101111;// nRESET // reset module 6935 delay(); delay(); //cho cho module 6935 reset PRT1DR|=0b00010000; // reset radio device Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 59 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS PRT1DR|=0b00100000; //nSS chan nSS set len 1 write_radio_reg(REG_CLOCK_MANUAL,0x41); write_radio_reg(REG_CLOCK_ENABLE,0x41); //ghi gia tri 0x41 den 2 thanh ghiREG_CLOCK_MANUAL ///va REG_CLOCK_ENABLE de module 6935 hoat dong dung (theo datasheet) write_radio_reg(REG_SERDES_CTL,7); // } void set_transmit_mode(){ write_radio_reg(REG_CONTROL,bTX_ENABLE|bPA_ENABLE); //Chon che do truyen // bTX_ENABLE = 0x40,bPA_ENABLE=0x04 write_radio_reg(REG_SERDES_CTL,bSERDES_ENABLE | mEND_OF_FRAME_LEN); // chon che do SERDES va 7 EOF bits write_radio_reg(REG_PA,mPA_BIAS); // chon de so kd cong suat lon nhat write_radio_reg(REG_VALID_TX,0xFF); //chon so bit valid, ->0xFF->8bit valid } void set_receive_mode(){ write_radio_reg(REG_CONTROL,bRX_ENABLE); // chon che do nhan write_radio_reg(REG_SERDES_CTL,bSERDES_ENABLE | mEND_OF_FRAME_LEN); //// chon che do SERDES va 7 EOF bits write_radio_reg(REG_RX_INT_EN,bRX_FULL_A); // Set RX interrupt: Full A //->phai set de khi co du lieu bit 0 trong thanh ghi trang thai->1 //write_radio_reg(REG_CONFIG,bIRQ_ACTIVE_LOW); } void send_byte(char x){ while( ! (SPIM_bReadStatus() & SPIM_SPIM_TX_BUFFER_EMPTY ) ); //cho bo dem Empty (du lieu truoc do duoc truyen xong) SPIM_SendTxData( x ); //gui byte x len bus spi } char read_byte(){ Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 60 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS while( ! (SPIM_bReadStatus() & SPIM_SPIM_RX_BUFFER_FULL ) ); //cho bo dem nhan Full (du lieu duoc nhan du) return SPIM_bReadRxData(); } void write_radio_reg(char reg, char value){ PRT1DR&=0b11011111;//cho chan nSS=0 send_byte(0x80 | reg);//Qua trinh Write->bit 7=1 read_byte(); send_byte(value); read_byte(); PRT1DR|=0b00100000;//nSS=1 } char read_radio_reg(char reg){ char x; PRT1DR&=0b11011111;//cho chan nSS=0 send_byte(reg); read_byte(); send_byte(0); x=read_byte(); PRT1DR|=0b00100000;//nSS=1 return x; } void display_all() { back1: I2Cm_Start(); display_time(); kiem_tra_hen_gio(mode_kt); if(get_key()) { if(key==4) { set_date(); i2cBuf[0]=0; for(i=0;i<8;i++) i2cBuf[i+1]=time[i]; I2Cm_bWriteBytes(0x68,i2cBuf,9,I2Cm_CompleteXfer); } if(key==1) Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 61 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS { back2: hien_thi_menu(menuchinh,5,0); if(key==1) { LCD_1_Control(LCD_1_DISP_CLEAR_HOME); goto back1; } else if(mode==4) { do { hien_thi_menu(menuthongtin,10,0); }while(key!=1); goto back2; } else if(mode==2) { xoa_cai_dat(); goto back2; } else if(mode==3) { mode_kt=1; goto back2; } else ///mode==1 { back3: hien_thi_menu(menucaidat,5,0); if(key==1) goto back2; else { thiet_bi=mode; back4: hien_thi_menu(menuthietbi,6,thiet_bi); if(key==1) goto back3; else { cai_dat_thiet_bi(thiet_bi,mode); goto back4; Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 62 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS } } } } } } void receive_data_PC(){ for(i=0;i<25;i++) { while(!(read_radio_reg(REG_RX_INT_STAT)&1)); flashBuf[i]=read_radio_reg(REG_DATA_RX_A); //doc 25 byte hen gio tu may tinh } write_flash(); } void update_time(){ i2cBuf[0]=0; for(i=1;i<8;i++) { while(!(read_radio_reg(REG_RX_INT_STAT)&1)); i2cBuf[i]=read_radio_reg(REG_DATA_RX_A); //doc 7 byte gio he thong tu may tinh } i2cBuf[8]=0x10; I2Cm_bWriteBytes(0x68,i2cBuf,9,I2Cm_CompleteXfer);//g hi du lieu gio he thog vao DS1307 } void send_data_PC(){ int kk; set_transmit_mode(); for (kk=0;kk<100;kk++) delay();//delay cho cho module wireless chuyen sang che do truyen write_radio_reg(REG_DATA_TX,PRT0DR); for (kk=0;kk<100;kk++) delay(); //cho module truyen xong 1 byte for(i=0;i<25;i++) { // while (!(read_radio_reg(REG_TX_INT_STAT) & 1)); Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 63 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS write_radio_reg(REG_DATA_TX,flashBuf[i]); for (kk=0;kk<100;kk++) delay(); } / set_receive_mode(); } void send_status(){ int kk; set_transmit_mode(); for (kk=0;kk<100;kk++) delay(); write_radio_reg(REG_DATA_TX,PRT0DR); for (kk=0;kk<100;kk++) delay(); set_receive_mode(); } void process_wusb_data(){ LCD_1_Control(LCD_1_DISP_CLEAR_HOME); while(!(read_radio_reg(REG_RX_INT_STAT)&1)); bWUSB=read_radio_reg(REG_DATA_RX_A);//Doc byte dau tien nhan tu may tinh switch (bWUSB){ case 0xFE: case 0xFD: case 0xFC: case 0xFB:turnOn(~bWUSB);break; //cac byte lenh bat(on) thiet bi case 0xFA: case 0xF9: case 0xF8: case 0xF7:turnOff(~bWUSB-4);break;//cac byte lenh tat thiet bi case 0xFF:receive_data_PC();break;//Lenh 0xFF:may tinh truyen du lieu cai dat case 0xF6:send_data_PC();break;//lenh 0xF6: may tinh yeu cau truyen du lieu len case 0xF5:update_time();break;//cap nhat gio he thong tu may tinh case 0xF4:send_status();break;//Lenh 0xF4: may tinh yeu cau truyen trang thai cua cac thiet bi Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 64 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS } LCD_1_Control(LCD_1_DISP_CLEAR_HOME); } void main() { PRT0DR|=0xFF;//tát ca cac pin cua P0 set len 1 mode_kt=0; LCD_1_Start(); //khoi dong module LCD I2Cm_Start(); //khoi dog module I2C LCD_1_Control(LCD_1_DISP_CLEAR_HOME);//Xoa man hinh LCD SPIM_Start(SPIM_SPIM_MODE_0 | SPIM_SPIM_MSB_FIRST);//khoi dong module SPI, che do 0, truyen MSB truoc radio_init(); //khoi dong module wireless set_receive_mode();//chon che do nhan read_flash();//doc bo nho eeprom cua PSOC while(1) { if((read_radio_reg(REG_RX_INT_STAT)&1))//doc thanh ghi trang thai nhan process_wusb_data(); //xu ly du lieu nhan tu may tinh display_all();//hien thi gio, kiem tra hen gio,hien thi menu } } 5.2 Chƣơng trình VB: Imports System.Drawing Imports System.Drawing.Drawing2D Public Class Form1 Public Declare Function Inp Lib "inpout32.dll" _ Alias "Inp32" (ByVal PortAddress As Integer) As Integer Public Declare Sub Out Lib "inpout32.dll" _ Alias "Out32" (ByVal PortAddress As Integer, ByVal Value As Integer) Dim data As Integer Dim Status As Integer Const SCK = 0 ' SCK Const MO = 1 ' DO (MOSI) Const nSS = 3 ' CS Const nPD = 2 'nPD Const nRESET = 4 Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 65 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS Const D5 = 5 Const D6 = 6 Const D7 = 7 Const DI = 7 'DI (MISO) Dim dataout As Integer Dim receive(25) As Integer Dim transmit(25) As Integer Dim LEDstatus As Byte = &HFF Dim timeout As Boolean Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ComboBox17.SelectedIndex = 0 ComboBox13.SelectedIndex = 0 ComboBox14.SelectedIndex = 0 ComboBox15.SelectedIndex = 0 ComboBox16.SelectedIndex = 0 data = &H378 Status = &H379 PictureBox1.Image = WindowsApplication6vb.My.Resources.ledoff PictureBox2.Image = WindowsApplication6vb.My.Resources.ledoff PictureBox3.Image = WindowsApplication6vb.My.Resources.ledoff PictureBox4.Image = WindowsApplication6vb.My.Resources.ledoff WUSB_Init() Set_Transmit_Mode() Timer2.Interval = 3000 Timer2.Enabled = True End Sub Private Sub setbit(ByVal port As Integer, ByVal bit As Integer) Dim data1 As Integer data1 = Inp(port) data1 = data1 Or (2 ^ bit) Out(port, data1) End Sub Private Sub ClrBit(ByVal port As Integer, ByVal bit As Integer) Dim data1 As Integer data1 = Inp(port) data1 = data1 And (Not (2 ^ bit)) Out(port, data1) End Sub Private Function readbit(ByVal port As Integer, ByVal bit As Integer) As Boolean Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 66 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS Dim data1 As Integer data1 = Inp(port) If (data1 And (2 ^ bit)) Then Return False 'Doc du lieu tu Chan BUSY -> dao bit Else Return True End If End Function Private Sub SendByteSPI(ByVal dat As Integer) Dim i As Integer setbit(data, D5) ClrBit(data, SCK) For i = 0 To 7 If (dat And (2 ^ (7 - i))) Then setbit(data, MO) Else ClrBit(data, MO) End If setbit(data, SCK) ClrBit(data, SCK) ClrBit(data, D5) Next End Sub Private Function readbyteSPI() As Integer Dim i, dat As Integer dat = 0 setbit(data, D5) 'cho LED báo hiệu sáng ClrBit(data, SCK) For i = 0 To 7 setbit(data, SCK) If readbit(Status, DI) Then dat = dat Xor (2 ^ (7 - i)) 'đọc MSB trước End If ClrBit(data, SCK) Next ClrBit(data, D5) Return dat End Function Private Sub Write_WUSB_REG(ByVal reg As Integer, ByVal value As Integer) ClrBit(data, nSS) 'chan nSS=0 SendByteSPI(&H80 Or reg) 'qua trinh ghi du lieu -> bit MSB =1 SendByteSPI(value) setbit(data, nSS) ClrBit(data, SCK) ClrBit(data, MO) Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 67 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS End Sub Private Function Read_WUSB_REG(ByVal reg As Integer) As Integer Dim value As Integer ClrBit(data, SCK) ClrBit(data, nSS) SendByteSPI(reg) value = readbyteSPI() setbit(data, nSS) ClrBit(data, SCK) Return value End Function Private Sub WUSB_Init() setbit(data, nPD) System.Threading.Thread.Sleep(1) ClrBit(data, nRESET) System.Threading.Thread.Sleep(1) setbit(data, nRESET) setbit(data, nSS) System.Threading.Thread.Sleep(1) Write_WUSB_REG(&H33, &H41) 'REG_CLOCK_MANUAL = 0x33 Write_WUSB_REG(&H32, &H41) 'REG_CLOCK_ENABLE 0x32 Write_WUSB_REG(&H6, &HF) 'REG_SERDES_CTL 0x06 End Sub Private Sub Set_Transmit_Mode() 'Set chế độ truyền Write_WUSB_REG(&H3, &H44) 'REG_CONTROL 0x03; (chọn chế độ truyền, và bộ khuêch đại bên trong chip) 'Write_WUSB_REG(&H33, &H41)'write_radio_reg(REG_SERDES_CTL,bSERDES_ENABLE | mEND_OF_FRAME_LEN); ' Write_WUSB_REG(&H6, &HF) 'Set he so khuech dai cong suat lon nhat Write_WUSB_REG(&H23, &H7) 'REG_PA 0x23;mPA_BIAS 0x07 'set so bit valid trong thanh ghi REG_SERDES Write_WUSB_REG(&H10, &HFF) 'REG_VALID_TX 0x10 End Sub Private Sub Set_Receive_Mode() 'Set chế độ nhận Write_WUSB_REG(&H3, &H80) 'REG_CONTROL 0x03,bRX_ENABLE 0x80 Write_WUSB_REG(&H6, &HF) 'write_radio_reg(REG_SERDES_CTL,bSERDES_ENABLE | mEND_OF_FRAME_LEN); Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 68 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS 'REG_RX_INT_EN 0x07,bRX_FULL_A 0x01 Write_WUSB_REG(&H7, &H1) 'REG_CONFIG 0x05 Write_WUSB_REG(&H5, &H0) 'write_radio_reg(REG_RX_INT_EN,bRX_FULL_A); 'write_radio_reg(REG_CONFIG,bIRQ_ACTIVE_LOW); End Sub Private Function ConverttoBCD(ByVal deci As Integer) As Integer Return ((deci \ 10) * 16 + (deci Mod 10)) End Function Private Sub UpdateLed() LEDstatus = (Not receive(0)) And &HFF If (LEDstatus And &H1) = 1 Then PictureBox1.Image = WindowsApplication6vb.My.Resources.ledon LEDstatus = LEDstatus And &HFE ElseIf (LEDstatus And &H1) = 0 Then PictureBox1.Image = WindowsApplication6vb.My.Resources.ledoff LEDstatus = LEDstatus Or &H1 End If If (LEDstatus And &H4) = 4 Then PictureBox2.Image = WindowsApplication6vb.My.Resources.ledon LEDstatus = LEDstatus And &HFB ElseIf (LEDstatus And &H4) = 0 Then PictureBox2.Image = WindowsApplication6vb.My.Resources.ledoff LEDstatus = LEDstatus Or &H4 End If If (LEDstatus And &H10) = &H10 Then PictureBox3.Image = WindowsApplication6vb.My.Resources.ledon LEDstatus = LEDstatus And &HEF ElseIf (LEDstatus And &H10) = 0 Then PictureBox3.Image = WindowsApplication6vb.My.Resources.ledoff LEDstatus = LEDstatus Or &H10 End If If (LEDstatus And &H40) = &H40 Then PictureBox4.Image = WindowsApplication6vb.My.Resources.ledon LEDstatus = LEDstatus And &HBF ElseIf (LEDstatus And &H40) = 0 Then PictureBox4.Image = WindowsApplication6vb.My.Resources.ledoff Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 69 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS LEDstatus = LEDstatus Or &H40 End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Timer2.Enabled = False Dim i As Integer Write_WUSB_REG(&HF, &HFF) 'lenh truyen du lieu hen gio System.Threading.Thread.Sleep(5) For i = 0 To 24 Write_WUSB_REG(&HF, transmit(i)) System.Threading.Thread.Sleep(5) Next i Timer2.Enabled = True End Sub Private Sub UpdateStatus() If (receive(1) And &H1) Then If (receive(1) And &H10) Then Label1.Text = "Giờ bật" Else Label1.Text = "Giờ tắt" End If Label2.Text = receive(5).ToString() + "/" + receive(6).ToString() + "/" + receive(7).ToString() Label3.Text = receive(4).ToString() + ":" + receive(3).ToString() + ":" + receive(2).ToString() Else Label1.Text = "Chưa cài đặt" Label2.Text = "" Label3.Text = "" End If If (receive(1) And &H2) Then If (receive(1) And &H20) Then Label4.Text = "Giờ bật" Else Label4.Text = "Giờ tắt" End If Label5.Text = receive(11).ToString() + "/" + receive(12).ToString() + "/" + receive(13).ToString() Label6.Text = receive(10).ToString() + ":" + receive(9).ToString() + ":" + receive(8).ToString() Else Label4.Text = "Chưa cài đặt" Label5.Text = "" Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 70 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS Label6.Text = "" End If If (receive(1) And &H4) Then If (receive(1) And &H40) Then Label7.Text = "Giờ bật" Else Label7.Text = "Giờ tắt" End If Label8.Text = receive(17).ToString() + "/" + receive(18).ToString() + "/" + receive(19).ToString() Label9.Text = receive(16).ToString() + ":" + receive(15).ToString() + ":" + receive(14).ToString() Else Label7.Text = "Chưa cài đặt" Label8.Text = "" Label9.Text = "" End If If (receive(1) And &H8) Then If (receive(1) And &H80) Then Label10.Text = "Giờ bật" Else Label10.Text = "Giờ tắt" End If Label11.Text = receive(23).ToString() + "/" + receive(24).ToString() + "/" + receive(25).ToString() Label12.Text = receive(22).ToString() + ":" + receive(21).ToString() + ":" + receive(20).ToString() Else Label10.Text = "Chưa cài đặt" Label11.Text = "" Label12.Text = "" End If End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick timeout = True 'MessageBox.Show("Lỗi truyền dữ liệu, kiểm tra kết nối", "Timeout Error", MessageBoxButtons.OK) Timer1.Enabled = False End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Timer2.Enabled = False Dim k As Integer Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 71 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS Write_WUSB_REG(&HF, &HF6) 'gui yeu cau truyen du lieu len may tinh System.Threading.Thread.Sleep(4) Set_Receive_Mode() timeout = False Timer1.Interval = 3000 Timer1.Enabled = True While True Application.DoEvents() 'REG_TX_INT_STAT 0x0e 'REG_RX_INT_STAT 0x08 'REG_DATA_RX_A 0x09 If (Read_WUSB_REG(&H8) And 1) Then receive(k) = Read_WUSB_REG(&H9) If k > 1 Then 'chuyen doi tu byte thu 3 receive(k) = (receive(k) \ 16) * 10 + (receive(k) Mod 16) End If k = k + 1 End If If (k = 26) Then Exit While End If If (timeout = True) Then MessageBox.Show("Lỗi truyền dữ liệu, kiểm tra kết nối", "Timeout Error", MessageBoxButtons.OK) Exit While End If End While If timeout = False Then UpdateLed() UpdateStatus() End If Timer1.Enabled = False Set_Transmit_Mode() Timer2.Enabled = True End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Timer2.Enabled = False 'disable Timer2 () WUSB_Init() setbit(data, D5) Set_Transmit_Mode() Timer2.Enabled = True End Sub Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 72 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS Private Sub updateBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles updateBtn.Click Timer2.Enabled = False Dim currentTime As System.DateTime = System.DateTime.Now Write_WUSB_REG(&HF, &HF5) System.Threading.Thread.Sleep(5) Write_WUSB_REG(&HF, ((currentTime.Second() \ 10) * 16 + (currentTime.Second() Mod 10))) System.Threading.Thread.Sleep(5) Write_WUSB_REG(&HF, ((currentTime.Minute() \ 10) * 16 + (currentTime.Minute() Mod 10))) System.Threading.Thread.Sleep(5) Write_WUSB_REG(&HF, ((currentTime.Hour() \ 10) * 16 + (currentTime.Hour() Mod 10))) System.Threading.Thread.Sleep(5) Write_WUSB_REG(&HF, ((currentTime.DayOfWeek() \ 10) * 16 + (currentTime.DayOfWeek() Mod 10))) System.Threading.Thread.Sleep(5) Write_WUSB_REG(&HF, ((currentTime.Day() \ 10) * 16 + (currentTime.Day() Mod 10))) System.Threading.Thread.Sleep(5) Write_WUSB_REG(&HF, ((currentTime.Month() \ 10) * 16 + (currentTime.Month() Mod 10))) System.Threading.Thread.Sleep(5) Write_WUSB_REG(&HF, (((currentTime.Year() And &HF) \ 10) * 16 + ((currentTime.Year() And &HF) Mod 10))) System.Threading.Thread.Sleep(5) Timer2.Enabled = True End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click End End Sub Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click Timer2.Enabled = False If (LEDstatus And &H1) = 1 Then 'neu bit thu 1 cua LEDstatus bang 1 (LED dang tat) thi gui lenh bat LED 1 Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 73 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS PictureBox1.Image = WindowsApplication6vb.My.Resources.ledon Write_WUSB_REG(&HF, &HFE) LEDstatus = LEDstatus And &HFE ElseIf (LEDstatus And &H1) = 0 Then 'Neu LED dang bat ->tat LED PictureBox1.Image = WindowsApplication6vb.My.Resources.ledoff Write_WUSB_REG(&HF, &HFA) LEDstatus = LEDstatus Or &H1 End If Timer2.Enabled = True End Sub Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click Timer2.Enabled = False If (LEDstatus And &H4) = 4 Then 'Neu bit thu 4 cua LEDstatus = 1 ->bat LED 2 (do LED 1,2,3,4 noi toi P0.0,P0.2,P0.4) PictureBox2.Image = WindowsApplication6vb.My.Resources.ledon Write_WUSB_REG(&HF, &HFD) LEDstatus = LEDstatus And &HFB ElseIf (LEDstatus And &H4) = 0 Then PictureBox2.Image = WindowsApplication6vb.My.Resources.ledoff Write_WUSB_REG(&HF, &HF9) LEDstatus = LEDstatus Or &H4 End If Timer2.Enabled = True End Sub Private Sub PictureBox3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox3.Click Timer2.Enabled = False If (LEDstatus And &H10) = &H10 Then PictureBox3.Image = WindowsApplication6vb.My.Resources.ledon Write_WUSB_REG(&HF, &HFC) LEDstatus = LEDstatus And &HEF ElseIf (LEDstatus And &H10) = 0 Then PictureBox3.Image = WindowsApplication6vb.My.Resources.ledoff Write_WUSB_REG(&HF, &HF8) LEDstatus = LEDstatus Or &H10 Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 74 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS End If Timer2.Enabled = True End Sub Private Sub PictureBox4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox4.Click Timer2.Enabled = False If (LEDstatus And &H40) = &H40 Then PictureBox4.Image = WindowsApplication6vb.My.Resources.ledon Write_WUSB_REG(&HF, &HFB) LEDstatus = LEDstatus And &HBF ElseIf (LEDstatus And &H40) = 0 Then PictureBox4.Image = WindowsApplication6vb.My.Resources.ledoff Write_WUSB_REG(&HF, &HF7) LEDstatus = LEDstatus Or &H40 End If Timer2.Enabled = True End Sub Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged If CheckBox1.Checked Then transmit(0) = transmit(0) Or &H1 Else transmit(0) = transmit(0) And (Not &H1) End If End Sub Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged If CheckBox2.Checked Then transmit(0) = transmit(0) Or &H2 Else transmit(0) = transmit(0) And (Not &H2) End If End Sub Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged If CheckBox3.Checked Then transmit(0) = transmit(0) Or &H4 Else Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 75 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS transmit(0) = transmit(0) And (Not &H4) End If End Sub Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox4.CheckedChanged If CheckBox4.Checked Then transmit(0) = transmit(0) Or &H8 Else transmit(0) = transmit(0) And (Not &H8) End If End Sub Private Sub ComboBox13_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox13.SelectedIndexChanged If ComboBox13.SelectedIndex() = 0 Then transmit(0) = transmit(0) Or &H10 ElseIf ComboBox13.SelectedIndex() = 1 Then transmit(0) = transmit(0) And (Not &H10) End If End Sub Private Sub ComboBox15_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox15.SelectedIndexChanged If ComboBox15.SelectedIndex() = 0 Then transmit(0) = transmit(0) Or &H20 ElseIf ComboBox15.SelectedIndex() = 1 Then transmit(0) = transmit(0) And (Not &H20) End If End Sub Private Sub ComboBox14_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox14.SelectedIndexChanged If ComboBox14.SelectedIndex() = 0 Then transmit(0) = transmit(0) Or &H40 ElseIf ComboBox14.SelectedIndex() = 1 Then transmit(0) = transmit(0) And (Not &H40) End If End Sub Private Sub ComboBox16_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox16.SelectedIndexChanged If ComboBox16.SelectedIndex() = 0 Then Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 76 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS transmit(0) = transmit(0) Or &H80 ElseIf ComboBox16.SelectedIndex() = 1 Then transmit(0) = transmit(0) And (Not &H80) End If End Sub Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged transmit(3) = ConverttoBCD(ComboBox1.SelectedIndex) End Sub Private Sub DateTimePicker1_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles DateTimePicker1.Validated transmit(4) = ConverttoBCD(DateTimePicker1.Value.Day()) transmit(5) = ConverttoBCD(DateTimePicker1.Value.Month()) transmit(6) = ConverttoBCD(DateTimePicker1.Value.Year() And &HF) End Sub Private Sub DateTimePicker2_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles DateTimePicker2.Validated transmit(10) = ConverttoBCD(DateTimePicker2.Value.Day()) transmit(11) = ConverttoBCD(DateTimePicker2.Value.Month()) transmit(12) = ConverttoBCD(DateTimePicker2.Value.Year() And &HF) End Sub Private Sub DateTimePicker4_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles DateTimePicker4.Validated transmit(16) = ConverttoBCD(DateTimePicker4.Value.Day()) transmit(17) = ConverttoBCD(DateTimePicker4.Value.Month()) transmit(18) = ConverttoBCD(DateTimePicker4.Value.Year() And &HF) End Sub Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 77 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS Private Sub DateTimePicker3_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles DateTimePicker3.Validated transmit(22) = ConverttoBCD(DateTimePicker3.Value.Day()) transmit(23) = ConverttoBCD(DateTimePicker3.Value.Month()) transmit(24) = ConverttoBCD(DateTimePicker3.Value.Year() And &HF) End Sub Private Sub ComboBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged transmit(1) = ConverttoBCD(ComboBox3.SelectedIndex) End Sub Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged transmit(2) = ConverttoBCD(ComboBox2.SelectedIndex) End Sub Private Sub ComboBox4_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox4.SelectedIndexChanged transmit(7) = ConverttoBCD(ComboBox4.SelectedIndex) End Sub Private Sub ComboBox5_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox5.SelectedIndexChanged transmit(8) = ConverttoBCD(ComboBox5.SelectedIndex) End Sub Private Sub ComboBox6_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox6.SelectedIndexChanged transmit(9) = ConverttoBCD(ComboBox6.SelectedIndex) End Sub Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 78 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS Private Sub ComboBox7_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox7.SelectedIndexChanged transmit(13) = ConverttoBCD(ComboBox7.SelectedIndex) End Sub Private Sub ComboBox8_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox8.SelectedIndexChanged transmit(14) = ConverttoBCD(ComboBox8.SelectedIndex) End Sub Private Sub ComboBox9_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox9.SelectedIndexChanged transmit(15) = ConverttoBCD(ComboBox9.SelectedIndex) End Sub Private Sub ComboBox10_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox10.SelectedIndexChanged transmit(19) = ConverttoBCD(ComboBox10.SelectedIndex) End Sub Private Sub ComboBox11_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox11.SelectedIndexChanged transmit(20) = ConverttoBCD(ComboBox11.SelectedIndex) End Sub Private Sub ComboBox12_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox12.SelectedIndexChanged transmit(21) = ConverttoBCD(ComboBox12.SelectedIndex) End Sub Private Sub ComboBox17_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox17.SelectedIndexChanged If ComboBox17.SelectedIndex = 0 Then data = &H378 Status = &H379 ElseIf ComboBox17.SelectedIndex = 1 Then data = &H278 Status = &H279 End If Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 79 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS End Sub Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick Dim count As Integer back1: Timer2.Enabled = False Write_WUSB_REG(&HF, &HF4) 'gui yeu cau truyen du lieu len may tinh System.Threading.Thread.Sleep(4) timeout = False Timer1.Interval = 3000 Timer1.Enabled = True Set_Receive_Mode() While True Application.DoEvents() 'REG_RX_INT_STAT 0x08 ''REG_DATA_RX_A 0x09 count = count + 1 If (Read_WUSB_REG(&H8) And 1) Then receive(0) = Read_WUSB_REG(&H9) Exit While End If If (timeout = True) Then Timer2.Enabled = False Exit While End If If count = 10 Then GoTo back1 End If End While If timeout = False Then UpdateLed() End If Timer1.Enabled = False Set_Transmit_Mode() Timer2.Enabled = True End Sub Private Sub StartToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartToolStripMenuItem.Click Button3.PerformClick() End Sub Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click Close() End Sub Nhóm 01 – Lớp 04ĐT2 – Khoa ĐTVT – ĐHBK Đà Nẵng 80 GIÁM SÁT, ĐIỀU KHIỂN THIẾT BỊ ĐIỆN QUA CỔNG PARALLEL DÙNG WIRELESS Private Sub SeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SeToolStripMenuItem.Click Button1.PerformClick() End Sub Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click MessageBox.Show("Chương trình điều khiển giám sát thiết bị điện qua cổng Parallel" & vbNewLine & "Thực hiện: Nhóm 1 - Lớp 04DT2" & vbNewLine & _ "Thành Viên Nhóm 1:" & vbNewLine & _ " Nguyễn Ngọc Nhân" & vbNewLine & _ " Đặng Vân Khánh" & vbNewLine & _ " Lê Văn Sanh" & vbNewLine & _ " Nguyễn Đức Khoa" & vbNewLine & _ " Nguyễn Trọng Trí" & vbNewLine & _ " Đinh Hoàng Cẩm Lệ" & vbNewLine & _ " Nguyễn Thị Hoàng Oanh" & vbNewLine & _ " Nguyễn Việt Trung" & vbNewLine & _ " Thongsoul Sengchaleung", "About", MessageBoxButtons.OK) End Sub Private Sub CậpNhậtToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CậpNhậtToolStripMenuItem.Click updateBtn.PerformClick() End Sub Private Sub NhậnDữLiệuToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NhậnDữLiệuToolStripMenuItem.Click Button2.PerformClick() End Sub Private Sub HelpToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HelpToolStripMenuItem1.Click Form2.Show() End Sub Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick Me.Text = "Paralel Port - " + Now.ToString() End Sub End Class

Các file đính kèm theo tài liệu này:

  • pdfGiám sát, điều khiển thiết bị điện qua cổng parallel dùng wireles.pdf