Tuesday, 25 November 2014

Polling

While working with any microcontroller, we generally interface multiple devices. Also, the microcontroller need to communicate with these devices regularly for input/output operations.

Now the question arises as to which device to communicate to? The solution to this lies in Polling.

Polling refers to checking the status of a device(s) continuously in a repeated manner. In other words, a the microcontroller performs certain operations repeatedly to check for any change in the status of the device.

Simple use of polling can be seen in this program:

Disadvantages of Polling:
  • Polling causes a wastage of energy and resource. It causes the microcontroller to perform several operations continuously, even when the system is idle.
However, polling can be useful while writing small programs or when a large number of devices need to be interfaced.

Masking

On the atmega16, to read the input from a port x, we use the PINx command. Now this command reads the input from the entire port.

Now consider a situation where you have connected 8 switches - one to each pin of PORT C.
However, now you want to only read the switches connected to PC3 and PC0. This is where masking comes into picture.

Masking, in simple words is nothing but hiding the information that is not required. In the situation we considered above, we do not require the input from all pins except PC3 and PC0. Therefore, we need to mask these pins.

A simple way to achieve this is to use the AND Mask.

AND Mask:

While using the AND masking technique, we perform a bitwise AND operation on the input we recieve from PINx command. We AND the input bit with 0 if the bit needs to be masked, else we AND it with 1.
Considering the above example, since we require the input only from PC3 and PC0, we mask all other bits by ANDing them with 0. We AND PC3 and PC0 with 1. Hence, the output that we get will contain all bits as 0 except PC3 and PC0, where these two bits will have the values read from the PINC command.


Masking techniques can not only be used while taking inputs, but also while reading bits from certain registers and providing output as well.

Use of masking technique can be seen here:
http://avrlogic.blogspot.in/2014/11/switch-interfacing.html



Debounce

When we interface a switch with a microcontroller, one end of the switch is connected to ground, while the other end is connected to the pin and to Vcc through a resistor.

So when the switch is open, we get a constant logic level (HIGH). Now as soon as we press the switch, there is a fluctuation in the voltage at the input pin. This is known as bounce.
This can be explained with the following figure.




The figure shows a constant logic level HIGH initially. Now as soon as the switch is pressed, there is a fluctuation as the levels change quickly between LOW and HIGH for a small duration of time before giving a constant value again.

Now this posses a problem, as these changing levels can be interpreted as multiple switch presses by the microcontroller.

Therefore, to overcome this problem, we run another switch press-checking loop within the first checking loop after a small delay (about 40 ms). If the switch continues to be pressed after this small delay, only then is the switch press registered. Else, the fluctuation is discarded. This is known as Debounce.

A practical usage of debounce can be found here:
http://avrlogic.blogspot.in/search/label/5.%20Switch%20Interfacing

Monday, 24 November 2014

Hangman Game - Using Atmega328 and Nokia 5110 LCD

Have you ever wanted to make your own video game? This project tutorial will help you do just that!


Components Required:
  • AVR usbasp programmer                                    X 1
  • Atmega328                                                          X 1
  • Push Buttons                                                       X 5
  • Resistors
    • 1 K ohm                                                    X 11
    • 2.2 K ohm                                                 X 7
    • 10 K ohm                                                  X 1
  • Capacitors
    • 22nF                                                          X 2
  • 16 Mhz Crystal Oscillator                                  X 1
  • Nokia 5110 LCD Module                                   X 1
  • Male Header strip                                               X 1
  • Female header Strip                                            X 1
  • Buzzer (optional)                                                X 1 
  • Vero board                                                          X 1
  • Wire  
Circuit Diagram:

Solder the above shown circuit on a vero board. You could also make the circuit on a breadboard. However, due to the large number of connections, it is advisable to use a vero board.

Use the male headers for the programmers pins, and use female headers for the LCD Display pins.

If your LCD module does not have male headers soldered to it, go ahead and solder that too. Your LCD module should look something like this-


You can also connect a buzzer to the Atmega328.We have connected the buzzer between pin 15 and ground.

Code:
Once the circuit is ready, it is time to burn the code. We have written the code in programmer's notepad. You can follow the steps to burn the program from the following link-

http://avrlogic.blogspot.in/search/label/4.%20Programming%20Atmega16%20-%20LED%20Blink

However, take care to change the atmega chip from atmega16 to atmega328 in all steps. Rest of the procedure remains the same. 



After saving the code, we now place the header files. These can be downloaded from the following link:

Header files

Extract the files and place them in the same directory as your code.

Compile and build the program. Then burn it to the atmega chip.

Your Hangman game should start working!

Notes:
If you want to change the graphics, or make your own pictures, run the FastLCD Program. Design your desired image, and follow the steps given in the word file.
Include these header files in your program, and call them using the
<object name>.printPictureOnLCD(<name of  header file>);    command.


Sunday, 23 November 2014

Switch Interfacing

In the previous post we learned how to blink an LED using Atmega16. In this post we will see how we can interface push button with the microcontroller.
Our objective will be to control the glowing of an LED with the help of a push button.The following is the circuit diagram:
Circuit Diagram

In this circuit the push button S1 is connected to PD0 pin of  PORTD and the LED is connected to the PC0 pin of PORTC.

Start by including the required header files and declaring PORTC as output and PORTD as input port.
As you can notice in the code, we have activated the internal pull-up resistance on port D. To know more about activating pull-up resistance, please refer to the following article-
http://avrlogic.blogspot.in/2014/11/pull-up-resistance.html

Now to detect if the switch is pressed or not, we use an if statement.
Just as PORTx command is used to write a logic level to port x, PINx command is used to read the logic level of a port x.
Now, since the switch is connected to PD0, we mask all bits of PORTD, except PD0 and check its logic level. If the logic level is LOW, the control goes within if statement.
The logic levels on PORTC are then inverted. A delay of 40 ms is added to remove debounce.
Hence, on pressing the switch once, the LED will start glowing. On pressing it again, the LED switches off.
The detection of the switch being pressed is placed within an infinite loop since we want this operation to continue indefinitely. This method of checking continuously is known as Pollling.


Therefore, we have successfully interfaced a switch with the Atmega16 microcontroller.



Pull-Up Resistance

To understand what a pull-up resistance is, we take a look at the following figure .

One end of the switch is connected to ground while the other end is connect to Vcc through a resistance R1 (known as pull-up resistor). This end is also connected to the microcontroller. So, the effective voltage at the input pin, when the switch is open, is Vcc. Now, as soon as the switch is pressed, the current will flow through the switch towards the ground, effectively giving us a LOW logic level at the input pin.Whenever the switch is open, the resistance R1 drives the input pin to HIGH logic level. This action is called pull-up.

Interestingly, the Atmega16 chip comes with a built-in pull-up resistor,i.e, we are not required to place an addition resistor and Vcc connection physically. This can be easily done with a single line of code. This is known as activating pull-up resistance.
To activate pull-up resistance in atmega16, first declare the port as an input port and then write HIGH on that port.

Saturday, 22 November 2014

Programming Atmega16 - LED Blink

Now that we have everything in place we will begin writing our first program.
In this program, we will blink an LED.

Circuit Diagram

As you can see in the circuit diagram, the anode of the LED has been connected to pin 22(that is PC0), through a resistor of 1K ohm. The cathode is connected to the ground. Therefore, to light up the LED, we need to send a HIGH signal on the PC0 pin. This will forward bias the LED and it will start glowing.

STEP 1 : Programmers Notepad:
Open programmers notepad. Write your code here. Start by including the header files and declaring input and output ports.
Below is an example code that you can use:

This code will turn the LED on and off in intervals of 1 second (1000 ms).

We need to include avr/io.h for all input and output operations.
util/delay.h is included to make use of the delay function.

All programs must contain a main function. Inside the main function, we first declare PORTC as output. This is done by use of the DDRx command. DDR(Data Direction Register) is used to define input or output. If the DDRx register is set to HIGH, it defines a port as output. If it is set as LOW, that port acts as input port.

PORTx command is used to write a logic level on the desired port. We write HIGH to PORTC to make all pins high. _delay_ms() is used to provide a delay. The parameter within this function determines the time delay. 1000 represents 1000 milli seconds. We then write LOW on PORTC for another 1000 ms. This will repeat indefinitely as these statements have been put inside an infinite while loop.

Once you have written the code, save it in a folder. Save the file with the extension '.c'. Minimize the programmers notepad.

STEP 2: M-FILE:
Click on start, and search for Mfile. Open this application. This application was installed along with programmers notepad, within the winavr package.
Mfile application
The Mfile application should be similar to the above picture. Now perform the following steps:
  1. Click on Makefile -> Main File name
    Type the same name as the file you saved in programmers notepad (without the extension)
  2. Click on Makefile -> MCU Type -> ATmega -> atmega16
  3. Click on Makefile -> Output format -> iHex
  4. Click on Makefile -> Programmer -> stk500v2
  5. Click on Makefile -> Port -> usb
  6. Click on File -> Save as
    Save it as the name Makefile in the same directory as the .c file that you saved earlier.
STEP 3 : Programmers Notepad:
Once you have saved the Makefile, you need to compile and build the program. Open programmers Notepad and click on Tool -> Make all.
This will compile and build your program. If any error is found, it will be displayed at the bottom of the screen. Incase of any errors, correct your code and compile and build the program again from the tools menu.

STEP 4 : AVRdude-GUI :
Now, connect the programmer to you circuit and your computer. 
Once your program has been successfully compiled and built, open the AVRdude-GUI application.
Under the 'Setup' tab, choose programmer as usbasp.
Under the 'Programming' tab, select target device as Atmega16. File format to use as Intel Hex.
Under the FLASH option, browse to the folder where you saved your code file. Here, you will notice many file have been generated after compilation. Select the file with the .hex extension.
Click on program.


You have now successfully programmed the Atmega16. You should be able to see a blinking LED.
You can now write different codes and have numerous LEDs connected to your microcontroller.

The above steps will be used for programming the atmega16 throughout this blog. We will be referring to this as burning the program in all future posts.

Getting Satrted

To start programming the Atmega16 microcontroller, we first need to get some hardware ready and some software installed.
There are 3 basic things required-

Hardware:
    • Programmer - we will be using AVR USBasp for this purpose throughout this blog.
Software:
    • Software for writing code - we will be using 'programmers notepad'.
    • Software for burning the code - we will be using 'AVRdude-GUI'.
AVR USBasp:


Connect the programmer to your computer. The computer will start searching and install the drivers on its own. Incase the driver is not found, you may download it from the following link and install it manually.


The programmer has 10 pins out of which 6 will be connected to the microcontroller.
These pins are :  
  • MISO
  • MOSI
  • SCK
  • RESET
  • GND
  • VCC
All the above pins will be connected to their respective pins on the microcontroller. The pin configuration of Atmega16 can be found here.


Programmer's Notepad:
To write the codes, we will be using 'Programmers  Notepad'. This is available within the winavr package , which is also required for programming the microcontroller. This can be downloaded from the following link:

http://sourceforge.net/projects/winavr/files/


Programmer's Notepad

Download and install winavr.

AVRdude GUI:
This is the software that we will be using to burn the program onto the microcontroller.
It can be downloaded from:


It does not require installation and can be run directly. Extract the folder and place it on your desktop.




You are now ready to go ahead and write your first program.

Friday, 21 November 2014

Pin Configuration


The above picture shows the pin configuration of the Atmega16 microcontroller. It is divided into 4 I/O ports - A,B,C,D. Each of these ports can be used be used a general input/output port. Also, these ports have some special functions. This is indicated in the brackets along with the pin name.

  • Port A : This port can be used as an ADC port, i.e, this port can be used to convert analog voltage levels to a digital signal.
                   
  • Port B :  This port will be used for programming the microcontroller.
  • Port C : This port is can be used for Two-Wire Interface. We will be using this port for most of the  general I/O operations.
  • Port D : This port can be used programming interrupts and performing USART operations.
The special functions of these ports is will be discussed in upcoming posts.

Pins Vcc, GND, and RESET are used to power the chip.

External oscillator can be connected in between XTAL1 and XTAL2.

About Atmega16

Atmega16 is an 8-bit microcontroller developed by Atmel. It is easily available in the market and can be used to make several projects. Some of the key features of this chip are:
  • 8-bit microcontroller
  • 4 I/O ports-each having 8 pins
  • 16 kB flash memory
  • 8-channel, 10-bit ADC(analog to digital converter)
  • 4-PWM(pulse width modulation) channel
  • SPI and USART interface
  • Operating Voltage : 5V
  • Upto 8Mhz Internal Clock
The above mentioned are just some of the key features that will be used on a regular basis. For detailed information regarding each of these parameters, one can refer to the datasheet.