07.07.2014 Views

Basic Microcontroller Programming - Shaastra

Basic Microcontroller Programming - Shaastra

Basic Microcontroller Programming - Shaastra

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Autonomous Robotics Tutorial 1<br />

1. INTRODUCTION<br />

A <strong>Microcontroller</strong> is a simple programmable device with which certain tasks can be performed<br />

as are required. A microcontroller can be called a very simplified version of usual computers<br />

having flash memory, a RAM, ALU and other. But obviously the capacity of microcontroller is<br />

much lesser then that of a computer.<br />

A microcontroller has certain pin-outs which are used by it for performing the tasks it has been<br />

programmed to do and also used for its programming. The microcontrollers which are very<br />

commonly used are the Atmega series by Atmel and PIC series, etc. In this tutorial we will<br />

concentrate on the Atmega series. Atmega16 and Atmega 32 are some of the microcontrollers in<br />

the Atmega series that are used on a large scale and are very similar in functioning.<br />

The first task is to download the ATMEGA-16 datasheet. Google for “Atmega16 datasheet”.<br />

Follow the datasheet according to instructions in this tutorial.<br />

2. PIN CONFIGURATION<br />

Now see the pin configuration in figure-1 in page no 2 of datasheet. This microcontroller<br />

contains 4 ports. Pins PA0-PA7 comes under Port A. Similarly PB0-PB7, PC0-PC7 and PD0-PD7<br />

are Port B, Port C, Port D respectively. Pin no 10, 11 and 31 are VCC, GROUND and GROUND<br />

respectively. You have to connect them to +5V, 0V, 0V respectively when you are using<br />

microcontroller in your hardware. Description about remaining pins will be given further.<br />

3. PROGRAM YOUR MICROCONTROLLER<br />

Before learning how to write a code, let us see how to program a micro. AVR STUDIO is the<br />

software used to program a micro. You can download Avr Studio from here. The software<br />

WINAVR can also be used to generate a .hex file, which can be programmed into<br />

microcontroller.


4. AVR STUDIO:<br />

Open AVR studio after installation. You will see a window like this.<br />

Select New project option and move further. You will see the window below.<br />

Create a folder with your project name in your comp.<br />

Select AVR GCC under project type. Name your project in project name column. Give the<br />

destination address for project folder you created in location column and click next. The next<br />

window is as shown below.


Select AVR simulator under debug platform. Select the micro-controller you are using under<br />

device column. I am using ATMEGA 16 so I selected it. Now click on finish option.<br />

Next window is as follows.<br />

Enter your code in the dialogue box at centre. Once the code is done, click on “build” in the<br />

toolbar on the top. Now the hex code will be saved in the default sub-folder of the created folder.<br />

Try it with sample code given below. Paste it in the dialogue box and click on build.<br />

And look for the hex file generated in the folder. This code is used to blink LED'S.<br />

#include <br />

#include <br />

main()<br />

{<br />

DDRA=0xFF;<br />

while(1)<br />

{<br />

PORTA=0b00000000;<br />

_delay_ms(1000);<br />

PORTA=0b11111111;<br />

_delay_ms(1000);<br />

}<br />

}<br />

5. WRITING THE CODE<br />

Now let's see how to write a code.<br />

5.1. Defining Directions<br />

Out of 40 pins on micro 32 pins (Pins in ports A, B, C, D) can be as input or output pins as<br />

defined by the user. User has the comfort of defining any pin as an input or output pin. So in the<br />

beginning of the code you have to define input and output pins as follows:<br />

For example I want to define Port A as input and Ports B, C, D as outputs I will do it as follows:<br />

DDRA=0b00000000 //All pins of port A defined as inputs.<br />

DDRB=0b11111111<br />

DDRC=0b11111111<br />

DDRD=0b11111111 //All pins of ports B, C, D defined as outputs.


In the above commands DDRA means you are calling port A to define.'0b' stands for binary<br />

information. The first zero after '0b' indicates PA7 while the last zero indicates PA0.<br />

Example:<br />

Suppose you want to define PA0-PA3 as input and PA4-PA7 as output, what will you write?<br />

It’s pretty simple. You will write<br />

DDRA=0b11110000.<br />

5.2. Reading inputs and giving outputs<br />

Now let's assume you want the micro to give 5V output at some of the pins in PORTA when<br />

some of the pins in PORTB are given a voltage of 5V (i.e. a logical high or 1). The first thing to do<br />

is obviously to define PORTA as output port and PORTB as input port as was explained in the<br />

previous part. The next part of the code is as follows:<br />

if (PINB==0b00000101)<br />

{<br />

PORTA=0b10101010;<br />

}<br />

else{}<br />

By this command you are asking the micro to read inputs from 'B' and check whether it is equal<br />

to 0b00000101 and if yes, then give output at 'A' as 0b10101010 i.e. PA7, PA5, PA3 and PA1 will<br />

read 5V if checked with a multimeter. So for reading data we have to use PIN command and for<br />

setting outputs we have to use PORT command.<br />

Now let's take up small code as an example and understand it.<br />

#include


when we use sensors.<br />

5.3.2. _delay_ms(1000) command is used to freeze the state of micro. You have to include the<br />

library util/delay.h for using it.<br />

5.3.3. Observe the usage of PORT and DDR commands.<br />

5.3.4. In defining input 0xFF is used. 0x implies that the information following it is in<br />

hexadecimal format and FF stands for 11111111 in binary. Thus it is an equivalent of<br />

0b11111111<br />

5.3.5. The code is included in an infinite loop by using “while”. Just think over it once.<br />

5.4. Example<br />

Blink 4 LED'S in a row such that only one of them switches on while remaining is switched off.<br />

Solution:<br />

#include <br />

#include <br />

main()<br />

{<br />

DDRA=0xFF;<br />

while(1)<br />

{<br />

PORTA=0b00000001;<br />

_delay_ms(500);<br />

PORTA=0b00000010;<br />

_delay_ms(500);<br />

PORTA=0b00000100;<br />

_delay_ms(500);<br />

PORTA=0b00001000;<br />

_delay_ms(500);<br />

}<br />

}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!