' ********************************************************************** ' ** Program : gmbiobe.bas - Version : 1.1 - 24 June 2003 ** ' ** Compiler : BASCOM 8051 DEMO, (IDE and LIB V.2.0.11.0) ** ' ** Board : GMB HR84 with CAN GMx or GMM yyyy ** ' ** 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 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. ' ' 09.04.03 - Rel 1.1 By Graziano Gaiba ' ' ' 12.05.03 - Rel 1.2 By Graziano Gaiba ' Update to show features of inputs (counter and interrupt) and outputs (PWM) ' ' ' ' The file compiled is smaller than 2048 bytes, so it can be compiled ' with free demo version of BASCOM 8051. ' '****************** Compiler directives ************************** ' ' Set to 80h internal memory limit by: ' Options/Compiler/Misc $regfile = "grifo_mm.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 ' Board where the demo is running Const Can_gm1 = "1" Const Can_gm2 = "2" Const Gmm_5115 = "3" ' '****************** Variables declaration ********************** ' ' Board where the demo is running' Dim Scheda As Byte ' 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 Get_inputs() ' Reads optocoupled inputs Declare Sub Set_outputs(relays_out As Byte) ' Sets relays outputs Declare Sub Richiedi_tasto() Declare Sub Leggi_attesa() ' '**************************** Main program ****************************** ' Main: Call Init() ' Initialize module Do Print Chr(clrscr); ' Clear screen Print "Demo 1.1 for GMB HR84 ds 100203" Print Print "Which Mini Modulo grifo(r) is the demo running on:" Print "1) CAN GM1" Print "2) CAN GM2" Print "3) GMM 5115" Print Print "SELECT: "; Do Scheda = Inkey() ' Stand by a key Loop Until Scheda <> 0 Do Print Chr(clrscr) Print "Exsmple 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" S = Adcf S.1 = 0 S.2 = 0 Adcf = S ' Sets P1.1 and P1.2 as I/O Do Call Richiedi_tasto() Do Call Get_inputs() Printhex In; Printbin Cret Call Leggi_attesa() Loop Until S <> 0 Print Print Print "Pin 3 of CN6 is also trigger for int0, pin 5 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 6 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 "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 Print Print "Signals connected to outputs of CN1 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 ******************************** ' ' Read signals of port 2. ' P2.2, P2.3 and P2.4 are available to the user Sub Get_inputs() In = P3 ' Bit from 2 to 7 are pins from 3 to 8 In.0 = P1.1 ' Bit 0 is pin 1 of CN6 In.1 = P1.2 ' Bit 1 is pin 2 of CN6 End Sub ' Set relays outputs Sub Set_outputs(relays_out) P1.4 = Not Relays_out.0 ' Bit 0 is pin 1 of CN1 P1.5 = Not Relays_out.1 ' Bit 1 is pin 3 of CN1 P1.6 = Not Relays_out.2 ' Bit 2 is pin 4 of CN1 P1.7 = Not Relays_out.3 ' Bit 3 is pin 5 of CN1 ' Other bit are irrilevant End Sub ' Initialize demo Sub Init() Disable Interrupts ' Disabiles interrupts Auxr = &H0C ' Selects Eram on external data area Eecon = &H00 ' Disables Eeprom of Micro Adcf = Adcf And &HF9 ' Set P1.1 and P1.2 as I/O P3 = &HFF ' Sets port 3 as input S = P3 End Sub Sub Richiedi_tasto() Print Print "Press a key to exit" Print End Sub Sub Leggi_attesa() Waitms 200 S = Inkey End Sub