Introduction to Microcontroller Lab v2_042915
Introduction to Microcontroller Lab v2_042915
CME414-
microcontroller lab manual
By Engr. S. O. Aliyu
Introduction to Microcontroller Lab (CME414)
Lab 1. General Introduction
Learning Objectives:
Identify the Atmel ATmega16 microcontroller pinout and configuration
Create a new project in AVR Studio
Create a test circuit using Proteus
Introduction
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. Even the most basic microcontroller can perform simple math operations, control digital
outputs, and monitor digital inputs.
Atmega16 Microcontroller Pin-out
Step 1:
2
Department of Telecommunication Engineering, Federal University of Technology, Minna. CME414
Laboratory Manual-Microcontroller
Lab 2: I/O Programming
Learning Objectives
To configure Atmega16 microcontroller for input or output
To program LED(s) for certain desired animation
To test the program using circuit diagram from Proteus
Introduction
The Atmega16 microcontroller has 32 I/O pins which can either be configured as input or output.
These ports are PA (Port A), PB, PC, and PD, each of which are 8-bit wide. The individual pins
of the port can be address or all the 8-bit address at once depending on the application. Each port
pins consist of three registers: DDxn in DDRx register, PINxn in PINx register and PORTxn in
PORTx register. The DDxn bit select the direction of the pin.
DDRx – Data Direction Register
PINx – input address register
PORTx – Data register (Note: x denotes A, B, C or D).
Data Direction Register (DDRx) is used to configure a specific port pin as output (1) or
input (0).
Data register (PORTx) is used to write output data to port.
Input register address is used to read input data from port.
Practice 1:
Create a new AVR studio project following the steps as discussed in Lab 1.
Enter the following code as shown:
/*
* instructor_sample.c
*
* Created: 11/26/2016 1:10:46 PM
* Author: Salihu O. Aliyu
*/
3
Department of Telecommunication Engineering, Federal University of Technology, Minna. CME414
Laboratory Manual-Microcontroller
_delay_ms(500);
PORTA &= ~(1<<0);
_delay_ms(500);
}
}
Click on F7 to build the program or from the Menu bar, click on ‘Build’, then ‘Build
Solution’.
int main(void)
{
led_ddr = 0xff;
led_port = 0x00;
//x = 0x55;
led_port = 0x55;
while(1)
{
//TODO:: Please write your application code
led_port = ~led_port;
_delay_ms(200);
}
}
Build the program
Create your simulation circuit as shown below:
5
Department of Telecommunication Engineering, Federal University of Technology, Minna. CME414
Laboratory Manual-Microcontroller
Exercise:
1. Write a c/c++ program to count from 0 to 255 and display the count on 8 LEDs
connected to PORT B. Insert 1 seconds delay between each count. Simulate your
program using Proteus.
2. Eight (8) LEDs are connected to PORT D, Write a c/c++ program to turn on the LEDs in
sequence starting from the most significant bit to the least. Insert 500ms delay between
each turn ON and OFF process.
3. Connect 3 LEDs (Green, Red, and Yellow) to the microcontroller Port and use it to
simulate a simple traffic light system.
int main(void)
{
led_ddr = 0xff;
led_port = 0x00;
//x = 0x55;
led_port = 0x55;
while(1)
{
//TODO:: Please write your application code
led_port = ~led_port;
_delay_ms(200);
}
}
#include <avr/io.h>
#include <util/delay.h>
#define led_port PORTC
#define led_ddr DDRC
unsigned int x;
int main(void)
{
led_ddr = 0xff;
led_port = 0x00;
//x = 0x55;
led_port = 0xff;
while(1)
6
Department of Telecommunication Engineering, Federal University of Technology, Minna. CME414
Laboratory Manual-Microcontroller
{
//TODO:: Please write your application code
led_port = ~led_port;
_delay_ms(200);
}
}
#include <avr/io.h>
#include <util/delay.h>
#define led_port PORTC
#define led_ddr DDRC
unsigned int x;
int main(void)
{
led_ddr = 0xff;
led_port = 0x00;
//x = 0x55;
led_port = 0xaa;
while(1)
{
//TODO:: Please write your application code
led_port = ~led_port;
_delay_ms(200);
}
}
7
Department of Telecommunication Engineering, Federal University of Technology, Minna. CME414
Laboratory Manual-Microcontroller
Lab 3: Reading Switches
Learning Objectives
To configure Atmega16 microcontroller for input and output
To read from switches connected to the Atmega16 microcontroller
To test the program using circuit diagram from Proteus
Introduction
A switch is an electrical component that can break an electrical circuit, interrupting the current or
diverting it from one conductor to another. Switches have two state ‘OPEN’ or ‘CLOSE’. They
are input devices, therefore the microcontroller ports need to be configure as ‘INPUT’. This can
be done as discussed in Lab 2.
Ways of Connecting Switches to Atmega16 controller
8
Department of Telecommunication Engineering, Federal University of Technology, Minna. CME414
Laboratory Manual-Microcontroller
*
* Created: 12/8/2016 10:02:50 PM
* Author: Salihu Aliyu
*/
int main(void)
{
DDRA = 0x00; // configure as input
PORTC = 0x00;
DDRC = 0xff; // configure as output
PORTA |= (1<<0);
while(1)
{
//TODO:: Please write your application code
i = PINA & 0x01;
if (i == 0)
{
PORTC |= (1<<0);
_delay_ms(200);
PORTC &= ~(1<<0);
_delay_ms(200);
}
}
}
9
Department of Telecommunication Engineering, Federal University of Technology, Minna. CME414
Laboratory Manual-Microcontroller
/*
* sw2.c
*
* Created: 12/8/2016 10:53:06 PM
* Author: Instructor
*/
int main(void)
{
sw_ddr = 0x00;
ledDDR = 0xff;
sw = 0xff;
ledPort = 0x00;
while(1)
{
}
}
10
Department of Telecommunication Engineering, Federal University of Technology, Minna. CME414
Laboratory Manual-Microcontroller
Double-click on the atmega16 chip to load the hex file
Click on ‘Play’ to simulate the circuit.
Exercises:
1. Write a c/c++ program to monitor a switch connected to your port of choice. Whenever
the switch is activated, increment a counter and display the count on a seven-segment
display. Initialize your count to 0, whenever the counter exceed 9, reset it back to 0. Note:
use a common-cathode seven segment.
2. Connect 8 switches and 8 LEDs to port A and port D respectively. Write a program to
continuously monitor the switches and display their status on the eight LEDs.
3. Consider two switches SW1 and SW2 connected to the microcontroller and two LEDs
(red and green). Monitor the switches continuously such that if SW1 is activated, the red
LEDs flashes for 5 times while the green LEDs flashes for 20 times if it is SW2 that is
activated.
11
Department of Telecommunication Engineering, Federal University of Technology, Minna. CME414
Laboratory Manual-Microcontroller
Lab 4: Interfacing with LCD
Learning Objectives
Familiarize with basic LCD pin out and circuit configuration
Interface the LCD to Atmega 16 microcontroller
Display simple message on the LCD
Introduction
A Liquid Crystal Display is an electronic device that can be used to show numbers or text. There
are two main types of LCD display, numeric displays (used in watches, calculators etc.) and
alphanumeric text displays (often used in devices such as photocopiers and mobile telephones). It
also support display of special characters and symbols.
LCD Basics
LCD displays designed around Hitachi's LCD HD44780 module, are inexpensive, easy to
use, and it is even possible to produce a readout using the 8x80 pixels of the display. They
have a standard ASCII set of characters and mathematical symbols. Most LCDs with 1
controller has 14 Pins and LCDs with 2 controller has 16 Pins (Two pins are extra in both for
back-light LED connections). Figure 1 shows the summary of the functions of the pins.
LCDs can either work in 8-bit mode or 4-bit mode. 8-bit mode requires the entire 8-bit data lines
while 4-bit mode requires 4 out of the 8-bit data lines. LCDs are specified based on the number
of characters and lines it can take. Popularly used LCDs includes 16 by 1, 16 by 2, 20 by 2, and
20 by 4 lines etc. However, this does not mean that LCD cannot display more characters than
their design specification. This is possible with the scrolling capability of the LCD.
12
Department of Telecommunication Engineering, Federal University of Technology, Minna. CME414
Laboratory Manual-Microcontroller
LCD Circuit Connection
Figure 2 shows the circuit connection of a 16 by 2 lines LCD to the Atmega16 microcontroller.
13
Department of Telecommunication Engineering, Federal University of Technology, Minna. CME414
Laboratory Manual-Microcontroller
Figure 3: Instruction Table
}
}
void send_string(char* ch)
{
while(*ch)
send_data(*ch++);
}
void send_data(unsigned char ch)
{
lcd_port = ch;
lcd_contr|= (1 << 0); // select data register
lcd_contr &= ~(1 << 1); // select write mode
lcd_contr |= (1 << 2); // activate enable
_delay_ms(10);
lcd_contr &= ~(1 << 2); // high to low
_delay_ms(10);
}
void lcd_init()
{
send_cmd(0x38);
send_cmd(0x0c);
send_cmd(0x06);
send_cmd(0x83);
}
void send_cmd(unsigned char ch)
{
lcd_port = ch;
lcd_contr &= ~(1 << 0); // select command register
lcd_contr &= ~(1 << 1); // select write mode
lcd_contr |= (1 << 2); // activate enable
_delay_ms(10);
lcd_contr &= ~(1 << 2); // high to low
_delay_ms(10);
}
15
Department of Telecommunication Engineering, Federal University of Technology, Minna. CME414
Laboratory Manual-Microcontroller
Practice 2
Write a program to display your name on the first line of the LCD and your matric
number on the second line.
Exercise:
1. Write a program to count 1-1000 and display the count on the LCD.
2. Connect two switch to the Atmega16 microcontroller, (one for up counting and the other
for down counting). Write a program to increment or decrement the count on an LCD
whenever either of the two switches is activated.
3. Write a program to display the name of your department such that it will keep scrolling
on the screen
16
Department of Telecommunication Engineering, Federal University of Technology, Minna. CME414
Laboratory Manual-Microcontroller
Lab 5: Design and Construction
This laboratory practical involves design and construction of typical elementary embedded
system. The topic are listed according to the groups. You are required to implement the project in
the lab under the supervision of the lab technologist. All group members are expected to
participate in the work. This will be graded towards the end of the semester.
Project Topics
Group 1: Design and construction of a four digit digital down/up counter with preset input using
Atmega 16 microcontroller. (Use seven segment display)
Group 2: Design and construction of a digital up/down counter with preset input using Atmega
16 microcontroller. (Use 16 by 2 LCD).
Group 3: Design and construction of intruder detection system using laser light and alarm. Use
Atmega 16 microcontroller.
Group 4: Design and construction of password door lock. (Use Atmega16, keypad and LCD)
Group 5: Design and construction of a visitors counter and room light controller. (Use Atmega
16, Relay, LCD, etc.).
18
Department of Telecommunication Engineering, Federal University of Technology, Minna. CME414
Laboratory Manual-Microcontroller