' ********************************************************************** ' * File: uk_BASAVR_054.BAS * ' * Version: 1.1 * ' * Date: 24.11.11 * ' * Development Tools: Bascom-AVR Demo Ver. 1.11.9.1 + * ' * + AVR bootloader grifo(r) Ver. 1.2 * ' * Cards: GMM AM08 + GMM TST3 * ' * Developed by: GRIFO(r) Italian Technology * ' * via Dell'Artigiano 8/6 * ' * 40016 S. Giorgio di Piano (BO) * ' * Tel. +39 051 892052 Fax. +39 051 893661 * ' * http://www.grifo.com http://www.grifo.it * ' * Author: Gianluca Angelini * ' ********************************************************************** ' Example program 054 of BASCOM AVR course. ' It manages an additional asynchronous serial line, with interrupts. ' The Mini Module GMM AM08 is provided with only one hw serial line, connected ' to USART peripheral device, and this program adds a second sw serial line, ' connected to two I/O lines. The sw serial line is managed through the high ' level instructions of BASCOM, joined with interrupt, in order to become not ' suspensive, or in other words, they doesn't stop program execution during ' transmission and reception of characters. ' By comparing a sw serial line to an hw one, many differences came out as ' limits on communication speed (Baud rate), a really higher execution load ' for the microcontroller and the denied possibility to transmit and receive ' contemporaneously. ' The program supports the following operations on sw serial line: ' - comunicate with 4800 Baud, 8 Bits x chr, 1 Stop bit, No parity physical ' protocol; ' - each received character is recogized by an interrupt, then received and ' saved inside a circular receive buffer; ' - the received characters in a not suspensive mode, are displayed on console; ' - transmit the characters typed on console. ' About electric protocol, the sw serial line of Mini Module is at TTL level ' and normally it must be buffered in order to communicate with other external ' serial devices. For this scope we remind the MSI 01 interface that convert a ' TTL serial line in RS 232, RS 422, RS 485 o Current Loop electric standard. ' The user can comunicate with sw serial line by using a console provided of ' monitor and keyboard, connected to hw serial line, with a fixed physical ' protocol at 19200 Baud, 8 Bits x chr, 1 Stop bit, No parity. ' This console can be another system capable to support a serial RS 232 ' communication. In order to simplify the use it can be used a PC provided of ' one COMx line, that execute a terminal emulation program as HYPERTERMINAL or ' the homonym modality provided by BASCOM AVR (see IDE Configuration). ' The program works only when the GMM AM08 is mounted on Z2 socket of GMM TST3!! ' ' Added instructions: ON INT1, CONFIG INT1, ENABLE INT1. ' ' 24/11/11: uk_BASAVR_054.BAS - Ver 1.1 - By G.A. ' First version. ' ' '**************************** IDE Configurations ******************************* ' NOTE: in order to coorectly use this demo program, please execute the following ' steps: ' 1) Check the availability of M8DEF.DAT file into the directory where the ' BASCOM AVR is installed, copy it if not present and then restart the IDE. ' 2) Into the window "Options | Compiler | Chip" set: ' Chip: m8def.dat ' XRAM: None ' HW Stack: 64 ' Soft Stack: 32 ' Framesize: 64 ' XRAM waitstate: disabled ' External Access Enable: disabled ' 3) Into the window "Options | Communication" set: ' COM port = the PC line connected to GMM AM08, through GMM TST3 ' Baudrate = 19200 ' Parity = None ' Databits = 8 ' Stopbit = 1 ' Handshake = None ' Emulation = TTY ' Font = Terminal, Normal, 12 points, white colour ' Backcolor = Navy ' 4) At the end of compilation, after the code is programmed on GMM AM08, open ' the terminal emulation window of BASCOM AVR with the option: Tools | ' Terminal emulator (Ctrl+T) and then reset or powen on the Mini Module. '************************* Compiler directives ********************************* $regfile "M8DEF.DAT" ' Definitions file for used microcontroller $romstart = &H0 ' Code start address on FLASH $crystal = 7372800 ' Microcontroller crystal frequency $hwstack = 64 ' Hardware stack space $swstack = 32 ' Software stack space $framesize = 64 ' Frame space $map ' Generate debug information $baud = 19200 ' Serial communication speed: 19200 Baud ' Other parameters fixed to: 8 bit x chr ' 1 Stop bit ' No parity '******************************* Definitions *********************************** ' The resources used by program are connected as described in following table. ' !!! Note: On GMM TST3 the jumpers must be configured as below described: ' J1 in 2-3 ; J2 in 1-2 ; J3 in 1-2 ; J5 in 2-3 ; J7 in 2-3 ; J8 in 2-3 ' J9 in 2-3 !!! ' ' Software GMM TST3 pin Z2 pin Signal Used uP ' serial line resource GMM TST3 GMM AM08 GMM AM08 signal ' RX SW CN4.16 24 18 PD3 INT1 PD3 ' TX SW CN4.13 23 17 PD4 T0 PD4 ' ' Signal pin COMx pin CN5 pin Z1 pin Signal Signal ' PC DB9 GMM TST3 GMM TST3 GMM AM08 GMM AM08 uP ' TX 3 3 9 3 RxD RS232 PD0 ' RX 2 2 10 4 TxD RS232 PD1 ' GND 5 5 20 14 GND - ' This table shows that the connection cable between PC COM line and CN5 of ' GMM TST3 is a normal pin to pin cable or direct. Grifo(r) can supply it by ' requesting the CCR 9+9E code. Pinrx Alias Ddrd.0 ' Bit with direction signal connected to GMM AM08 Rxd Pintx Alias Ddrd.1 ' Bit with direction signal connected to GMM AM08 TxD '************************* Constants declaration ******************************* '************************* Variables declaration ******************************* Dim Crxsw As Byte ' Character received from sw serial line Dim Ctxsw As Byte ' Character to transmit on sw serial line Dim Rxcsw As Byte ' Character received by interrupt Dim Bufrx(5) As Byte ' Receive buffer for 5 characters Dim Wpntrx As Byte ' Write pointer to receive buffer Dim Rpntrx As Byte ' Read pointer to receive buffer Dim Isrxsw As Bit ' Flag for character received from sw serial line Dim Key As Byte ' Key pressed on console '************************ Subroutines declaration ****************************** On Int1 Ricev_sw ' INT1 service routine Declare Sub Iniser_sw() ' Initialize and arrange the sw serial line use Declare Sub Closeser_sw() ' Close and finish sw serial line use Declare Sub Isrxser_sw() ' Manage sw serial line reception Declare Sub Txser_sw() ' Transmit character on sw serial line '****************************** Main program *********************************** Main: Pinrx = 0 ' Initialize signals for serial communication Pintx = 0 ' as digital inputs Print ' Separate from previous visualization by showing 2 empty new line on console Print Print " Sw serial line management with interrupt on GMM AM08 + GMM TST3" Print "Mount Mini Module on Z2 of GMM TST3 and connect the external serial device" Print "(configured at 4800 Baud, 8 Bits, 1 Stop, No parity) as described in electric" Print "diagram." Print "The program waits the reception of characters from sw serial line, it shows" Print "them on console, and it transmits the keys pressed on sw serial line" Print Print "The reception of sw serial line is not suspensive and the program goes on even" Print "when the serial device is not connected or it doesn't transmit characters!!" ' Initializes the sw serial line for 4800 Baud, 8 Bits x chr, 1 stop Bit, No parity ' on the I/O signals defined in previous table. ' In the program source the following two instructions must be used before each ' other instructions that use the sw serial line. Open "comd.4:4800,8,N,1" For Output As #1 ' Associate signal used as transmission line, to channel 1 Open "comd.3:4800,8,N,1" For Input As #2 ' Associate signal used as reception line, to channel 2 Call Iniser_sw() ' Initialize and arrange the sw serial line use Do ' Begin endless loop Call Isrxser_sw() ' Verify and receive character from sw serial line If Isrxsw = 1 Then Printbin Crxsw ' Shows received characters on console End If Key = Inkey() ' Get possible key pressed on console If Key <> 0 Then ' If key pressed Ctxsw = Key ' Copy key pressed on character to transmit Call Txser_sw() ' Transmit character on sw serial line, associated to channel 1 End If Loop ' End endless loop Disable Interrupts ' Interrupt general disable Call Closeser_sw() ' Close and finish sw serial line use End '*************************** End of main program ******************************* '*********************** Subroutines used by program *************************** ' INT1 interrupt service routine, activated by Start bit of received character. ' This method can be used only when the receive signal of the sw serial line ' (RX SW) is capable to generate interrupt on falling edge. Moreover this method ' makes the reception from the same serial line not suspensive and it frees up ' the microcontroller from useless work. ' Input: Wpntrx = current write pointer to receive buffer ' Output: Wpntrx = updated write pointer to receive buffer ' Bufrx() = receive buffer with received character Ricev_sw: Get #2 , Rxcsw ' Receive chr just started with sw serial, associated to channel 2 Bufrx(wpntrx) = Rxcsw ' Save received chr into the receive buffer Incr Wpntrx ' Increase write pointer in circular manner If Wpntrx > 5 Then Wpntrx = 1 End If Gifr.7 = 1 ' Clear INTF1 flag to erase all the chr's transitions Return ' Initialize and arrange sw serial line use ' Input: None ' Output: Wpntrx = write pointer to receive buffer initialized ' Rpntrx = read pointer to receive buffer initialized Sub Iniser_sw() Wpntrx = 1 ' Initialize write pointer to receive buffer Rpntrx = 1 ' Initialize read pointer to receive buffer Config Int1 = Falling ' INT1 trigger on falling edge Enable Int1 ' Enable INT1 interrupt Enable Interrupts ' General interrupt enable End Sub ' Manage sw serial reception ' Input: Rpntrx = current read pointer to receive buffer ' Output: Rpntrx = updated read pointer to receive buffer ' Isrxsw = flag for character received from sw serial line ' Crxsw = possible received character Sub Isrxser_sw() If Wpntrx <> Rpntrx Then ' Verify if character received through INT1 and saved in circular buffer Crxsw = Bufrx(rpntrx) ' Acquire received character from receive buffer Incr Rpntrx ' Increase read pointer in circular manner If Rpntrx > 5 Then Rpntrx = 1 End If Isrxsw = 1 ' Signalize character received and returned Else Isrxsw = 0 ' Signalize character not received End If End Sub ' Transmit character on sw serial line, by ensuring all interrupts disabled in ' order to mantain timings and correct framing of character under transmission. ' Input: Ctxsw = character to transmit ' Output: None Sub Txser_sw() Disable Interrupts ' General interrupt disable Put #1 , Ctxsw ' Transmit character on sw serial line, associated to channel 1 Enable Interrupts ' General interrupt enable End Sub ' Close and finish sw serial line use ' In the program source the following two instructions must be used after each ' other instructions that use the sw serial line. ' Input: None ' Output: None Sub Closeser_sw() Close #1 ' Close channels associated to sw serial line Close #2 End Sub '******************* End of subroutines used by program ************************