Académique Documents
Professionnel Documents
Culture Documents
Proyecto de Graduación
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
Índice
Consola GSM…………………………………………………………………….……………………..2
Alarma Puerta…………………………………………………………………………………………19
Alarma Movimiento………………………………………………………………………………..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]
10: dim txt_numero_p as char[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]
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: dim Telf as string[10][9]
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
41: viajero=1
42: else
43: viajero=0
44: end if
45:
46: case 1
47: if (received_byte=ID) then 'I' ID
48: viajero=2
49: else
50: viajero=0
51: end if
52:
53: case 2
54: dato[0]=(received_byte) 'Codigo Alarma
55: viajero=3
56: case 3
57: dato[1]= received_byte 'Estado Alarma
58: viajero=4
59: case 4
60: dato[2]= received_byte 'Alerta Alarma
61: viajero=5
62: case 5
63: dato[3]= received_byte 'acknoledge
64: viajero=6
65: case 6
66: dato[4]= received_byte 'checksun_recibido
67: checksun_recibido =dato[4]
68: viajero=7
69: case 7
70: dato[5]=received_byte
71: if(dato[5]= FIN)then 'F' STOP
72: checksun_calculado = dato[0]+dato[1]+dato[2]+dato[3]
73: if(checksun_calculado = checksun_recibido )then
74: cod_alarma=dato[0]
75: Estado_Alarma=dato[1]
76: alert_alarma =dato[2]
77: acknoledge=dato[3]
78: viajero=0
79: else
80: viajero=0
81: end if
82: else
83: viajero=0
84: end if
85: end select
86:
87: end if
88: PIR1.RCIF=0 ' Si el dato a llegado limpio la bandera de recepcion
89: 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
byref texto as string[40])
94: dim j as byte
95: dim k as Byte
96:
97: for j = 0 to i-1
98:
99: Usart_Write_Text("AT+CMGF=1")
100: Usart_Write(10)
101: Usart_Write(13)
102: delay_ms(1000)'500
103: Usart_Write_Text("AT+CMGS=")
104: Usart_Write(34)' "
105: Usart_Write_Text(Telefonos.Telf[j])
106: Usart_Write(34)' "
107: Usart_Write(10)
108: Usart_Write(13)
109: 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)
116: Usart_Write(10)
117: Usart_Write(13)
118: delay_ms(8000)'8000
119: next j
120: texto=""
121:
122: end sub
123: '************************FUNCION LEER TECLADO**********************************
124: sub procedure leer_teclado()
125: kp = 0
126: portb.0=0
127: portb.1=1
128: portb.2=1
129: portb.3=1
130: '' portb=%11111110
131: if(portb.4=0)then
132: while(portb.4=0) wend
133: kp=$31 end if'1
134: if(portb.5=0)then
135: while(portb.5=0) wend
136: kp=$32 end if'2
137: if(portb.6=0)then
138: while(portb.6=0) wend
139: kp=$33 end if'3
140: if (portb.7=0)then
141: while(portb.7=0) wend
142: kp=$41 end if'A
143: if(kp=0)then
144: portb.0=1
145: portb.1=0
146: portb.2=1
147: portb.3=1
148: ''portb=%11111101
149: if(portb.4=0)then
150: while(portb.4=0) wend
151: kp=$34 end if'4
152: if(portb.5=0)then
153: while(portb.5=0) wend
154: kp=$35 end if'5
155: if(portb.6=0)then
156: while(portb.6=0) wend
157: kp=$36 end if'6
158: if (portb.7=0)then
159: while(portb.7=0) wend
160: kp=$42 end if'B
161: else return
162: end if
163: if(kp=0) then
164: portb.0=1
165: portb.1=1
166: portb.2=0
167: portb.3=1
168:
169: ''=%111101011
170: if(portb.4=0)then
171: while(portb.4=0) wend
172: kp=$37 end if'7
173: if(portb.5=0)then
174: while(portb.5=0) wend
175: kp=$38 end if'8
176: if(portb.6=0)then
236: next j
237:
238: for j = 1 to 8
239: strText[j]=txt_save[j][2]
240: next j
241: strText[0]="0"
242: end sub
243: '********************OBTENER CODIGO DE ALARMA DESDE EEPROM*********************
244: sub procedure GetCode(dim byref Codigo as byte[6], dim pos as byte)
245: dim numeroObtenido,j as byte
246: dim valor_inicio,valor_fin as byte
247: numeroObtenido=0
248:
249: valor_inicio = 64 + (pos -1)*8
250: valor_fin = valor_inicio +1
251: for j=valor_inicio to valor_fin
252: numeroObtenido = EEprom_read(j)
253: delay_ms(30)
254: Codigo[j-valor_inicio]=numeroObtenido
255: next j
256:
257: end sub
258: '*************************SETEAR ARREGLO****************************************
259: sub procedure SetStr(dim byref strText as char[16], dim caracter as string,dim
tamano as byte)
260: dim j as byte
261: for j=0 to tamano-1
262: strText[j] = caracter
263: next j
264: end sub
265: '**********************GUARDAR NUMERO DE TELEFONO EN EEPROM*********************
266: sub function SaveEprom_Number(dim byref number as byte[9]) as boolean
267: dim estado as boolean
268: dim cont as byte
269: dim num as byte
270: dim j as word
271: estado=1 cont =1 j=176
272: while j<=255
273: Delay_ms(30)
274: num=EEprom_read(j)
275: Delay_ms(30)
276: if num<>$FF then
277: j= j+7
278: else
279: EEprom_write(j,number[cont])
280: Inc(cont)
281: end if
282: if (cont = 9) then
283: result = estado
284: return
285: end if
286: inc(j)
287: wend
288: result=0
289: end sub
290: '**********************GUARDAR CODIGO DE ALARMA EN EEPROM *********************
291: sub function SaveEprom_Code(dim byref code as byte[9]) as boolean
292: dim estado as boolean
293: dim cont as byte
294: dim num as byte
413: inc(num_telf)
414: end if
415: SetStr (txt_numero_p ," ",9)
416: next i
417: if(num_telf>0) then
418: enviar_sms (Telefonos ,num_telf ,"Alerta Alarma" )
419: end if
420: alert_alarma=0
421: end if
422: end sub
423: '****************ENVIA TRAMA DE ACTIVACION O DESACTIVACION DE ALARMAS***********
424: sub procedure Activar_Desactivar_Alarmas(dim estado as byte)
425: dim indicador ,cont as byte
426: indicador=0
427: acknoledge=0
428: cont=0
429: led()
430: num_telf =0
431: for i=1 to 10
432: GetCode(Codigos_Alarmas ,i)
433: if(Codigos_Alarmas [0]<>$FF)then
434: Codigos[num_telf]=(Codigos_Alarmas[0]*10) + Codigos_Alarmas[1]
435: inc(num_telf)
436: end if
437: next i
438: if(num_telf>0) then
439: while(num_telf>0)
440: if(indicador=0)then
441: dec(num_telf)
442: Enviar_Trama_Alarma(Codigos[num_telf],estado,acknoledge)
443: end if
444: Delay_ms(1000)
445: if(acknoledge =1)then
446: indicador=0
447: acknoledge =0
448: cont=0
449: else
450: inc(cont)
451: if(cont<5)then
452: num_telf =num_telf +1
453: else cont=0
454: end if
455: indicador=0
456: end if
457: wend
458:
459: end if
460:
461: end sub
462: '***************************PROGRAMA PRINCIPAL**********************************
463: main:
464:
465: ''RCON.7=0
466: INTCON = %11000000 'habilito int globales y periferifericos int
467: ADCON0=0
468: ADCON1 =$0F 'digital input
469:
470: INTCON2=$00
471: PIE1=%00100000
472: PIR1=0
473: IPR1=%00100000
474: CMCON= $07
475: '' T1CON =$31 'preescalador (8)
476: ''TMR1H = $3C ' 10ms
477: ''TMR1L = $AF '(Prescaler=8)*(2^16 -$0BDB)*(4/Fosc) = 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
485: PORTE=0
486: PORTD=0
487: alert_alarma =0
488: cod_alarma=0
489: viajero=0
490: acknoledge=0
491: veces=0
492: i=0
493: ind_clave=0
494: Usart_Init(9600)
495:
496: Usart_Write_Text("Inicio Alarma GSM")
497: Lcd_Config(PORTD,3,2,1,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: if (portA.3 = 1) then
508: for i=0 to 4
509: EEprom_write(i,claveInicial[i])
510: delay_ms(30)
511: next i
512: '' SavePassword (claveInicial)
513: led()
514: end if
515: '***************************** INICIO ******************************************
516: INICIO:
517: LCD_Cmd(LCD_CLEAR)
518: txt_clave =""
519: Delay_ms(500)
520: Lcd_Out(1, 1," ALARMA GSM ")
521: Lcd_Out(2, 1,"PASSWORD:")
522: ind_clave=0
523: SetStr (txt_clave ," ",7)
524: while true
525: leer_teclado()
526: if kp = esc then
527: ind_clave=0
528: goto INICIO '**
529: end if
530: if kp =enter then 'Tecla ENTER
531: led()
532: Lcd_Cmd(LCD_CLEAR)
593: case 4
594: Lcd_Out(2, 1,"4 DELETE ALARM ")
595: if(kp=enter) then goto DeleteAlarm end if
596: case 5
597: Lcd_Out(2, 1,"5 ENTER NUMBER ")
598: if(kp=enter) then goto EnterNumber end if
599: case 6
600: Lcd_Out(2, 1,"6 DELETE NUMBER ")
601: if(kp=enter) then goto DeleteNumber end if
602: case 7
603: Lcd_Out(2, 1,"7 CHANGE PASSWOR")
604: ' if(kp=enter) then goto ChangePassword end if
605: end select
606: Verificar_Alerta_Alarma()
607: wend
608: '************************** OPCION 1 TURN ON ALARM******************************
609: TurnOnAlarm:
610: LCD_Cmd(LCD_CLEAR)
611: Lcd_Out(1, 1,"1 TURN ON ALARM ")
612: 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,"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, 1,"1 TURN OFF ALARM")
633: 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,"WAIT PLEASE.....")
645: goto MENU
646: end if
647:
648: wend''************************** OPCION 3 ADD
ALARM**********************************
649: AddAlarm:
650: ind_clave=0
651: bandera=0
652: LCD_Cmd(LCD_CLEAR)
653: Lcd_Out(1,1," ENTER CODE: ")
654: SetStr(txt_numero," ",16)
655: while True
656: leer_teclado()
657:
658: if (kp >=$30) and (kp <= $39) and (ind_clave<2)then
659: bte_numero_add[ind_clave] = kp - 48
660: txt_numero[ind_clave]= kp
661: Lcd_Out(2,1,txt_numero)
662: inc(ind_clave) 'contador de digitos ingresados
663: end if
664:
665: if (kp = esc) and (bandera=0) then
666: goto MENU '**
667: end if
668:
669: if (kp = $43) and (ind_clave>0) and (bandera=0) then 'Tecla DELETE
670: dec(ind_clave)
671: txt_numero[ind_clave]=" "
672: Lcd_Out(2,1,txt_numero)
673: end if
674:
675: if (kp = esc) and (bandera=1) then 'Estan en YES/CANCEL presiono cancel
vuelve a mostrar el numero
676: ShowMessage(1,1,0," ENTER CODE: ")
677: Lcd_Out(2,1,txt_numero)
678: bandera=0
679: end if
680:
681: if (kp=enter) and (ind_clave<2) then
682: ShowMessage(1,1,500,"CODE INCOMPLETO")
683: ShowMessage(1,1,0," ENTER CODE: ")
684: Lcd_Out(2,1,txt_numero)
685: end if
686:
687: if (kp=enter) and (ind_clave=2) then
688: LCD_Cmd(LCD_CLEAR)
689: Lcd_Out(1,1,"Add #:")
690: Lcd_Out(1,7,txt_numero)
691: Lcd_Out(1,9,"?")
692: Lcd_Out(2,1," YES/CANCEL")
693: bandera=1
694: end if
695:
696: if (kp=$44) and (bandera=1) then'Tecla YES
697: ShowMessage(2,1,0," SAVING...")
698: if SaveEprom_Code (bte_numero_add )=1 then
699: ShowMessage(2,1,500," SAVED OK")
700: else
701: Lcd_Out(1,1," SAVE ERROR ") Lcd_Out(2,1," MEMORY FULL ")
702: Delay_ms(500) LCD_Cmd(LCD_CLEAR) Delay_ms(200)
703: Lcd_Out(1,1," SAVE ERROR ") Lcd_Out(2,1," MEMORY FULL ")
704: Delay_ms(500) LCD_Cmd(LCD_CLEAR) Delay_ms(200)
705: Lcd_Out(1,1," SAVE ERROR ") Lcd_Out(2,1," MEMORY FULL ")
706: Delay_ms(500)
707: end if
708: goto MENU
709: end if
710: wend
711: ''************************** OPCION 4 DELETE
ALARM*******************************
712: DeleteAlarm:
713: LCD_Cmd(LCD_CLEAR)
714: Lcd_Out(1, 1,"4 DELETE ALARM ")
715: opcion=1
716: bandera=0
717: veces=0
718: while true
719: leer_teclado()
720: DesplazarNumeros()
721: if (kp = esc) and (bandera=0) then
722: ind_clave=0
723: goto MENU '**
724: end if
725: if (bandera=0) and (veces=0) then
726: SetStr(txt_codigo," ",4)
727: SetStr(str_cadena ," ",4)
728: ByteToStr(opcion,str_numero_convertido)
729: if opcion = 10 then str_cadena[0] = "1" else str_cadena[0] = "0" end if
730: str_cadena[1] = str_numero_convertido[2]
731: str_cadena[2] = "."
732: str_cadena[3] = "-"
733: Lcd_Out(2, 1,str_cadena)
734: GetCode(Codigos_Alarmas,opcion)
735: if(Codigos_Alarmas [0]<>$FF)then
736: Codigos[0] =(Codigos_Alarmas[0]*10)+Codigos_Alarmas[1]
737: ByteToStr(Codigos[0] ,txt_codigo)
738: end if
739: end if
740:
741: if (txt_codigo[2]=" ") and (veces=0) then
742: Lcd_Out(2, 5," ")
743: Lcd_Out(2, 6,"(VACIO) ")
744: veces=1
745: end if
746:
747: if (txt_codigo [2]<>" ") and (bandera=0)and (veces=0) then
748: Lcd_Out(2, 5," ")
749: Lcd_Out(2, 6,"Code:" )
750: Lcd_Out(2, 11,txt_codigo )
751: Lcd_Out(2, 14," " )
752: veces=1
753: end if
754:
755: if (kp = $43) and (txt_codigo [2]<>" ") then 'Tecla DELETE
756: LCD_Cmd(LCD_CLEAR)
757: Lcd_Out(1,1,"DEL ALARM #:")
758: Lcd_Out(1,13,txt_codigo )
759: Lcd_Out(1,16,"?")
760: Lcd_Out(2,1," YES/CANCEL")
761: bandera=1
762: end if
763:
764: if (kp=$44) and (bandera=1) then'Tecla YES
765: DeleteEprom_Number(64,opcion)
766: LCD_Cmd(LCD_CLEAR)
767: Lcd_Out(2,1," DELETING...")
768: LCD_Cmd(LCD_CLEAR)
827:
828: if (kp=$44) and (bandera=1) then'Tecla YES
829: ShowMessage(2,1,0," SAVING...")
830: if SaveEprom_Number(bte_numero_add)=1 then
831: ShowMessage(2,1,500," SAVED OK")
832: else
833: Lcd_Out(1,1," SAVE ERROR ") Lcd_Out(2,1," MEMORY FULL ")
834: Delay_ms(500) LCD_Cmd(LCD_CLEAR) Delay_ms(200)
835: Lcd_Out(1,1," SAVE ERROR ") Lcd_Out(2,1," MEMORY FULL ")
836: Delay_ms(500) LCD_Cmd(LCD_CLEAR) Delay_ms(200)
837: Lcd_Out(1,1," SAVE ERROR ") Lcd_Out(2,1," MEMORY FULL ")
838: 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, 1," DELETE NUMBER ")
847: SetStr(txt_numero_p," ",16)
848: opcion=1
849: bandera=0
850: veces=0
851: while true
852: leer_teclado()
853: DesplazarNumeros()
854:
855: if (kp = esc) and (bandera=0) then
856: ind_clave=0
857: goto MENU '**
858: end if
859:
860: if (bandera=0) and (veces=0) then
861: SetStr(txt_numero_p," ",16)
862: SetStr(str_cadena ," ",4)
863: ByteToStr(opcion,str_numero_convertido)
864: if opcion = 10 then str_cadena[0] = "1" else str_cadena[0] = "0" end if
865: str_cadena[1] = str_numero_convertido[2]
866: str_cadena[2] = "."
867: str_cadena[3] = "-"
868: Lcd_Out(2, 1,str_cadena)
869: GetFono(txt_numero_p,opcion)
870:
871: end if
872:
873: if (txt_numero_p[1]=" ") AND (veces=0) then
874: Lcd_Out(2, 5," ")
875: Lcd_Out(2, 6,"(VACIO) ")
876: Delay_ms(1)
877: veces=1
878: end if
879:
880: if (txt_numero_p[1]<>" ") and (bandera=0) AND (veces=0) then
881: Lcd_Out(2, 5," ")
882: Lcd_Out(2, 6,txt_numero_p)
883: Lcd_Out(2, 15," ")
884: Delay_ms(1)
885: veces=1
886: end if
887:
888: if (kp = $43) and (txt_numero_p[1]<>" ") then 'Tecla DELETE
889: LCD_Cmd(LCD_CLEAR)
890: Lcd_Out(1,1,"DEL #:")
891: Lcd_Out(1,7,txt_numero_p)
892: Lcd_Out(1,16,"?")
893: Lcd_Out(2,1," YES/CANCEL")
894: bandera=1
895: end if
896:
897: if (kp=$44) and (bandera=1) then'Tecla YES
898: DeleteEprom_Number(176,opcion)
899: LCD_Cmd(LCD_CLEAR)
900: Lcd_Out(2,1," DELETING...")
901: LCD_Cmd(LCD_CLEAR)
902: Lcd_Out(2,1," DELETE OK...")
903: Delay_ms(500)
904: goto MENU
905: end if
906:
907: if (kp = esc) and (bandera=1) then
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
915: LCD_Cmd(LCD_CLEAR)
916: Lcd_Out(1,1,"OLD PASS:")
917: SetStr(txt_numero," ",16)
918: Lcd_Out(2,1,txt_numero)
919: while True
920: leer_teclado() 'Lee del Teclado y se guarda en kp
921: if (kp >=$30) and (kp <= $39) and (ind_clave<5) then
922: txt_numero[ind_clave]= "*"
923: if bandera_oldPass =1 then
924: bte_old_pass[ind_clave] = kp - 48
925: 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,11,txt_numero) end if
929: if bandera_repPass =1 then
930: bte_rep_pass[ind_clave] = kp - 48
931: Lcd_Out(2,11,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: if (kp = $43) and (ind_clave>0) then 'Tecla DELETE
938: dec(ind_clave)
939: txt_numero[ind_clave]=" "
940: if bandera_oldPass =1 then Lcd_Out(2,1,txt_numero) end if
941: if bandera_newPass =1 then Lcd_Out(1,11,txt_numero) end if
942: if bandera_repPass =1 then Lcd_Out(2,11,txt_numero) end if
943: end if
944:
945: if (kp=enter) and (bandera_oldPass =1) then
946: Lcd_Cmd(LCD_CLEAR)
1: program Codigo_Alarma_Puerta
2: dim n_envios,received_byte,viajero,cod_alarma as byte
3: dim Estado_Alarma,checksun_enviar, acknoledge as byte
4: dim checksun_recibido, checksun_calculado ,cont as byte
5: dim dato as byte[5]
6: const INICIO = $24
7: const ID = $49
8: const FIN = $46
9: const CODIGO=$07
10:
11: sub procedure interrupt()
12:
13: ' Usart_Data_Ready es lo mismo que haber puesto PIR1.RCIF
14: if Usart_Data_Ready = 1 then
15: received_byte = Usart_Read
16: select case viajero
17:
18: case 0
19: if (received_byte=INICIO) then '$'
20: viajero=1
21: else
22: viajero=0
23: end if
24:
25: case 1
26: if (received_byte=ID) then 'I' ID
27: viajero=2
28: else
29: viajero=0
30: end if
31:
32: case 2
33: dato[0]=(received_byte) 'Codigo Alarma
34: viajero=3
35: case 3
36: dato[1]= received_byte 'Estado Alarma
37: viajero=4
38: case 4
39: dato[2]= received_byte 'Acknoledge
40: viajero=5
41:
42: case 5
43: dato[3]= received_byte 'checksun_recibido
44: checksun_recibido =dato[3]
45: viajero=6
46: case 6
47: dato[4]=received_byte
48: if(dato[4]= FIN)then 'F' STOP
49: checksun_calculado = dato[0]+dato[1]+dato[2]
50: if(checksun_calculado = checksun_recibido )then
51: cod_alarma =dato[0]
52: Estado_Alarma=dato[1]
53: acknoledge =dato[2]
54: viajero=0
55: else
56: viajero=0
57: end if
58: else
59: viajero=0
60: end if
119: viajero=0
120: cod_alarma=0
121: Estado_Alarma=0
122: acknoledge=0
123: cont=0
124: checksun_enviar =0
125: checksun_recibido =0
126: checksun_calculado =0
127: 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)
137: Enviar_Alerta_Respuesta (1,0,1)
138:
139:
140: else
141: ClearBit(PORTB,3)
142: delay_ms(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: if(PortB.0=1)then
149: if(n_envios =0) then
150: Enviar_Alerta_Respuesta (1,1,1)
151: delay_ms(1000)
152: if(acknoledge =1)then
153: n_envios=1
154: acknoledge =0
155: cont=0
156: else
157: inc(cont)
158: if(cont<5)then
159: n_envios=0
160: else
161: 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.
1: program Codigo_Alarma_Movimiento
2: dim n_envios,received_byte,viajero,cod_alarma,n_presencia as byte
3: dim Alert_Alarma,Estado_Alarma,checksun_enviar, acknoledge as byte
4: dim checksun_recibido, checksun_calculado ,cont as byte
5: dim dato as byte[5]
6: const INICIO = $24
7: const ID = $49
8: const FIN = $46
9: const CODIGO=$0A
10:
11: sub procedure interrupt()
12:
13: ' Usart_Data_Ready es lo mismo que haber puesto PIR1.RCIF
14: if Usart_Data_Ready = 1 then
15: received_byte = Usart_Read
16: select case viajero
17:
18: case 0
19: if (received_byte=INICIO) then '$'
20: viajero=1
21: else
22: viajero=0
23: end if
24:
25: case 1
26: if (received_byte=ID) then 'I' ID
27: viajero=2
28: else
29: viajero=0
30: end if
31:
32: case 2
33: dato[0]=(received_byte) 'Codigo Alarma
34: viajero=3
35: case 3
36: dato[1]= received_byte 'Estado Alarma
37: viajero=4
38: case 4
39: dato[2]= received_byte 'Acknoledge
40: viajero=5
41:
42: case 5
43: dato[3]= received_byte 'checksun_recibido
44: checksun_recibido =dato[3]
45: viajero=6
46: case 6
47: dato[4]=received_byte
48: if(dato[4]= FIN)then 'F' STOP
49: checksun_calculado = dato[0]+dato[1]+dato[2]
50: if(checksun_calculado = checksun_recibido )then
51: cod_alarma =dato[0]
52: Estado_Alarma=dato[1]
53: acknoledge =dato[2]
54: viajero=0
55: else
56: viajero=0
57: end if
58: else
59: viajero=0
60: end if
121:
122: 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 Input RA6 OUTPUT
126: TRISB = %00000011
127:
128: PORTA=0
129: PORTB=0
130: n_envios =0
131: received_byte=0
132: viajero=0
133: cod_alarma=0
134: Estado_Alarma=0
135: Alert_Alarma=0
136: acknoledge=0
137: n_presencia =0
138: cont=0
139: checksun_enviar =0
140: checksun_recibido =0
141: checksun_calculado =0
142: 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)
152: Enviar_Alerta_Respuesta (1,0,1)
153:
154:
155: else
156: ClearBit(PORTB,3)
157: delay_ms(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(1000)
167: if(acknoledge =1)then
168: n_envios=1
169: Alert_Alarma =0
170: acknoledge =0
171: cont=0
172: else
173: inc(cont)
174: if(cont<5)then
175: n_envios=0
176: else
177: cont=0
178: n_envios=1
179: end if
180: end if
181: end if
182: else
183: n_envios =0
184: end if
185: end if
186:
187: wend
188:
189: end.