Basic Coding

60 downloads 442 Views 1MB Size Report
The ATmega16 microcontroller generally used in IITM is a 40- pin wide DIP (Dual In Line) package chip of the atmega series by Atmel. This chip is robust, and ...
Basic Coding iBOT – Robotics Club of IIT Madras RB002 (Tutorial 2) Microcontroller A microcontroller often serves as the “brain” of a mechatronic system. Like a mini, self-contained computer, it can be programmed to interact with both the hardware of the system and the user. As the computer industry has evolved, so has the technology associated with microcontrollers.

1. Atmega 16 The ATmega16 microcontroller generally used in IITM is a 40pin wide DIP (Dual In Line) package chip of the atmega series by Atmel. This chip is robust, and the DIP package interfaces easily with Breadboards and is useful while making the first circuit for any system.

This same microcontroller is available in a surface mount package, about the size of a dime. Surface mount devices are more useful for circuit boards built for mass production. Figure on the side shows the ‘pin-out’ diagram of theATmega16. This diagram is very useful, because it tells you where power and ground should be connected, which pins tie to which functional hardware, etc. Task: Your first task is to locate the ATmega16 datasheet, and save it for yourself for future reference. Flip through the datasheet once and see what all features atmega16 has. From the datasheet, you can find information about every feature

of ATmega16 and how to use them. In short Datasheet is the bible for any Microcontroller programmer.

2. Pin Configuration Now see the pin configuration in figure 1 of tutorial (Same figure is available in the initial pages of datasheet). The numbers on the inner side of microcontroller in figure tell the pin number on the overall chip. This microcontroller contains 4 ports which are nothing but a set of eight input-output pins. The four ports are Port A (PA), Port B (PB), Port C (PC) and Port D (PD).The number suffixed to the ports (PAn) denotes the pin number of the pin within that port. Eg. Pin 39 - PA1 is the second pin in PORTA (The numbering starts from 1 i.e. PA0, PA1 and so on) Task: Tell the pin number on micro for third pin in PORTD or PA4. Pin no 10, 11, 31 are VCC(5V), GROUND(0V), GROUND(0V) respectively. Other than this some pins also have additional function written in brackets. Task: Find out from the datasheet what additional feature does PORTA have i.e. what does ADC in brackets stands for. Do the same for PD2 or pin 16.

Programming your Microcontroller 1. Compiler

First you should have a compiler which converts your program into the hex code of the microcontroller. If you use C for programming, then you can use WinAVR, CodeVision AVR,

ImageCraft AVR. We will use WinAvr for all the program compilation. Same code is valid for AVRGCC in Linux. Install WinAvr and start Programmer’s Notepad. Shown above is a snap of Programmer’s Notepad. Let's start with first program of blinking LED's on all ports Open Programmer's Notepad and type the program in Folder: blink Program : main.c

2. Makefile Now you have to create Makefile to compile the C program. Makefile is the one which tells compiler about the pin configurations of the microcontroller. Open Makefile.

Then go to MakeFile -> MCU Type and select Atmega16

Rest options are the default most of the time Output format – ihex Optimization – s You also need to mention the programmer type. But Makefile by default doesn’t have USBtiny in the list of programmers. So as for now save the Makefile in same folder Blink with name Makefile .Now to change the programmer type, open the saved Makefile in Programmer’s Notepad. Then go down and you will see Programmer Type. By default it will show a different programmer

Change it to usbtiny. And the save the makefile. And now you are ready to compile the program. Compile by clicking on Tools->Make All And the save themakefile. And now you are ready to compile the program. Compile by

And the save the makefile. And now you are ready to compile the program. Compile by clicking on Tools-> Make All

If the code is free of errors the code will be compiled and the output window will say ‘Process Exit Code: 0’

Now open the folder Blink and you will see new files with extensions .hex, .eep and so on.

2. Programmer Second requirement is a programmer which transfers the Hex code (machine code for AVR) into the chip. The programmer which we will be using is USBTiny. Now connect your programmer and click on Tools-> Program And there it is the Atmega16 all set to command your system!!

Some basic functions of the Atmega16 and how to use them. The most basic function that an Atmega 16 or for that matter any other microcontroller can carry out is taking inputs and giving out outputs. So here our main aim will be learning how to do this and some more additional things.

1. Input-Output The ports of the atmega 16 can be used for input and output. The input can be from a sensor and the output might go to a LED or to a motor driver. The answer is that in order to use them as input or output we have to declare them as input pins or output pins and the Data Direction Register of Port (DDRX) is used to declare this. For Port A this register is DDRA, for port B it is DDRB and so on. The DDRX is an 8-bit register in which each bit corresponds to one pin in the port.

From the figure above you can very clearly understand that the last bit corresponds to PIN A0, second last to PIN A1 and so on. So if you want to declare any pin as input you just need to write the bit corresponding to that pin in the DDRX as 0 and 1 for output. 0 – Input 1 – Output Suppose you want all pins of Port A to be outputs then, DDRA = 0b11111111; would be your line in the code. Similarly for having pins B0 to B3 as input and B4 to B7 as output write DDRB = 0b11110000;

Task: Write the line to declare pins C1, C3 and C4 as output and rest other pins in Port C as inputs. One thing to keep in mind over here is that by default the pins will act as inputs. So you don’t really need to declare a Port or pin as input. But it is a good practice to do so because declaring it becomes a reference for you to see which pins are actually being used as inputs.

2. Header Files For all those who have done their CS110 might remembering always using some line “#include”at the start of the C code. We need such line in our code. These are nothing but header files which are to be included into your code so that the compiler understand the functions being used in the code. E.g. For AVR compiler in the Programmer’s Notepad to understand what DDRA means you need to include the io.h (input-output) file by writing this#include So the rule of thumb is always use this line at the start of your code because it is the most basic header file which is always required by the compiler. Other than io.h there are many other header files which will be required and I will introduce you to them as and when they are required.

3. Assigning output to a pin So once you have declared a pin or port as output you can assign output to it by using PORTX. PORTX register is similar to DDRX with 8 bit data and each bit corresponding to one pin in the PORT. 0 – 0V 1 – 5V Assigning 0 gives output as 0V and 1 gives 5V. So in order to assign 5V to all the pins of Port A PORTA = 0b11111111; For assigning 5V to pin B7 and rest all zero in Port B, PORTB = 0b10000000; Task: Write the line to assign pins C1, C3 and C4 as 5V and rest other pins in Port C as 0V. One important point to be noted is that the binary values that you have been using till now can also be written in hexadecimal format. E.g. PORTA = 0b11111111; . PORTA = 0xFF; PORTA = 0b11110000; . PORTA = 0xF0;

4. Reading Input For reading inputs at the pin after declaring it as input you have to use the PINX register. This register has the values of voltage received at the pins of that X port. Suppose you want to read inputs at Port D then,

f = PIND; where f is any variable. ”f” will store the value of voltages at Port D at that particular instant. More will be illustrated on this in the coming tutorials

5. First Code Your first code will be to blink a LED. You now very well know how to glow a LED. That can be done by assigning 5V to a pin and connecting LED to that pin. ***Code to glow LED*** #include int main(void) { DDRA = 0b11111111; while(1) { PORTA = 0b11111111; } } ***End of Code*** In this code we have used a “while(1)” so that the code runs forever. “1” signifies that the loop will go on forever. Also you can connect the LED to any pin of Port A because all of them have 5V. The only task that is left is to toggle the voltage at that pin at regular intervals. This can be done either by putting empty for loops in between in order that the LED glows for some time and then switches off for some time. ***Code to blink LED*** #include int main(void) { DDRA = 0b11111111; while(1) { PORTA = 0b11111111; for(i=0; i++; i