You are on page 1of 33

The NEXTSAPIENS Development Board

Version 1.1.1

User Manual
www.nextsapiens.com

The NEXTSAPIENS Development Board is a hand-held, battery-powered


microcontroller board ideal for personal, educational and research robotics
projects. Based on the Atmel ATmega16/32 microcontroller, the NEXTSAPIENS
Development Board includes 16/32K of FlashRAM, outputs for four DC motors or
two stepper motors, inputs for a variety of sensors, and a 16*2 alphanumeric
LCD screen. The NEXTSAPIENS Development Board runs BASCOM-AVR, a
cross-platform, specialized AVR version of the BASIC programming language.
Table of Contents

Section Name

1. Specifications and Feature

2. Ports and Connector Details

3. Installation and Programming Setup

4. Programming in BASIC language

5. Precautions

6. Troubleshooting
1. Specifications and Features

The NEXTSAPIENS Development Board Features are:

• 40 Pin Atmel ATmega16/32 microcontroller with internal system clock upto


8 MHz and externally upto 16 MHz
• 16/32 KB FlashRAM memory for programs
• 1/2 KB of SRAM
• 512/1024 Bytes of EEPROM
• One 6x1 Pin SPI Relimate Header
• Eight 3x1 Pin Relimate header inputs for 8 analog sensors
• One 16 Pin header to connect 16*2 alphanumeric LCD
• Two onboard L293D drivers for motors (upto 600 mA per channel)
• Dual 7805 Voltage regulator
• Dual power input options (Through molex connector or through DC Jack)
• Two programmable Micro-Switches
• Two programmable LEDs
• Two DPDT switches (one for power on/off and one for reset)
• MAX 232 Level shifter for RS232 communication
• One 3x1 Pin relimate header for RS2332 communication
• Four 8 Pin bergistick headers (male) from each port of ATmega16/32
• Wide input power range from 7 volts to 24 volts at 1.5-2 Amps
• Board size of 6 x 3 inches, designed for educational and hobby purpose,
on high quality PCB
2. Ports and Connectors Details

Here is the detailed information of the NEXTSAPIENS Development Board –

Regulated 5v L 293D
Power Output Motor 2 Motor 1 ISP header

DC Jack
Max 232
Regulated
5v Power RS 232
Output
Microswitches

Sensor Sensor
Input Input

Port D Atmega 16 LCD Connector


Port C Port A

Power Switch, 7805 and Power Connector: Power Switch is used to turn on
the power supply of the board. Power will flow to voltage regulators only when
the power switch is in down position. 7805 Voltage regulators will regulate the
power supply to 5 volts. 7805 will work only when the input power is at least 7
volts.
Power supply can be given either through the Power Connector or through
the DC Jack. Be sure to use ONLY ONE power source.
Regulated 5 Volts Power Output: This connector can be used to provide power
to any external device. Taking into consideration, that the board can only
withdraw power up to 2 Amps, power output from this jack may vary.

Reset Switch: This switch is used to reset the program counter to zero and
restart the program execution. In execution mode, Reset switch is needed to be
kept in down position. While downloading the program to the chip, reset switch
should be kept in up position

LEDs: Four LEDs are provided on the board for the testing purpose. These LEDs
can be programmed to glow. Connection details of the LEDs and microcontroller
pins are given below –

LED 1 : PortC.1
LED 2 : PortC.2
LED 3 : PortC.3
LED 4 : PortD.2

Each individual LED can be programmed to glow or to blink by


programming the corresponding microcontroller pin.

Programmable Micro-Switches: These switches are connected with


microcontroller and can be used as input device to the microcontroller. These
switches can be used for testing purpose. Connection details of these pins are
given below –

S1 : PortD.7
S2 : PortC.0

When these switches are pressed, they actually short the corresponding
microcontroller pin with GND, so it is required to use internal pull up registers to
use these switches as input device.

40 Pin Base for microcontroller: This base is provided to install and remove
microcontroller chip easily. This board supports ONLY two microcontrollers –
Atmel ATmega16 and Atmel ATmega32. Both microcontrollers have exactly
same pin configuration but they differ in terms of the memory. For more
information on microcontroller, refer to the respective datasheet.

16 Pin Base for L293D Driver ICs: There are two bases provided onboard to
use L293D H-Bridge with microcontroller. This board supports ONLY L293D
Driver. No other H-Bridge IC can be installed instead of L293D. For more
information on L293D, refer to its datasheet.

Motor Connectors: The NEXTSAPIENS Development Board’s four DC motor


outputs are located bottom left side of the board by means of molex connectors.
These connectors can also be used to drive two stepper motors. These
connectors have following configuration –

MOTOR1
For Connector 1 –
PWM Channel = PWM1B
Direction Bit = PortD.3
For Connector 2 –
PWM Channel = PWM1A
Direction Bit = PortD.6
MOTOR2
For Connector 3 –
PWM Channel = PortC.6
Direction Bit = PortC.7
For Connector 4 –
PWM Channel = PortC.4
Direction Bit = PortC.5

For Connector 3 and 4, PWM is needed to be generated using port C pins 6 & 4.

Eight 3 Pin Headers for Sensors: This board provides capability to connect
upto eight sensors directly to ADC port of the ATmega16/32. ADC need to be
configured and started while working with Analog sensors whereas ADC need to
kept off while working with Digital sensor with same Port A. However both types
of sensors can be used at a time but they both should be different ports, i.e.
analog sensors should be on Port A (with ADC started) and digital sensors
should be on any other port (with that port in input mode).
Given below is the connection details of all eight headers with
corresponding microcontroller pin –

J12 = PortA.7 (or) ADC(7)


J13 = PortA.5 (or) ADC(5)
J14 = PortA.3 (or) ADC(3)
J15 = PortA.1 (or) ADC(1)
J17 = PortA.6 (or) ADC(6)
J16 = PortA.4 (or) ADC(4)
J18 = PortA.2 (or) ADC(2)
J19 = PortA.0 (or) ADC(0)
RS232 Connector: This connector is provided for RS232 communication. Pin
configuration of RS232 is given below –

RX = PortD.0
TX = PortD.1

16 Pin LCD Header: A 16 Pin header is provided to interface one 16 * 2


Alphanumeric LCD. Given below are the pin configuration details –
Lcdpin = Pin
Db4 = Portb.4
Db5 = Portb.5
Db6 = Portb.6
Db7 = Portb.7
E = Portb.3
Rs = Portb.2

These pin configuration are only for the LCD which is 16 * 2 alphanumeric
display. For other type of LCD this may vary. Refer to related datasheet of the
LCD incase if you are using a different one.
3. Installation and Programming Setup

Installation and Setup of Bascom Software for Programming Purpose

Step 1: Download the Software Folder from website www.nextsapiens.com

Step 2: In the Software Folder, you will find the Bascom Setup, Win Driver (drivers for
USB Programmer) and AVR Dude Folder as displayed below

Step 3: Install the Bascom software in your system.

After installation open BASCOM-AVR from:


Start Menu-> MCS Electronics -> Bascom AVR as shown below:

Now we will start writing our Program in Bascom IDE. Programming has been explained
on page no:

Step 4: To write a new program in Bascom IDE, Click File and open a new file.
A blank page will appear on the careen as shown below:
After the user write the code in the IDE environment of BASCOM AVR , the code
should be compiled to check the errors as well as convert the high level language code to
machine code in HEX format .

Step 5: After writing the program, User will compile the program either by pressing F7 or
by clicking Program -> Compile in menu bar.

If your program is correct it will be compiled without showing any error.


Any error present in program will be displayed at the bottom of the page.

By compiling the program, .HEX file will be generated with same file name as of the
Program file name and it will be generated in the same folder/ path where the Program
file is saved (For example: Motor.Bas will generate Motor.Hex after compilation).

NOTE: - This compilation process also creates some other files like .bin, .obj and many
more.
But user don’t have to bother about these files, they are just supportive file, not concern
to user.

After compilation of Program file user have to transfer / burn / download this .hex file to
the hardware chip i.e. Atmega16 Microcontroller
Installation and Setup of AVR Dude for Burning Purpose

Step 1: Going back to the Software folder, now we will install the drivers for USB
Programmer available in “win-driver” folder.

Step 2: Win Drivers: - The Win Driver folder contains drivers for USB programmer as
shown below
- For the first time when the user connects the USB Cable provided in the kit to the
USB Port of the computer/Laptop, the computer will ask to install some
supportive drivers for programmer.
- WinDriver folder contains the file named “lilbus0.dll”.
- User simply has to browse the path of driver location to WinDriver folder and the
computer automatically installs the required drivers.
- The popup message from the taskbar appears with text :- “ The new hardware is
installed successfully and is ready to use”

Step 3: After installing the Window Drivers for USB Programmer, Now user will open
the AVR Dude Folder.

The AVR Dude folder will contain the following files:


AVR Dude folder is having the following files :-
1. Avrdude execution file i.e. avrdude.exe
2. Avrdude config file i.e. avrdude.conf
3. AVR Dude gui file i.e. avrdude-gui

Step 4: User will click the “avrdude-gui”.exe file.


On clicking, following window will appear on the screen.
Configuration of AVRDude GUI

1. Location of avrdude : - User have to browse the location of the


“avrdude.exe “file . This file is provided with your software and user just
has to browse the location of that file.

2. –C Location of alternate configuration: - User has to browse the location


of the “avrdude.conf “file. This file is provided with your software and user
just has to browse the location of that file.
3. – p Device : - User Have to choose the name of the chip that is to be
burned. With NextSapiens Atmega16 controller board, user will select
“ATMEGA16”

4. –c Programmer : - user have to choose The name of USB programmer it


is using . With NextSapiens Atmega16 controller board, user will select
“usbasp”

5. –p Port :- user have to choose the port name / number on which this USB
programmer is attached. With NextSapiens Atmega16 controller board,
user will leave this field as it is, as the software will automatically detect it
and configure it by itself.

6: - Flash Frame :- User have to tick on all the three check boxes i.e. write , read
and verify . This flash depicts the memory where our program will be stored.

And in the text field, user will browse the “.hex” file generated after compiling the
program in “Bascom Software”.

7. EEPROM:- To change the settings of EPROM is optional for users. This


EPROM basically signifies the area where the intermediate date i.e. data
required during processing, is required to be stored. User will leave this field as it
is.

8. LOW FUSE/ HIGH FUSE/LOCK FUSE / Extended Fuse/CALIBRATION /


SIGNATURE/-E Exit Mode/ cycle Counter:-

It should be very noticeable to make changes in this frame. As lock and fuse
bits if configured wrongly can lead to the damage to the microcontroller
(ATMEGA16). User will uncheck all the checked boxes under these specified
headings.
Note: - USER SHOULD CAREFULLY CHECK OFF ALL THE OPTIONS OF
LOCK AND FUSE BITS.

9. Options: - This column is generally for the ease and advancement of practice
to the uses like verbose, verify device signature, etc.
User is required to check the following options while all other options should be
unchecked by the user.
Option 1- “-e Perform a chip erase”
Option 2 - “ –F Verify the device signature”

10:- EXECUTE: - This button is used o transfer / burn the .hex file i.e. code to the
microcontroller (ATMEGA16).

11. Output: - it displays the status of the event generated.


Now how to start with the Development board:
• After writing the program using Bascom Software and compiling it to check the
errors and generate the .HEX file, user will load the .HEX file in AVRDUDE-
GUI software using steps as mentioned above
• Now connect the power supply to power terminal(Power Jack) of the board,

• Now attach one side of the USB programmer to ISP header on the board and
other to USB post of the computer,

• Keep the reset button in up position and on the power button i.e. S3 switch
present on the controller board.

• Make sure that power is coming to the board and power LED is glowing.

• The board is now ready to download the program from the computer.

• Now click the “Execute” button present on “AVRDUDE-GUI” software.

• On completion of the burning process of program to controller board, the output


window will show the status.

• Now the Board is ready to use. Switch of the power supply and disconnect the
USB programmer cable from ISP header on the board.

• Now switch on the board and press down the “Reset” button to execute the burned
program.

4. Programming in BASIC Language using BASCOM-


AVR and Sample codes

4.1 Overview
BASCOM – AVR is an IDE based development platform and is developed
by MCS Electronics. BASCOM uses BASIC programming language. It is very
easy to write, compile and download the program with BASCOM.

4.2 Basics
To write program with Basic language for AVR, start with following
sentences –
1. Define $regfile – instruct the compiler to use the specified register file.

Syntax
$REGFILE = "name_of_file"

“Name_of_file” - It refers to the name of register file. The register files are stored
in the BASCOM-AVR application directory with .DAT extension.

The register file holds information about the chip such as the internal registers
and interrupts addresses.

Since we are using Atmega16 Microcontroller, we will define

$regfile= “m16def.dat” ‘this file is loaded for Atmel atmega16

Note: For Atmega32, we will define $regfile= “m32def.dat”

2 . $crystal – It defines the clock speed at which you want to run your
microcontroller.

Syntax
$CRYSTAL = Value
Value - A numeric constant defining the Frequency of the crystal.

Example –

$crystal = 4000000 ‘ it set the clock speed at 4MHz

3. Config - The CONFIG statement is used to configure the various hardware


devices and other features of microcontroller.
We are required to configure the following hardware and features:

a) LCD
b) ADC
c) Timer
d) Port

(a) Configuring LCD


BASCOM allows us to configure LCD with ease. You can configure various types
of LCDs with BASCOM like 16*2, 16*4, 20*2, 20*4 OR 40*4 alphanumeric LCDs.

The Lcd provided on NEXTSAPIENS Controller Board is of size 16*2 i.e. 16 char
and double line

Syntax for Configuring LCD:

CONFIG LCD = LCD_type

CONFIG LCDPIN = PIN , DB4= PN,DB5=PN, DB6=PN, DB7=PN, E=PN, RS=PN

LCD_type – It is the type of LCD you want to configure. It can be:

40 * 4,16 * 1, 16 * 2, 16 * 4, 16 * 4, 20 * 2 or 20 * 4 or 16 * 1a or 20*4A.

Config Lcdpin - Override the LCD-PIN select options.

To configure a 16*2 alphanumeric LCD of NextSapiens Development Board, the


command is –

Config LCD = 16*2


Config lcdpin = pin, Db4 = Portb.4 , Db5 = Portb.5 , Db6 = Portb.6 , Db7 =
Portb.7 , E = Portb.3 , Rs = Portb.2

(b) Configuring ADC

Syntax

CONFIG ADC = single, PRESCALER = AUTO, REFERENCE = opt

ADC – It defines the Running mode. Its value can be SINGLE or FREE.

PRESCALER - A numeric constant for the clock divider. Use AUTO to let the
compiler generate the best value depending on the XTAL

REFERENCE - Some chips like the M163 have additional reference options.Its
value may be OFF , AVCC or INTERNAL. See the data sheets for the different
modes.

Configuring ADC in BASCOM is also very easy. To configure ADC in


BASCOM for NEXTSAPIENS Development Board, the statement is –
Config Adc = Single , Prescaler = Auto , Reference = Avcc
Start ADC

(C) Configuring Timers to generate PWM

Syntax
CONFIG TIMER1 = COUNTER | TIMER | PWM,
PRESCALE= 1|8|64|256|1024,
PWM = 8 | 9 |10,
COMPARE A PWM = CLEAR UP| CLEAR DOWN | DISCONNECT
COMPARE B PWM = CLEAR UP| CLEAR DOWN | DISCONNECT

TIMER1: It is a 16 bit counter.

PRESCALE - The TIMER is connected to the system clock in this case. You can
select the division of the system clock with this parameter.
Valid values are 1 , 8, 64, 256 or 1024

PWM - Can be 8, 9 or 10.

COMPARE A PWM – It refers to PWM compare mode. It can be CLEAR UP or


CLEAR DOWN

With BASCOM, again it is very easy task. To generate PWM, the statement is –

Config Timer1 = Pwm , Pwm = 8 , Prescale = 1 , Compare A Pwm = Clear


Down , Compare B Pwm = Clear Down
Start Timer1

Timer1 is a 16 bit timer which actually works in two parts, each one of 8 bit,
simultaneously. So the above statement is actually generating two PWMs, PWM
1A and PWM 1B. Same way timer 2 can be configured. Refer ATmega16/32
datasheet and BASCOM help for more information regarding timers and PWM
generation.

(d) Configuring Port

It is use to configure the port or a port pin in order to take output or provide input
to the microcontroller.

Syntax

CONFIG PORTx = state


CONFIG PINx.y = state

State – Input or Output

Example

Config portC=input
or
Config pinC.7 = output

Sample Program defining the above steps:

$regfile = "m16def.dat" // instruct the compiler to use the amtega16 register file.
$crystal = 4000000 //set the internal crystal to 4 MHz.

Config Lcd = 16 * 2 //config the LCD display


Config Lcdpin = Pin , Db4 = Portb.4 , Db5 = Portb.5 , Db6 = Portb.6 , Db7 = Portb.7
, E = Portb.3 , Rs = Portb.2 // config the LCD Type

Config Adc = Single , Prescaler = Auto , Reference = Avcc // config A/D converter

Config Timer1 = Pwm , Pwm = 8 , Prescale = 1 , Compare A Pwm = Clear Down ,


Compare B Pwm = Clear Down //configure timer1 , to generate pwm

Config porta = Input // config port a as input port


Config portb = Output // config port b as output port

4.4 Defining Variables

To define a variable in BASCOM , the syntax is:

Syntax
DIM var AS type
Var- Name of Variable
Type - Bit, Byte, Word, Integer, Long, Single, Double or String

Example
Dim A as Integer
Dim B as String * 8
First statement is defining A variable as integer and second one is defining B
variable as String of 8 characters long. Other than Integer and String there many
data types available in BASCOM. To know more about all data types, refer the
help provided in BASCOM.

4.5 Start Command

This command is use to start the specified device.

Syntax
START device

Device - TIMER0, TIMER1, COUNTER0 or COUNTER1, WATCHDOG, AC


(Analog comparator power) or ADC(A/D converter power)

Example –
Start ADC

4.6 CLS Command

Clear the LCD display and set the cursor to home.

Syntax/ Example –
Cls

4.7 If – Else statement, Loops and Select – case statement


BASCOM allows using all types of loops in the program like do, while and
for. Concept of using these loops is same as using them in other languages like
C. Given below are syntaxes of all loops you can use in BASCOM –
1. Do Loop
Do
<statements>
Loop

2. Do Until Loop
Do
<statements>
Loop until (condition)

3.While Loop
While (condition)
<statements>
Wend

4.If – else statement


If (condition) then
<statements>
else
<statements>
endif

5.If – elseif – else ladder statement


If (condition) then
<statements>
else if (condition)
<statements>
else
<statements>
endif

6.For loop
For (varname) = (starting point) To (end point)
STEP (value)
<statements>
Next

Example of For loop –

For A = 1 To 5 STEP 1
Print “Hello”
Next
7. Case – Select Statement
select case varname
case (test1 varname)
<statement>
case (test2 varname)
<statement>
case else
<statement>
End select

4.8 GETADC command

This command is used to take input from the analog sensor connected to the
development board.
This command retrieves the analog value from channel 0-7 of port A. The range
of analog value is from 0 to 1023.

Syntax
var = GETADC(channel [,offset])

Var - The variable in which the value will be stored.


Channel – It is the pin no of port A to which anolog sensor is connected.
Offset – It is an optional numeric variable that specifies gain or mode.

Example
L = Getadc(2)

Here, in above example, the analog value of the input provided by the sensor
connected to pin 2 of port A is stored in variable L.

4.9 LCD Command

It is used to display a constant or variable on LCD screen.

Syntax
LCD x
X - Variable or constant to be displayed on LCD

For displaying string / text , Use LCD “text”

For displaying variable, Use LCD A ( A refers to the variable)

For displaying text/variable in next line ,We use command LOWERLINE

Example
Lcd a; “ hello”
Lowerline
Lcd “ Nextsapiens”

Output on LCD will be:


“Value of Variable a “, Hello
Nextsapiens

4.10 Waitms command

Suspends program execution for a given time in mS.

Syntax
WAITMS mS
Ms-The number of milliseconds to wait. (1-65535)

Example
Waitms 200

4.11 PWMXX command


It is used to set the speed of motor

Syntax
PwmXX = value
xx- it is the channel of a motor
value – any integer value ranging from 0 to maximum speed .

Example
Pwm1a = 180 // refer to Page 6 for details about motor connector

4.12 PORTX.y command

PORTX.y – it is used to set the direction of the motor

Syntax
PORTX.y = value
X.y - ‘X’ as port number and ‘y’ as pin number
Value - 0 for clock rotation and 1 for anti clock rotation

Example:
Portd.3 = 1 // refer to page 6 for details about motor connector
4.13 Controlling Out put Ports

The output ports in NextSapiens Development board are defined as Motor 1 and
Motor 2.

They are use to drive the motors as well they can be used to trigger any other
event.

The speed and direction of motor can be controlled using commands defined in
4.11 and 4.12 i.e defining PWM for speed and configuring PortX.y for direction.

Example:
Pwm1b = 200 - set the speed of motor 1
Portd.3 = 0 - set the direction of motor 1
Pwm1a = 180 - set the speed of motor 2
Portd.6 = 0 - set the direction of motor 2

Important Note: Here motor 1 and motor 2 refers to DC motors connected to


molex connector with name Motor 1.
Wires of Motor 1 will be inserted into first 2 pins of Molex connector with name
Motor 1 from left side( i.e. from side of ISP)
Wires of Motor 2 will be inserted into rest of the 2 pins of Molex connector with
name Motor 1.
Similarly, we can connect 2 DC motors to Motor 2 molex connectors of
development board.

To move the motor into Backward direction, set PWM1a=0 and PWM1b =0.

Sample program configuring LCD, ADC, Timer and displaying the following
output on LCD screen:
Welcome to Nextsapiens
123

$regfile = "m16def.dat"
$crystal = 4000000

Config Lcd = 16 * 2
Config Lcdpin = Pin , Db4 = Portb.4 , Db5 = Portb.5 , Db6 = Portb.6 , Db7 =
Portb.7 , E = Portb.3 , Rs = Portb.2

Config Adc = Single , Prescaler = Auto , Reference = Avcc

Config Timer1 = Pwm , Pwm = 8 , Prescale = 1 , Compare A Pwm = Clear Down


, Compare B Pwm = Clear Down

Dim A As integer // declaring variable “A” of type integer


A = 123 // assigning the value “123” to the variable “A”
LCD “ Welcome to Nextsapiens” // displaying the text in first line of LCD
Lowerline // configuring the LCD to display the text in lower line
Lcd A // displaying the value of variable “A” in next line.
End // to end the program

Sample program of taking input from a sensor and displaying the Input on
LCD

$regfile = "m16def.dat"
$crystal = 4000000

Config Lcd = 16 * 2
Config Lcdpin = Pin , Db4 = Portb.4 , Db5 = Portb.5 , Db6 = Portb.6 , Db7 =
Portb.7 , E = Portb.3 , Rs = Portb.2

Config Adc = Single , Prescaler = Auto , Reference = Avcc


Config Timer1 = Pwm , Pwm = 8 , Prescale = 1 , Compare A Pwm = Clear Down
, Compare B Pwm = Clear Down

Config Porta = input // Configuring the Port a as input port.


Our sensor is connected to port A. so it has been declared
as Input port.

Dim A As integer // declaring variable “A” of type integer


Start ADC // starting the ADC

Cls // Clear screen


Do // Start the loop
A= GetADC (0) // The input of sensor connected to Pin 0 of port A will be
stored in variable A. Here, in this case, Pin 0 refers to J19
connector of Development Board. Please refer to Page 6 for
pin connection details.
LCD A // Display the value of variable A on LCD

Loop // End of Loop

End // to end the program

Detailed Description of PC Control robot code

$regfile = "m16def.dat" // using atmega16 data file


$crystal = 8000000 // set the crystal 8Mhz
$baud = 9600 // Set the data transfer rate to 9600 baud .
Config Lcd = 16 * 2
Config Lcdpin = Pin , Db4 = Portb.4 , Db5 = Portl = B.5 , Db6 = Portb.6 , Db7 = Portb.7
, E = Portb.3 , Rs = Portb.2
Config Adc = Single , Prescaler = Auto , Reference = Avcc
Config Timer1 = Pwm , Pwm = 8 , Prescale = 1 , Compare A Pwm = Clear Down ,
Compare B Pwm = Clear Down

Dim A As String * 10 //defining a variable ‘A’ as string of length 10 character


Start Adc
Start Timer1
Cls // clear screen

Do // loop start
A = Waitkey() // wait until a character is received

If A = "l" Then // using if for comparison


Print " moving left " // printing on Terminal Emulator Code for Moving Left Motor
Cls //clear screen
Lcd "left" // print on Lcd
Lowerline // shifth the cursor to nextline of lcd
Lcd "NEXTSAPIENS" // printing on next line
Pwm1b = 200
Portd.3 = 0
Pwm1a = 0
Portd.6 = 0

Elseif A = "r" Then // Code for Moving right Motor


Print " moving right "
Cls
Lcd "right"
Lowerline
Lcd "NEXTSAPIENS"
Pwm1b = 0
Portd.3 = 0
Pwm1a = 200
Portd.6 = 0

Elseif A = "s" Then //Code for Moving Stopping Motor


Cls
Lcd "stop"
Lowerline
Lcd "NEXTSAPIENS"
Print " stop moving "
Pwm1b = 0
Portd.3 = 0
Pwm1a = 0
Portd.6 = 0

Elseif A = "f" Then // Code for Moving Motor Forward


Cls
Lcd "forward"
Lowerline
Lcd "NEXTSAPIENS"
Print " moving forward "
Pwm1b = 200
Portd.3 = 0
Pwm1a = 200
Portd.6 = 0

Elseif A = "b" Then //Code for Moving Motor backward


Cls
Lcd "backward"
Lowerline
Lcd "NEXTSAPIENS"
Print " moving backward"
Pwm1b = 0
Portd.3 = 1
Pwm1a = 0
Portd.6 = 1

End If // end of if statement

Loop // end of loop


End // program termination

// program terminate

Detailed description of Self calibrating Line follower Robot code

$regfile = "m16def.dat"
$crystal = 4000000

Config Lcd = 16 * 2
Config Lcdpin = Pin , Db4 = Portb.4 , Db5 = Portb.5 , Db6 = Portb.6 , Db7 =
Portb.7 , E = Portb.3 , Rs = Portb.2

Config Adc = Single , Prescaler = Auto , Reference = Avcc


Config Timer1 = Pwm , Pwm = 8 , Prescale = 1 , Compare A Pwm = Clear Down
, Compare B Pwm = Clear Down
Dim L As Integer , C As Integer , R As Integer
Dim Lw As Integer, Lb As Integer, Lm As Integer, Cw As Integer, Cb As Integer,
Cm As Integer, Rw As Integer, Rb As Integer, Rm As Integer,

Start Adc //start Analog / digital converter


Cls // clear screen
Start Timer1 //start timer1
Lcd “ Calibrate white surface”
Lowerline
Lcd “Place ur bot on white surface”
Waitms 10000
Cls
Lcd “configuring”
Lw = getadc(0)
Cw = getadc (2)
Rw = getadc(1)
Waitms 1000

Cls
Lcd “ Calibrate Black surface”
Lowerline
Lcd “Place ur bot on Black surface”
Waitms 10000
Cls
Lcd “configuring”
Lb = getadc(0)
Cb = getadc (2)
Rb = getadc(1)
Waitms 1000

Cls
Lcd “calculating mean”
Lm = Lw + Lb
Lm = Lm/2
Rm = Rw + Rb
Rm = Rm/2
Cm = Cw + Cb
Cm = Cm/2

Do //loop start
L = Getadc(0)
C = Getadc(2)
R = Getadc(1)
Cls //again clear screen
Lcd L ; "- " ; C ; "- " ; R //display the stored value on the LCD

If L < Lm And C > Cm And R < Rm Then // if condition 1


Pwm1b = 200 // set the speed of motor 1
Portd.3 = 0 // set the direction of motor 1
Pwm1a = 200
Portd.6 = 0
Lowerline //set the pointer of LCD to next line home
Lcd "STRAIGHT"
Elseif L < Lm And C > Cm And R > Rm Then
Pwm1a = 135
Portd.3 = 0
Pwm1b = 200
Portd.6 = 0
Lowerline
Lcd "RIGHT "
Elseif L < Lm And C < Cm And R > Rm Then
Pwm1a = 135
Portd.3 = 0
Pwm1b = 200
Portd.6 = 0
Lowerline
Lcd "RIGHT "

Elseif L > Lm And C < Cm And R < Rm Then


Pwm1a = 180
Portd.3 = 0
Pwm1b = 150
Portd.6 = 0
Lowerline
Lcd "LEFT"
Elseif L > Lm And C > Cm And R < Rm Then
Pwm1a = 180
Portd.3 = 0
Pwm1b = 150
Portd.6 = 0
Lowerline
Lcd "LEFT"
End If // end of if statement

Loop //End of loop


End //end of program / terminate program execution

4.14 Functions and Subroutines


Like C and other programming languages, you can use functions and
subroutines in BASCOM. A function is used at the time when return from that
function is desired. A subroutine is used at the time when return is not required.

Given next are syntaxes for functions and subroutines -

Syntax for declaring a function –

DECLARE FUNCTION funcname (varname as data type)


as data type

Syntax for defining a function –


FUNCTION funcname (var as data type) as data type
<function statements>
End function

Syntax for calling a function –


Varname = call funcname (passing variable name)
OR
Varname = funcname (passing variable name)

Syntax for declaring a Subroutine –


DECLARE SUB subname (varname as data type) as data type

Syntax for defining a subroutine –


SUB subname (varname as data type) as data type
<subroutine statement>
End SUB

Syntax for calling subroutine –


Varname = subname (passing variable name)

5. Precautions
Here are a few things you need to take care to prevent your board being
damaged –

• Always connect the power supply with right polarity, do not interchange
them.
• Give power supply through ONLY ONE channel, either through Molex
Power connector or through DC Jack. Do not use both at same time.
• This board works perfect with current range of 1.5 – 2.0 amps on full load
conditions. So it is advised to supply the rated current.
• Do not short any pin or any soldering on the board.
• In case if you are withdrawing power from the Regulated 5 Volts Power
Output connector, make sure that you are not withdrawing more than 700
mA as well as you are supplying rated current to the board.
• When ever you connect any external device or sensor, don’t forget to
match the exact pins on both sides, i.e. Vcc, GND and Vo all should be
connected to respective pins of the sensors. You may need to refer
datasheet of the sensor or the device you are connecting with.
• Do not use motors which need more than 1.2 amps current as L293D is
capable of upto 1.2 Amps only.
• Always switch off while connecting or disconnecting ISP cable.

6. Troubleshooting

In general, if the above given precautions are being followed, there will not
be problem. However, there may be some exceptional problems. You can go
through the following points if you find your board is not working –

- The Power LED is not turning on when you switch on the power button
o Power supply may not be properly connected to the molex
connector or to DC Jack.
o Polarity may be reversed.
o Input voltage may be less than 7 volts.

- BASCOM is unable to identify the chip


o ISP cable is not connected properly.
o Power button is switch off.
o Reset button is not in up position.
o BASCOM settings are not correct, check them and use
BASCOM help to set them properly.
In case you face any problem other than these, please approach us
through email, we will try to sort that out.

You might also like