{ **************************************************************** * File: gmbiot.ppas - Ver. 1.1 * * Compilatore: mikroPascal for PIC by mikroElektronica * * IDE: mikroPascal for PIC by mikroElektronica * * Versione Compilatore: 2.1.6.0 * * Schede: GMM 4620 + GMB HR168 * * GRIFO(R) via Dell'Artigiano 8/6 * * 40016 S. Giorgio di Piano (BO) * * Tel. +39 051 892052 Fax. +39 051 893661 * * http://www.grifo.com http://www.grifo.it * * by Graziano Gaiba 09.03.05 * **************************************************************** Questo Demo permette di utilizzare immediatamente le linee di I/O digitale TTL presenti su CN7. Alla partenza viene presentata la lista delle linee disponibili. Una Volta Selezionata Quella d'interesse la si puo' gestire in input (rappresentando Il Suo Stato Sulla Console) o in output (settandola alta o bassa). Rel 1.1 - by Graziano Gaiba Demo di utilizzo dei segnali TTL I/O digitali usando una GMB HR168 pilotata da un Mini Modulo GMM 4620 } { ***************** Definizioni del compilatore ****************** } program gmbiot; { *************** Dichiarazione delle contanti ******************* } const ASC_5:byte = 53; const ASC_4:byte = 52; const ASC_3:byte = 51; const ASC_2:byte = 50; const ASC_1:byte = 49; const ASC_0:byte = 48; { ******************* Dichiarazione delle Variabili ******************** } // Variabili di uso generico var chr_in, scelta, d_out: byte; Label for_ever; { ******************* Definizione delle procedure ********************* } // Inizializzazione direzione segnali della CPU procedure Init_cpu; begin ADCON1 :=$0f; // Imposta come I/O digitale i pin AN0..12 CMCON :=$07; // Imposta come I/O digitale RA0..4 { Gli ingressi optoisolati di CN1 sono: IN1-1 <-> RA0 IN2-1 <-> RA1 IN3-1 <-> RB0 IN4-1 <-> RB1 IN5-1 <-> RA4 IN6-1 <-> RC0 IN7-1 <-> RC1 IN8-1 <-> RC5 } trisa := trisa or $13; trisb := trisb or $03; trisc := trisc or $23; { Gli ingressi optoisolati di CN2 sono: IN1-2 <-> RD0 IN2-2 <-> RD1 IN3-2 <-> RD2 IN4-2 <-> RD3 IN5-2 <-> RD4 IN6-2 <-> RD5 IN7-2 <-> RD6 IN8-2 <-> RD7 } trisd := $ff; { Le uscite relays di CN3 sono: OUT A1 <-> RB4 OUT A2 <-> RB5 OUT B1 <-> RB6 OUT B2 <-> RB7 OUT C1 <-> RB3 OUT C2 <-> RB2 } trisb := trisb and $03; { Le uscite relays di CN4 sono: OUT D1 <-> RA3 OUT D2 <-> RC2 (J10 in posizione 3-4) } trisa := trisa and $f7; trisc := trisc and $fb; // Inizialzza modulo USART (8 bit, 19200 baud rate, // no parity) Usart_init(19200); end; // Invia una stringa di caratteri alla porta seriale procedure print_USART(var txt: string[100]); var temp1, temp2: byte; begin temp1 := txt[0]; for temp2 := 1 to temp1 do begin USART_Write(txt[temp2]); end; end; // Invia CR + LF procedure print_CRLF; begin USART_Write(10); USART_Write(13); end; // Pulisce lo schermo, inviando 25 volte CR + LF. procedure clrscr; var temp: byte; begin for temp := 0 to 24 do begin print_CRLF; end; end; // Chiede la pressione di un tasto procedure wait_key; var temp: byte; begin print_usart('Premere un tasto...'); repeat begin nop; end; until USART_Data_Ready = 1; temp := USART_Read; end; { Procedura per impostare lo stato dei relays sui connettori CN3 e CN4. A seconda del valore dei bits di port_val, ogni relay viene attivato (contatto chiuso) o disattivato (contatto aperto). I bit di port_val hanno il seguente significato: -- CN3 port_val.0 pilota il relay OUT A1 port_val.1 pilota il relay OUT A2 port_val.2 pilota il relay OUT B1 port_val.3 pilota il relay OUT B2 port_val.4 pilota il relay OUT C1 port_val.5 pilota il relay OUT C2 -- CN4 port_val.6 pilota il relay OUT D1 port_val.7 pilota il relay OUT D2 Ogni bit ha il seguente significato: bit Significato 0 Relay disattivato (contatto aperto) 1 Relay attivato (contatto chiuso) } procedure set_relays(port_val: byte); begin // I relays sono pilotati in logica complementata , quindi anche port_val // deve essere complementato port_val:=port_val xor $ff; if TestBit(port_val, 0) = 0 then begin ClearBit(portb, 4); end else begin SetBit(portb, 4); end; if TestBit(port_val, 1) = 0 then begin ClearBit(portb, 5); end else begin SetBit(portb, 5); end; if TestBit(port_val, 2) = 0 then begin ClearBit(portb, 6); end else begin SetBit(portb, 6); end; if TestBit(port_val, 3) = 0 then begin ClearBit(portb, 7); end else begin SetBit(portb, 7); end; if TestBit(port_val, 4) = 0 then begin ClearBit(portb, 3); end else begin SetBit(portb, 3); end; if TestBit(port_val, 5) = 0 then begin ClearBit(portb, 2); end else begin SetBit(portb, 2); end; if TestBit(port_val, 6) = 0 then begin ClearBit(porta, 3); end else begin SetBit(porta, 3); end; if TestBit(port_val, 7) = 0 then begin ClearBit(portc, 2); end else begin SetBit(portc, 2); end; end; {Inserimento di un valore decimale fino a cinque cifre. Il parametro in ingresso e' il numero di cifre, restituisce una word contenente il valore immesso. Gestisce il backspace (ASCII 08) ed accetta cifre in input da '0' a '9'. Iniva un carattere di bell (ASCII 07) quando riceve un dato non accettabile per l'input, o si tenta un inserimento o una cancellazione fuori dal range ammesso. } function inputDEC(n_char: byte): word; var str_input:array[6] of byte; idx, chr_input: byte; pow_of_10:array[6] of word; value: word; begin pow_of_10[1] := 1; pow_of_10[2] := 10; pow_of_10[3] := 100; pow_of_10[4] := 1000; pow_of_10[5] := 10000; // Indice deve partire da 1 idx := 1; Inc(n_char); // Nessun carattere in input chr_input := 0; // Valore iniziale value := 0; // Continua finche' non riceve un carattere CR while chr_input <> 13 do begin // Se un dato e' pronto if usart_data_ready = 1 then begin // Leggilo chr_input := USART_Read; // Se e' backspace (ASCII BS) if chr_input = 8 then begin // e il buffer non e' vuoto if idx > 1 then begin // Invia dato letto USART_Write(chr_input); // Cancella carattere USART_Write(32); USART_Write(chr_input); // Elimina ultimo dato letto Dec(idx); end else begin // Carattere bell per indicare un errore USART_Write(7); end; end; // Se CR ricevuto, elabora il valore e restituiscilo if chr_input = 13 then begin n_char := 0; while idx > 1 do begin Dec(idx); Inc(n_char); value := value + word((str_input[n_char] - 48)) * pow_of_10[idx]; end; end else begin // Se il carattere puo' essere inserito if idx < n_char then begin // Se il dato ricevuto e' valido: // 0..9 if (chr_input >= 48) or(chr_input <= 57) then begin // Invia il dato letto USART_Write(chr_input); // Memorizzalo nel buffer di ingresso str_input[idx] := chr_input; // Punta alla posizione successiva Inc(idx); end; end else begin // Carattere bell per indicare un errore USART_Write(7); end; end; end; end; print_CRLF; Result := value; end; { ********************** Programma principale *************************** } begin Init_cpu; // Spegne i relays set_relays(0); for_ever: clrscr; print_USART('Demo TTL I/O su CN7 Rel 1.1 per GMM 4620 rel 120304 e GMB HR168 rel 110104'); print_CRLF; print_CRLF; print_USART('Se un segnale viene scelto come ingresso, viene visualizzato continuamente.'); print_CRLF; print_USART('Se viene scelto come uscita, premendo 1 va alto, 0 va basso, oppure esce.'); print_CRLF; print_CRLF; print_USART('1) pin 2 di CN7'); // RE0 print_CRLF; print_USART('2) pin 3 di CN7'); // RE1 print_CRLF; print_USART('3) pin 4 di CN7'); // /INTRTC print_CRLF; print_USART('4) pin 6 di CN7'); // RC2 print_CRLF; print_USART('5) pin 8 di CN7'); // RA5 print_CRLF; print_CRLF; print_USART('Scelta: '); repeat begin repeat begin nop; end; until USART_Data_Ready = 1; scelta := USART_Read; end; until (scelta >= ASC_1) and (scelta <= ASC_5); print_CRLF; print_CRLF; print_USART('1) Input'); print_CRLF; print_USART('2) Output'); print_CRLF; repeat begin repeat begin nop; end; until USART_Data_Ready = 1; chr_in := USART_Read; end; until (chr_in >= ASC_1) and (chr_in <= ASC_2); print_CRLF; if chr_in = ASC_1 then begin print_USART('Input'); end else begin print_USART('Output'); end; print_CRLF; // pin 2 di CN7 - RE0 if scelta = ASC_1 then begin if chr_in = ASC_1 then begin SetBit(trise, 0); repeat begin print_USART('Stato: '); if TestBit(porte, 0) = 0 then begin USART_Write(ASC_0); end else begin USART_Write(ASC_1); end; USART_Write(13); end; until USART_Data_Ready = 1; chr_in := USART_Read; end else begin ClearBit(trise, 0); repeat begin print_USART('Stato: '); d_out := inputDEC(1); if d_out = 0 then begin ClearBit(porte, 0); end; if d_out = 1 then begin SetBit(porte, 0); end; end; until (d_out <> 0) and (d_out <> 1); end; end; // pin 3 di CN7 - RE1 if scelta = ASC_2 then begin if chr_in = ASC_1 then begin SetBit(trise, 1); repeat begin print_USART('Stato: '); if TestBit(porte, 1) = 0 then begin USART_Write(ASC_0); end else begin USART_Write(ASC_1); end; USART_Write(13); end; until USART_Data_Ready = 1; chr_in := USART_Read; end else begin ClearBit(trise, 1); repeat begin print_USART('Stato: '); d_out := inputDEC(1); if d_out = 0 then begin ClearBit(porte, 1); end; if d_out = 1 then begin SetBit(porte, 1); end; end; until (d_out <> 0) and (d_out <> 1); end; end; // pin 4 di CN7 - /INTRTC if scelta = ASC_3 then begin print_USART('Pin 4 collegato ad interrupt del Real Time Clock.'); print_CRLF; print_USART('Genera onde quadre; un interrupt si attiva'); print_CRLF; print_USART('ogni decimo,secondo,minuto,ora,giorno.'); print_CRLF; print_USART('Il LED LD28 ne mostra lo stato.'); print_CRLF; wait_key; end; // pin 6 di CN7 - RC2 if scelta = ASC_4 then begin if chr_in = ASC_1 then begin SetBit(trisc, 2); repeat begin print_USART('Stato: '); if TestBit(portc, 2) = 0 then begin USART_Write(ASC_0); end else begin USART_Write(ASC_1); end; USART_Write(13); end; until USART_Data_Ready = 1; chr_in := USART_Read; end else begin ClearBit(trisc, 2); repeat begin print_USART('Stato: '); d_out := inputDEC(1); if d_out = 0 then begin ClearBit(portc, 2); end; if d_out = 1 then begin SetBit(portc, 2); end; end; until (d_out <> 0) and (d_out <> 1); end; end; // pin 8 di CN7 - RA5 if scelta = ASC_5 then begin if chr_in = ASC_1 then begin SetBit(trisa, 5); repeat begin print_USART('Stato: '); if TestBit(porta, 5) = 0 then begin USART_Write(ASC_0); end else begin USART_Write(ASC_1); end; USART_Write(13); end; until USART_Data_Ready = 1; chr_in := USART_Read; end else begin ClearBit(trisa, 5); repeat begin print_USART('Stato: '); d_out := inputDEC(1); if d_out = 0 then begin ClearBit(porta, 5); end; if d_out = 1 then begin SetBit(porta, 5); end; end; until (d_out <> 0) and (d_out <> 1); end; end; goto for_ever; end.