' ********************************************************************** ' ** Program: gmbinbe.BAS - Version : 1.1 - 29 January 2004 ** ' ** Compiler : Bascom AVR IDE - LIB 1.11.7.4 ** ' ** Scheda : GMB HR84 and GMM AM08 ** ' ** Society: grifo(r) ITALIAN TECHNOLOGY ** ' ** Via Dell' Artigiano 8/6 40016 San Giorgio di Piano (BO) ** ' ** Tel.+39 051 892 052 Fax +39 051 893 661 ** ' ** http://www.grifo.com http://www.grifo.it ** ' ** sales@grifo.it tech@grifo.it grifo@grifo.it ** ' ** ** ' ** Written by: Graziano Gaiba ** ' ********************************************************************** ' ' Rel. 1.1 29.01.04 - By Graziano Gaiba ' ' This demo allows to use immediatly both buffered inputs and relay outputs ' available respectively on CN1 and CN6. The console can visualize the status ' of eight NPN/PNP inputs or set the status of four output relayses. ' In addition, high level functions available on Mini Module are used: inputs ' may generate interrupts or counted by hardware, outputs can generate ' automatically periodic signals, etc. ' ' Compiled file is smaller than 2048 bytes, so it can becompliled with free ' demo of BASCOM AVR. ' ' ' !!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ' In menu Options | Compiler | Chip, set: ' ' Chip: M8 ' HW Stack: at least 64 ' Soft Stack: at least 32 ' Framesize: at least 64 ' ' $regfile = "m8def.dat" $crystal = 7372800 $baud = 19200 On Int0 Risp_int0 Nosave On Int1 Risp_int1 Nosave On Timer0 Risp_int_t0 Nosave ' ' ********************* Constans declarations ************************* ' 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 ' '****************** Variables declaration ********************** ' ' Generic use Dim V As Byte , S As Byte , T As Byte ' Interrupts counter Dim Cntintr1 As Byte , Cntintr2 As Byte , Cntintt0 As Byte ' '********************** Procedures declaration ************************* ' Declare Sub Init() ' Initializtion Declare Function Get_inputs() As Byte ' Reads optocoupled inputs Declare Sub Set_outputs(byval relays_out As Byte) ' Sets relays outputs Declare Sub Richiedi_tasto() Declare Function Leggi_attesa() As Byte ' '**************************** Main program ****************************** ' Main: Call Init() ' Initialize module Do Call Set_outputs(0) Print Chr(clrscr); ' Clears the screen Print "Demo 1.1 for GMB HR84 ds 220503" Print Do Print Chr(clrscr) Print "Example on how to use inputs on CN6 and outputs on CN1" Print Print "1) Digital inputs" Print "2) Relays poutputs" Print Print "SELECT: "; Do V = Inkey() Loop Until V <> 0 Print Chr(v) If V = "1" Then Print "Optocoupled inputs on CN6" Do Call Richiedi_tasto() Do T = Get_inputs() Print Hex(t); Printbin Cret S = Leggi_attesa() Loop Until S <> 0 Print Print Print "IN3 is also trigger for int0, IN4 for int1." Print "A transition H-L activates interrupt which increments its counter." 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 is configured as counter, IN5 is its trigger" Print "Every overflow generates an interrupt" Call Richiedi_tasto() Cntintt0 = 0 ' ALWAYS stop a timer before changing its parameters Stop Timer0 ' Configure timer 0 as counter, triggered on H->L transitions of pin T0. Config Timer0 = Counter , Edge = Falling Counter0 = &HD0 ' Starting value Enable Timer0 ' Enables interrupt overflow of timer 0 Enable Interrupts ' Enables interrupts Start Timer0 Print "T0 N.INT" Do Print Hex(counter0) ; " " ; Hex(cntintt0); 'Prints counter ' Reading counter0 automatically stops timer0, must be restarted Start Timer0 Printbin Cret ' Back to line beginning S = Leggi_attesa() Loop Until S <> 0 ' Exits when key pressed Disable Interrupts Disable Timer0 Stop Timer0 End If If V = "2" Then Print Chr(clrscr); Print "Demo relays outputs" Print Do Print "Input hex figure (0 exits): "; Do S = Inkey() If S.6 = 1 Then S.5 = 0 ' Converts in uppercase S = S - 48 ' Numeric value If S > 9 Then S = S - 7 ' Adjust Numeric value Loop Until S >= 0 And S <= 15 Print Hex(s ) Call Set_outputs(s) ' Sets outputs Loop Until S = 0 Print Print "Signals connected to outputs of CN1 may perform special features" Print "like timings, PWM generation, etc." Print "Output A2 toggles with frequency of about 1 Hz and duty cicle about 50%" ' ALWAYS stop a timer before changing its parameters Tccr1b = 0 ' Stop Timer 1 Ddrb.2 = 1 ' Set portb.2 as output ' To write into 16 bit registers of Timer 1, first write the most ' significant byte then write least significant byte. ' To read from 16 bit registers of Timer 1, first read the most ' In addition, operation must be atomic, so it cannot be interrupted Ocr1ah = &H1C ' Period duration Ocr1al = &H71 Ocr1bh = &H0E ' Duty cycle 50% Ocr1bl = &H38 ' Timer starting value Tcnt1h = 0 Tcnt1l = 0 ' Configure Timer 1 as 10 bit Fast PWM, Prescaler clock / 1024, toggles ' pin 22 of socket on every match between register OCR1B counter ' register of timer, starts Timer 1. Tccr1a = &H33 Tccr1b = &H1D ' Wait for a key (blocking) S = Waitkey() Tccr1b = 0 ' Stop Timer 1 Tccr1a = 0 ' Disables PWM Portb.2 = 1 ' Opens relay on A2 End If Loop Loop Risp_int0: Incr Cntintr1 ' Increment interrupt counter Return Risp_int1: Incr Cntintr2 ' Increment interrupt counter Return Risp_int_t0: Incr Cntintt0 ' Increment interrupt counter Return End ' '************************ Program end *************************** ' ' '**************************** Procedures ******************************** ' ' ' Reads optocoupled inputs. ' Function Get_inputs() As Byte Local Temp As Byte , Temp1 As Byte ' Opto_in.0 <-- PC.0 = IN1 ' Opto_in.1 <-- PC.1 = IN2 ' Opto_in.2 <-- PD.2 = IN3 ' Opto_in.3 <-- PD.3 = IN4 ' Opto_in.4 <-- PD.4 = IN5 ' Opto_in.5 <-- PD.5 = IN6 ' Opto_in.6 <-- PC.2 = IN7 ' Opto_in.7 <-- PC.3 = IN8 Temp = Pinc And &H03 Temp1 = Pind And &H3C Temp1.6 = Pinc.2 Temp1.7 = Pinc.3 Get_inputs = Temp Or Temp1 End Function ' ' ' Sets relay outputs ' Sub Set_outputs(byval Relays_out As Byte) Local Temp As Byte Relays_out = Not Relays_out Portb.0 = Relays_out.0 ' Bit 0 is pin 1 of CN1 Portb.2 = Relays_out.1 ' Bit 1 is pin 3 of CN1 Temp = Portd And &H3F Temp.6 = Relays_out.2 ' Bit 2 is pin 4 of CN1 Temp.7 = Relays_out.3 ' Bit 3 is pin 5 of CN1 Portd = Temp End Sub ' ' ' Initializes demo ' Sub Init() Ddrc = Ddrc And &HF0 ' Sets PC0..3 in input Ddrd = Ddrd And &HC3 ' Sets PD2..5 in input Ddrd = Ddrd Or &HC0 ' Sets PD6..7 in output Ddrb = Ddrb Or &H05 ' Sets PB0 and 2 in output End Sub ' ' ' Prints "Press a key to exit" ' Sub Richiedi_tasto() Print Print "Press a key to exit" Print End Sub ' ' ' Waits for 200 msec then reads a key and returns it ' Function Leggi_attesa() Waitms 200 Leggi_attesa = Inkey() End Function