Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Bascom Coding

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

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 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 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
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 RoboGear
Output on LCD will be:
Value of Variable a , Hello
RoboGear
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 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 Robogear
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 Robogear // 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

You might also like