' ********************************************************************** ' ** Program : gmbiobe.bas - Version : 1.1 - 06 June 2005 ** ' ** Compiler : BASCOM 8051 DEMO, (IDE and LIB V.2.0.11.0) ** ' ** Board : GMB HR84 with GMM 936 ** ' ** Corporate: 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 ** ' ********************************************************************** ' ' ' 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. ' ' ' 06.06.05 - Rel 1.1 By Graziano Gaiba ' ' '****************** Compiler directives ************************** 'N.B. In Options | Compiler | Misc perform these settings: ' Register File = 89LPC936.DAT ' Byte End(Hex) = A0 ' Size warining = 7167 $regfile = "89lpc936.dat" $romstart = &H0 ' start address of code for FLASH $ramstart = &H0 ' start address of external RAM $ramsize = &H200 ' 256 bytes of external RAM $crystal = 11059200 ' Maximun Microcontroller clock $baud = 19200 ' RS-232 baud rate $large ' 16 bit addressing for jumps ' (Not for demo version) $map ' Generates address map On Int1 Risp_int1 Nosave On Timer1 Risp_int_t1 Nosave ' '****************** Constants declatation *********************** ' Const Cret = 13 ' Carriage return Const Nl = 10 ' New line Const Clrscr = 12 ' Clear screen Const Bell = 7 ' Bell Const Rcint = 7372800 ' Internal RC clock frequency Const Qz11m = 11059200 ' External clock frequency ' '****************** Variables declaration ********************** ' ' General purpose variables Dim I As Byte , V As Byte , S As Byte , T As Byte Dim S0 As Bit Dim Hlpb As Byte , M As Byte ' General purpose Byte Dim Hlpw As Word , Ind As Word ' General purpose Word Dim Hlpl As Long ' General purpose Long Dim Choice As String * 1 ' One char string ' Variables for serial communication Dim St As Byte ' Stop bit number Dim Br As Long ' Serial Buad Rate ' Variables for clock and delays management Dim Cclk As Long ' CPU clock requency Dim D As Word , Del1ms As Word , Del As Word ' Delays management ' Variables for digital I/O ports management Dim M1 As Byte , M2 As Byte ' Interrupts counters Dim Cntintr As Byte , Cntintt1 As Byte ' '********************** Procedures declaration ************************* ' Declare Sub Get_clk ' Acquires clock frequency Declare Sub Init ' General initialization Declare Sub Ritardo(del As Word) ' Calibrated delay Declare Sub Iniser(br As Long , St As Byte) ' Serial initialization Declare Sub Check ' Verifies peripherals Declare Sub Set_outputs(v As Byte) ' Set relays status Declare Sub Get_inputs() ' Get optocoupled inputs ' '**************************** Main program ****************************** ' Main: Call Init ' Initializes the module Call Get_clk ' Acquires CPU clock frequency Call Iniser(19200 , 1) ' Initializes serial for console ' Performs a delay of about 2 seconds to allows the presence of emulation ' terminal program for console (i.e. HYPERTERMINAL) and contemporaneosly blinks ' the activity LED DL1 of the card. ' Uses general purpose variable i P0m1 = P0m1 Or &H40 ' Sets P0.6 in mode 3=OpenDrain P0m2 = P0m2 Or &H40 For I = 1 To 16 ' 16 cycles of 125 msec=2 sec P0.6 = Not P0.6 ' Complements activity LED Call Ritardo(125) ' Delays about 125 msec Next I Do Print Chr(clrscr); ' Clear screen Print "Demo 1.1 for GMM936 ds300803 + GMBHR84 ds220503" Call Check ' Internal check Print Do Print Chr(clrscr) Print "Demo for relays outputs on CN1 and optocoupled inputs on CN6" Print Print "1) Digital inputs" Print "2) Relays outputs" Print Print "SELECT: "; Do V = Inkey Loop Until V <> 0 Print Chr(v) Print If V = "1" Then Print "Optocoupled inputs on CN6" Do Print "Press a key to exit" Do Call Get_inputs() Printhex "IN1..8: " ; S ; Printbin Cret; S = Inkey Loop Until S <> 0 Print Print Print "Pin 4 of CN6 is also trigger for int1." Print "A transition H-L activates interrupt which increments its counter." Print "Press a key to exit" Print "INT1" Tcon.2 = 1 ' Int1 triggers on falling edge Enable Int1 ' Enables int1 Enable Interrupts ' Enables interrupts Cntintr = 0 ' Resets interrupt counter Do Printhex Cntintr ; ' Prints interrupt counters Printbin Cret ' Back to line beginning S = Inkey Loop Until S <> 0 ' Exits when key pressed Disable Interrupts ' Disables interrupts Disable Int1 ' Disables int1 Loop Until S <> 0 Print Print Print "T1 is set as counter, IN8 of CN6 is its trigger." Print "Every overflow generates an interrupt." Print "Press a key to exit" Cntintt1 = 0 ' ALWAYS stop timers before changing their parameters Stop Timer1 ' Configure timer 1 as 8 bit timer, trigger is pin T1, autoreload P0m1 = P0m1 Or &H80 ' Configures P0.7 (T1) in P0m2 = P0m2 And &H7F ' input only Tcon.6 = 0 ' Stop Timer Counter 1 Tl1 = &H00 ' Reset current value Th1 = &H00 Tamod = Tamod And &HEF ' Set Timer Counter 1 in mode Tmod = Tmod And &H0F ' 2,gate from TR1, as Counter Tmod = Tmod Or &H60 Tl1 = &HF0 ' Start value Th1 = &HE1 ' Autoreload value Enable Timer1 ' Enable interrupt overflow timer 1 Enable Interrupts ' Enable interrupts Print "T1 N.INT" Do S = Counter1 Printhex S ; " " ; Cntintt1; ' Print counter ' Reading counter 1 stops it automatically, must restared Start Timer1 Printbin Cret ' Back to line beginning S = Inkey Loop Until S <> 0 ' Exits when key pressed Disable Interrupts Disable Timer1 Stop Timer1 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 Printhex S Call Set_outputs(s) ' Sets outputs Loop Until S = 0 End If Loop Loop Risp_int1: Incr Cntintr ' Increment interrupt counter Return Risp_int_t1: Incr Cntintt1 ' Increment interrupt counter Return End ' '************************ Program end *************************** ' ' '**************************** Procedures ******************************** ' ' Read signals of port 2. ' P2.2, P2.3 and P2.4 are available to the user. ' Inputs work in complemented logic. ' IN1 <- P0.0 ' IN2 <- P0.1 ' IN3 <- P0.2 ' IN4 <- P1.4 ' IN5 <- P0.3 ' IN6 <- P0.4 ' IN7 <- P0.5 ' IN8 <- P0.7 Sub Get_inputs() S = P0 S.3 = P1.4 S.4 = P0.3 S.5 = P0.4 S.6 = P0.5 End Sub ' Set relays outputs. ' First four bits of the parameter are used. ' If bit is set to 1, correspondig relay is closed, or it is open. ' OUT A1 -> P1.6 ' OUT A2 -> P1.7 ' OUT B1 -> P2.1 ' OUT B2 -> P2.7 Sub Set_outputs(relays_out) P1.6 = Not V.0 P1.7 = Not V.1 P2.1 = Not V.2 P2.7 = Not V.3 End Sub ' ' '********************** General purpose subroutines **************************** ' Reads the configuration byte UCFG1 on FLASH thanks to IAP procedures of ' P89LPC932 Boot Rom. Returns the global variable cclk set with the CPU clock ' frequency, with no control of the possible errors. Sub Get_clk mov A,#&H03 ' Uses command Misc. Read mov R7,#&H00 ' Reads register UCFG1 lcall &HFF03 ' Calls IAP proc. at PGM_MTP addr. mov A,R7 ' Saves result anl A,#&H07 ' Masks bits with CPU clock Select Case Acc Case &H00 : Cclk = Qz11m ' Ext. crystal clock frequency Case &H03 : Cclk = Rcint ' Internal RC clock frequency End Select End Sub ' ' ' Inizializza risorse, variabili e periferiche in modo da poter eseguire ' correttamente tutto il programma demo. Sub Init Disable Interrupts ' Disabilita gli interrupts P1m1 = 0 ' Necessario per la seriale RS232 ' IN1 <- P0.0 ' IN2 <- P0.1 ' IN3 <- P0.2 ' IN4 <- P1.4 ' IN5 <- P0.3 ' IN6 <- P0.4 ' IN7 <- P0.5 ' IN8 <- P0.7 ' ' OUT A1 -> P1.6 ' OUT A2 -> P1.7 ' OUT B1 -> P2.1 ' OUT B2 -> P2.7 Pt0ad = 0 ' Set P0.1-5 come as digital I/O. M1 = P0m1 ' Set P0.0-5 and P0.7 as input only. M2 = P0m2 M1 = M1 Or &HDF M2 = M2 And &H40 P0m1 = M1 P0m2 = M2 ' M1 = P1m1 ' Set P1.4 as input only and M2 = P1m2 ' P1.6 and P1.7 as open drain output M1 = M1 Or &HD0 M2 = M2 Or &HC0 M2 = M2 And &HEF P1m1 = M1 P1m2 = M2 ' M1 = P2m1 ' Set P2.1 and P2.7 as M2 = P2m2 ' open drain output M1 = M1 Or &H82 M2 = M2 Or &H82 P2m1 = M1 P2m2 = M2 End Sub ' ' ' Checks right functionality of internal devices with possible remakes. ' Uses general purpose variable st,t,m,ind,hlpw Sub Check Deecon = &H01 T = Peek(&Hff) Deeadr = &HFF T = 1 Ind = &HF7 Do M = Peek(&H07) If M.7 = 1 Then M = M And &H03 Ind = M + &HF8 T = Peek(ind) Else T = 0 End If St = Deecon St = St And &H80 Loop Until St <> 0 T = St St = Deedat Do M = Peek(ind) Incr Ind If Ind = &HFF Then Ind = &HF9 Hlpw = 0 Do Incr Hlpw M = Peek(ind) Loop Until St.0 = 0 Or Hlpw >= 1000 T = T + M Hlpw = Ind + T M = Peek(t) If St = 0 Then Incr T Else Call Ritardo(1) ' 1 msec pause End If Loop Until St = 0 End Sub ' ' ' Performs a calibrated delay long as many milliseconds as specified in the ' passed parameter del, according with the clock frequency saved in cclk ' variable, with no use of BASCOM instructions that use a fixed and preset ' value of clock. Sub Ritardo(del As Word) If Cclk = Rcint Then Del1ms = 73 ' Value for 1 msec at 7 MHz Else Del1ms = 110 ' Value for 1 msec at 11 MHz End If Do For D = 0 To Del1ms ' Cycle for 1 msec delay Next D Del = Del - 1 Loop Until Del = 0 End Sub ' ' '*********************** Serial management subroutines ************************* ' Initializes the serial line with: ' Bit x chr = 8 ' Stop bit = 1 or 2 according with ST parameter ' Parity = None ' Baud rate = value of BR parameter ' by using the proper baud rate generator of microcontroller and his clock ' frequency saved on global variable cclk. ' Uses general purpose variables hlpw,hlpl Sub Iniser(br As Long , St As Byte) Pcon = Pcon And &HBF ' Resets bit SMOD0 to set mode If St = 1 Then Scon = &H52 ' Mode 1 (1 stop),No multiproc,Rx enable Else Scon = &HDA ' Mode 3 (2 stop),No multiproc,Rx enable End If Brgcon = &H02 ' Sets passed baud rate Hlpl = Br * 16 ' Calculates divisor for baud Hlpl = Cclk - Hlpl Hlpl = Hlpl / Br Hlpw = Loww(hlpl) Brgr0 = Low(hlpw) Brgr1 = High(hlpw) Brgcon = &H03 End Sub