' ********************************************************************** ' ** Program: gmbiobe.BAS - Version : 1.1 - 17 June 2005 ** ' ** Compiler : Bascom AVR IDE - LIB 1.11.7.4 ** ' ** Board : GMB HR168 and GMM AM128 ** ' ** 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 ** ' ** Written by: Graziano Gaiba ** ' ********************************************************************** ' ' 17.06.05 - Rel 1.1 By Graziano Gaiba ' This demo allows to use immediately relays outputs available on CN3 ' and CN4 and buffered inputs available on CN1 and CN2. ' Through the console can be set the status of the four output relays ' or it can visualize the status of the eight NPN/PNP inputs. ' In addition, high level functions available on Mini Module are used: ' outputs driven by periodic automatic signals, inputs that generate ' interrupts, inputs counted by hardware, etc. ' ' ' !!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ' In menu Options | Compiler | Chip, set: ' ' Chip: M128 ' HW Stack: at least 64 ' Soft Stack: at least 32 ' Framesize: at least 64 ' ' $regfile = "m128def.dat" $crystal = 7372800 $baud = 19200 On Int4 Risp_int4 Nosave On Int5 Risp_int5 Nosave On Int6 Risp_int6 Nosave On Timer2 Risp_int_t2 Nosave ' ' ********************* Constans declarations ************************* ' Const Cret = 13 ' Carriage return Const Nl = 10 ' New line Const Clrscr = 12 ' Clear screen Const Bell = 7 ' Bell ' Const Ee_timeout = 50000 ' '****************** Variables declaration ********************** ' ' Generic use Dim V As Byte , S As Byte , T As Byte ' Interrupts counter Dim Cntintr4 As Byte , Cntintr5 As Byte , Cntintr6 As Byte , Cntintt2 As Byte ' '********************** Procedures declaration ************************* ' Declare Sub Init() ' Initialization Declare Sub Set_relays_out(byval V As Byte) ' Set relays status Declare Function Get_opto_in_a() As Byte ' Read inputs Opto A Declare Function Get_opto_in_b() As Byte ' Read inputs Opto B Declare Sub Richiedi_tasto() Declare Function Leggi_attesa() As Byte Declare Sub Check_ready() Declare Function Check_ready_2() As Byte ' '**************************** Main program ****************************** ' Main: Call Init() ' Initialize module Do Call Set_relays_out(0) Print Chr(clrscr); ' Clears the screen Print "Demo 1.1 for GMM AM128 + GMB HR168" Print Call Check_ready() 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 outputs" 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() 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 is also trigger for int4, IN4 for int5 and IN6 for int6." Print "A transition H-L activates interrupt which increments its counter." Call Richiedi_tasto() Print "INT4 INT5 INT6" Eicrb.0 = 0 ' Int4 triggers on falling edge Eicrb.1 = 1 Eicrb.2 = 0 ' Int5 triggers on falling edge Eicrb.3 = 1 Eicrb.4 = 0 ' Int5 triggers on falling edge Eicrb.5 = 1 Enable Int4 ' Enables int4 Enable Int5 ' Enables int5 Enable Int6 ' Enables int6 Enable Interrupts ' Enables interrupts Cntintr4 = 0 ' Resets interrupt counters Cntintr5 = 0 Cntintr6 = 0 Do Print Hex(cntintr4) ; " " ; Hex(cntintr5) ; " " ; Hex(cntintr6) ; ' 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 Int6 ' Disables int6 Disable Int5 ' Disables int5 Disable Int4 ' Disables int4 Loop Until S <> 0 Print Print Print "Timer 2 is configured as counter, IN5 is its trigger" Print "Every overflow generates an interrupt" Call Richiedi_tasto() Cntintt2 = 0 ' ALWAYS stop a timer before changing its parameters Stop Timer2 ' Configure timer 2 as counter, triggered on H->L transitions of pin T2. Config Timer2 = Counter , Edge = Falling Counter2 = &HF0 ' Starting value Enable Timer2 ' Enables interrupt overflow of timer 0 Enable Interrupts ' Enables interrupts Start Timer0 Print "T2 N.INT" Do Print Hex(counter2) ; " " ; Hex(cntintt2); 'Prints counter ' Reading counter2 automatically stops timer2, must be restarted Start Timer2 Printbin Cret ' Back to line beginning S = Leggi_attesa() Loop Until S <> 0 ' Exits when key pressed Disable Interrupts Disable Timer2 Stop Timer2 End If If V = "2" Then Print Chr(clrscr); Print "Demo relays outputs" Print Do Inputhex "Type hex value (00 exits): " , S Print Hex(s) Call Set_relays_out(s) ' Set outputs Loop Until S = 0 End If Loop Loop Risp_int4: Incr Cntintr4 ' Increment interrupt counter Return Risp_int5: Incr Cntintr5 ' Increment interrupt counter Return Risp_int6: Incr Cntintr6 ' Increment interrupt counter Return Risp_int_t2: Incr Cntintt2 ' Increment interrupt counter Return End ' '************************ Program end *************************** ' ' '****************************************************************************** '* Procedures to manage I/O signals of GMB HR168 * '****************************************************************************** ' ' ' . ' ' Set relays outputs ' If Bit value is 1, corresponding relay is closed, otherwise it is open ' OUT A1 - Portf.4 ' OUT A2 - Portf.5 ' OUT B1 - Portf.6 ' OUT B2 - Portf.7 ' OUT C1 - Portb.3 ' OUT C2 - Portb.2 ' OUT D1 - Portb.1 ' OUT D2 - Porte.2 (by default, that is J10 in 3-4) ' V.0 controls RL1 ' V.1 controls RL2 ' V.2 controls RL3 ' V.3 controls RL4 ' V.4 controls RL5 ' V.5 controls RL6 ' V.6 controls RL7 ' V.7 controls RL8 Sub Set_relays_out(byval V As Byte) Portf.4 = Not V.0 Portf.5 = Not V.1 Portf.6 = Not V.2 Portf.7 = Not V.3 Portb.3 = Not V.4 Portb.2 = Not V.5 Portb.1 = Not V.6 Porte.2 = Not V.7 End Sub ' ' ' Reads optocoupled inputs of CN2 ' Function Get_opto_in_a() As Byte Local V_in As Byte ' IN1-1 - Portb.7 ' IN2-1 - Portb.6 ' IN3-1 - Porte.4 ' IN4-1 - Porte.5 ' IN5-1 - Portd.7 ' IN6-1 - Porte.6 ' IN7-1 - Portb.4 ' IN8-1 - Portb.0 ' ' V_in.0 reads IN1A ' V_in.1 reads IN2A ' V_in.2 reads IN3A ' V_in.3 reads IN4A ' V_in.4 reads IN5A ' V_in.5 reads IN6A ' V_in.6 reads IN7A ' V_in.7 reads IN8A V_in.0 = Not Pinb.7 V_in.1 = Not Pinb.6 V_in.2 = Not Pine.4 V_in.3 = Not Pine.5 V_in.4 = Not Pind.7 V_in.5 = Not Pine.6 V_in.6 = Not Pinb.4 V_in.7 = Not Pinb.0 Get_opto_in_a = V_in End Function ' ' ' Reads optocoupled inputs of CN1 ' Function Get_opto_in_b() As Byte Local V_in As Byte ' IN1-2 - Portc.0 ' IN2-2 - Portc.1 ' IN3-2 - Portc.2 ' IN4-2 - Portc.3 ' IN5-2 - Portc.4 ' IN6-2 - Portc.5 ' IN7-2 - Portc.6 ' IN8-2 - Portc.7 ' ' V_in.0 reads IN1B ' V_in.1 reads IN2B ' V_in.2 reads IN3B ' V_in.3 reads IN4B ' V_in.4 reads IN5B ' V_in.5 reads IN6B ' V_in.6 reads IN7B ' V_in.7 reads IN8B V_in = Not Pinc Get_opto_in_b = V_in End Function ' '****************************************************************************** '* Demo specific procedures * '****************************************************************************** ' ' ' Initializes demo ' Sub Init() ' Initializes directionality of ports that drive optocouped inputs and relay ' outputs: ' IN1-1 - Portb.7 ' IN2-1 - Portb.6 ' IN3-1 - Porte.4 ' IN4-1 - Porte.5 ' IN5-1 - Portd.7 ' IN6-1 - Porte.6 ' IN7-1 - Portb.4 ' IN8-1 - Portb.0 Ddrb.7 = 0 Ddrb.6 = 0 Ddre.4 = 0 Ddre.5 = 0 Ddrd.7 = 0 Ddre.6 = 0 Ddrb.4 = 0 Ddrb.0 = 0 ' IN1-2 - Portc.0 ' IN2-2 - Portc.1 ' IN3-2 - Portc.2 ' IN4-2 - Portc.3 ' IN5-2 - Portc.4 ' IN6-2 - Portc.5 ' IN7-2 - Portc.6 ' IN8-2 - Portc.7 Ddrc = 0 ' OUT A1 - Portf.4 ' OUT A2 - Portf.5 ' OUT B1 - Portf.6 ' OUT B2 - Portf.7 ' OUT C1 - Portb.3 ' OUT C2 - Portb.2 ' OUT D1 - Portb.1 ' OUT D2 - Porte.2 (by default, that is J10 in 3-4) Ddrf.4 = 1 Ddrf.5 = 1 Ddrf.6 = 1 Ddrf.7 = 1 Ddrb.3 = 1 Ddrb.2 = 1 Ddrb.1 = 1 Ddre.2 = 1 End Sub ' '****************************************************************************** '* Generic procedures * '****************************************************************************** ' ' ' ' 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 ' ' ' Supporting function for Check_ready(). ' Function Check_ready_2() As Byte Local Time_out As Word , I As Byte Time_out = 0 ' Timeout counter Do Incr Time_out ' Increment counter Loop Until Twcr.twint = 1 Or Time_out = Ee_timeout I = Twsr I = I And &HF8 Check_ready_2 = I End Function ' ' ' Check for card ready ' Sub Check_ready() Local Check As Byte , Test As Byte Do ' Loop to check for card ready Twsr = Twsr And &HFC Check = Twdr Or &HF4 Test.4 = Not Check.3 Twbr = 72 Twcr = &HA4 Check = Check_ready_2() Test = Check Or Twcr If Check = &H09 Then Test = &H67 Else Test.1 = Not Test.1 End If Twdr = &HA0 Twcr = &H84 Check = Check_ready_2() Twcr = &H94 Waitms 27 Loop Until Check = &H18 Or Test <> 1 Twcr.twen = 0 End Sub