' ********************************************************************** ' * File: uk_BASAVR_043.BAS * ' * Version: 1.1 * ' * Date: 13.03.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 43 of BASCOM AVR course. ' Servomotor. ' The program move a Servomotor in both directions through the push buttons T1, ' T2 of GMM TST3. By pushing both buttons contemporaneously the seromotor is ' placed on its middle or neutral position (90°) and an audible Bell is ' generated with buzzer. ' The signal selected to drive the Servomotor is the PB3 of Mini Module, ' reported on CN4.9 connector of GMM TST3. ' The program has been tested with the model Servo standard 900-00005 produced ' by Parallax. ' The program describes its functionalities and uses a serial console provided ' of monitor and keyboard 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 homonym modality provided by BASCOM AVR (see IDE Configuration). ' The console is not strictly required, in fact the demo works even without it. ' The program works only when the GMM AM08 is mounted on Z1 socket of GMM TST3!! ' ' Added instructions: None. ' ' 13/03/11: uk_BASAVR_043.BAS - Ver 1.1 - By G.A. ' First version. ' ' '**************************** IDE Configurations ******************************* ' NOTE: in order to correctly 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 jumpers must be set in following positions: ' J1 in 2-3 ; J2 in 2-3 ; J3 in 1-2 ; J5 in 2-3 ; J7 in 2-3 ; J8 in 2-3 ' J9 in 2-3 !!! ' ' Servo GMM TST3 pin Z1 pin Signal Signal ' signal resource GMM TST3 GMM AM08 GMM AM08 uP ' - P.Button T1 12 6 PC5 ADC5 SCL PC5 ' - P.Button T2 13 7 PC4 ADC4 SDA PC4 ' - Buzzer BZ1 15 9 PB4 MISO PB4 ' PCM CN4.9 14 8 PB3 MOSI OC2 PB3 ' ' 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. Pint1 Alias Pinc.5 ' Bit with input signal connected to red button T1 Pint2 Alias Pinc.4 ' Bit with input signal connected to green button T2 Pinbz1 Alias Portb.4 ' Bit with output signal connected to buzzer BZ1 Pinservo1 Alias Portb.3 ' Bit with output signal connected to Servomotor 1 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 ******************************* Const Nrel90 = 85 ' Reload number equal to 90° position Const Nrelmin = 25 ' Reload number equal to minimum position (start rotation) Const Nrelmax = 144 ' Reload number equal to maximum position (stop rotation) '************************* Variables declaration ******************************* Dim T1stat As Bit ' Boolean variable with T1 red button status Dim T2stat As Bit ' Boolean variable with T2 green button status Dim Reln As Byte ' Reload number for duration of pulse on PCM signal used to drive Servo Dim Oldreln As Byte ' Previous reload number for duration of pulse '************************ Subroutines declaration ****************************** '****************************** Main program *********************************** Main: Pinrx = 0 ' Initialize signals for serial communication Pintx = 0 ' as digital inputs Ddrc.5 = 0 ' Initialize signal connected to T1 button as digital input Ddrc.4 = 0 ' Initialize signal connected to T2 button as digital input Pinbz1 = 1 ' Initialize signal connected to BZ1 buzzer as digital output at high level Ddrb.4 = 1 Pinservo1 = 1 ' Initialize signal connected to Servo 1 as digital output at high level Ddrb.3 = 1 Print ' Separate from previous visualization by showing an empty new line Print Print " Servomotor movement with T1 and T2 buttons" Print "Mount Mini Module on Z1 of GMM TST3, connect Servo PCM signal to CN4.9." ' The Servomotor management is performed with the high level instructions of ' BASCOM. These generates a signal that doesn't exactly match the timing ' specifications of the Servo control signal PCM, but they allows anyway a ' right movement. ' The BASCOM instructions use a periodic interrupt generated by TIMER0, that ' can't be used for other functions!!! ' The timing resolution (=duration) is defined by Reload value used in ' Config Servo... instruction and by selected position with Servo... ' instruction, as illustrated in BASCOM on line help. ' This demo define a minimum reload time, equal to 10 usec, in order to avoid ' vibrations and to obtain a maximum resolution on positions. The positions ' are set in the range defined by Nrelmin and Nrelmax constants, that are the ' start and stop positions for rotation without vibrations. Config Servos = 1 , Servo1 = Portb.3 , Reload = 10 ' Define Servo management with BASCOM Enable Interrupts ' General interrupts enable Servo(1) = Nrel90 ' Servo on middle neutral position (half rotation) Reln = Nrel90 Oldreln = Nrel90 Print Print "Press T1 red button to rotate Servo in clockwise direction;" Print "Press T2 green button to rotate Servo in reverse clockwise direction;" Print "Press both buttons contemporaneously to reach central position." Do ' Start endless loop T1stat = Pint1 ' Save T1 red button status T2stat = Pint2 ' Save T2 green button status If T1stat = 0 Then ' If T1 pressed Decr Reln ' Servo on new position with clockwise rotation If Reln < Nrelmin Then ' Ensure validity of new position Reln = Nrelmin End If End If If T2stat = 0 Then ' If T2 pressed Incr Reln ' Servo on new position with reverse clockwise rotation If Reln > Nrelmax Then ' Ensure validity of new position Reln = Nrelmax End If End If If T1stat = 0 And T2stat = 0 Then ' If T1 and T2 pressed Reln = Nrel90 ' Servo on new central position at middle rotation Pinbz1 = 0 ' Enable buzzer for beep End If If Reln <> Oldreln Then ' If Servo position changed Servo(1) = Reln ' Define PCM pulse duration with reload number of new position Oldreln = Reln ' Update previous position End If Waitms 10 ' Generates a delay inside endless loop that define reaction speed Pinbz1 = 1 ' Disable buzzer Loop ' End endless loop End '*************************** End of main program *******************************