' ********************************************************************** ' ** Program : gmbsere.bas - Version : 1.2 - 18 November 2004 ** ' ** Compiler : BASCOM 8051 DEMO, (IDE and LIB V.2.0.11.0) ** ' ** Board : GMB HR84 with GMM 935 ** ' ** 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 is a simple example of communication capableto work with all ' electric protocols available on CN2 (RS 232, RS 422, RS 485, current loop or ' TTL). ' In detail, through high level functions, it is possible to program baud rate ' from console, then each character received from serial port is sent to the ' port itself; reception of character 'r' decides how to manage the ' communication direction (signal DIR) for RS 422 and RS 485. ' ' 24.06.03 - Rel 1.1 By Graziano Gaiba ' ' 18.11.04 - Rel 1.2 By Graziano Gaiba ' Modified to run on a GMM 935 ' '****************** Compiler directives ************************** 'N.B. In Options | Compiler | Misc perform these settings: ' Register File = 89LPC935.DAT ' Byte End(Hex) = A0 ' Size warining = 7167 $regfile = "89lpc935.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 ' '****************** 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 Const Enable_422_485 = 1 ' Enables driver RS 422 or 485 Const Disable_422_485 = 0 ' Disable driver RS 422 or 485 ' '****************** Variables declaration ********************** ' ' General purpose Dim S1 As String * 1 ' General purpose Byte Dim S0 As Bit ' General purpose Bit Dim I As Byte Dim Hlpb As Byte , M As Byte ' General purpose Byte Dim M1 As Byte , M2 As Byte ' General purpose Byte Dim T As Byte , S As Byte , V As Byte ' General purpose Byte Dim Hlpw As Word , Ind As Word ' General purpose Word Dim Hlpl As Long ' General purpose Long Dim Scelta As String * 1 ' One character string ' Variables to manage serial communication, clock and delays Dim Chav As Bit Dim St As Byte , Chser As Byte Dim Br As Long ' Serial Baud Rate Dim Cclk As Long ' CPU clock frequency Dim D As Word , Del1ms As Word , Del As Word ' Delays management ' '********************* 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 Demoser() Declare Sub Txser(chser As Byte) ' Sends a character Declare Sub Rxser ' Checks and receives a character ' non-blocking ' '************************** 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); ' Pulisce lo schermo Print "Demo 1.2 for GMM935 ds300803 + GMBHR84 ds220503" Call Check ' Controllo interno Call Demoser() Loop End ' '************************ Program end *************************** ' ' '**************************** Procedures ******************************** ' Sub Demoser Print Print "RS 232,RS 422,RS 485,current loop or TTL serial line on CN2" Print Print "This demo receives and transmits at low level, if F is received it resets to" Print " 19200 Baud, 1 stop." Print "If r is received it complements the status of signal dir." Print "Jumper J7 must be in 1-2." Input "Baud Rate= " , Br Input "Stop Bit= " , St ' Sets P2.0 as output (push pull) P2m1.0 = 0 P2m2.0 = 1 S0 = Disable_422_485 P2.0 = 1 Call Ritardo(10) ' Wait end of tx of last char Call Iniser(br , St) ' Initialize serial with user input Chser = 0 ' To avoid exiting immediatly Do Call Rxser ' Check for char received If Chav = 1 Then ' IF char available Call Txser(chser) ' Send it If Chser = "r" Then If S0 = Disable_422_485 Then S0 = Enable_422_485 P2.0 = 0 Else S0 = Disable_422_485 P2.0 = 1 End If End If End If Loop Until Chser = "F" ' Wait to receive F Call Ritardo(10) ' Wait end of tx of last char Call Iniser(19200 , 1) ' Initialize serial for console End Sub ' ' Sends through hardware serial line of the card the character in ' parameter char. Sub Txser(chser As Byte) Bitwait Scon.1 , Set ' Wait for transmitter free Scon.1 = 0 ' Reset bit of transmission completed Sbuf = Chser ' Send character End Sub ' ' ' Check for reception on hardware line of the card and return the ' eventually received character in variable chser; ' also set the boolean flag chav to indicate whether the character is available ' or not. Sub Rxser If Scon.0 = 1 Then ' Check if char received Scon.0 = 0 ' Reset bit char received Chser = Sbuf ' Get char received Chav = 1 Else Chav = 0 ' No char received End If 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