' ********************************************************************** ' ** Program : gmbiobe.bas - Version : 1.1 - 03 March 2004 ** ' ** Compiler : BASCOM 8051 DEMO, (IDE and LIB V.2.0.11.0) ** ' ** Board : GMB HR168 with GMM AC2 ** ' ** 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 ** ' ** sales@grifo.it tech@grifo.it grifo@grifo.it ** ' ** ** ' ** Written by: Graziano Gaiba ** ' ********************************************************************** ' ' ' This demo allows to use immediatly both buffered inputs and relay outputs ' available respectively on CN1,CN2 and CN3,CN4. The console can visualize ' the status of sixteen NPN/PNP inputs or set the status of eight output ' relays. ' 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. ' ' 03.03.043 - Rel 1.1 By Graziano Gaiba ' ' ' !!!!!!!!!!!!!!!!!!!!!!! ATTENTION !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ' In menu Options/Compiler/Misc set: ' - Byte End(hex) at least A0 ' ' ' ' The file compiled is smaller than 2048 bytes, so it can be compiled ' with free demo version of BASCOM 8051. ' '****************** Compiler directives ************************** ' $regfile = "89c51AC2.dat" $romstart = &H0 ' start address of code for FLASH $ramstart = &H0 ' start address of external RAM $ramsize = &H100 ' 256 bytes of external RAM $crystal = 14745600 ' Microcontroller clock $baud = 19200 ' RS-232 baud rate '$large ' 16 bit addressing for jumps ' (Not for demo version) $map ' Generates address map On Int0 Risp_int0 Nosave On Int1 Risp_int1 Nosave On Timer0 Risp_int_t0 Nosave ' ' '****************** Constants declatation *********************** ' Const Cret = 13 ' Carriage return Const Nl = 10 ' New line Const Clrscr = 12 ' Clear screen Const Bell = 7 ' Bell Const R = 1 ' Read I2C Bus Const W = 0 ' Write I2C Bus ' ' '****************** Variables declaration ********************** ' ' Generic use Dim V As Byte , S As Byte , T As Byte ' Optocoupled inputs Dim In As Byte ' Relays outputs Dim Relays_out As Byte ' Interrupts counter Dim Cntintr1 As Byte , Cntintr2 As Byte , Cntintt0 As Byte ' ' '********************** Procedures declaration ************************* ' Declare Sub Init() ' Initializtion Declare Sub Setp01234input() ' Sets all port in input Declare Sub Set_relays_out(v As Byte) ' Sets relays outputs Declare Sub Get_opto_in_a(v As Byte) ' Reads optocoupled inputs of CN2 Declare Sub Get_opto_in_b(v As Byte) ' Reads optocoupled inputs of CN1 Declare Sub Richiedi_tasto() Declare Sub Leggi_attesa() ' ' '**************************** Main program ****************************** ' Main: Disable Interrupts ' Disabiles interrupts Auxr = &H0C ' Selects Eram on external data area Eecon = &H00 ' Disables Eeprom of Micro Call Setp01234input() Do Print Chr(clrscr); ' Clear screen Print "Demo 1.1 for GMB HR168 ds 110104" Do Print "Inputs CN1,CN2 and outputs on CN3,CN4" 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 "Inputs on CN1 and CN2" S = Adcf S.1 = 0 S.2 = 0 Adcf = S ' Sets P1.1 and P1.2 as I/O Do Call Richiedi_tasto() Print "IN CN2 IN CN1" Do Call Get_opto_in_a() ' Optocoupled inputs of CN2 Printhex " " ; V ; " "; Call Get_opto_in_b() ' Optocoupled inputs of CN1 Printhex V ; Printbin Cret Call Leggi_attesa() Loop Until S <> 0 Print Print Print "Pin 3 of CN1 is trigger for int0, pin 4 for int1." Print "A transition H-L activates interrupt which increments its counter." Call Richiedi_tasto() Print "INT0 INT1" Tcon.0 = 1 ' Int0 triggers on falling edge Tcon.2 = 1 ' Int1 triggers on falling edge Enable Int0 ' Enables int0 Enable Int1 ' Enables int1 Enable Interrupts ' Enables interrupts Cntintr1 = 0 ' Resets interrupt counters Cntintr2 = 0 Do Printhex Cntintr1 ; " " ; Cntintr2 ; ' Prints interrupt counters Printbin Cret ' Back to line beginning Call 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 set as counter, pin 5 is its trigger" Print "Every overflow generates an interrupt" Call Richiedi_tasto() Cntintt0 = 0 ' ALWAYS stop timers before changing their parameters Stop Timer0 ' BASCOM uses timer 1 as baud rate generator. ' If the user wants to use Timer 1 then has to manage in autonomy ' serial communication and must not usa instructions like input, inkey ' or print. ' Configure timer 0 as 8 bit timer, trigger is pin T0, autoreload Config Timer0 = Counter , Mode = 2 , Gate = Internal Tl0 = &HF0 ' Starting value Th0 = &HE1 ' Autoreload value Enable Timer0 ' Enable interrupt overflow timer 0 Enable Interrupts ' Enable interrupt Start Timer0 Print "T0 N.INT" Do S = Counter0 Printhex S ; " " ; Cntintt0; ' Print counter ' Reading counter0 stops it automatically, must restared Start Timer0 Printbin Cret ' Back to line beginning Call 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 "Relays outputs" Print Do Inputhex "Type hexadecimal figure (00 exits): " , S Printhex S Call Set_relays_out(s) ' Set relays outputs Loop Until S = 0 Print Print "Signals connected to outputs of CN3 may perform special features" Print "derived from PCA signals, like timings, PWM generation, etc." Print "Output A1 activates with frequency of about 1 Hz and duty cicle about" Print "50%." Stop Timer0 ' Disables Timer Counter 0 ' Setta timer 0 as 13 bit Timer (mode 0) Config Timer0 = Timer , Gate = Internal , Mode = 0 Counter0 = 0 ' Reset value Start Timer0 ' Enable Timer Counter 0 Cmod = &H04 ' Run In Idle , Tmr 0 Ovr , No Interrupt Ccon = &H40 ' Enable PCA timer/counter Ccapm1 = &H42 ' Enable PWM on module 1 PCA with duty cycle = 50% Ccap1h = &H80 Call Richiedi_tasto() Do Call Leggi_attesa() Loop Until S <> 0 Ccon = &H00 ' Disable Pca Timer / Counter Stop Timer0 ' Disable Timer Counter 0 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 ******************************** ' ' ' ' Set relays outputs ' If Bit is 1, corresponding relay is closed, otherwise it is opened ' V.0 commands RL1 ' V.1 commands RL2 ' V.2 commands RL3 ' V.3 commands RL4 ' V.4 commands RL5 ' V.5 commands RL6 ' V.6 commands RL7 ' V.7 commands RL8 Sub Set_relays_out(v As Byte) P1.4 = Not V.0 P1.5 = Not V.1 P1.6 = Not V.2 P1.7 = Not V.3 P4.0 = Not V.4 P4.1 = Not V.5 P2.2 = Not V.6 P1.3 = Not V.7 End Sub ' ' ' Reads optocoupled inputs of CN2 ' Sub Get_opto_in_a(v As Byte) ' V.0 reads IN1A ' V.1 reads IN2A ' V.2 reads IN3A ' V.3 reads IN4A ' V.4 reads IN5A ' V.5 reads IN6A ' V.6 reads IN7A ' V.7 reads IN8A V.0 = P0.0 V.1 = P0.1 V.2 = P0.2 V.3 = P0.3 V.4 = P0.4 V.5 = P0.5 V.6 = P0.6 V.7 = P0.7 End Sub ' ' ' Reads optocoupled inputs ofi CN1 ' Sub Get_opto_in_b(v As Byte) ' V.0 reads IN1B ' V.1 reads IN2B ' V.2 reads IN3B ' V.3 reads IN4B ' V.4 reads IN5B ' V.5 reads IN6B ' V.6 reads IN7B ' V.7 reads IN8B V.0 = P1.1 V.1 = P1.2 V.2 = P3.2 V.3 = P3.3 V.4 = P3.4 V.5 = P3.5 V.6 = P3.6 V.7 = P3.7 End Sub ' ' ' Set all ports as input ' Sub Setp01234input() P0 = &HFF ' Set P0 as input S = P0 Adcf = &H00 ' Set P1 as I/O P1 = &HFF ' Set P1 as input S = P1 P2 = &HFF ' Set P2 as input S = P2 P3 = &HFF ' Set P3 as input S = P3 P4 = &HFF ' Set P4 as input S = P4 End Sub ' ' ' Sub Richiedi_tasto() Print Print "Press a key to exit" Print End Sub ' ' ' Sub Leggi_attesa() Waitms 200 S = Inkey() End Sub