Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Introduction to Microcontroller Lab v2_042915

The document is a laboratory manual for the CME414 Microcontroller course at the Federal University of Technology, detailing the use of the Atmel ATmega16 microcontroller. It covers various labs including microcontroller introduction, I/O programming, reading switches, and interfacing with LCDs, providing learning objectives, programming examples, and circuit configurations. The manual emphasizes the use of C programming and AVR Studio for project development and simulation.

Uploaded by

ibrahimhammed898
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Introduction to Microcontroller Lab v2_042915

The document is a laboratory manual for the CME414 Microcontroller course at the Federal University of Technology, detailing the use of the Atmel ATmega16 microcontroller. It covers various labs including microcontroller introduction, I/O programming, reading switches, and interfacing with LCDs, providing learning objectives, programming examples, and circuit configurations. The manual emphasizes the use of C programming and AVR Studio for project development and simulation.

Uploaded by

ibrahimhammed898
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Department of telecommunication engineering, Federal university of technology.

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

Why using ATmega16?


1
Department of Telecommunication Engineering, Federal University of Technology, Minna. CME414
Laboratory Manual-Microcontroller
 To support different design requirements, the Atmel AVR family has many
microcontrollers: ATmega8515, ATmega16, etc.
 Microcontrollers in the 8-bit AVR family share a similar instruction set and architecture.
 The ATmega16 has components that are useful for microcontroller applications, such as
 analogue-to-digital converter,
 Pulse-width-modulator.
Why using C?
 It is a high-level programming language: code is understand compared to other
languages.
 C supports low-level programming: We can use C to access all hardware components of
the microcontroller.
 C has standard libraries for complex tasks: data type conversions, standard input/output,
long-integer arithmetic.
 The Atmel AVR instruction set is designed to support C compilers: C code is converted
efficiently to assembly code.
Atmel Studio 6.
Atmel Studio is an integrated development environment for Atmel AVR microcontrollers.
It includes text editor, C compiler, assembler, emulator, HEX file downloader.
It is available for free at ATMEL website.

Development Cycle for C

Step 1: Creating AVR Studio project


 project name
 project type C
 select device
Step 2: Entering a C program.
Step 3: Compiling the C program to produce a HEX file.
Step 4: Downloading/testing the HEX file on Atmel AVR microcontroller.
We’ll illustrate these steps using an example.

Step 1:

Start the Atmel Studio 6.


Select menu File | New Project…
 project type: GCC C Executable Project C/C++
 project name: led
 project location: H:\Work
 option ‘Create directory for solution’
Click ‘OK’.

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
*/

#define F_CPU 8000000UL


#include <avr/io.h>
#include <util/delay.h>
//
int main(void)
{
DDRA = 0x01;
//
PORTA = 0x00;
while(1)
{
//TODO:: Please write your application code
PORTA |= (1<<0);

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’.

 Create a new project in Proteus as discussed in Lab 1.


 Draw in the Proteus environment the circuit shown below:

 Double-click on the atmega16 chip to load hex file.

 Click on ‘play’ button to simulate the circuit.


Practice 2:
 Create a new AVR studio project following the steps as discussed in Lab 1.
 Enter the following code as shown
/*
* instructor_sample2.c
*
* Created: 11/29/2016 1:24:37 PM
* Author: Salihu Aliyu
*/

#define F_CPU 8000000UL


4
Department of Telecommunication Engineering, Federal University of Technology, Minna. CME414
Laboratory Manual-Microcontroller
#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 = 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:

 Double-click on the atmega16 chip to load the hex file


 Click on ‘Play’ to simulate the circuit.

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.

NB: necessary modifications for turning on the leds


#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 = 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

Logic zero activates switch

Logic 1 activates switch


Practice 1:
 Create a new AVR studio project following the steps as discussed in Lab 1.
 Enter the following code as shown
/*
instruct3.c

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
*/

#define F_CPU 8000000UL


#include <avr/io.h>
#include <util/delay.h>
unsigned int i;

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);
}
}
}

 Build the program


 Create your simulation circuit as shown below:

 Double-click on the atmega16 chip to load the hex file


 Click on ‘Play’ to simulate the circuit.
Practice 2:
 Create a new AVR studio project following the steps as discussed in Lab 1.
 Enter the following code as shown
/*

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
*/

#define F_CPU 8000000UL


#include <avr/io.h>
#include <util/delay.h>
#define sw PORTB
#define sw_ddr DDRB
#define ledPort PORTD
#define ledDDR DDRD

int main(void)
{
sw_ddr = 0x00;
ledDDR = 0xff;
sw = 0xff;
ledPort = 0x00;
while(1)
{

//TODO:: Please write your application code


while((PINB & 0b00000001) == 0)
{
for(int k = 1;k<5;k++)
{
ledPort |= (1<<0);
_delay_ms(200);
ledPort &= ~(1<<0);
_delay_ms(200);
}
}
while ((PINB & 0b10000000) == 0)
{
for(int k = 1;k<3;k++)
{
ledPort |= (1<<1);
_delay_ms(200);
ledPort &= ~(1<<1);
_delay_ms(200);
}
}

}
}

 Build the program


 Create your simulation circuit as shown below:

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.

Figure 1: LCD pin description

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.

Figure 2: Circuit connection of 16 by 2 LCD.


The LCD also requires 3 control lines from the microcontroller: enable (E), allows access to the
display through R/W and RS lines, R/W, determines the direction of data between the LCD and
microcontroller, and Register select, helps the LCD to interprets the type of data on the data
lines. The LCD instruction table is summarized in Figure 3.

13
Department of Telecommunication Engineering, Federal University of Technology, Minna. CME414
Laboratory Manual-Microcontroller
Figure 3: Instruction Table

Figure 4: Cursor position and their respective code


Practice 1
 Create a new project from the Atmel studio
 Type in the code as shown below:
/*
* LCD_project.c
*
* Created: 1/8/2016 3:50:32 PM
* Author: Instructor
*/

#define F_CPU 1000000UL


#include <avr/io.h>
#include <util/delay.h>
#define lcd_port PORTD
#define lcd_contr PORTC
void send_string(char* ch);
void send_data(unsigned char ch);
void send_cmd(unsigned char ch);
void lcd_init();
int main(void)
{
DDRD = 0xff;
14
Department of Telecommunication Engineering, Federal University of Technology, Minna. CME414
Laboratory Manual-Microcontroller
DDRC = 0x7f;
lcd_init();
send_string("WELCOME");
_delay_ms(1000);
while(1)
{
//TODO:: Please write your application code
Send_cmd(0x01);
Send_string(“CME414 Student…”);
send_cmd(0xc1);
send_string("Hello Class!!!");
_delay_ms(5000);

}
}
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);
}

 Build the program to generate program Hex file


 Create a new project from the Proteus 8 environment
 Create your circuit diagram as shown in Figure 1.
 Load the generated Hex file and click on run.

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.).

#define F_CPU 800000000UL


#include <avr/io.h>
#include<util/delay.h>
int main(void)
{
while(1)
{
int main(void)
{
DDRA = 0x01;
//
PORTA = 0x00;
while(1)
{
//TODO:: Please write your application code
PORTA |= (1<<0);
_delay_ms(500);
PORTA &= ~(1<<0);
_delay_ms(500);
}
17
Department of Telecommunication Engineering, Federal University of Technology, Minna. CME414
Laboratory Manual-Microcontroller
}

//TODO:: Please write your application code


}
}

18
Department of Telecommunication Engineering, Federal University of Technology, Minna. CME414
Laboratory Manual-Microcontroller

You might also like