Embedded Systems Lab01 V2
Embedded Systems Lab01 V2
LAB MANUAL
EMBEDDED SYSTEMS
(EE-354) For T.E.(EE)
Instructor name:
Student name:
Roll # Batch:
Semester: Year:
LAB MANUAL
For the course
EMBEDDED SYSTEMS
(EE-354) For T.E.(EE)
Developed by:
Spring, 2023
Approved By
____________________ ____________________
____________________ ___________________
____________________ ____________________
EE-354 Embedded Systems
In this lab, the characteristics of a series and parallel circuits, the fundamental electric circuit laws
and theorems will be learned and verified experimentally. Students will use them extensively
during the entire semester.
You will be communicated Problem Based Learning (PBL) Lab at the start of the session which
will be evaluated at the respective weeks during the session. In these labs, students will be working
in group of 3-5 students to develop, investigate and perform experiments and submit lab report
accordingly.
Lab Assessment:
Labs will be evaluated according to the decided rubrics individually and then the final score will
be calculated. The lab marking rubrics are attached with the rubric based experiments.
After the successful completion of lab tasks, student will be able to:
● Measure Resistance and Capacitance in an electric circuit.
● Verify the equivalent forms of Ohm’s Law.
● Have command on understanding the characteristics of a series and parallel DC circuit.
● Have command on understanding different circuit analysis techniques and laws.
● Have command on understanding the characteristics of an AC circuits.
● Develop ability to think logically and verify the results of developed experiments through
Problem Based Learning labs.
EE-354 Embedded Systems
Individual: >50%
Psychomotor Lab demonstration will be considered as At the end of every
Level 3 which will be Average lab
evaluated as per Cohort level: >=60%
decided lab rubrics will be considered as
Good
CONTENTS
Psychomotor: P3
CLO: Duplicate wiring connections for given circuit design while manipulating the embedded software with
C/Assembly IDE in order to change system behavior.
PLO: Lifelong Learning- PLO 12
10
11
12
LAB SESSION 01
OBJECTIVE:
To set-up the Code::Blocks IDE with AVR toolchain and test an AVR project on the Atmega328p
microcontroller
LAB OUTCOMES:
By the end of the lab, you would be able to:
RATIONALE / BACKGROUND:
Block diagram or flow to understand the need of each,
AVRDUDE
WinAVR (Toolchain)
LAB TASKS
The first two lab tasks guide you to installation of the required IDE and toolchain. The setup files can be
easily found and downloaded from the internet. However, these setup files are available on this shared
folder too.
1) To download the required setup files from the internet, search CodeBlocks and go to
https://www.codeblocks.org/downloads/. Select Download the binary releases. Select the setup
package depending upon your platform like Microsoft Windows in our case. From the given setup files,
select codeblocks-20.03-setup.exe as shown in Figure 1. Click on one of the Download From options
for example Sourceforge.net. For a 32-bit operating system type, you can select codeblocks-20.03-
32bit-setup.exe.
2) Once the setup file is downloaded, click on it to begin the installation process. Select the default option
and follow the installation steps as suggested by the wizard. After a few minutes of decompressing and
install files, click YES when prompted to start Code::Blocks. This should yield the IDE shown in
Figure. For now, select OK if it fails to auto-detect the compiler.
1
Embedded Systems Lab Lab 01 Setting-up the Code::Blocks IDE with AVR Toolchain
NED University of Engineering & Technology Electrical Engineering Department
2
Embedded Systems Lab Lab 01 Setting-up the Code::Blocks IDE with AVR Toolchain
NED University of Engineering & Technology Electrical Engineering Department
3) The Code::Blocks IDE is now ready and would allow you to create projects. Since our aim is to develop
AVR Projects, therefore, we first need to install AVR Toolchain. For this we will make use of WinAVR.
4) Search for WinAVR and go to https://sourceforge.net/projects/winavr/files/latest/download. Click on
Download. It will start downloading the latest WinAVR package.
5) Click on the downloaded file to start the setup wizard. Follow the steps shown in figures and select the
correct features to be installed. The WinAVR package has the required avrdude as well as GNU GCC
Compilers.
3
Embedded Systems Lab Lab 01 Setting-up the Code::Blocks IDE with AVR Toolchain
NED University of Engineering & Technology Electrical Engineering Department
Figure 15: Destination Folder Figure 16: Choose Components for Installation
4
Embedded Systems Lab Lab 01 Setting-up the Code::Blocks IDE with AVR Toolchain
NED University of Engineering & Technology Electrical Engineering Department
6) Before you create and start working on a project, verify that directories are correctly added to the system
path. To do this, open the command prompt by searching cmd in the Windows search option. In the
command prompt, simply write avrdude. You will see the message shown in Figure if it is correctly
added else you will see the error message shown in Figure.
5
Embedded Systems Lab Lab 01 Setting-up the Code::Blocks IDE with AVR Toolchain
NED University of Engineering & Technology Electrical Engineering Department
7) After this write path to verify that the required directories shown in the figure are added to the system
path.
1) Open the Code::Blocks. Go to File> Create >Project…. Select AVR Project and click Go. Select
Next.
2) At this step, you are asked to give project title and select folder to create project files. Give any
suitable name to your project for example here, we have called it Project1. A new folder ES Labs is
created on the desktop and is chosen as project folder. After this, click Next.
3) Now, make sure the selected compiler is GNU GCC for AVR and keep the remaining settings as
shown in the figure. Then click Next.
4) The processor we have selected for ES labs is Atmega328p. Select it from the dropdown menu
carefully and keep the rest of the settings as shown in the Figure. Then click Finish
5) You will see your created project Project1 folder created in the Projects tab workspace. Click on the
Project Name > Sources. This folder will show 2 files; main.c and fuse.c, double click on the main.c
file to open it. It is a blank file template created for the AVR project. This is where you will write a
code.
6
Embedded Systems Lab Lab 01 Setting-up the Code::Blocks IDE with AVR Toolchain
NED University of Engineering & Technology Electrical Engineering Department
7
Embedded Systems Lab Lab 01 Setting-up the Code::Blocks IDE with AVR Toolchain
NED University of Engineering & Technology Electrical Engineering Department
b) Compiler Settings:
Before we write instructions for our first test project, let’s first complete the required compiler settings
that will be needed for all AVR projects.
7) Under the Selected Compiler dropdown menu, select GNU GCC Compiler for AVR and click Set
as default. From the different tabs right below this, select Toolchain executables. For Compiler’s
installation directory, click Auto detect. It should show the auto detected installation path as the one
where WinAVR destination folder was selected earlier. If it fails to do so, you can manually select the
folder by browsing through … option beside Auto-detect. Click OK.
8
Embedded Systems Lab Lab 01 Setting-up the Code::Blocks IDE with AVR Toolchain
NED University of Engineering & Technology Electrical Engineering Department
Figure 28: Specifying the Compiler and Installation Directory in Toolchain Executables
This will update a number of things for these settings. Verify each as shown in the following figures.
Figure 29: Specifying the Program Files in the Compiler’s Installation Directory
The Program Files and Additional Paths tabs under the Toolchain executables will be updated as
shown.
Go to Search directories tab and check that C:WinAVR\avr\include is added in the Compiler tab.
Select Compiler settings tab at the left-most. Check the Optimize generated code (for size) [-Os]
option.
8) Click OK to close the window. The GNU GCC Compiler is set as default with the required compiler
settings needed for now.
10
Embedded Systems Lab Lab 01 Setting-up the Code::Blocks IDE with AVR Toolchain
NED University of Engineering & Technology Electrical Engineering Department
#include <avr/io.h>
#define BLINK_DELAY_MS 1000
#include <util/delay.h>
int main (void)
{
// Arduino digital pin 13 (pin 5 of PORTB) for output
DDRB |= 0B100000; // PORTB5
while(1) {
// turn LED on
PORTB |= 0B100000; // PORTB5
_delay_ms(BLINK_DELAY_MS);
// turn LED off
PORTB &= ~ 0B100000; // PORTB5
_delay_ms(BLINK_DELAY_MS);
}
}
2) Now select Build > Build. For a successful build, the Build Log below will show the following
message with 0 errors and 0 warnings.
11
Embedded Systems Lab Lab 01 Setting-up the Code::Blocks IDE with AVR Toolchain
NED University of Engineering & Technology Electrical Engineering Department
3) Upon successful build, you will see some new folders and files added to the project folder. Go to bin
and verify the addition of .hex file as shown in the figure.
Congratulations! You have generated the output file for our code written in the C language. This .hex
file is the one that will be uploaded to the flash memory of microcontroller atmega328p for execution
of the code.
12
Embedded Systems Lab Lab 01 Setting-up the Code::Blocks IDE with AVR Toolchain
NED University of Engineering & Technology Electrical Engineering Department
1) Connect Arduino UNO USB cable with your system port. The LEDs on your board should light-up to
verify that connection is made. The system might start installing the required drivers. To verify the
correct connection, go to Device Manager of your system, and check Ports (COM & LPT). If the
device is detected as Arduino UNO or USB Serial Device, note the port it is connected to. Here, we
can see Arduino UNO (COM3) so COM3 is the port. In case the system fails to identify the device,
you need to install Arduino Drivers. For this download the drivers from the internet or access through
the shared drive. Extract the folder contents. Then, right click on the device under Ports in the Device
Manager, and select Update Driver Software… and follow the Wizard. You will have to specify the
path of downloaded driver files.
There are 2 approaches to upload the generated code on Atmega328p flash memory. First we will
use AVRDUDE through Command Prompt instructions. Through this, you will be able to understand
the working of AVRDUDE for uploading the file to our microcontroller. Later, we will integrate this tool
to our Code::Blocks to eliminate the manual Command Prompt steps for our ease. Let’s begin the
interesting part of this lab.
13
Embedded Systems Lab Lab 01 Setting-up the Code::Blocks IDE with AVR Toolchain
NED University of Engineering & Technology Electrical Engineering Department
3) Now, type avrdude to verify that it is recognized (as has already been done). It display a list
of options available for usage with avrdude command. Read the description written with
each.
4) For now, we will use only the required ones and the details of which are given below. Note
that these are case sensitive. You can read more about it in
Table 1: avrdude Usage Options and Description
-U Memory operation specification. In our case, flash is the memory type where
Required format is: we want to write the code saved in the
<memory type>:w:<file name> generated Project1.hex file
Where, w shows
read the specified file and write it to the
specified device memory
5) Based on the above, write (type, don’t copy-paste) the following command in the Command
Prompt.
avrdude –p m328p –P com3 –c arduino –U flash:w:Project1.hex
It will display some messages shown in Figure below. For successful upload of the hex file,
you will see the LED blinking at the specified rate.
Figure 40: Uploading the .hex File to Atmega328p through Command Prompt avrdude
14
Embedded Systems Lab Lab 01 Setting-up the Code::Blocks IDE with AVR Toolchain
NED University of Engineering & Technology Electrical Engineering Department
To avoid the time-consuming and intimidating Command Prompt interface, we will now add the
AVRDUDE tool to Code::Blocks. Follow the following steps:
1) In your Code:Blocks IDE, click on Tools > Configure Tools > Add. In the Edit Tool
window, set the Name, Executable, Parameters and Working Directory as shown in Figure.
To set the executable browse the specified path of WinAVR folder, and select avrdude.exe.
Note, that in the parameters option, we have written the same avrdude command used earlier.
The working directory is set to be the one containing the .hex file. Click OK and close the
Tools Window.
2) Now, select Tools from the top menu bar once again. The name of tool just added for
example: avrdude will be available now above the Configure Tools option. Click the
avrdude and that’s it. The Log Window below will show the execution. Once the code is
uploaded to Atmega328p, you will observe the blinking LED.
15
Embedded Systems Lab Lab 01 Setting-up the Code::Blocks IDE with AVR Toolchain
NED University of Engineering & Technology Electrical Engineering Department
3) To make it more generalized, we will now add a tool using macros. Here, instead of
specifying the exact file and project names and path, we will use macros that will do the
job and you won’t have to type it again and again for each of your projects.
4) Go to Tools> Configure Tools. Either Edit the previous tool or use Add to add another
one. This time set the Parameters and Working Directory in terms of macros replacing
project name and path. Click OK.
5) Now, modify the delay in your main.c file by replacing 1000 to 5000 for 5sec delay. Save it and
Rebuild the file using the Build> Rebuild option. The new file will replace the previously generated
.hex file.
6) Now, upload this one by selecting the newly added Tool through Tools>avrdudedirect. Verify the
successful upload by blinking of LED at 5msec delays.
Congratulations! You have successfully created, built and tested your first AVR project on
Atmega328p using Code::Blocks with AVR Toolchain.
16