program grotta; var attesa_1, attesa_2, attesa_3 : byte; counter, time_out, no_dato : byte; received_byte, read_byte, selector : byte; txt : string[3]; procedure LF_CR; begin Usart_Write($0A); // Line Feed Usart_Write($0D); // Carriage Return end; procedure rele_1; begin Usart_Write_Text(' Reset Rele 1'); LF_CR; ClearBit(PORTB,0); ClrWdt; delay_ms(2000); ClrWdt; delay_ms(2000); ClrWdt; SetBit(PORTB,0); end; procedure rele_2; begin Usart_Write_Text(' Reset Rele 2'); LF_CR; ClearBit(PORTB,1); ClrWdt; delay_ms(2000); ClrWdt; delay_ms(2000); ClrWdt; SetBit(PORTB,1); end; procedure rele_1_2; begin Usart_Write_Text(' Reset Rele 1 e Rele 2'); LF_CR; ClearBit(PORTB,0); ClearBit(PORTB,1); ClrWdt; delay_ms(2000); ClrWdt; delay_ms(2000); ClrWdt; SetBit(PORTB,0); SetBit(PORTB,1); end; procedure laser_1_1; begin Usart_Write_Text(' Laser calibrazione 1.1'); LF_CR; SetBit(PORTB,2); ClrWdt; delay_ms(2000); ClrWdt; delay_ms(2000); ClrWdt; ClearBit(PORTB,2); end; procedure laser_1_2; begin Usart_Write_Text(' Laser calibrazione 1.2'); LF_CR; SetBit(PORTB,3); ClrWdt; delay_ms(2000); ClrWdt; delay_ms(2000); ClrWdt; ClearBit(PORTB,3); end; procedure laser_1_3; begin Usart_Write_Text(' Laser calibrazione 1.3'); LF_CR; SetBit(PORTB,4); ClrWdt; delay_ms(2000); ClrWdt; delay_ms(2000); ClrWdt; ClearBit(PORTB,4); end; procedure laser_1_4; begin Usart_Write_Text(' Laser calibrazione 1.4'); LF_CR; SetBit(PORTB,5); ClrWdt; delay_ms(2000); ClrWdt; delay_ms(2000); ClrWdt; ClearBit(PORTB,5); end; procedure laser_2_1; begin Usart_Write_Text(' Laser calibrazione 2.1'); LF_CR; SetBit(PORTD,0); ClrWdt; delay_ms(2000); ClrWdt; delay_ms(2000); ClrWdt; ClearBit(PORTD,0); end; procedure laser_2_2; begin Usart_Write_Text(' Laser calibrazione 2.2'); LF_CR; SetBit(PORTD,1); ClrWdt; delay_ms(2000); ClrWdt; delay_ms(2000); ClrWdt; ClearBit(PORTD,1); end; procedure laser_2_3; begin Usart_Write_Text(' Laser calibrazione 2.3'); LF_CR; SetBit(PORTD,2); ClrWdt; delay_ms(2000); ClrWdt; delay_ms(2000); ClrWdt; ClearBit(PORTD,2); end; procedure laser_2_4; begin Usart_Write_Text(' Laser calibrazione 2.4'); LF_CR; SetBit(PORTD,3); ClrWdt; delay_ms(2000); ClrWdt; delay_ms(2000); ClrWdt; ClearBit(PORTD,3); end; procedure laser_1_1_2_1; begin Usart_Write_Text(' Laser calibrazione: laser 1.1 e laser 2.1'); LF_CR; SetBit(PORTB,2); SetBit(PORTD,0); ClrWdt; delay_ms(2000); ClrWdt; delay_ms(2000); ClrWdt; ClearBit(PORTB,2); ClearBit(PORTD,0); end; procedure laser_1_sequence; begin Usart_Write_Text(' In sequenza laser calibrazione: laser 1.1 laser 1.2 laser 1.3 laser 1.4'); LF_CR; SetBit(PORTB,2); ClrWdt; delay_ms(2000); ClrWdt; delay_ms(2000); ClrWdt; ClearBit(PORTB,2); SetBit(PORTB,3); ClrWdt; delay_ms(2000); ClrWdt; delay_ms(2000); ClrWdt; ClearBit(PORTB,3); SetBit(PORTB,4); ClrWdt; delay_ms(2000); ClrWdt; delay_ms(2000); ClrWdt; ClearBit(PORTB,4); SetBit(PORTB,5); ClrWdt; delay_ms(2000); ClrWdt; delay_ms(2000); ClrWdt; ClearBit(PORTB,5); end; procedure laser_2_sequence; begin Usart_Write_Text(' In sequenza laser calibrazione: laser 2.1 laser 2.2 laser 2.3 laser 2.4'); LF_CR; SetBit(PORTD,0); ClrWdt; delay_ms(2000); ClrWdt; delay_ms(2000); ClrWdt; ClearBit(PORTD,0); SetBit(PORTD,1); ClrWdt; delay_ms(2000); ClrWdt; delay_ms(2000); ClrWdt; ClearBit(PORTD,1); SetBit(PORTD,2); ClrWdt; delay_ms(2000); ClrWdt; delay_ms(2000); ClrWdt; ClearBit(PORTD,2); SetBit(PORTD,3); ClrWdt; delay_ms(2000); ClrWdt; delay_ms(2000); ClrWdt; ClearBit(PORTD,3); end; procedure all_laser_sequence; begin Usart_Write_Text(' Tutti i laser calibrazione in sequenza'); LF_CR; SetBit(PORTB,2); ClrWdt; delay_ms(2000); ClrWdt; delay_ms(2000); ClrWdt; ClearBit(PORTB,2); SetBit(PORTB,3); ClrWdt; delay_ms(2000); ClrWdt; delay_ms(2000); ClrWdt; ClearBit(PORTB,3); SetBit(PORTB,4); ClrWdt; delay_ms(2000); ClrWdt; delay_ms(2000); ClrWdt; ClearBit(PORTB,4); SetBit(PORTB,5); ClrWdt; delay_ms(2000); ClrWdt; delay_ms(2000); ClrWdt; ClearBit(PORTB,5); SetBit(PORTD,0); ClrWdt; delay_ms(2000); ClrWdt; delay_ms(2000); ClrWdt; ClearBit(PORTD,0); SetBit(PORTD,1); ClrWdt; delay_ms(2000); ClrWdt; delay_ms(2000); ClrWdt; ClearBit(PORTD,1); SetBit(PORTD,2); ClrWdt; delay_ms(2000); ClrWdt; delay_ms(2000); ClrWdt; ClearBit(PORTD,2); SetBit(PORTD,3); ClrWdt; delay_ms(2000); ClrWdt; delay_ms(2000); ClrWdt; ClearBit(PORTD,3); end; procedure laser_on; begin Usart_Write_Text(' Tutti i laser calibrazione accesi'); LF_CR; SetBit(PORTB,2); SetBit(PORTB,3); SetBit(PORTB,4); SetBit(PORTB,5); SetBit(PORTD,0); SetBit(PORTD,1); SetBit(PORTD,2); SetBit(PORTD,3); ClrWdt; end; procedure laser_off; begin Usart_Write_Text(' Tutti i laser calibrazione spenti'); LF_CR; ClearBit(PORTB,2); ClearBit(PORTB,3); ClearBit(PORTB,4); ClearBit(PORTB,5); ClearBit(PORTD,0); ClearBit(PORTD,1); ClearBit(PORTD,2); ClearBit(PORTD,3); ClrWdt; end; procedure spegni_lasermisura_1; begin Usart_Write_Text(' Spegni il laser di misura 1'); LF_CR; ClearBit(PORTD,4); ClrWdt; end; procedure spegni_lasermisura_2; begin Usart_Write_Text(' Spegni il laser di misura 2'); LF_CR; ClearBit(PORTD,5); ClrWdt; end; procedure accendi_lasermisura_1; begin Usart_Write_Text(' Accendi il laser di misura 1'); LF_CR; SetBit(PORTD,4); ClrWdt; end; procedure accendi_lasermisura_2; begin Usart_Write_Text(' Accendi il laser di misura 2'); LF_CR; SetBit(PORTD,5); ClrWdt; end; procedure errore_comando; begin Usart_Write_Text(' ERRORE COMANDO.'); LF_CR; Usart_Write_Text(' I COMANDI CORRETTI SONO:'); LF_CR; Usart_Write_Text(' a: rele_1; b: rele_2; c: rele_1 e rele_2;'); LF_CR; Usart_Write_Text(' 1: laser_1_1; 2:laser_1_2; 3:laser_1_3; 4:laser_1_4;'); LF_CR; Usart_Write_Text(' 5: laser_2_1; 6:laser_2_2; 7:laser_2_3; 8:laser_2_4;'); LF_CR; Usart_Write_Text(' m: laser di calibrazione 1_1 e 2_1;'); LF_CR; ClrWdt; Usart_Write_Text(' n: sequenza laser calibrazione 1;'); Usart_Write_TExt(' o: sequenza laser calibrazione 2;'); LF_CR; Usart_Write_Text(' p: tutti i laser di calibrazione in sequenza;'); LF_CR; Usart_Write_Text(' q: laser di calibrazione on;'); Usart_Write_Text(' r: laser di calibrazione off;'); LF_CR; Usart_Write_Text(' s: spegni laser di misura 1;'); Usart_Write_Text(' t: spegni laser di misura 2;'); LF_CR; Usart_Write_Text(' u: accendi laser di misura 1;'); Usart_Write_Text(' v: accendi laser di misura 2;'); LF_CR; ClrWdt; end; // ************************************** main *****************************************************8 begin // 1 counter := 0; // inizializzazione time_out := 0; no_dato := 0; read_byte := '.'; received_byte := '.'; txt := '----'; attesa_1 := 1; attesa_2 := 1; attesa_3 := 1; selector := 0; OPTION_REG := %00001111; //Prescaler Assignment Bit, Prescaler Rate Select Bit TRISB := %00000000; // PORTB usata come uscita TRISD := %00000000; // PORTD usata come uscita SetBit(TRISC,7); // input (ricezione ClearBit(TRISC,6); // output (trasmissione // setto le uscite dei laser di calibrazione a 0 ClearBit(PORTB,5); ClearBit(PORTB,4); ClearBit(PORTB,3); ClearBit(PORTB,2); ClearBit(PORTD,3); ClearBit(PORTD,2); ClearBit(PORTD,1); ClearBit(PORTD,0); // le uscite dei rel่ settate a 1 SetBit(PORTB,1); SetBit(PORTB,0); // setto le uscite dei laser di misura a 1 SetBit(PORTD,5); SetBit(PORTD,4); Usart_Init(9600); while true do begin // 2 while attesa_1 = 1 do begin if Usart_Data_Ready () = 1 then begin received_byte := Usart_Read; txt[counter] := received_byte; Usart_Write(txt[counter]); attesa_1 := 0; end else begin ClrWdt; // Clear Watch Dog Timer: 2,3 sec time_out := time_out + 1; if time_out = 100 then begin attesa_1 := 0; attesa_2 := 0; attesa_3 := 0; no_dato := 1; end else delay_ms(100); end end; time_out := 0; counter := 1; while attesa_2 = 1 do begin if Usart_Data_Ready () = 1 then begin received_byte := Usart_Read; txt[counter] := received_byte; Usart_Write(txt[counter]); attesa_2 := 0; end else begin ClrWdt; // Clear Watch Dog Timer: 2,3 sec time_out := time_out + 1; if time_out = 100 then begin attesa_1 := 0; attesa_2 := 0; attesa_3 := 0; no_dato := 1; end else delay_ms(100); end end; time_out := 0; counter := 2; while attesa_3 = 1 do begin if Usart_Data_Ready () = 1 then begin received_byte := Usart_Read; txt[counter] := received_byte; Usart_Write(txt[counter]); attesa_3 := 0; end else begin ClrWdt; // Clear Watch Dog Timer: 2,3 sec time_out := time_out + 1; if time_out = 100 then begin attesa_1 := 0; attesa_2 := 0; attesa_3 := 0; no_dato := 1; end else delay_ms(100); end end; if no_dato = 0 then begin // 3 if txt[0] = '@' then begin // 4 Usart_Write_Text(' INIZIO TRASMISSIONE'); if txt[2] = '#' then begin // 5 Usart_Write_Text(' TRASMISSIONE CORRETTA'); selector := txt[1]; case selector of 'a' : rele_1; 'b' : rele_2; 'c' : rele_1_2; '1' : laser_1_1; '2' : laser_1_2; '3' : laser_1_3; '4' : laser_1_4; '5' : laser_2_1; '6' : laser_2_2; '7' : laser_2_3; '8' : laser_2_4; 'm' : laser_1_1_2_1; 'n' : laser_1_sequence; 'o' : laser_2_sequence; 'p' : all_laser_sequence; 'q' : laser_on; 'r' : laser_off; 's' : spegni_lasermisura_1; 't' : spegni_lasermisura_2; 'u' : accendi_lasermisura_1; 'v' : accendi_lasermisura_2 else begin errore_comando; ClrWdt; end; end end // 5 else begin Usart_Write_Text(' ERRORE: carattere di fine stringa diverso da "#"'); // ERRORE LF_CR; ClrWdt; end end // 4 else begin Usart_Write_Text(' ERRORE: carattere inizio stringa diverso da "@"'); // ERRORE LF_CR; ClrWdt; end; end else // 3 begin ClrWdt; end; ClrWdt; attesa_1 := 1; // reinizializzazione attesa_2 := 1; attesa_3 := 1; selector := 0; no_dato := 0; counter := 0; time_out := 0; read_byte := '.'; received_byte := '.'; txt := '...'; end; // 2 end. // 1