' ********************************************************************** ' * File: uk_BASAVR_012.BAS * ' * Version: 1.1 * ' * Date: 17.05.10 * ' * 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 012 of BASCOM AVR course. ' It manages a calculator that performs the 4 basic operations, on the RS 232 ' serial communication line of GMM AM08. ' The program requires a formula composed by a first operand, one operator and ' a second operand on the same row and then it shows the result obtained from ' the formula. The operands can have sign and decimal point, up to 8 maximum ' significant digits. ' The formula is inserted through a serial console that shows also the result. ' The console must be provided of a keyboard and a monitor and it must ' communicate with a fixed physical protocol at 19200 Baud, 8 Bit 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 homonimous modality provided by BASCOM AVR (see IDE Configuration). ' ' Added instructions: Dim ... As String, Declare Sub, Instr, Right, Left, Val, ' Sub, End Sub. ' ' 17/05/10: uk_BASAVR_012.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 and copy it if not present. ' 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 J1 jumper must be in position 2-3 and J3 in 1-2. !!! ' ' 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 Operand1 As Single ' First operand Dim Operand2 As Single ' Second operand Dim Result As Single ' Operation result Dim Formula As String * 22 ' String for formula Dim Operstr As String * 11 ' String for operands Dim Operat As String * 1 ' Searche operator Dim Posop As Byte ' Operator position in the formula Dim Lenstr As Byte ' Strings length '************************ Subroutines declaration ****************************** Declare Sub Conv_operand() ' Converts operands from string to number '****************************** Main program *********************************** Main: Pinrx = 0 ' Initialize signals for serial communication Pintx = 0 ' as digital inputs Print ' Separate from previous visualization by showing an empty new line Print " Calculator that performs foundamental operations of a formula" Do ' Begin endless loop Print "Insert formula with first operand operator second operand, with no spaces:" Input Formula ' Get formula Operat = "+" ' Search Sum operator Posop = Instr(formula , Operat) If Posop = 0 Then ' If Sum operator is not available Operat = "*" ' Search Multiply operator Posop = Instr(formula , Operat) If Posop = 0 Then ' If Multiply operator is not available Operat = "/" ' Search Divisor operator Posop = Instr(formula , Operat) If Posop = 0 Then ' If Divisor operator is not available Operat = "-" ' Search minus sign or Subtract operator Posop = Instr(formula , Operat) If Posop = 1 Then ' If there is minus sign on first operand Operat = "-" ' Re-search Subtract operator Posop = Instr(2 , Formula , Operat) If Posop = 0 Then ' If Subtract operator is not available Print "Invalid operator: it accepts + - * /" ' Formula with no valid operator End If End If End If End If End If If Posop <> 0 Then ' Formula with valid operator Call Conv_operand() ' Converts operands into numbers Select Case Operat ' Check operator found in formula Case "+": ' If Sum operator Result = Operand1 + Operand2 ' Perform addition of two operands Case "-": ' If Subtract operator Result = Operand1 - Operand2 ' Perform subtraction of two operands Case "*": ' If Multiply operator Result = Operand1 * Operand2 ' Perform multiplication of two operands Case "/": ' If Divide operator Result = Operand1 / Operand2 ' Perform division of two operands End Select Print Formula ; "=" ; Result ' Show formula and result End If Print ' Separate following operations, by showing a new line Loop ' End endless loop End '*************************** End of main program ******************************* '*********************** Subroutines used by program *************************** ' Converts the two operands available inside the string with formula into the ' two equivalent numbers. ' Input: Formula = string variable with formula ' Posop = position of operator in the formula ' Output: Operand1 = first operand ' Operand2 = second operand Sub Conv_operand() Lenstr = Posop - 1 ' Obtain characters number of first operand Operstr = Left(formula , Lenstr) ' Extract string with first operand from formula Operand1 = Val(operstr) ' Convert first operand string in number Lenstr = Len(formula) ' Obtain characters number of second operand Lenstr = Lenstr - Posop Operstr = Right(formula , Lenstr) ' Extract string with second operand from formula Operand2 = Val(operstr) ' Convert second operand string in number End Sub '******************* End of subroutines used by program ************************