Getting Startet With Bascom AVR

Introduction: Getting Startet With Bascom AVR

About: I am a Maker since i was 10. After my apprenticeship as Mechatronic Technician i was able to do all these things i want every time to do. I built a 3D printer as the 3D printers just fresh on the maket publish…

This is the beginning of an series to teach you program your AVR microcontroller with Bascom AVR.

Why i am doing this.

Most of the programm samples in this series can you make with Arduino.

Some easier and some more difficult, but at the end both will run on the same controller.

But the way of programming is different in every development environment. Arduino needs a library for everything except the basic functions. Bascom also works with libraries, but I rarely have to include one. With Arduino, all hardware-specific settings are made via the libraries. you have very little influence on the actual power of the microcontroller. Starting with the timers that the controller has. with arduino you need a library again. if you have the timer until it works, it may be another library collides with your settings. At bascom you have free access to the complete hardware including the boot sector that is occupied by arduino. for example, some libraries at bascom ask you which timer you want to use.
on the other hand, since arduino makes it very easy to create a library yourself, it naturally makes it a platform where new hardware and sensors usually have a library directly. what is often associated with a lot of research at bascom and the functions that a library would normally take on must then be painstakingly incorporated into the program code. but good news the bascom community is also very big which is why there is a solution for every idea.

So it depends partly on the project what is used for a development environment and partly on the know-how of the programming person.


but why am I doing this series. on the one hand it saves much money. I don't have to buy an arduino board for every project. For example: A noname Arduino uno costs about 12 € the controller that is on it costs just 2.5 € with the minimum circuitry required for a stable function, it costs around 4 €. on the other hand you have the complete selection of avr chips that are supported available. atmegas 8 to 256 and attiny 8 to 2313 and many xmega types about which I have no experience. If you just want to use a servo and an ultrasonic sensor that can recognize a hand, for example, and then open a lid of a trash can, you can use the smallest possible chip. So there are many reasons to learn a second language.

So let's get started

Supplies

This ist a list of minimum required parts for stable operation of the chip and programming.

Breadboard for testing

Atmega 8-16PU (better you buy 2 or 3 if you kill them by mistake)

7805 5V voltage regulator

10Kohm resistor

100nF film capacitor

10µF electrolytic capacitor

100µF electrolytic capacitor

some wires for breadboard

Windows PC 7/8/8.1/10

ISP Programmer (i will use here the USBasp you can buy it at amazon for little money)

Bascom AVR (you can download here a DEMO. All funktions are unlocked, but you can write code only til 4Kb size that is enough for lots of code).

Optional parts:

LED´s with resistors

push switches

project-specific parts

Step 1: Installation of Bascom and Setup

Download the file and install Bascom AVR. Install all parts of it including the last checkbox after install.

After that reboot your PC otherwise bascom will not start.

After the reboot start bascom.

Go to Options -> Programmer and choose USBasp from the list, save the settings and close Bascom.

Use this Program to install usbasp. After that, reboot your PC again. Now connect the USBasp with your PC and start the device manager. USBasp should appear at the libusb devices.

Stat Bascom again and create a new file. Save it to your pc and hit the F7 button on your keyboard.

The compiler starts and compiles the empty program. Now you can test the functionality of the programmer.

Hit the F4 button on your keyboard to start the programmer window. Now go to chip -> identify to start an interaction. The LED`s from the USBasp should now short blink. You should get a message like chip Id FFFFFF could not read device. That´s a good sign the Programmer is working but it found no chip.

Now we can start to build the first circuit.

Step 2: Let's Take a Closer Look at the Chip

If you look at the pinout of the chip it seems the chip has no similarity to the arduino board. Sure, we use an Atmega8 and on the Arduino uno is an Atmega328. But the Pinout is the nearly the same but he chip of the Arduino Uno board have more functions. Here the names of the pins.
VCC and GND are the pins for power supply.

AREF and AVCC are pins for the reference voltage and power supply for the analog to digital converter.

PB 0-7 PC 0-6 PD 0-7 are general purpose input output pins with multiple occupancy.

reset pin is what the name says. To restart the chip. The line above the reset name means negation. That means, to reset the chip you have to pull it down to 0V.

For the following pins separate instructables comming soon.

RXD TXD are hardware pins for serial communication UART.

INT0 INT1 are Hardware Interrupt pins

XCK /T0 UART Clock source / Timer/Counter0 Clock source

XTAL /TOSC pins are for an external crystal up to 16MHz (different models up to 20MHz) /Crystal pins for an internal RTC

T1 is similar to T0

AIN pins are for the analog comparator

ICP1 is similar to T0/T1

OC1A is the hardware output pin for pwm timer1 channel A

SS /OC2 chip select pin for SPI / like OC1B but channel B

MOSI MISO SCK/ OC2 are the hardware SPI pins and the pins for programming /PWM output timer2

ADC0 to ADC5 are the analog inputs

SDA SCL are the Pins for hardware I2C

The normal chip can work from 4,5V to 5,5V the Atmega 8L can work with much lower Voltage.

You see even this chip can do more than an Arduino Uno seems can not to do. But the Arduino can do it also, you only have to program it.

Step 3: The First Circuit

Now it´s time to build your first circuit.

What is typically the first circuit? Right! Let´s blink a LED.

The LED is connected with PB0. The resistor next to the chip have 10k Ohms.

The resistor next to the LED have 470 Ohms.

Now you can connect the USBasp with the Atmega as shown in the picture.

But before you switch on the power let us write the program.

Step 4: Write the First Program

Create a new file in Bascom and type in the follow text.

$regfile "m8def.dat"

$crystal = 1000000


config portb.0 = output

do
portb.0 = 1
wait 1
portb.0 = 0
wait 1
loop

after that compile it by hitting F7 on your keyboard.

Now we can programm the chip by pressing F4. The programmer window appears. Now it´s time to switch on the power from the breadboard. You should apply something between 6 and 12 Volts.

Now go to chip -> autoprogram. If the programmer window closes automatically the programming was successful.

The LED should be flashing in one second frequency.

Now take a closer look to the program to unterstand the syntax.

$regfile "m8def.dat"
$crystal = 1000000

with $regfile we tell the compiler the type of the used chip the name of the Arduino chip would be "m328pdef.dat"

with $crystal we tell him the cpu speed about 1MHz.

config portb.0 = Output

that means PB0 should act as output.

By the way, the abbreviation PB0 means port B bit 0.
The chip is divided into several ports. Each port is given a letter for clear identification. and each portpin a bit from 0 to 7. For example, I can write a complete byte into the port output register, which will outputted via the individual port pins.

do 
loop

This is what in Arduino the void loop statement means. All between that two commands will repeat forever. (with some exceptions but later more about that)

Portb.0 = 1

wait 1

portb.0 = 0

wait 1

Here we gernerate the blinking of the led.

Portb.0 = 1 tells the chip to switch the output PB0 to 5V

the wait 1 command let the chip wait for one second. If you want to switch the led faster you have to replace the wait command with waitms now you can enter some time now in milliseconds e.g. waitms 500. (waitus means wait in nanoseconds)

Portb.0 = 0 tells the chip to switch the output PB0 to 0V.

Step 5: Add a Button to Use Inputs

Now we add a button to light up the led if the button pressed.

Insert the button as shown in the picture.

now type in the follow program.

$regfile "m8def.dat"
$crystal = 1000000


config portb.0 = output
config portd.7 = input
Portd.7 = 1


do
if pind.7 = 0 then portb.0 = 1 else portb.0 = 0
loop

If you upload that program to the chip, the led only lights up when the button is pressed. But why?

the program starts identically like the last one till

config portd.7 = input. That means, the pin PD7 who connected with button acts as an input.

Portd.7 = 1 does not switch the pin to high, but it activates the internal pull up resistor of the Atmega.

The if statemend looks a bit wierd if you are used to arduino.

if you use the if statement you have to use the "then" statement. In this sample the if statement is used for single command operations. If you want to use more commands you have to write it like this.

if pind.7=0 then
   portb.0=1
   some code
   some code
   some code
else 
   portb.0 = 0
end if

for this use of the if statement you have to use the "end if" statement at the end.

what is still important. Maybe you've already seen it. the inputs are not queried with portx.x, but with pinx.x.
You can easily remember that. Outputs have the "o" (port) in the word and inputs have the "i" (pin).

Now it´s your turn to play a little around.

My next instructable will coming soon (standard statements like while, select case, for, and variables.)

If you like my instructable and want more tell me in the comments.

Be the First to Share

    Recommendations

    • Maps Challenge

      Maps Challenge
    • Fruit and Veggies Speed Challenge

      Fruit and Veggies Speed Challenge
    • First Time Author Contest

      First Time Author Contest

    2 Comments

    0
    abhijit9477896192
    abhijit9477896192

    1 year ago

    Now... that was very very helpful. I am learner in c language and i have seen some really simple programs like... you can read them out loud.. you know :{ well i wonderer what those could be, and now here it is. Most useful is that some small programs can be written in "realtime even by minors and they would never again move to c++ or etc etc.

    0
    schraubenonkel
    schraubenonkel

    Reply 1 year ago

    Hello thanks for that good comment.
    I hope you enjoy the following parts oft this series. The next is coming soon I hope you will enjoy it and I hope you can easy learn to program.