ĐOẠN CODE CHƯƠNG TRÌNH 65

Một phần của tài liệu ứng dụng pic16f877a điều khiển thiết bị trong nhà (Trang 68 - 82)

#include <16f877a.h>

#device 16F877*=16 ADC=10

#include <DEFS_16f877a.h>

#FUSES NOWDT, XT, PUT, NOPROTECT, NODEBUG, BROWNOUT, NOLVP,NOCPD, NOWRT

#use delay(clock=4000000)

#include <lcd.c> // thu vien su dung di kem

#use fast_io(A)

#use fast_io(B)

#use fast_io(C)

#use fast_io(D)

#use fast_io(E) float nhietdo;

int8 j=0, i=0,l=0,k=0,kp,sd=3,r=1,n;

//kp là lưu số nhập từ bàn phím //j là biến phần tử trong mảng ss

//i là biến phần tử trong mảng mật mã 1 //k là biến phần tử trong mảng mật mã 2 //sd là số đém nhập pw1 sai 3 lần

char matma1[]={2,4,8,0}; // số lưu cài định sẵn ở pw1 char matma2[]={1,3,0,0}; // số lưu cài định san ở pw2 unsigned int8 ss[4]; // mảng lưu số nhập từ bàn phím void quat_on() {

rc3=1;

} void quat_off() {

rc3=0;

}

void cua_on() { sd=3;l=0;

RC0=1;RC1=0;

delay_ms(2500);

RC0=0;RC1=0;

} void cua_off() {

RC0=0;RC1=1;

delay_ms(2500);

RC0=0;RC1=0;

} void loa_on() {

rc2=1;

delay_ms(20000);

rc2=0;

} void den_on() {

rc4=1;

} void den_off(){

rc4=0;

} Void quet_phim() { T11: while(j<4){

if(re2==0) {

printf(lcd_putc,"\f"); //xoa man hinh printf(lcd_putc," CLOSE2");

delay_ms(10);

cua_off();

break;

} else

goto D;

D: PORTb=0b11111110;

delay_ms(50);

{

if(Rb4==0) //số 1 {

delay_ms(50);

printf(lcd_putc,"*");

delay_ms(10);

kp=1;

ss[j++]=kp;

delay_ms(150);

} else

goto T1;

T1: if(Rb5==0) //số 4 {

delay_ms(50);

printf(lcd_putc,"*") ; delay_ms(10);

kp= 4;

ss[j++]=kp;

delay_ms(150);

} else

goto T2;

T2: if(Rb6==0) //số 7 {

delay_ms(50);

printf(lcd_putc,"*") ; delay_ms(10);

kp= 7;ss[j++]=kp;

delay_ms(150);

} else

goto T3;

}

T3: PORTb=0b11111101;

delay_ms(50);

{

if (Rb4==0) //số 2 {

delay_ms(50);

printf(lcd_putc,"*") ; delay_ms(10);

kp= 2;ss[j++]=kp;

delay_ms(150);

} else

goto T4;

T4: if (Rb5==0) //số 5 {

delay_ms(50);

printf(lcd_putc,"*") ; delay_ms(10);

kp= 5;ss[j++]=kp;

delay_ms(150);

} else

goto T5;

T5: if(Rb6==0) //số 8 {

delay_ms(50);

printf(lcd_putc,"*") ; delay_ms(10);

kp=8;ss[j++]=kp;

delay_ms(150);

} else

goto T6;

T6: if(Rb7==0) //số 0 {

delay_ms(50);

printf(lcd_putc,"*") ; delay_ms(10);

kp= 0;ss[j++]=kp;

delay_ms(150);

} else

goto T7;

}

T7: PORTb=0b11111011 ; delay_ms(50);

{

if (Rb4==0) //số 3 {

delay_ms(50);

printf(lcd_putc,"*") ; delay_ms(10);

kp= 3;ss[j++]=kp;

delay_ms(150);

} else

goto T8;

T8: if (Rb5==0) //số 6 {

delay_ms(50);

printf(lcd_putc,"*") ; delay_ms(10);

kp= 6;ss[j++]=kp;

delay_ms(150);

} else

goto T9;

T9: if (Rb6==0) //số 9 {

delay_ms(50);

printf(lcd_putc,"*") ; delay_ms(10);

kp= 9;ss[j++]=kp;

delay_ms(150);

} else

goto T10;

T10: if(Rb7==0) { j=0;

printf(lcd_putc,"\f"); //xoa man hinh printf(lcd_putc,"moi ban nhap 4so");

delay_ms(10);

lcd_gotoxy(1,2);

printf(lcd_putc,"PW1:");

delay_ms(10);

} else

goto T11;

}

}//kết thúc vòng lặp while với 4 lần nhấn }// kết thúc vòng void quét phím

void main() {

set_tris_A(0xff);

set_tris_B(0xf0);

set_tris_C(0x00); portc=0x00;

set_tris_D(0x00);

set_tris_E(0xFF);

LCD_Init(); //khoi tao lcd lai: printf(lcd_putc,"\f"); //xoa man hinh printf(lcd_putc,"school:CDKTCT");

delay_us(100);

lcd_gotoxy(1,2);

printf(lcd_putc,"Street:H_T_K");

delay_ms(300);

if(ra2==0) goto loop;

else

goto lai;

loop:

while (1) { pw1: j=0;

printf(lcd_putc,"\f"); //xoa man hinh printf(lcd_putc,"moi ban nhap 4so");

delay_ms(10);

lcd_gotoxy(1,2);

printf(lcd_putc,"PW1:");

delay_ms(100);

while(1){

quet_phim();

tiep1: if(ra2==0)

goto lai;

else

goto xoa_pw1;

xoa_pw1: Portb=0b11111011 ; if(rb7==0)

{

printf(lcd_putc,"\f");

printf(lcd_putc,"moi ban nhap 4so");

delay_ms(10);

lcd_gotoxy(1,2);

printf(lcd_putc,"PW1:");

delay_ms(100);

j=0;

goto pw1;

} else

goto ok_pw1;

ok_pw1: portb=0b11111110;

if(rb7==0){ //nut nhan ok for(i=0;i<4;i++){

( matma1[i]==ss[i]);

delay_ms(250);

if((matma1[i]!=ss[i])) goto saima;

} printf(lcd_putc,"\f");

printf(lcd_putc,"chap nhan");

delay_ms(100);

den_on();

cua_on();//cua hoat dong goto loop1;

saima: if(l==0) {

printf(lcd_putc,"\f");

printf(lcd_putc,"chua chap nhan");

delay_ms(1000);

printf(lcd_putc,"\f");

printf(lcd_putc,"so lan nhap sai ");

delay_ms(10);

lcd_gotoxy(1,2);

printf(lcd_putc,"PW1 duoc 3 lan");

delay_ms(3000);

l++;

goto pw1;

} else

goto saima1;

saima1: printf(lcd_putc,"\f");

printf(lcd_putc,"chua chap nhan");

delay_ms(1000);

sd--;

n=sd;

if(sd==0)

{

printf(lcd_putc,"\f");

printf(lcd_putc,"loa phat");

delay_ms(50);

loa_on();

goto pw2;

} else {

printf(lcd_putc,"\f");

printf(lcd_putc,"so lan nhap sai”);

delay_ms(50);

lcd_gotoxy(1,2);

printf(lcd_putc,"PW1 con:%u",n);

delay_ms(3000);

goto pw1;

} }

else

goto tiep1;

}//kết thúc vòng quét phím và so sánh mảng ở pw1

//************************************************//

Pw2: sd=3;

j=0;

printf(lcd_putc,"\f"); //xoa man hinh printf(lcd_putc,"moi ban nhap4so:");

delay_ms(10);

lcd_gotoxy(1,2);

printf(lcd_putc,"PW2:");

delay_ms(100);

while(1){

quet_phim();

tiep2: if(ra2==0)

goto lai;

else

goto xoa_pw2;

xoa_pw2: Portb=0b11111011 ; if(rb7==0)

{

printf(lcd_putc,"\f");

printf(lcd_putc,"moi ban nhap 4so");

delay_ms(10);

lcd_gotoxy(1,2);

printf(lcd_putc,"PW2:");

delay_ms(100);

goto pw2;

} else

goto ok_pw2;

ok_pw2: portb=0b11111110;

if(rb7==0)//nut nhan ok { for(k=0;k<4;k++){

(matma2[k]==ss[k]);

delay_ms(250);

if((matma2[k]!=ss[k])) goto saima2;

} printf(lcd_putc,"\f");

printf(lcd_putc,"chap nhan");

delay_ms(1000);

sd=3;

l=0;

goto pw1;

saima2: printf(lcd_putc,"\f");

printf(lcd_putc,"chua chap nhan");

delay_ms(1000);

goto pw2;

} else

goto tiep2;

} //kết thúc vòng quét phím và so sánh mảng ở pw2 loop1: setup_adc(adc_clock_internal);

setup_adc_ports(AN0);

set_adc_channel(0);

delay_ms(100);

printf(lcd_putc,"\f");

nhietdo=read_adc();

nhietdo=nhietdo*0.48828125;

/////số 0.48828125 là kết quả của việc chọn điện áp chuẩn 5v , chế độ đọc ADC là 10 bit và đầu ra điện áp LM35 Dz là 10mv/10c /////

printf(lcd_putc,"Nhiet do:%f",nhietdo);

delay_ms(1000);

if(nhietdo>=30) quat_on();

else

quat_off();

if(r==0) goto M2;

else goto M1;

M1: if(re0==0) {

printf(lcd_putc,"\f"); //xoa man hinh printf(lcd_putc," CLOSE1");

delay_ms(10);

cua_off();

r--;

Goto loop1;

} Else

Goto loop1;

M2: if(re1==0) {

printf(lcd_putc,"\f"); //xoa man hinh printf(lcd_putc,"ban muon tat den");

delay_ms(10);

lcd_gotoxy(1,2);

printf(lcd_putc," ,quat khong ?");

delay_ms(1000);

sd=3;

l=0;

goto MC1;

} else

goto loop1;

MC1: if(re0==0) //không muốn tắt đèn và quạt

{

printf(lcd_putc,"\f"); //xoa man hinh delay_ms(1000);

goto loop1;

} else

goto MC2;

MC2: if(re1==0) // muốn tắt đèn và quạt {

printf(lcd_putc,"\f"); //xoa man hinh printf(lcd_putc," den va quat ");

delay_ms(10);

lcd_gotoxy(1,2);

printf(lcd_putc," da tat");

delay_ms(1000);

den_off();

quat_off();

cua_on();

r=1;

goto pw1; }

else

goto MC1;

}// kết thúc vòng lập while nhập pw1, pw2 và đọc nhiệt độ }// kết thúc vòng main

5.2 ĐOẠN CODE THƢ VIỆN LCD:

struct lcd_pin_map { BOOLEAN enable;

BOOLEAN rs;

BOOLEAN rw;

BOOLEAN unused;

int data : 4;

} lcd;

#if defined use_portb_lcd

#ifdef __pch__

#locate lcd = 0xf81

#else

#locate lcd = 6

#endif

#define set_tris_lcd(x) set_tris_b(x)

#else

#ifdef __pch__

#locate lcd = 0xf83

#else

#locate lcd = 8

#endif

#define set_tris_lcd(x) set_tris_d(x)

#endif

#define lcd_type 2 // 0=5x7, 1=5x10, 2=2 lines

#define lcd_line_two 0x40 // LCD RAM address for the second line BYTE const LCD_INIT_STRING[4] = {0x20 | (lcd_type << 2), 0xc, 1, 6};

struct lcd_pin_map const LCD_WRITE = {0,0,0,0,0};

struct lcd_pin_map const LCD_READ = {0,0,0,0,15};

BYTE lcd_read_byte() {

BYTE low,high;

set_tris_lcd(LCD_READ);

lcd.rw = 1;

delay_cycles(1);

lcd.enable = 1;

delay_cycles(1);

high = lcd.data;

lcd.enable = 0;

delay_cycles(1);

lcd .enable = 1;

delay_us(1);

low = lcd.data;

lcd.enable = 0;

set_tris_lcd(LCD_WRITE);

return( (high<<4) | low);

}

void lcd_send_nibble( BYTE n ) { lcd.data = n;

delay_cycles(1);

lcd.enable = 1;

delay_us(2);

lcd.enable = 0;

}

void lcd_send_byte( BYTE address, BYTE n ) { lcd.rs = 0;

while ( bit_test(lcd_read_byte(),7) ) ; lcd.rs = address;

delay_cycles(1);

lcd.rw = 0;

delay_cycles(1);

lcd.enable = 0;

lcd_send_nibble(n >> 4);

lcd_send_nibble(n & 0xf);

} void lcd_init() {

BYTE i;

set_tris_lcd(LCD_WRITE);

lcd.rs = 0;

lcd.rw = 0;

lcd.enable = 0;

delay_ms(15);

for(i=1;i<=3;++i) { lcd_send_nibble(3);

delay_ms(5);

} lcd_send_nibble(2);

for(i=0;i<=3;++i)

lcd_send_byte(0,LCD_INIT_STRING[i]);

}

void lcd_gotoxy( BYTE x, BYTE y) { BYTE address;

if(y!=1)

address=lcd_line_two;

else

address=0;

address+=x-1;

lcd_send_byte(0,0x80|address);

} void lcd_putc( char c) {

switch (c) {

case '\f' : lcd_send_byte(0,1);

delay_ms(2);

break;

case '\n' : lcd_gotoxy(1,2); break;

case '\b' : lcd_send_byte(0,0x10); break;

default : lcd_send_byte(1,c); break;

} }

char lcd_getc( BYTE x, BYTE y) { char value;

lcd_gotoxy(x,y);

while ( bit_test(lcd_read_byte(),7) );

lcd.rs=1;

value = lcd_read_byte();

lcd.rs=0;

return(value);

}

Một phần của tài liệu ứng dụng pic16f877a điều khiển thiết bị trong nhà (Trang 68 - 82)

Tải bản đầy đủ (PDF)

(86 trang)