' ********************************************************************** ' * File: gmbiob.bas - Rel. 1.1 con Bascom AVR IDE e LIB 1.11.7.4 * ' * Schede: GMB HR168 + GMM AM32 * ' * 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 del 09.06.05 * ' ********************************************************************** ' ' 09.06.05 - Rel 1.1 By Graziano Gaiba ' Questo Demo permette di utilizzare immediatamente le uscite bufferate ' a rele` disponibili su CN3 e CN4 e gli ingressi optoisolati su CN1 e ' CN2. ' Tramite la console si puo' settare lo stato deglii otto rele' di ' uscita oppure rappresentare lo stato dei 16 ingressi NPN/PNP. ' Inoltre vengono applicate le funzionalita' evolute offerte dal Mini ' Modulo, come ad esempio ingressi contati via hw o che generano interrupt, ecc. ' ' ' ' !!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANTE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ' Nel menu Options | Compiler | Chip, impostare: ' ' Chip: M32 ' HW Stack: almeno 64 ' Soft Stack: almeno 32 ' Framesize: almeno 64 ' ' '****************** Direttive del compilatore ************************** ' $regfile = "m32def.dat" $crystal = 7372800 $baud = 19200 On Int0 Risp_int0 Nosave On Int1 Risp_int1 Nosave On Timer0 Risp_int_t0 Nosave ' '****************** Dichiarazione delle costanti *********************** ' Const Cret = 13 ' Codice di ritorno di carrello Const Nl = 10 ' Codice nuova linea Const Clrscr = 12 ' codice di clear screen Const Bell = 7 ' codice di Bell ' '****************** Dichiarazione delle variabili ********************** ' ' Uso generico Dim V As Byte , S As Byte , T As Byte ' Byte di uso generico Dim A As Eram Byte At 1023 ' Contatore interrupts Dim Cntintr1 As Byte , Cntintr2 As Byte , Cntintt0 As Byte ' '****************** Dichiarazione delle procedure ********************** ' Declare Sub Init() ' Inizializzazione Declare Sub Set_relays_out(byval V As Byte) ' Imposta lo stato dei rele' Declare Function Get_opto_in_a() As Byte ' Legge gli ingressi Opto A Declare Function Get_opto_in_b() As Byte ' Legge gli ingressi Opto B Declare Sub Richiedi_tasto() Declare Function Leggi_attesa() As Byte Declare Sub Check_ready() ' '************************* Programma main ****************************** ' Main: Call Init() ' Inzializza il modulo Do Call Set_relays_out(0) Print Chr(clrscr); ' Pulisce lo schermo Print "Demo 1.1 per GMM AM32 + GMB HR168" Print Call Check_ready() ' Verifica scheda pronta Do Print "Esempio di utilizzo degli ingressi su CN1,CN2 e delle uscite su CN3,CN4" Print Print "1) Input digitali" Print "2) Uscite a rele'" Print Print "SELEZIONE: "; Do V = Inkey() Loop Until V <> 0 Print Chr(v) If V = "1" Then Print "Ingressi optoisolati su CN1 e CN2" Do Call Richiedi_tasto() Print "CN1 CN2" Do T = Get_opto_in_a() Print Hex(t) ; " "; T = Get_opto_in_b() Print Hex(t) ; " "; Printbin Cret S = Leggi_attesa() Loop Until S <> 0 Print Print Print "IN3 e' anche trigger di int0, IN4 di int1." Print "Una transizione H-L attiva l'interrupt che incrementa il relativo contatore." Call Richiedi_tasto() Print "INT0 INT1" Mcucr.0 = 0 ' Int0 triggers on falling edge Mcucr.1 = 1 Mcucr.2 = 0 ' Int1 triggers on falling edge Mcucr.3 = 1 Enable Int0 ' Enables int0 Enable Int1 ' Enables int1 Enable Interrupts ' Enables interrupts Cntintr1 = 0 ' Resets interrupt counters Cntintr2 = 0 Do Print Hex(cntintr1) ; " " ; Hex(cntintr2) ; ' Prints interrupt counters Printbin Cret ' Back to line beginning S = Leggi_attesa() Loop Until S <> 0 ' Exits when key pressed Disable Interrupts ' Disables interrupts Disable Int0 ' Disables int0 Disable Int1 ' Disables int1 Loop Until S <> 0 Print Print Print "T0 e' configurato come contatore,IN5 e' il suo trigger" Print "Ogni overflow genera un interrupt" Call Richiedi_tasto() Cntintt0 = 0 ' Bisogna SEMPRE fermare i timer prima di riconfigurarli Stop Timer0 ' Configura Timer 0 come counter sulle transizione H->L del pin T0. Config Timer0 = Counter , Edge = Falling Counter0 = &HD0 ' Valore iniziale Enable Timer0 ' Abilita interrupt overflow timer 0 Enable Interrupts ' Abilita gli interrupt Start Timer0 Print "T0 N.INT" Do Print Hex(counter0) ; " " ; Hex(cntintt0); 'Stampa contatore ' Leggere counter0 ferma automaticamente timer0, bisogna riattivarlo Start Timer0 Printbin Cret ' Torna a inizio riga S = Leggi_attesa() Loop Until S <> 0 ' Esce alla pressione di un tasto Disable Interrupts Disable Timer0 Stop Timer0 End If If V = "2" Then Print Chr(clrscr); Print "Demo uscite rele'" Print Call Set_relays_out(0) Do Inputhex "Digitare cifra hex (00 esce): " , S Print Hex(s) Call Set_relays_out(s) ' Imposta le uscite Loop Until S = 0 Call Set_relays_out(0) End If Loop Loop Risp_int0: Incr Cntintr1 ' Incrementa contatore interrupt Return Risp_int1: Incr Cntintr2 ' Incrementa contatore interrupt Return Risp_int_t0: Incr Cntintt0 ' Incrementa contatore interrupt Return End ' '************************ Fine del programma *************************** ' ' ' ' '****************************************************************************** '* Procedure di gestione degli I/O della GMB HR168 * '****************************************************************************** ' ' ' ' Imposta le uscite a rele' ' Se il Bit e' 1 il corrispondente rele' viene chiuso, altrimenti viene aperto ' V.0 comanda RL1 ' V.1 comanda RL2 ' V.2 comanda RL3 ' V.3 comanda RL4 ' V.4 comanda RL5 ' V.5 comanda RL6 ' V.6 comanda RL7 ' V.7 comanda RL8 Sub Set_relays_out(byval V As Byte) Portd.6 = Not V.0 Portb.4 = Not V.1 Portb.2 = Not V.2 Portb.3 = Not V.3 Portb.5 = Not V.4 Portb.6 = Not V.5 Portb.7 = Not V.6 Porta.6 = Not V.7 End Sub ' ' ' Legge gli ingressi optoisolati di CN2 ' Function Get_opto_in_a() As Byte Local V_in As Byte ' V_in.0 legge IN1A ' V_in.1 legge IN2A ' V_in.2 legge IN3A ' V_in.3 legge IN4A ' V_in.4 legge IN5A ' V_in.5 legge IN6A ' V_in.6 legge IN7A ' V_in.7 legge IN8A V_in.0 = Not Pina.0 V_in.1 = Not Pina.1 V_in.2 = Not Pind.2 V_in.3 = Not Pind.3 V_in.4 = Not Pinb.0 V_in.5 = Not Pinb.1 V_in.6 = Not Pina.2 V_in.7 = Not Pina.3 Get_opto_in_a = V_in End Function ' ' ' Legge gli ingressi optoisolati di CN1 ' Function Get_opto_in_b() As Byte Local V_in As Byte ' V_in.0 legge IN1B ' V_in.1 legge IN2B ' V_in.2 legge IN3B ' V_in.3 legge IN4B ' V_in.4 legge IN5B ' V_in.5 legge IN6B ' V_in.6 legge IN7B ' V_in.7 legge IN8B V_in.0 = Not Pina.4 V_in.1 = Not Pina.5 V_in.2 = Not Pinc.2 V_in.3 = Not Pinc.3 V_in.4 = Not Pinc.4 V_in.5 = Not Pinc.5 V_in.6 = Not Pinc.6 V_in.7 = Not Pinc.7 Get_opto_in_b = V_in End Function ' '****************************************************************************** '* Procedure di gestione specifiche del demo * '****************************************************************************** ' ' ' ' Inizializza il demo ' Sub Init() ' Inizializza la direzionalita' dei port che pilotano gli ingressi optoisolati ' e le uscite a rele': ' IN1-1 - Porta.0 ' IN2-1 - Porta.1 ' IN3-1 - Portd.2 ' IN4-1 - Portd.3 ' IN5-1 - Portb.0 ' IN6-1 - Portb.1 ' IN7-1 - Porta.2 ' IN8-1 - Porta.3 Ddra.0 = 0 Ddra.1 = 0 Ddrd.2 = 0 Ddrd.3 = 0 Ddrb.0 = 0 Ddrb.1 = 0 Ddra.2 = 0 Ddra.3 = 0 ' IN1-2 - Porta.4 ' IN2-2 - Porta.5 ' IN3-2 - Portc.2 ' IN4-2 - Portc.3 ' IN5-2 - Portc.4 ' IN6-2 - Portc.5 ' IN7-2 - Portc.6 ' IN8-2 - Portc.7 Ddra.4 = 0 Ddra.5 = 0 Ddrc.2 = 0 Ddrc.3 = 0 Ddrc.4 = 0 Ddrc.5 = 0 Ddrc.6 = 0 Ddrc.7 = 0 ' OUT A1 - Portd.6 ' OUT A2 - Portb.4 ' OUT B1 - Portb.2 ' OUT B2 - Portb.3 ' OUT C1 - Portb.5 ' OUT C2 - Portb.6 ' OUT D1 - Portb.7 ' OUT D2 - Porta.6 (di default, ovvero con J10 in 3-4) Ddrd.6 = 1 Ddrb.4 = 1 Ddrb.2 = 1 Ddrb.3 = 1 Ddrb.5 = 1 Ddrb.6 = 1 Ddrb.7 = 1 Ddra.6 = 1 End Sub ' '****************************************************************************** '* Procedure di uso generico * '****************************************************************************** ' ' ' Stampa il messaggio "Premere tasto per uscire" ' Sub Richiedi_tasto() Print Print "Premere tasto per uscire" Print End Sub ' ' ' Attende 200 msec poi legge un tasto e lo restituisce ' Function Leggi_attesa() Waitms 200 Leggi_attesa = Inkey() End Function ' ' ' Controlla che la scheda sia pronta ' Sub Check_ready() Test: If A = &H55 Then Goto L2 Else Goto Test End If L2: End Sub