Bascom Coding
Bascom Coding
Bascom Coding
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
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
<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.
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
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