ESCUELA SUPERIOR POLITÉCNICA DEL LITORAL FACULTAD DE INGENIERÍA EN ELECTRICIDAD Y COMPUTACIÓN
Proyecto de Graduación
Diseño e implementación de un sistema Inalámbrico de A larma Domiciliaria Domiciliaria con alerta vía celular Contenido Código fuente en lenguaje Basic de los microcontroladores PIC16F628 y PIC18F4550 comprendidos en los bloques Alarma y Controlador respectivamente
Autores Jorge Galarza Rosas Titulo Ing. Telemático Ricardo Cajo Díaz Titulo Ing. Electrónica y Telecomunicaciones T elecomunicaciones –
–
Índice Consola GSM Alarma Puerta Alarma Movimiento
.
..2 19 ..22
…………………………………………………………………… ……………………
…………………………………………………………………………………………
………………………………………………………………………………
Codigo Consola GSM
Autores: Ricardo Cajo - Jorge Galarza
1: program ConsolaGSM 2: '***************************DECLARACION DE VARIABLES**************************** 3: Dim kp,received_byte,viajero,alert_alarma,cod_alarma, Estado_Alarma,acknoledge, num_telf as byte 4: Dim checksun_recibido,checksun_calculado as byte 5: dim claveInicial,ClaveIngresada,Codigos_Alarmas,Codigos as byte[6] ' clave de 5 digitos 6: dim bte_old_pass,bte_new_pass,bte_rep_pass , dato as byte[6] 7: dim bandera_oldPass,bandera_newPass,bandera_repPass as byte 8: dim i,ind_clave,opcion,bandera,veces as byte 9: dim txt_numero as char[16 16] ] 10: dim txt_numero_p as char[16 16] ] 11: dim txt_clave as char[7] 12: dim txt_codigo as char[4] 13: dim str_cadena as char[4] 14: dim str_numero_convertido as char[3] 15: dim bte_numero_add as byte[10 10] ] 16: 17: '*****************************CONSTANTES**************************************** 18: const esc = $23 'ESC/CANCEL esta Tecla sirve para salir o para CANCELAR OJO 19: const enter = $70 20: const INICIO = $24 21: const ID = $49 22: const FIN = $46 23: '*********************************ESTRUCTURAS*********************************** * 24: structure Telf_Usuarios 25: 10][ ][9 9] dim Telf as string[10 26: end structure 27: 28: dim Telefonos as Telf_Usuarios 29: 30: '*******************************INTERRUPCION************************************ 31: sub procedure interrupt() 32: 33: ' Usart_Data_Ready es lo mismo que haber puesto PIR1.RCIF 34: if Usart_Data_Ready = 1 then 35: received_byte = Usart_Read 36: select case viajero 37: 38: case 0 39: if (received_byte=INICIO) then '$' 40: porta. 0=0 porta.0 41: viajero= 1 viajero=1 42: else 43: viajero= 0 viajero=0 44: end if 45: 46: case 1 47: 'I' ID if (received_byte=ID) then 48: viajero= 2 viajero=2 49: else 50: viajero= 0 viajero=0 51: end if 52: 53: case 2 54: dato[ 0]=(received_byte) 'Codigo Alarma dato[0 55: viajero= 3 viajero=3 56: case 3 57: dato[ 1]= received_byte 'Estado Alarma dato[1
2 /25
mikroBasic compiler for PIC by mikroElektronika
Codigo Consola GSM
Autores: Ricardo Cajo - Jorge Galarza
58: viajero= 4 viajero=4 59: 4 case 60: dato[ 2]= received_byte 'Alerta Alarma dato[2 61: viajero= 5 viajero=5 62: case 5 63: dato[ 3]= received_byte 'acknoledge dato[3 64: viajero= 6 viajero=6 65: case 6 66: dato[ 4]= received_byte 'checksun_recibido dato[4 67: checksun_recibido =dato[4 =dato[ 4] 68: viajero= 7 viajero=7 69: case 7 70: dato[ 5]=received_byte dato[5 71: (dato[ 5]= FIN) then 'F' STOP if(dato[5 72: checksun_calculado = dato[0 dato[0]+dato[1 ]+dato[ 1]+dato[2 ]+dato[2]+dato[3 ]+dato[3] 73: if(checksun_calculado = checksun_recibido )then 74: cod_alarma=dato[ 0] 75: Estado_Alarma=dato[ 1] 76: alert_alarma =dato[2 =dato[2] 77: acknoledge=dato[ 3] 78: viajero= 0 viajero=0 79: else 80: viajero= 0 viajero=0 81: end if 82: else 83: viajero= 0 viajero=0 84: end if 85: end select 86: 87: end if 88: PIR1.RCIF=0 PIR1.RCIF= 0 ' Si el dato a llegado limpio la bandera de recepcion 89: PIE1.RCIE=1 PIE1.RCIE= 1 ' Habilitar nuevamente la interrupcion por USART 90: end sub 91: 92: '****************************ENVIAR MENSAJES GSM******************************** 93: sub procedure enviar_sms(dim byref Telefonos as Telf_Usuarios, dim i as byte,dim 40]) ]) byref texto as string[40 94: dim j as byte 95: dim k as Byte 96: 97: i-1 for j = 0 to i-1 98: 99: Usart_Write_Text( "AT+CMGF=1" "AT+CMGF=1") ) 100: Usart_Write( 10) Usart_Write(10 ) 101: Usart_Write( 13) Usart_Write(13 ) 102: delay_ms(1000 delay_ms( 1000) )'500 103: Usart_Write_Text( "AT+CMGS=" "AT+CMGS=") ) 104: Usart_Write( 34) Usart_Write(34 )' " 105: Usart_Write_Text(Telefonos.Telf[j]) 106: Usart_Write( 34) Usart_Write(34 )' " 107: Usart_Write( 10) Usart_Write(10 ) 108: Usart_Write( 13) Usart_Write(13 ) 109: delay_ms(1000 delay_ms( 1000) ) 110: 111: for k = 0 to Strlen(texto)- 1 112: Usart_Write(texto[k]) 113: next k 114: 115: Usart_Write( 26) Usart_Write(26 ) 116: Usart_Write( 10) Usart_Write(10 )
3 /25
mikroBasic compiler for PIC by mikroElektronika
Codigo Consola GSM
Autores: Ricardo Cajo - Jorge Galarza
117: Usart_Write( 13) Usart_Write(13 ) 118: delay_ms(8000 delay_ms( 8000) )'8000 119: next j 120: texto="" texto= "" 121: 122: end sub 123: '************************FUNCION LEER TECLADO********************************** 124: sub procedure leer_teclado() 125: kp = 0 126: portb.0 portb. 0=0 127: portb.1 portb. 1=1 128: portb.2 portb. 2=1 129: portb.3 portb. 3=1 130: '' portb=%11111110 131: (portb. 4=0)then if(portb.4 132: (portb.4=0) wend while(portb.4 133: kp=$31 kp= $31 end if'1 134: (portb. 5=0)then if(portb.5 135: (portb.5=0) wend while(portb.5 136: kp=$32 kp= $32 end if'2 137: (portb.6 (portb. 6=0)then if 138: (portb.6=0) wend while(portb.6 139: kp=$33 kp= $33 end if'3 140: (portb.7=0)then if (portb.7 141: (portb.7=0) wend while(portb.7 142: kp=$41 kp= $41 end if'A 143: (kp=0)then if(kp=0 144: portb.0 portb. 0=1 145: portb.1 portb. 1=0 146: portb.2 portb. 2=1 147: portb.3 portb. 3=1 148: ''portb=%11111101 149: (portb. 4=0)then if(portb.4 150: (portb.4=0) wend while(portb.4 151: kp=$34 kp= $34 end if'4 152: (portb. 5=0)then if(portb.5 153: (portb.5=0) wend while(portb.5 154: kp=$35 kp= $35 end if'5 155: (portb. 6=0)then if(portb.6 156: (portb.6=0) wend while(portb.6 157: kp=$36 kp= $36 end if'6 158: (portb.7=0)then if (portb.7 159: (portb.7=0) wend while(portb.7 160: kp=$42 kp= $42 end if'B 161: else return 162: end if 163: (kp=0) then if(kp=0 164: portb.0 portb. 0=1 165: portb.1 portb. 1=1 166: portb.2 portb. 2=0 167: portb.3 portb. 3=1 168: 169: ''=%111101011 170: (portb. 4=0)then if(portb.4 171: (portb.4=0) wend while(portb.4 172: kp=$37 kp= $37 end if'7 173: (portb. 5=0)then if(portb.5 174: (portb.5=0) wend while(portb.5 175: kp=$38 kp= $38 end if'8 176: (portb. 6=0)then if(portb.6
4 /25
mikroBasic compiler for PIC by mikroElektronika
Codigo Consola GSM
Autores: Ricardo Cajo - Jorge Galarza
177: (portb.6=0) wend while(portb.6 178: kp=$39 kp= $39 end if'9 179: (portb.7=0)then if (portb.7 180: (portb.7=0) wend while(portb.7 181: kp=$43 kp= $43 end if'C 182: else return 183: end if 184: (kp=0)then if(kp=0 185: portb.0 portb. 0=1 186: portb.1 portb. 1=1 187: portb.2 portb. 2=1 188: portb.3 portb. 3=0 189: ''portb=%11110111 190: (portb. 4=0)then if(portb.4 191: (portb.4=0) wend while(portb.4 192: kp=$23 kp= $23 end if'esc 193: (portb. 5=0)then if(portb.5 194: (portb.5=0) wend while(portb.5 195: kp=$30 kp= $30 end if'0 196: (portb. 6=0)then if(portb.6 197: (portb.6=0) wend while(portb.6 198: kp=$70 kp= $70 end if'enter <199: (portb.7=0)then if (portb.7 200: (portb.7=0) wend while(portb.7 201: kp=$44 kp= $44 end if'D 202: else return 203: end if 204: 205: end sub 206: '***************************MOSTRAR MENSAJE LCD********************************* 207: sub procedure ShowMessage( dim fil,col as byte,dim delayms as word,dim byref msg 16]) ]) as string[16 208: Lcd_Cmd(LCD_CLEAR) 209: Lcd_Out(fil, col,msg) 210: VDelay_ms(delayms) 211: end sub 212: '**********************GUARDAR PASSWORD***************************************** 213: sub procedure SavePassword( dim byref clave as byte[6] ) 214: i=0 to 4 for i=0 215: EEprom_write(i,clave[i]) 216: delay_ms(25 delay_ms( 25) ) 217: next i 218: end sub 219: '********************OBTENER NUMERO TELEFONICO DESDE EEPROM********************* 220: sub procedure GetFono( dim byref strText as char[16 16], ], dim pos as byte) 221: dim numeroObtenido,j as byte 222: ][3] dim txt_save as char[9][3 223: valor_inicio,valor_fin dim as byte 224: 225: j=0 to 8 for j=0 226: txt_save[j][ 2] = " " txt_save[j][2 227: next j 228: numeroObtenido= 0 229: 230: valor_inicio = 176 + (pos -1)*8 )*8 231: valor_fin = valor_inicio +7 232: for j=valor_inicio to valor_fin 233: numeroObtenido = EEprom_read(j) 234: if numeroObtenido = 255 then return end if 235: ByteToStr(numeroObtenido,txt_save[j-valor_inicio+ 1])
5 /25
mikroBasic compiler for PIC by mikroElektronika
Codigo Consola GSM
236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294:
Autores: Ricardo Cajo - Jorge Galarza
next j for j = 1 to 8 strText[j]=txt_save[j][ 2] next j strText[0 strText[ 0]="0" ]="0" end sub '********************OBTENER CODIGO DE ALARMA DESDE EEPROM********************* sub procedure GetCode( dim byref Codigo as byte[6], dim pos as byte) dim numeroObtenido,j as byte dim valor_inicio,valor_fin as byte numeroObtenido= 0
valor_inicio = 64 + (pos -1)*8 )*8 valor_fin = valor_inicio +1 for j=valor_inicio to valor_fin numeroObtenido = EEprom_read(j) delay_ms(30 delay_ms( 30) ) Codigo[j-valor_inicio]=numeroObtenido next j end sub '*************************SETEAR ARREGLO**************************************** 16], ], dim caracter as string,dim sub procedure SetStr(dim byref strText as char[16 tamano as byte) dim j as byte j=0 to tamano-1 tamano-1 for j=0 strText[j] = caracter next j end sub '**********************GUARDAR NUMERO DE TELEFONO EN EEPROM********************* sub function SaveEprom_Number(dim byref number as byte[9]) as boolean dim estado as boolean dim cont as byte dim num as byte dim j as word estado=1 estado= 1 cont =1 j=176 j=176 j<=255 while j<=255 Delay_ms(30 Delay_ms( 30) ) num=EEprom_read(j) Delay_ms(30 Delay_ms( 30) ) num<>$FF then if num<>$FF j= j+7 j+7 else EEprom_write(j,number[cont]) Inc(cont) end if if (cont = 9) then result = estado return end if inc(j) wend result=0 result= 0 end sub '**********************GU '*********** ***********GUARDAR ARDAR CODIGO DE ALARMA EN EEPROM ************ ********************* ********* sub function SaveEprom_Code(dim byref code as byte[9]) as boolean dim estado as boolean dim cont as byte dim num as byte
6 /25
mikroBasic compiler for PIC by mikroElektronika
Codigo Consola GSM
295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354:
Autores: Ricardo Cajo - Jorge Galarza
dim j as word estado=1 estado= 1 cont =0 j=64 j=64 j<=143 while j<=143 Delay_ms(30 Delay_ms( 30) ) num=EEprom_read(j) Delay_ms(30 Delay_ms( 30) ) num<>$FF then if num<>$FF j= j+7 j+7 else EEprom_write(j,code[cont]) Inc(cont) end if if (cont = 2) then result = estado return end if inc(j) wend result=0 result= 0 end sub '**********************BORRAR NUMERO DE TELEFONO DESDE EEPROM******************* sub procedure DeleteEprom_Number( dim addres as byte , dim pos as byte) dim valor_inicio,valor_fin as byte valor_inicio = addres + (pos -1)*8 )*8 valor_fin = valor_inicio +7 for i=valor_inicio to valor_fin EEprom_write(i, $FF $FF) ) delay_ms(30 delay_ms( 30) ) i next end sub '*********************************INDICADOR************************************* sub procedure led() PORTA.0 PORTA. 0=1 delay_ms(200 delay_ms( 200) ) PORTA.0 PORTA. 0=0 delay_ms(200 delay_ms( 200) ) PORTA.0 PORTA. 0=1 delay_ms(200 delay_ms( 200) ) PORTA.0 PORTA. 0=0 delay_ms(200 delay_ms( 200) ) PORTA.0 PORTA. 0=1 end sub '*****************************DESPLAZAR MENU************************************ sub procedure DesplazarMenu() (porta.1=1)then if(porta.1 (porta.1=1) wend while(porta.1 if(opcion= 7) then opcion=1 opcion= 1 else inc(opcion) end if end if (porta.2=1)then if(porta.2 (porta.2=1) wend while(porta.2 if(opcion= 1)then opcion=7 opcion= 7 else dec(opcion) end if end if
7 /25
mikroBasic compiler for PIC by mikroElektronika
Codigo Consola GSM
355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391:
Autores: Ricardo Cajo - Jorge Galarza
end sub '****************************DESPLAZAR NUMEROS********************************** sub procedure DesplazarNumeros() (porta.1=1)then if(porta.1 (porta.1=1) wend while(porta.1 veces=0 veces= 0 10) ) then if(opcion= 10 opcion=1 opcion= 1 else inc(opcion) end if end if (porta.2=1)then if(porta.2 (porta.2=1) wend while(porta.2 veces=0 veces= 0 if(opcion= 1)then opcion=10 opcion= 10 else dec(opcion) end if end if end sub '********************************VALIDAR CLAVE********************************** sub function validar_clave( dim byref clave,clv as byte[6],dim digitos as byte) as boolean dim estado as boolean digitos= 5 then if digitos=5 estado=1 estado= 1 i=0 to 4 for i=0 estado = ((clave[i]) = (clv[i]))and estado next i result=estado else result=0 result= 0 end if end sub '****************ENVIA TRAMA DE ACTIVACION O DESACTIVACION DE ALARMAS*********** sub procedure Enviar_Trama_Alarma( dim code as byte,dim estado as byte,dim respuesta as byte) checksun_calculado =code + estado + respuesta USART_Write (INICIO) USART_Write (ID) USART_Write (code) USART_Write (estado) USART_Write (respuesta) USART_Write (checksun_calculado) Usart_Write (FIN) end sub
392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: '***************************VERIFICAR ALERTA DE ALARMA************************** 403: sub procedure Verificar_Alerta_Alarma() 404: if(alert_alarma =1)then 405: Enviar_Trama_Alarma (cod_alarma,1 (cod_alarma,1,1) 'Envio respuesta de Acknoledge 406: led() 407: num_telf =0 408: SetStr (txt_numero_p ," ", ",9) 409: i=1 to 10 for i=1 410: GetFono(txt_numero_p,i) 411: ]<>" ") ")then if(txt_numero_p[ 1]<>" 412: strcpy(Telefonos.Telf[num_telf],txt_numero_p )
8 /25
mikroBasic compiler for PIC by mikroElektronika
Codigo Consola GSM
413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465: 466: 467: 468: 469: 470: 471: 472:
Autores: Ricardo Cajo - Jorge Galarza
inc(num_telf) end if SetStr (txt_numero_p ," ", ",9) next i if(num_telf> 0) then enviar_sms (Telefonos ,num_telf ,"Alerta Alarma" ) end if alert_alarma= 0 end if end sub '****************ENVIA TRAMA DE ACTIVACION O DESACTIVACION DE ALARMAS*********** sub procedure Activar_Desactivar_Alarmas( dim estado as byte) dim indicador ,cont as byte indicador= 0 acknoledge= 0 acknoledge=0 cont=0 cont= 0 led() num_telf =0 i=1 to 10 for i=1 GetCode(Codigos_Alarmas ,i) ]<>$FF) )then if(Codigos_Alarmas [0]<>$FF Codigos[num_telf]=(Codigos_Alarmas[ 0]*10 ]*10) ) + Codigos_Alarmas[ 1] inc(num_telf) end if next i if(num_telf> 0) then while(num_telf> 0) if(indicador= 0)then dec(num_telf) Enviar_Trama_Alarma(Codigos[num_telf],estado,acknoledge) end if Delay_ms(1000 Delay_ms( 1000) ) if(acknoledge =1)then indicador= 0 acknoledge =0 cont=0 cont= 0 else inc(cont) (cont< 5)then if(cont<5 num_telf =num_telf +1 cont=0 else cont=0 end if indicador= 0 indicador=0 end if wend end if end sub '***************************PROGRAMA main: main :
''RCON.7=0 INTCON = %11000000 ADCON0=0 ADCON0= 0 ADCON1 =$0F
PRINCIPAL**********************************
'habilito int globales y periferifericos int 'digital input
INTCON2=$00 INTCON2= $00 PIE1=%00100000 PIE1= %00100000 PIR1=0 PIR1= 0
9 /25 /25
mikroBasic compiler for PIC by mikroElektronika
Codigo Consola GSM
Autores: Ricardo Cajo - Jorge Galarza
473: IPR1=%00100000 IPR1= %00100000 474: CMCON= $07 475: '' T1CON =$31 'preescalador 'preescalado r (8) 476: ''TMR1H = $3C ' 10ms 477: ''TMR1L = $AF '(Prescaler=8)*(2^16 '(Prescaler= 8)*(2^16 -$0BDB)*(4/Fo -$0BDB)*(4/Fosc) sc) = 500ms 478: 479: TRISA = $0E ' PORTB is output 480: TRISC = $80 ' PORTB is output 481: TRISB = $F0 482: TRISD = $00 483: TRISE= $00 484: PORTC=0 PORTC= 0 485: PORTE=0 PORTE= 0 486: PORTD=0 PORTD= 0 487: alert_alarma =0 488: cod_alarma= 0 cod_alarma=0 489: viajero=0 viajero= 0 490: acknoledge= 0 acknoledge=0 491: veces=0 veces= 0 492: i=0 i= 0 493: ind_clave= 0 494: Usart_Init( 9600) Usart_Init(9600 ) 495: 496: Usart_Write_Text( "Inicio Alarma GSM" ) 497: Lcd_Config(PORTD, 3,2,1,0,PORTE,0 ,PORTE,0,1,2) 498: Lcd_Cmd(Lcd_CURSOR_OFF) 499: led() 500: '************************ '*********** ***************** **** CLAVE DEFAULT ************* ************************** ********************** ********* 501: claveInicial[ 0]= 1 502: claveInicial[ 1]= 2 503: claveInicial[ 2]= 3 504: claveInicial[ 3]= 4 505: claveInicial[ 4]= 5 506: 507: (portA.3 = 1) then if (portA.3 508: i=0 to 4 for i=0 509: EEprom_write(i,claveInicial[i]) 510: delay_ms(30 delay_ms( 30) ) 511: next i 512: '' SavePassword (claveInicial (claveInicial) ) 513: led() 514: end if 515: '***************************** INICIO ****************************************** 516: INICIO INICIO: : 517: LCD_Cmd(LCD_CLEAR) 518: txt_clave ="" 519: Delay_ms(500 Delay_ms( 500) ) 520: Lcd_Out(1 Lcd_Out( 1, 1," ALARMA GSM ") 521: Lcd_Out(2 Lcd_Out( 2, 1,"PASSWORD:" ) 522: ind_clave= 0 ind_clave=0 523: SetStr (txt_clave ," ", ",7) 524: while true 525: leer_teclado() 526: if kp = esc then 527: ind_clave= 0 ind_clave=0 528: goto INICIO '** 529: end if 530: 'Tecla ENTER if kp =enter then 531: led() 532: Lcd_Cmd(LCD_CLEAR)
10/25 10/25
mikroBasic compiler for PIC by mikroElektronika
Codigo Consola GSM
Autores: Ricardo Cajo - Jorge Galarza
533: i=0 to 4 for i=0 534: claveInicial[i]=EEprom_read(i) 535: Delay_ms( 30) Delay_ms(30 ) 536: next i 537: 538: if validar_clave(claveInicial,ClaveIngresada,ind_clave)= 1 then 539: led() 540: opcion= 1 opcion=1 541: ind_clave= 0 ind_clave=0 542: goto MENU 543: else 544: Lcd_Out( 2, 7,"ERROR" Lcd_Out(2 "ERROR") ) 545: delay_ms( 500) delay_ms(500 ) 546: Lcd_Cmd(LCD_CLEAR) 547: ind_clave= 0 ind_clave=0 548: goto INICIO 549: end if 550: end if 551: $43) ) and (ind_clave> 0) then 'Tecla DELETE if (kp = $43 552: dec(ind_clave) 553: txt_clave [ind_clave]= " " 554: Lcd_Out( 2,10 Lcd_Out(2 10,txt_clave ,txt_clave ) 555: end if 556: 557: 558: >=$30) ) and (kp <= $39 $39) )and (ind_clave< 5) then if (kp >=$30 559: txt_clave [ind_clave]= "*" 560: ClaveIngresada[ind_clave]=kp- 48 561: Delay_ms( 20) Delay_ms(20 ) 562: Lcd_Out( 2,10 Lcd_Out(2 10,txt_clave ,txt_clave ) 563: inc(ind_clave) 'contador de digitos ingresados 564: Delay_ms( 20) Delay_ms(20 ) 565: end if 566: 'Verificar_Alerta_Alarma() 567: wend 568: '********************************** MENU *************************************** 569: MENU MENU: : 570: LCD_Cmd(LCD_CLEAR) 571: Lcd_Out(1 Lcd_Out( 1, 1," BIENVENIDOS ") 572: Delay_ms(500 Delay_ms( 500) ) 573: opcion=1 opcion= 1 574: ind_clave= 0 ind_clave=0 575: while True 576: leer_teclado() 577: DesplazarMenu() 578: if kp = esc then 579: ind_clave= 0 580: goto INICIO '** 581: end if 582: Lcd_Out(1 Lcd_Out( 1, 1," ALARMA GSM ") 583: select case opcion 584: case 1 585: Lcd_Out(2 Lcd_Out( 2, 1,"1 TURN ON ALARM " ) 586: if(kp=enter) then goto TurnOnAlarm end if 587: case 2 588: Lcd_Out(2 Lcd_Out( 2, 1,"2 TURN OFF ALARM" ) 589: if(kp=enter) then goto TurnOffAlarm end if 590: case 3 591: Lcd_Out(2 Lcd_Out( 2, 1,"3 ADD ALARM ") 592: if(kp=enter) then goto AddAlarm end if
11 /25
mikroBasic compiler for PIC by mikroElektronika
Codigo Consola GSM
Autores: Ricardo Cajo - Jorge Galarza
593: case 4 594: Lcd_Out(2 Lcd_Out( 2, 1,"4 DELETE ALARM " ) 595: if(kp=enter) then goto DeleteAlarm end if 596: case 5 597: Lcd_Out(2 Lcd_Out( 2, 1,"5 ENTER NUMBER " ) 598: if(kp=enter) then goto EnterNumber end if 599: case 6 600: Lcd_Out(2 Lcd_Out( 2, 1,"6 DELETE NUMBER " ) 601: if(kp=enter) then goto DeleteNumber end if 602: case 7 603: Lcd_Out(2 Lcd_Out( 2, 1,"7 CHANGE PASSWOR" ) 604: ' if(kp=enter) then goto ChangePasswor ChangePassword d end if 605: end select 606: Verificar_Alerta_Alarma() 607: wend 608: '************************** OPCION 1 TURN ON ALARM****************************** 609: TurnOnAlarm: TurnOnAlarm : 610: LCD_Cmd(LCD_CLEAR) 611: Lcd_Out(1 Lcd_Out( 1, 1,"1 TURN ON ALARM " ) 612: Lcd_Out(2 Lcd_Out( 2, 1," PRESS ENTER " ) 613: while True 614: leer_teclado() 615: 616: if kp = esc then 617: ind_clave= 0 618: goto MENU '** 619: end if 620: 621: if(kp=enter) then 622: ShowMessage( 2,1,0,"WAIT PLEASE....." ) 623: Activar_Desactivar_Alarmas( 1) 624: ShowMessage( 2,1,1000 1000, ,"WAIT PLEASE....." ) 625: goto MENU 626: end if 627: 628: wend 629: '''******** '''******************* ****************** ******* OPCION 2 TURN OFF ALARM***************************** 630: TurnOffAlarm : 631: LCD_Cmd(LCD_CLEAR) 632: Lcd_Out(1 Lcd_Out( 1, 1,"1 TURN OFF ALARM" ) 633: Lcd_Out(2 Lcd_Out( 2, 1," PRESS ENTER " ) 634: while True 635: leer_teclado() 636: 637: if kp = esc then 638: ind_clave= 0 639: goto MENU '** 640: end if 641: if(kp=enter) then 642: ShowMessage( 2,1,0,"WAIT PLEASE....." ) 643: Activar_Desactivar_Alarmas( 0) 644: ShowMessage( 2,1,1000 1000, ,"WAIT PLEASE....." ) 645: goto MENU 646: end if 647: 648: wend''************************** OPCION 3 ADD ALARM********************************** 649: AddAlarm: AddAlarm : 650: ind_clave= 0
12 /25
mikroBasic compiler for PIC by mikroElektronika
Codigo Consola GSM
651: 652: 653: 654: 655: 656: 657: 658: 659: 660: 661: 662: 663: 664: 665: 666: 667: 668: 669: 670: 671: 672: 673: 674: 675:
bandera=0 bandera= 0 LCD_Cmd(LCD_CLEAR) Lcd_Out(1 Lcd_Out( 1,1," ENTER CODE: SetStr(txt_numero, " ", ",16 16) ) while True leer_teclado()
Autores: Ricardo Cajo - Jorge Galarza
")
>=$30) ) and (kp <= $39 $39) ) and (ind_clave<2 (ind_clave<2)then if (kp >=$30 bte_numero_add[ind_clave] = kp - 48 txt_numero[ind_clave]= kp Lcd_Out(2 Lcd_Out( 2,1,txt_numero) inc(ind_clave) 'contador de digitos ingresados end if (bandera=0) then if (kp = esc) and (bandera=0 goto MENU '** end if $43) ) and (ind_clave>0 (ind_clave>0) and (bandera= 0) then 'Tecla DELETE if (kp = $43 dec(ind_clave) txt_numero[ind_clave]= " " Lcd_Out(2 Lcd_Out( 2,1,txt_numero) end if
(bandera=1) then 'Estan en YES/CANCEL presiono cancel if (kp = esc) and (bandera=1 vuelve a mostrar el numero 676: ShowMessage( 1,1,0," ENTER CODE: " ) 677: Lcd_Out(2 Lcd_Out( 2,1,txt_numero) 678: bandera=0 bandera= 0 679: end if 680: 681: (ind_clave<2) then if (kp=enter) and (ind_clave<2 682: ShowMessage( 1,1,500 500, ,"CODE INCOMPLETO") INCOMPLETO" ) 683: ShowMessage( 1,1,0," ENTER CODE: " ) 684: Lcd_Out(2 Lcd_Out( 2,1,txt_numero) 685: end if 686: 687: (ind_clave=2) then if (kp=enter) and (ind_clave=2 688: LCD_Cmd(LCD_CLEAR) 689: Lcd_Out(1 Lcd_Out( 1,1,"Add #:") #:" ) 690: Lcd_Out(1 Lcd_Out( 1,7,txt_numero) 691: Lcd_Out(1 Lcd_Out( 1,9,"?" "?") ) 692: Lcd_Out(2 Lcd_Out( 2,1," YES/CANCEL" ) YES/CANCEL") 693: bandera=1 bandera= 1 694: end if 695: 696: (kp= $44) ) and (bandera=1 (bandera=1) then'Tecla YES if (kp=$44 697: ShowMessage( 2,1,0," SAVING..." ) SAVING...") 698: )=1 then if SaveEprom_Code (bte_numero_add )=1 699: ShowMessage( 2,1,500 500, ," SAVED OK" OK") ) 700: else 701: Lcd_Out(1 Lcd_Out( 1,1," SAVE ERROR " ) Lcd_Out( 2,1," MEMORY FULL " ) 702: Delay_ms(500 Delay_ms( 500) ) LCD_Cmd(LCD_CLEAR) Delay_ms( 200 200) ) 703: Lcd_Out(1 Lcd_Out( 1,1," SAVE ERROR " ) Lcd_Out( 2,1," MEMORY FULL " ) 704: Delay_ms(500 Delay_ms( 500) ) LCD_Cmd(LCD_CLEAR) Delay_ms( 200 200) ) 705: Lcd_Out(1 Lcd_Out( 1,1," SAVE ERROR " ) Lcd_Out( 2,1," MEMORY FULL " ) 706: Delay_ms(500 Delay_ms( 500) ) 707: end if 708: goto MENU 709: end if
13 /25
mikroBasic compiler for PIC by mikroElektronika
Codigo Consola GSM
Autores: Ricardo Cajo - Jorge Galarza
710: wend 711: ''************************** OPCION 4 DELETE ALARM******************************* 712: DeleteAlarm : 713: LCD_Cmd(LCD_CLEAR) 714: Lcd_Out(1 Lcd_Out( 1, 1,"4 DELETE ALARM " ) 715: opcion=1 opcion= 1 716: bandera=0 bandera= 0 717: veces=0 veces= 0 718: while true 719: leer_teclado() 720: DesplazarNumeros() 721: (bandera=0) then if (kp = esc) and (bandera=0 722: ind_clave= 0 723: goto MENU '** 724: end if 725: (bandera=0) and (veces=0 (veces= 0) then if (bandera=0 726: SetStr(txt_codigo, " ", ",4) 727: SetStr(str_cadena ," ", ",4) 728: ByteToStr(opcion,str_numero_convertido) 729: str_cadena[0] = "1" else str_cadena[0 str_cadena[0] = "0" end if if opcion = 10 then str_cadena[0 730: str_cadena[ 1] = str_numero_convertido[ 2] 731: str_cadena[ 2] = "." 732: str_cadena[ 3] = "-" 733: Lcd_Out(2 Lcd_Out( 2, 1,str_cadena) 734: GetCode(Codigos_Alarmas,opcion) 735: ]<>$FF) )then if(Codigos_Alarmas [0]<>$FF 736: Codigos[0 Codigos[ 0] =(Codigos_Alarmas[ 0]*10 ]*10)+Codigos_Alarmas[ )+Codigos_Alarmas[ 1] 737: ByteToStr(Codigos[ 0] ,txt_codigo) 738: end if 739: end if 740: 741: ]=" ") ") and (veces=0 (veces=0) then if (txt_codigo[ 2]=" 742: Lcd_Out(2 Lcd_Out( 2, 5," ") ") 743: Lcd_Out(2 Lcd_Out( 2, 6,"(VACIO) ") ") 744: veces=1 veces= 1 745: end if 746: 747: ]<>" ") ") and (bandera= 0)and (veces=0 (veces= 0) then if (txt_codigo [2]<>" 748: Lcd_Out(2 Lcd_Out( 2, 5," ") ") 749: Lcd_Out(2 Lcd_Out( 2, 6,"Code:" ) 750: Lcd_Out(2 Lcd_Out( 2, 11 11,txt_codigo ,txt_codigo ) 751: Lcd_Out(2 Lcd_Out( 2, 14 14, ," " ) 752: veces=1 veces= 1 753: end if 754: 755: $43) ) and (txt_codigo [2]<>" ]<>" ") ") then 'Tecla DELETE if (kp = $43 756: LCD_Cmd(LCD_CLEAR) 757: Lcd_Out(1 Lcd_Out( 1,1,"DEL ALARM #:" ) 758: Lcd_Out(1 Lcd_Out( 1,13 13,txt_codigo ,txt_codigo ) 759: Lcd_Out(1 Lcd_Out( 1,16 16, ,"?" "?") ) 760: Lcd_Out(2 Lcd_Out( 2,1," YES/CANCEL" ) YES/CANCEL") 761: bandera=1 bandera= 1 762: end if 763: 764: (kp= $44) ) and (bandera=1 (bandera=1) then'Tecla YES if (kp=$44 765: DeleteEprom_Number( 64 64,opcion) ,opcion) 766: LCD_Cmd(LCD_CLEAR) 767: Lcd_Out(2 Lcd_Out( 2,1," DELETING..." ) DELETING...") 768: LCD_Cmd(LCD_CLEAR)
14 /25
mikroBasic compiler for PIC by mikroElektronika
Codigo Consola GSM
769: 770: 771: 772: 773: 774: 775: 776: 777: 778: 779: 780: 781: 782: 783: 784: 785: 786: 787: 788: 789: 790: 791: 792: 793: 794: 795: 796: 797: 798: 799: 800: 801: 802: 803: 804: 805: 806: 807:
Lcd_Out(2 Lcd_Out( 2,1," Delay_ms(500 Delay_ms( 500) ) goto MENU end if
Autores: Ricardo Cajo - Jorge Galarza
DELETE OK..." )
(bandera=1) then if (kp = esc) and (bandera=1 '** goto DeleteAlarm end if wend'************************** OPCION 5 ENTER NUMBER ****************************** EnterNumber: EnterNumber : ind_clave= 0 ind_clave=0 bandera=0 bandera= 0 txt_numero= "" LCD_Cmd(LCD_CLEAR) Lcd_Out(1 Lcd_Out( 1,1," ENTER NUMBER: " ) SetStr(txt_numero, " ", ",16 16) ) while True leer_teclado()
'Lee del Teclado y se guarda en kp
>=$30) ) and (kp <= $39 $39) ) and (ind_clave< 9)then if (kp >=$30 bte_numero_add[ind_clave] = kp - 48 txt_numero[ind_clave]= kp Lcd_Out(2 Lcd_Out( 2,1,txt_numero) inc(ind_clave) 'contador de digitos ingresados end if (bandera=0) then if (kp = esc) and (bandera=0 goto MENU '** end if $43) ) and (ind_clave>0 (ind_clave>0) and (bandera= 0) then 'Tecla DELETE if (kp = $43 dec(ind_clave) txt_numero[ind_clave]= " " Lcd_Out(2 Lcd_Out( 2,1,txt_numero) end if
(bandera=1) then 'Estan en YES/CANCEL presiono cancel if (kp = esc) and (bandera=1 vuelve a mostrar el numero 808: ShowMessage( 1,1,0," ENTER NUMBER: " ) 809: Lcd_Out(2 Lcd_Out( 2,1,txt_numero) 810: bandera=0 bandera= 0 811: end if 812: 813: (ind_clave<9) then if (kp=enter) and (ind_clave<9 814: ShowMessage( 1,1,500 500, ,"NUMERO INCOMPLETO" ) 815: ShowMessage( 1,1,0," ENTER NUMBER: " ) 816: Lcd_Out(2 Lcd_Out( 2,1,txt_numero) 817: end if 818: 819: (ind_clave=9) then if (kp=enter) and (ind_clave=9 820: LCD_Cmd(LCD_CLEAR) 821: Lcd_Out(1 Lcd_Out( 1,1,"Add #:") #:" ) 822: Lcd_Out(1 Lcd_Out( 1,7,txt_numero) 823: Lcd_Out(1 Lcd_Out( 1,16 16, ,"?" "?") ) 824: Lcd_Out(2 Lcd_Out( 2,1," YES/CANCEL" ) YES/CANCEL") 825: bandera=1 bandera= 1 826: end if
15 /25
mikroBasic compiler for PIC by mikroElektronika
Codigo Consola GSM
Autores: Ricardo Cajo - Jorge Galarza
827: 828: (kp= $44) ) and (bandera=1 (bandera=1) then'Tecla YES if (kp=$44 829: ShowMessage( 2,1,0," SAVING..." ) SAVING...") 830: if SaveEprom_Number(bte_numero_add)= 1 then 831: ShowMessage( 2,1,500 500, ," SAVED OK" OK") ) 832: else 833: Lcd_Out(1 Lcd_Out( 1,1," SAVE ERROR " ) Lcd_Out( 2,1," MEMORY FULL " ) 834: Delay_ms(500 Delay_ms( 500) ) LCD_Cmd(LCD_CLEAR) Delay_ms( 200 200) ) 835: Lcd_Out(1 Lcd_Out( 1,1," SAVE ERROR " ) Lcd_Out( 2,1," MEMORY FULL " ) 836: Delay_ms(500 Delay_ms( 500) ) LCD_Cmd(LCD_CLEAR) Delay_ms( 200 200) ) 837: Lcd_Out(1 Lcd_Out( 1,1," SAVE ERROR " ) Lcd_Out( 2,1," MEMORY FULL " ) 838: Delay_ms(500 Delay_ms( 500) ) 839: end if 840: goto MENU 841: end if 842: wend 843: '************************** OPCION 6 DELETE NUMBER ***************************** 844: DeleteNumber : 845: LCD_Cmd(LCD_CLEAR) 846: Lcd_Out(1 Lcd_Out( 1, 1," DELETE NUMBER " ) 847: SetStr(txt_numero_p, " ", ",16 16) ) 848: opcion=1 opcion= 1 849: bandera=0 bandera= 0 850: veces=0 veces= 0 851: while true 852: leer_teclado() 853: DesplazarNumeros() 854: 855: (bandera=0) then if (kp = esc) and (bandera=0 856: ind_clave= 0 857: goto MENU '** 858: end if 859: 860: (bandera=0) and (veces=0 (veces= 0) then if (bandera=0 861: SetStr(txt_numero_p, " ", ",16 16) ) 862: SetStr(str_cadena ," ", ",4) 863: ByteToStr(opcion,str_numero_convertido) 864: str_cadena[0] = "1" else str_cadena[0 str_cadena[0] = "0" end if if opcion = 10 then str_cadena[0 865: str_cadena[ 1] = str_numero_convertido[ 2] 866: str_cadena[ 2] = "." 867: str_cadena[ 3] = "-" 868: Lcd_Out(2 Lcd_Out( 2, 1,str_cadena) 869: GetFono(txt_numero_p,opcion) 870: 871: end if 872: 873: ]=" ") ") AND (veces=0 (veces=0) then if (txt_numero_p[ 1]=" 874: Lcd_Out(2 Lcd_Out( 2, 5," ") ") 875: Lcd_Out(2 Lcd_Out( 2, 6,"(VACIO) ") ") 876: Delay_ms(1 Delay_ms( 1) 877: veces=1 veces= 1 878: end if 879: 880: ]<>" ") ") and (bandera=0 (bandera=0) AND (veces=0 (veces=0) then if (txt_numero_p[ 1]<>" 881: Lcd_Out(2 Lcd_Out( 2, 5," ") ") 882: Lcd_Out(2 Lcd_Out( 2, 6,txt_numero_p) 883: Lcd_Out(2 Lcd_Out( 2, 15 15, ," ") 884: Delay_ms(1 Delay_ms( 1) 885: veces=1 veces= 1 886: end if
16 /25
mikroBasic compiler for PIC by mikroElektronika
Codigo Consola GSM
Autores: Ricardo Cajo - Jorge Galarza
887: 888: $43) ) and (txt_numero_p[ 1]<>" ]<>" ") ") then 'Tecla DELETE if (kp = $43 889: LCD_Cmd(LCD_CLEAR) 890: Lcd_Out(1 Lcd_Out( 1,1,"DEL #:") #:" ) 891: Lcd_Out(1 Lcd_Out( 1,7,txt_numero_p) 892: Lcd_Out(1 Lcd_Out( 1,16 16, ,"?" "?") ) 893: Lcd_Out(2 Lcd_Out( 2,1," YES/CANCEL" ) YES/CANCEL") 894: bandera=1 bandera= 1 895: end if 896: 897: (kp= $44) ) and (bandera=1 (bandera=1) then'Tecla YES if (kp=$44 898: DeleteEprom_Number( 176 176,opcion) ,opcion) 899: LCD_Cmd(LCD_CLEAR) 900: Lcd_Out(2 Lcd_Out( 2,1," DELETING..." ) DELETING...") 901: LCD_Cmd(LCD_CLEAR) 902: Lcd_Out(2 Lcd_Out( 2,1," DELETE OK..." ) 903: Delay_ms(500 Delay_ms( 500) ) 904: goto MENU 905: end if 906: 907: (bandera=1) then if (kp = esc) and (bandera=1 908: '** goto DeleteNumber 909: end if 910: 911: wend 912: ************************** OPCION 7 CHANGE PASSWORD *************************** 913: ChangePassword : 914: ind_clave= 0 bandera_oldPass =1 bandera_newPass =0 bandera_repPass =0 ind_clave=0 915: LCD_Cmd(LCD_CLEAR) 916: Lcd_Out(1 Lcd_Out( 1,1,"OLD PASS:") PASS:" ) 917: SetStr(txt_numero, " ", ",16 16) ) 918: Lcd_Out(2 Lcd_Out( 2,1,txt_numero) 919: while True 920: leer_teclado() 'Lee del Teclado y se guarda en kp 921: >=$30) ) and (kp <= $39 $39) ) and (ind_clave< 5) then if (kp >=$30 922: txt_numero[ind_clave]= "*" 923: if bandera_oldPass =1 then 924: bte_old_pass[ind_clave] = kp - 48 925: Lcd_Out(2 Lcd_Out( 2,1,txt_numero) end if 926: if bandera_newPass =1 then 927: bte_new_pass[ind_clave] = kp - 48 928: Lcd_Out(1 Lcd_Out( 1,11 11,txt_numero) ,txt_numero) end if 929: if bandera_repPass =1 then 930: bte_rep_pass[ind_clave] = kp - 48 931: Lcd_Out(2 Lcd_Out( 2,11 11,txt_numero) ,txt_numero) end if 932: inc(ind_clave) 'contador de digitos ingresados 933: end if 934: 935: if kp = esc then goto MENU end if 'Presiona ESC 936: 937: $43) ) and (ind_clave>0 (ind_clave>0) then 'Tecla DELETE if (kp = $43 938: dec(ind_clave) 939: txt_numero[ind_clave]= " " 940: Lcd_Out( 2,1,txt_numero) if bandera_oldPass =1 then Lcd_Out(2 end if 941: Lcd_Out( 1,11 11,txt_numero) ,txt_numero) end if if bandera_newPass =1 then Lcd_Out(1 942: bandera_repPass = 1 Lcd_Out(2 Lcd_Out( 2 , 11,txt_numero) 11 ,txt_numero) end if if then 943: end if 944: 945: if (kp=enter) and (bandera_oldPass =1) then 946: Lcd_Cmd(LCD_CLEAR)
17/25
mikroBasic compiler for PIC by mikroElektronika
Codigo Consola GSM
947: 948: 949: 950: 951: 952: 953: 954: 955: 956: 957: 958: 959: 960: 961: 962: 963: 964: 965: 966: 967: 968: 969: 970: 971: 972: 973: 974: 975: 976: 977: 978: 979: 980:
Autores: Ricardo Cajo - Jorge Galarza
i=0 to 4 for i=0 claveInicial[i]=EEprom_read(i) next i
if validar_clave(claveInicial,bte_old_pass,ind_clave)= 1 then led() Lcd_Out(1, 1,"NEW PASS:") Lcd_Out(1 PASS:") ind_clave=0 bandera_oldPass =0 bandera_newPass =1 ind_clave=0 bandera_repPass =0 SetStr(txt_numero, " ", SetStr(txt_numero," ",16 16) ) else ShowMessage(2,7,500 ShowMessage(2 500, ,"ERROR" "ERROR") ) ind_clave=0 ind_clave=0 goto ChangePassword end if end if (ind_clave=5) then if (kp=enter) and (bandera_newPass =1) and (ind_clave=5 Lcd_Out(2 Lcd_Out(2, 1,"REP PASS:") PASS:") ind_clave=0 bandera_oldPass =0 bandera_newPass =0 bandera_repPass =1 ind_clave=0 SetStr(txt_numero, " ", SetStr(txt_numero," ",16 16) ) end if (ind_clave=5) then if (kp=enter) and (bandera_repPass =1) and (ind_clave=5 ,ind_clave)= 1 then if validar_clave(bte_new_pass,bte_rep_pass ,ind_clave)=1 SavePassword(bte_new_pass) ShowMessage( 2,3,900 ShowMessage(2 900, ,"PASS CHANGED") CHANGED") ind_clave=0 ind_clave=0 goto INICIO else ShowMessage( 2,3,900 ShowMessage(2 900, ,"PASS NO MATCH") MATCH" ) ind_clave=0 ind_clave=0 goto MENU end if end if wend END.
18/25
mikroBasic compiler for PIC by mikroElektronika
Codigo Alarma Puerta
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60:
Autores: Ricardo Cajo - Jorge Galarza
program Codigo_Alarma_Puerta dim n_envios,received_byte,viajero,cod_alarma as byte dim Estado_Alarma,checksun_enviar, acknoledge as byte dim checksun_recibido, checksun_calculado ,cont as byte dim dato as byte[5] const INICIO = $24 const ID = $49 const FIN = $46 CODIGO=$07 const CODIGO=$07 sub procedure interrupt()
' Usart_Data_Ready es lo mismo que haber puesto PIR1.RCIF if Usart_Data_Ready = 1 then received_byte = Usart_Read select case viajero case 0 if (received_byte=INICIO) then viajero= 1 viajero=1 else viajero= 0 viajero=0 end if case 1 if (received_byte=ID) then viajero= 2 viajero=2 else viajero= 0 viajero=0 end if
'I'
'$'
ID
case 2 dato[ 0]=(received_byte) 'Codigo Alarma dato[0 viajero= 3 viajero=3 case 3 dato[ 1]= received_byte 'Estado Alarma dato[1 viajero= 4 viajero=4 case 4 dato[ 2]= received_byte 'Acknoledge dato[2 viajero= 5 viajero=5 case 5 dato[ 3]= received_byte 'checksun_recibido dato[3 checksun_recibido =dato[3 =dato[ 3] viajero= 6 viajero=6 case 6 dato[ 4]=received_byte dato[4 (dato[ 4]= FIN) then 'F' STOP if(dato[4 checksun_calculado = dato[0 dato[0]+dato[1 ]+dato[ 1]+dato[2 ]+dato[2] if(checksun_calculado = checksun_recibido )then cod_alarma =dato[0 =dato[ 0] Estado_Alarma=dato[ 1] acknoledge =dato[2 =dato[ 2] viajero= 0 viajero=0 else viajero= 0 viajero=0 end if else viajero= 0 viajero=0 end if
19/25
mikroBasic compiler for PIC by mikroElektronka
Codigo Alarma Puerta
Autores: Ricardo Cajo - Jorge Galarza
61: end select 62: 63: end if 64: PIR1.RCIF= PIR1.RCIF=0 0 ' Si el dato a llegado limpio la bandera de recepcion 65: PIE1.RCIE= PIE1.RCIE=1 1 ' Habilitar nuevamente la interrupcion por USART 66: end sub 67: 68: sub procedure Led() 69: SetBit(PORTB, 3) 70: Delay_ms(200 Delay_ms( 200) ) 71: ClearBit(PORTB, 3) 72: Delay_ms(200 Delay_ms( 200) ) 73: SetBit(PORTB, 3) 74: Delay_ms(200 Delay_ms( 200) ) 75: ClearBit(PORTB, 3) 76: Delay_ms(200 Delay_ms( 200) ) 77: SetBit(PORTB, 3) 78: Delay_ms(200 Delay_ms( 200) ) 79: ClearBit(PORTB, 3) 80: end sub 81: 82: sub procedure Enable_HMTR() 83: SetBit(PORTB,4 SetBit(PORTB, 4) 'Enable HM-TR 84: end sub 85: 86: sub procedure Disable_HMTR() 87: ClearBit(PORTB, 4) 'Enable HM-TR 88: end sub 89: sub procedure Enviar_Alerta_Respuesta( dim Estado,Alerta,respuesta as byte) 90: 91: checksun_enviar =Estado +Alerta +CODIGO+ respuesta 92: USART_Write (INICIO ) 93: USART_Write (ID) 94: USART_Write (CODIGO) 95: USART_Write (Estado) 96: USART_Write (Alerta) 97: USART_Write (respuesta) 'Acknoledge 98: USART_Write (checksun_enviar ) 99: Usart_Write (FIN ) 100: 101: end sub 102: main main: : 103: 104: OPTION_REG = %10000000 105: INTCON = %11000000 ' Enable external interrupts bit(7) and bit(6) de permiso q no se controlan con INTCON 106: 107: PIE1=%00100000 PIE1= %00100000 108: PIR1=0 PIR1= 0 109: 110: CMCON=$07 CMCON= $07 'Para Configurar el PORTA COMO I/O Digitales y apagar el comparador Analogico 111: 112: TRISA = %10000000 'RA0,RA1,RA2,RA3,RA4,RA7 'RA0,RA1,RA2, RA3,RA4,RA7 Input RA6 OUTPUT 113: TRISB = %00000011 114: 115: PORTA=0 PORTA= 0 116: PORTB=0 PORTB= 0 117: n_envios =0 118: received_byte= 0
20/25
mikroBasic compiler for PIC by mikroElektronika
Codigo Alarma Puerta
Autores: Ricardo Cajo - Jorge Galarza
119: viajero=0 viajero= 0 120: cod_alarma= 0 cod_alarma=0 121: Estado_Alarma= 0 122: acknoledge= 0 acknoledge=0 123: cont=0 cont= 0 124: checksun_enviar =0 125: checksun_recibido =0 126: checksun_calculado =0 127: Usart_Init( 9600) Usart_Init(9600 ) 128: Led() 129: 'sleep 130: Enable_HMTR () 131: while(1) ' While there is no interrupt, program runs in endless loop: 132: 133: if(cod_alarma =CODIGO )then 134: if( Estado_Alarma =1)then 135: SetBit(PORTB, 3) 136: delay_ms(50 delay_ms( 50) ) 137: Enviar_Alerta_Respuesta (1,0,1) 138: 139: 140: else 141: ClearBit(PORTB, 3) 142: delay_ms( delay_ms(50 50) ) 143: Enviar_Alerta_Respuesta (0,0,1) 144: end if 145: cod_alarma= 0 146: end if 147: if(Estado_Alarma= 1)then 148: (PortB.0=1)then if(PortB.0 149: if(n_envios =0) then 150: Enviar_Alerta_Respuesta (1,1,1) 151: delay_ms( delay_ms(1000 1000) ) 152: if(acknoledge =1)then 153: n_envios= n_envios=1 1 154: acknoledge =0 155: cont=0 cont= 0 156: else 157: inc(cont) 158: (cont< 5)then if(cont<5 159: n_envios= 0 160: else 161: cont=0 cont= 0 162: n_envios= 1 163: end if 164: end if 165: end if 166: else 167: n_envios =0 168: end if 169: end if 170: 171: wend 172: 173: end.
21/25
mikroBasic compiler for PIC by mikroElektronika
Codigo Alarma Movimiento
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60:
Autores: Ricardo Cajo - Jorge Galarza
program Codigo_Alarma_Movimiento dim n_envios,received_byte,viajero,cod_alarma,n_presencia dim Alert_Alarma,Estado_Alarma,checksun_enviar, acknoledge dim checksun_recibido, checksun_calculado ,cont as byte dim dato as byte[5] const INICIO = $24 const ID = $49 const FIN = $46 CODIGO=$0A const CODIGO=$0A
as byte as byte
sub procedure interrupt()
' Usart_Data_Ready es lo mismo que haber puesto PIR1.RCIF if Usart_Data_Ready = 1 then received_byte = Usart_Read select case viajero case 0 if (received_byte=INICIO) then viajero= 1 viajero=1 else viajero= 0 viajero=0 end if case 1 if (received_byte=ID) then viajero= 2 viajero=2 else viajero= 0 viajero=0 end if
'$'
'I'
ID
case 2 dato[ 0]=(received_byte) 'Codigo Alarma dato[0 viajero= 3 viajero=3 case 3 dato[ 1]= received_byte 'Estado Alarma dato[1 viajero= 4 viajero=4 case 4 dato[ 2]= received_byte 'Acknoledge dato[2 viajero= 5 viajero=5 case 5 dato[ 3]= received_byte 'checksun_recibido dato[3 checksun_recibido =dato[3 =dato[ 3] viajero= 6 viajero=6 case 6 dato[ 4]=received_byte dato[4 (dato[ 4]= FIN) then 'F' STOP if(dato[4 checksun_calculado = dato[0 dato[0]+dato[1 ]+dato[ 1]+dato[2 ]+dato[2] if(checksun_calculado = checksun_recibido )then cod_alarma =dato[0 =dato[ 0] Estado_Alarma=dato[ 1] acknoledge =dato[2 =dato[ 2] viajero= 0 viajero=0 else viajero= 0 viajero=0 end if else viajero= 0 viajero=0 end if
22/25
mikroBasic compiler for PIC by mikroElektronika
Codigo Alarma Movimiento
61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120:
Autores: Ricardo Cajo - Jorge Galarza
end select end if if(INTCON.INTF =1)THEN Inc(n_presencia ) if(n_presencia =4)then Alert_Alarma =1 n_presencia =0 end if end if INTCON.INTF= 0 INTCON.INTE =1 PIR1.RCIF= PIR1.RCIF=0 0 ' Si el dato a llegado limpio la bandera de recepcion PIE1.RCIE= PIE1.RCIE=1 1 ' Habilitar nuevamente la interrupcion por USART end sub sub procedure Led() SetBit(PORTB, 3) Delay_ms(200 Delay_ms( 200) ) ClearBit(PORTB, 3) Delay_ms(200 Delay_ms( 200) ) SetBit(PORTB, 3) Delay_ms(200 Delay_ms( 200) ) ClearBit(PORTB, 3) Delay_ms(200 Delay_ms( 200) ) SetBit(PORTB, 3) Delay_ms(200 Delay_ms( 200) ) ClearBit(PORTB, 3) end sub sub procedure Enable_HMTR() SetBit(PORTB, 4) SetBit(PORTB,4 'Enable HM-TR end sub sub procedure Disable_HMTR() ClearBit(PORTB, 4) 'Enable HM-TR end sub sub procedure Enviar_Alerta_Respuesta( dim Estado,Alerta,respuesta
as byte)
checksun_enviar =Estado +Alerta +CODIGO+ respuesta USART_Write (INICIO ) USART_Write (ID) USART_Write (CODIGO) USART_Write (Estado) USART_Write (Alerta) USART_Write (respuesta) 'Acknoledge USART_Write (checksun_enviar ) Usart_Write (FIN ) end sub main: main : OPTION_REG = %10000000 INTCON = %11010000
' Enable external interrupts bit(7) and bit(6) de ' permiso q no se controlan con INTCON
PIE1=%00100000 PIE1= %00100000 PIR1=0 PIR1= 0
23/25
mikroBa si sic compiler f or or PIC by mikroE le lektron ik ika
Codigo Alarma Movimiento
Autores: Ricardo Cajo - Jorge Galarza
121: 122: CMCON=$07 CMCON= $07 'Para Configurar el PORTA COMO I/O Digitales y apagar el 123: 'comparador Analogico 124: 125: TRISA = %10000000 'RA0,RA1,RA2,RA3,RA4,RA7 'RA0,RA1,RA2, RA3,RA4,RA7 Input RA6 OUTPUT 126: TRISB = %00000011 127: 128: PORTA=0 PORTA= 0 129: PORTB=0 PORTB= 0 130: n_envios =0 131: received_byte= 0 132: viajero=0 viajero= 0 133: cod_alarma= 0 cod_alarma=0 134: Estado_Alarma= 0 135: Alert_Alarma= 0 136: acknoledge= 0 acknoledge=0 137: n_presencia =0 138: cont=0 cont= 0 139: checksun_enviar =0 140: checksun_recibido =0 141: checksun_calculado =0 142: Usart_Init( 9600) Usart_Init(9600 ) 143: Led() 144: 'sleep 145: Enable_HMTR () 146: while(1) ' While there is no interrupt, program runs in endless loop: 147: 148: if(cod_alarma =CODIGO )then 149: if( Estado_Alarma =1)then 150: SetBit(PORTB, 3) 151: delay_ms(50 delay_ms( 50) ) 152: Enviar_Alerta_Respuesta (1,0,1) 153: 154: 155: else 156: ClearBit(PORTB, 3) 157: delay_ms( delay_ms(50 50) ) 158: Enviar_Alerta_Respuesta (0,0,1) 159: end if 160: cod_alarma= 0 161: end if 162: if(Estado_Alarma= 1)then 163: if(Alert_Alarma =1)then 164: if(n_envios =0) then 165: Enviar_Alerta_Respuesta (1,1,1) 166: delay_ms( delay_ms(1000 1000) ) 167: if(acknoledge =1)then 168: n_envios= n_envios=1 1 169: Alert_Alarma =0 170: acknoledge =0 171: cont=0 cont= 0 172: else 173: inc(cont) 174: (cont< 5)then if(cont<5 175: n_envios= 0 176: else 177: cont=0 cont= 0 178: n_envios= 1 179: end if 180: end if
24/25
mikroBasic compiler for PIC by mikroElektronika
Codigo Alarma Movimiento
Autores: Ricardo Cajo - Jorge Galarza
181: end if 182: else 183: n_envios =0 184: end if 185: end if 186: 187: wend 188: 189: end.
25/2 25/25 5
mikro ikroBa Basi sic c comp compiiler ler for for PIC PIC by mik mikroEl roElek ekttroni ronika ka