Microprocessor and Microcontroller Lab Manual: Government Engineering College Bharuch
Microprocessor and Microcontroller Lab Manual: Government Engineering College Bharuch
Lab Manual
CERTIFICATE
Date:
Participation (2)
Experiments (2)
Implementation
Punctuality (2)
Readability &
Program (2)
Efficiency of
Sr. Start End
Output of
Name of Experiments Marks Sign
Active
No Date Date
(2)
Understand arduino UNO open
9 single LED.
2. Write a C program to blink 8
LED.
3. Write a C program for
scrolling of 8 LED.
4. Write a C program to display
8-bit binary counter value on
8 LED.
Interfacing of switch and LED
with ATmega32
microcontroller.
1. Write a C program to blink
LED by pressing a switch.
11 0 to 9 on single seven
segment.
2. Write a C program to display
00 to 99 on two seven
segment.
Interfacing of LCD with
ATmega32 microcontroller.
A. Program the LCD in 4-bit
mode.
1. Write a C program for display
some message on LCD.
2. Write a C program for
scrolling the message on
12
LCD.
3. Write a C program for LCD
counter.
B. Program the LCD in 8-bit
mode.
1. Write a C program for display
some message on LCD.
2. Write a C program for
scrolling the message on
LCD.
3. Write a C program for LCD
counter.
Interfacing of 4x4 keypad with
13
ATmega32 microcontroller.
Interfacing of ADC with
ATmega32 microcontroller.
1. Write a C program to use
single channel ADC in
14
ATmega32.
2. Write a C program to
interface Temperature
sensor LM35.
Establish Serial Communication
in ATmega32.
1. Write a C program to receive
and transmit character
through serial
communication.
2. Write a C program to receive
15
and transmit character
through serial
communication and also
display the same on LCD.
3. Write a C program to receive
and transmit string through
serial communication.
Program Timer/Counter in
ATmega32.
1. Write a C program to toggle
LED every 100msec by
generating delay using
Timer0.
16
2. Write a C program to turn on
LED connected to PB3/OC0
pin after 4 external clock
pulse.
3. Write a C program to toggle
LED connected to PB3/OC0
pin after 4 external clock
pulse.
4. Write a C Program to
generate square wave by
using Timer0.
Interfacing of motor with
ATmega32.
1. Write a C program to monitor
the status of switch and
Institute Mission
1. Providing State-of-the-art infrastructure by strengthening industry-institute-
interaction to achieve excellence in engineering education.
Department Mission
EXPERIMENT - 1
Introduction to Arduino
The lab will be based on the Arduino Uno. The Arduino Uno is a microcontroller
board based on the ATmega328. It has 14 digital input/output pins (of which 6
can be used as PWM outputs), 6 analog inputs, a 16 MHz ceramic resonator, a
USB connection, a power jack, an ICSP header, and a reset button. It contains
everything needed to support the microcontroller; simply connect it to a
computer with a USB cable or power it with a AC-to-DC adapter or battery to get
started.
Power
The Arduino Uno can be powered via the USB connection or with an external
power supply. The power source is selected automatically. External (non-USB)
power can come either from an AC-to-DC adapter (wall-wart) or battery. The
adapter can be connected by plugging a 2.1mm center-positive plug into the
board's power jack. Leads from a battery can be inserted in the Gnd and Vin pin
headers of the POWER connector.
The board can operate on an external supply of 6 to 20 volts. If supplied with less
than 7V, however, the 5V pin may supply less than five volts and the board may
be unstable. If using more than 12V, the voltage regulator may overheat and
damage the board. The recommended range is 7 to 12 volts.
• VIN. The input voltage to the Arduino board when it's using an external power
source (as opposed to 5 volts from the USB connection or other regulated power
source). You can supply voltage through this pin, or, if supplying voltage via the
power jack, access it through this pin.
• 5V. This pin outputs a regulated 5V from the regulator on the board. The board
can be supplied with power either from the DC power jack (7 - 12V), the USB
connector (5V), or the VIN pin of the board (7-12V).
• 3V3. A 3.3 volt supply generated by the on-board regulator. Maximum current
draw is 50 mA.
Each of the 14 digital pins (pins 0 to 13) on the Uno can be used as an input or
output, using pinMode(), digitalWrite(), and digitalRead() functions. They
operate at 5 volts. Each pin can provide or receive a maximum of 40 mA and has
an internal pull-up resistor (disconnected by default) . In addition, some pins
have specialized functions:
Serial: 0 (RX) and 1 (TX). Used to receive (RX) and transmit (TX) TTL serial
data.
These pins are connected to the corresponding pins of the ATmega8U2 USB-to-
TTL Serial chip.
• PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite()
function.
• LED: 13. There is a built-in LED connected to digital pin 13. When the pin is
HIGH value, the LED is on, when the pin is LOW, it's off.
The Uno has 6 analog inputs, labeled A0 through A5, each of which provide 10
bits of resolution (i.e. 1024 different values). By default they measure from
ground to 5 volts, though it is possible to change the upper end of their range
using the AREF pin and the analogReference( ) function.
• TWI: A4 or SDA pin and A5 or SCL pin. Support TWI communication using
the Wire library.
Arduino Programming
The programs written for Arduino are called sketches. For the sketch to work on
the Arduino Uno, there are two hardware related settings need to be done in the
Arduino IDE –
• Board
• Serial Port
For selecting the board, go to the Tools tab and select Board. From the menu
select Uno.
When you connect your Arduino Uno to the USB port of your laptop, it will be
mapped as a serial port.
To know the serial port to which your Arduino is mapped, follow the following
procedure:
Right click on My Computer
Select the Manage option
In the pop up screen for Computer Management, select the Device Manager
Expand the Ports item; the Arduino Uno will appear as one of the drop down
items
In the Arduino IDE, select the Serial Port as the port to which the Arduino is
mapped.
The basic structure of the Arduino sketch is fairly simple and has two required
functions:
void setup( )
{
statements;
}
void loop( )
{
statements;
}
Where setup( ) is the preparation, loop() is the execution. Both functions are
required for the program to work. The setup function should follow the
declaration of any variables at the very beginning of the program. It is the first
function to run in the program, is run only once, and is used to set pin Mode or
initialize serial communication.
The loop function follows next and includes the code to be executed continuously
reading inputs, triggering outputs, etc. This function is the core of all Arduino
programs and does the bulk of the work.
setup( )
The setup() function is called once when your program starts. Use it to initialize
pin modes, or begin serial. It must be included in a program even if there are no
statements to run.
void setup( )
{
pinMode(pin, OUTPUT); // sets the 'pin' as output
}
loop( )
After calling the setup() function, the loop() function does precisely what its
name suggests, and loops consecutively, allowing the program to change,
respond, and control the Arduino board.
void loop( )
{
digitalWrite(pin, HIGH); // turns 'pin' on
pinMode(pin, mode)
Used in void setup() to configure a specified pin to behave either as an INPUT or
an OUTPUT.
pinMode(pin, OUTPUT); // sets ‘pin’ to output
There are also convenient pullup resistors built into the Atmega chip that can be
accessed from software. These built-in pullup resistors are accessed in the
following manner:
pinMode(pin, INPUT); // set ‘pin’ to input
digitalWrite(pin, HIGH); // turn on pullup resistors
Pullup resistors would normally be used for connecting inputs like
switches. Notice in the above example it does not convert pin to an output, it is
merely a method for activating the internal pull-ups.
Pins configured as OUTPUT can provide 40 mA (milliamps) of current to other
devices/circuits. This is enough current to brightly light up an LED (don't forget
the series resistor), but not enough current to run most relays, solenoids, or
motors.
Short circuits on Arduino pins and excessive current can damage or destroy the
output pin, or damage the entire Atmega chip. It is often a good idea to connect
an OUTPUT pin to an external device in series with a 470Ω or 1KΩ resistor.
digitalRead(pin)
Reads the value from a specified digital pin with the result either HIGH or LOW.
The pin can be specified as either a variable or constant (0-13).
value = digitalRead(Pin); // sets 'value' equal to the input pin
digitalWrite(pin, value)
Outputs either logic level HIGH or LOW at (turns on or off) a specified digital pin.
The pin can be specified as either a variable or constant (0-13).
analogRead(pin)
Reads the value from a specified analog pin with a 10-bit resolution. This
function only works on the analog in pins (0-5). The resulting integer values
range from 0 to 1023.
value = analogRead(pin); // sets 'value' equal to 'pin'
Note: Analog pins unlike digital ones, do not need to be first declared as INPUT
or OUTPUT.
analogWrite(pin, value)
Writes a pseudo-analog value using hardware enabled pulse width modulation
(PWM) to an output pin marked PWM. On Uno, this function works on pins 3, 5,
6, 9, 10, and 11. The value can be specified as a variable or constant with a value
from 0-255.
analogWrite(pin, value); // writes 'value' to analog 'pin'
A value of 0 generates a steady 0 volts output at the specified pin; a value of 255
generates a steady 5 volts output at the specified pin. For values in between 0
and 255, the pin rapidly alternates between 0 and 5 volts - the higher the value,
the more often the pin is HIGH (5 volts). For example, a value of 64 will be 0 volts
three-quarters of the time, and 5 volts one quarter of the time; a value of 128 will
be at 0 half the time and 255 half the time; and a value of 192 will be 0 volts one
quarter of the time and 5 volts three-quarters of the time. Because this is a
hardware function, the pin will generate a steady wave after a call to analogWrite
in the background until the next call to analogWrite (or a call to digitalRead or
digitalWrite on the same pin).
Note: Analog pins unlike digital ones do not need to be first declared as INPUT or
OUTPUT.
The following example reads an analog value from an analog input pin, converts
the value by dividing by 4, and outputs a PWM signal on a PWM pin:
int led = 10; // LED with 220 resistor on pin 10
int pin = A0; // potentiometer on analog pin 0
int value; // value for reading
void setup( )
{
} // no setup needed
void loop( )
{
value = analogRead(pin); // sets 'value' equal to 'pin'
value /= 4; // converts 0-1023 to 0-255
analogWrite(led, value); // outputs PWM signal to led
}
delay(ms)
Pauses a program for the amount of time as specified in milliseconds, where
1000 equals 1 second.
delay(1000); // waits for one second
millis( )
Returns the number of milliseconds since the Arduino board began running the
current program as an unsigned long value.
Serial.begin(rate)
Opens serial port and sets the baud rate for serial data transmission. The typical
baud rate for communicating with the computer is 9600 although other speeds
are supported.
void setup( )
{
Serial.begin(9600); // opens serial port
} // sets data rate to 9600 bps
Note: When using serial communication, digital pins 0 (RX) and 1 (TX) cannot be
used at the same time.
Serial.println(data)
Prints data to the serial port, followed by an automatic carriage return and line
feed. This command takes the same form as Serial.print(), but is easier for
reading data on the Serial Monitor.
Serial.println(analogValue); // sends the value of // 'analogValue'
Note: For more information on the various permutations of the Serial.println()
and Serial.print( ) functions please refer to the Arduino website.
The following simple example takes a reading from analog pin0 and sends this
data to the
computer every 1 second.
void setup( )
{
Serial.begin(9600); // sets serial to 9600bps
}
void loop( )
{
Serial.println(analogRead(A0)); // sends analog value
After entering your program, click on the Verify button for compilation. If there
are errors, the line numbers of the errors are shown in the bottom window.
Correct the errors. After successful verification, upload your program to the
Arduino using the Upload button. A common cause for failure in uploading is that
your Arduino is not connected to a different COM port than the one shown in
the Arduino IDE.
Conclusion:
EXPERIMENT - 2
Interfacing diagram:
Schematic Diagram:
Arduino program:
void setup( )
{
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
OR
int LED = 13;
void setup( )
{
pinMode(LED, OUTPUT);
}
void loop( )
{
digitalWrite(LED, HIGH);
delay(1000); // wait for a second
digitalWrite(LED, LOW);
delay(1000); // wait for a second
}
Observation:
Conclusion:
EXPERIMENT - 3
Aim: Interface switch and LED to arduino UNO and write a program to turn on
LED by pressing switch.
Hardware required: Arduino Uno development board, LED, Resistor, jumper
wire and Switch
Interfacing diagram:
Schematic Diagram:
Arduino program:
int pushbutton = 2;
void setup( )
{
pinMode(LED_BUILTIN, OUTPUT);
pinMode(pushbutton, INPUT);
}
void loop( )
{
int switch_status = digitalRead(pushbutton);
if(switch_status == HIGH)
digitalWrite(LED_BUILTIN, HIGH);
else
digitalWrite(LED_BUILTIN, LOW);
}
Observation:
Conclusion:
EXPERIMENT - 4
Aim: Interface LCD with arduino UNO and write program to display some string
on LCD display.
Hardware required: Arduino Uno development board, 2x16 LCD Display,
Resistor, one potentiometer and jumper wire
Introduction:
The LCDs have a parallel interface, meaning that the microcontroller has to
manipulate several interface pins at once to control the display. The interface
consists of the following pins:
A register select (RS) pin that controls where in the LCD's memory you're
writing data to. You can select either the data register, which holds what goes on
the screen, or an instruction register, which is where the LCD's controller looks
for instructions on what to do next.
A Read/Write (R/W) pin that selects reading mode or writing mode
An Enable pin that enables writing to the registers
8 data pins (D0 -D7). The states of these pins (high or low) are the bits that
you're writing to a register when you write, or the values you're reading when
you read.
There's also a display constrast pin (Vo), power supply pins (+5V and
Gnd) and LED Backlight (Bklt+ and BKlt-) pins that you can use to power the
LCD, control the display contrast, and turn on and off the LED backlight,
respectively.
The process of controlling the display involves putting the data that form the
image of what you want to display into the data registers, then putting
instructions in the instruction register. The LiquidCrystal Library simplifies this
for you so you don't need to know the low-level instructions.
The Hitachi-compatible LCDs can be controlled in two modes: 4-bit or 8-bit. The
4-bit mode requires seven I/O pins from the Arduino, while the 8-bit mode
requires 11 pins. For displaying text on the screen, you can do most everything in
4-bit mode, so example shows how to control a 16x2 LCD in 4-bit mode.
LCD library functions
LiquidCrystal( )
Description
Creates a variable of type LiquidCrystal. The display can be controlled using 4 or
8 data lines. If the former, omit the pin numbers for d0 to d3 and leave those
lines unconnected. The RW pin can be tied to ground instead of connected to a
pin on the Arduino; if so, omit it from this function's parameters.
Syntax
LiquidCrystal(rs, enable, d4, d5, d6, d7)
LiquidCrystal(rs, rw, enable, d4, d5, d6, d7)
LiquidCrystal(rs, enable, d0, d1, d2, d3, d4, d5, d6, d7)
LiquidCrystal(rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7)
Parameters
rs: the number of the Arduino pin that is connected to the RS pin on the LCD
rw: the number of the Arduino pin that is connected to the RW pin on the LCD
(optional)
enable: the number of the Arduino pin that is connected to the enable pin on the
LCD
d0, d1, d2, d3, d4, d5, d6, d7: the numbers of the Arduino pins that are connected
to the corresponding data pins on the LCD. d0, d1, d2, and d3 are optional; if
omitted, the LCD will be controlled using only the four data lines (d4, d5, d6, d7).
begin( )
Description
Specifies the dimensions (width and height) of the display.
Syntax
lcd.begin(cols, rows)
Parameters
lcd: a variable of type LiquidCrystal
Syntax
lcd.setCursor(col, row)
Parameters
lcd: a variable of type LiquidCrystal
col: the column at which to position the cursor (with 0 being the first column)
row: the row at which to position the cursor (with 0 being the first row)
Interfacing diagram:
Schematic Diagram:
Arduino program:
#include <LiquidCrystal.h>
void setup( )
{
lcd.begin(16, 2);
}
void loop( )
{
lcd.setCursor(0, 0);
lcd.print("GEC Bharuch");
lcd.setCursor(0, 1);
lcd.print("EC Department");
}
Observation:
Conclusion:
EXPERIMENT - 5
Introduction
The Arduino Uno board is capable of serial communication used for
communication between the Arduino board and a computer or other devices.
The Uno has a single serial port (also known as a UART or USART): Serial. It
communicates on digital pins 0 (RX) and 1 (TX) as well as with the computer via
USB. Thus, if you use these functions, you cannot also use pins 0 and 1 for digital
input or output.
You can use the Arduino environment's built-in serial monitor to communicate
with an Arduino board. Click the serial monitor button in the toolbar and select
the same baud rate used in the call to begin( ).
Functions
begin( )
Description
Sets the data rate in bits per second (baud) for serial data transmission. For
communicating with the computer, use one of these rates: 300, 1200, 2400,
4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200.
Syntax
Serial.begin(speed)
println( )
Description
Prints data to the serial port as human-readable ASCII text followed by a carriage
return character (ASCII 13, or '\r') and a newline character (ASCII 10, or '\n').
This command takes the same forms as Serial.print( ).
Syntax
Serial.println(val)
Serial.println(val, format)
Parameters
val: the value to print - any data type
format: specifies the number base (for integral data types) or number of decimal
places (for
floating point types)
Returns
byte
println( ) will return the number of bytes written, though reading that number is
optional.
Arduino program:
void setup( )
{
Serial.begin( );
}
void loop( )
{
lcd.setCursor(0, 1);
lcd.print("GEC Bharuch");
lcd.setCursor(1, 1);
lcd.print("EC Department");
}
Observation:
Conclusion:
EXPERIMENT - 6
Aim: Write and execute Arduino program to read analog value. Sense
temperature using LM35 sensor and display temperature value on LCD.
Introduction
analogRead( )
Description
Reads the value from the specified analog pin. The Arduino Uno board contains a
6 channel, 10-bit analog to digital converter. This means that it will map input
voltages between 0 and 5 volts into integer values between 0 and 1023. This
yields a resolution between readings of: 5 volts / 1024 units or, .0049 volts (4.9
mV) per unit.
Syntax
analogRead(pin)
Parameters
pin: the number of the analog input pin to read from (A0 to A5 on Uno)
Returns
int (0 to 1023)
Interfacing diagram:
Arduino program:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int sensorPin = A0;
int tempC, tempF;
void setup( )
{
lcd.begin(16, 2);
}
void loop( )
{
int temperature = analogRead(sensorPin);
float voltage = temperature * 5.0;
voltage = voltage / 1024.0;
tempC=(voltage - 0.5) * 100;
lcd.setCursor(0,0);
lcd.print(“Temperature in ‘C”);
lcd.print(tempC);
delay(200);
}
Observation:
Conclusion:
EXPERIMENT - 7
Introduction:
It comes with its own integrated C compiler the AVR GNU C Compiler (GCC). As
such you do not need a third party C compiler.
It provides a single environment to develop programs for both the 8-bits and 32-
bits AVR series of microcontrollers.
Provides support for several programmers including the STK500, AVR Dragon,
etc.
This AVR tutorial will go through the steps to create a AVR C project in AVR
Studio 5. This tutorial assume that you have already install AVR Studio 5 on your
computer
Step 1: To create a C project first start AVR Studio 5 by going to the start menu
on your PC select Atmel AVR Tools then AVR Studio 5.0. See the figure below.
After AVR Studio 5 starts the following window will appear. From which get help,
open an existing project, create a new project, open an example project, etc.
Step 2: Click on New Project pointed to by the red arrow in the diagram above
to start a new project. The following window will appear.
Step 3: To start a new C project select C from the panel to the left pointed to by
the red arrow. Also type the file Name and Location pointed to by the red
arrows at the bottom of the window. The following window will then appear.
Step 4: The window above is the device selection screen for AVR Studio 5. Scroll
down and select the microcontroller you will be using. The following window
will then appear.
This is the AVR studio 5 editor where you type your C program. The editor starts
your C program for you by providing you with the structure shown in the editor
of the figure above.
This AVR tutorial discusses how to generate or create an hex file in AVR Studio 5.
We will be generating/creating the hex file for the AVR C code shown in the AVR
Studio 5 editor below.
After typing your AVR program may it be C or assembly to generate the hex file
goes to Build menu and click Build Solution. See the figure below
If the AVR C or Assembly program was builds successfully a message will display
at the bottom of the AVR Studio 5 Editor indicating that Build succeeded. See the
bottom of the figure below.
You can view the content of the generated hex file by double clicking on the file
with the .hex extension in the Solution Explore to the left of the AVR Studio 5
editor. The hex file that we just generated is displayed in the AVR Studio 5 Editor
shown in the figure below.
Observation:
Conclusion:
EXPERIMENT – 8
Introduction:
Software
1. Windows 10/ Windows 8/ Windows 7
2. Atmel Studio 7 / Atmel Studio 6/ Atmel Studio 5
3. WinAVR
Hardware
USBasp
Or
USBasp based AVR Development board
USBasp
Make sure you have Atmel Studio, WinAVR and a USBasp to follow this tutorial.
First Step
Install both Atmel Studio and WinAVR. You can follow the guided instruction
embedded in the installation wizard.
Next step
Step 1
Go to Tools > Options > Toolchain
Select Atmel AVR 8-bit option from the dropdown menu.
Step 2
Click Add Flavour, then set the package name “AVRdudeDev” or anything you may
like.
For the package base path go to WinAVR installation directory and find
the bin folder. Now copy the path and paste it. [here it is C:\WinAVR-
20100110\bin].
Click Add.
You can make the newly created toolchain package default by clicking on to
the Set as Default button.
If you’re done with the toolchain package configuration, click OK to close the
Dialog Window.
To upload the compiled hex files. You need to configure the external tool for
USBasp. If you don’t create an external tool for USBasp that’s fine too, then you
need to type the avrdude commands to upload or use other software to upload
the hex file for you.
Why bother when Atmel Studio can help you on both code compilation and hex
file uploading process?
Step 1
Command: This will initiate the avrdude program, so you need to put the
avrdude directory path in this blank. For me it was E:\WinAVR-
20100110\bin\avrdude.exe
Arguments: This is the most tricky part. This is the arguments section where
you need to pass specIfic arguments to do specIfic tasks. Since here we have used
external tool as the program code loader. We need to put the command which
we’d use in avrdude for uploading codes.
AVRDude Tutorial : Burning hex files on Atmel AVR using USBasp and
AVRdude
The AVRdude is excellent program for burning hex code into Atmel
AVR microcontroller. USBasp is awesome USB based Atmel AVR programmer. In
this tutorial we will see how to use AVRdude for burning hex files into AVR
microcontroller using USBasp.
In order to program AVR microcontroller you need the .HEX file. It is nothing but
the machine code for the microcontroller. This file is generated by the AVR
assembler, which converts assembly code into machine code. Assembly code can
be produced by third party C cross compiler software or can be handwritten.
Typically everyone uses Atmel Studio, or Arduino environment to write
programs in C language. After compiling, these tools generate .hex file as their
output.
AVRdude executables for Windows (or tar archive for linux) can be found at:
All releases : http://download.savannah.gnu.org/releases/avrdude/ look for
version 6.3
Windows exe : http://download.savannah.gnu.org/releases/avrdude/avrdude-
6.3-mingw32.zip
NOTE 1: Make sure the path for the avrdude’s folder is added to the PATH
environment variable, otherwise “avrdude.exe” cannot be accessed fron any
random directory on the command prompt. Refer to this page on how to add a
path to PATH environment variable.
NOTE 2: When you connect your USBasp hardware to the Windows 10 machine,
you will need to download and install the drivers for the same.
Connections
Pinout of USBasp
If you are burning a fresh microcontroller, close the Slow Serial Clock jumper of
USBasp. Since many brand new microcontroller are factory programmed for
internal 1MHz oscillator. USBasp uses very high speed serial clock for faster
programming. Thus you will have to specIfically tell USBasp to use slow serial
clock. This setting is done by above mentioned jumper.
NOTE: If you have microcontroller which has internal oscillator enabled and
after the programming you are not planning to change its fuse bits back to
external clock setting, then you can skip the crystal.
Executing AVRdude:
Fortunately AVRdude is command line tool, so that you can be very sure of what
you are doing with your uC Or Unfortunately AVRdude is command line tool, so
you will
have to spend little time to get familiar with it.
o Open the command prompt. (Press WinKey + R. Run dialogbox will appear.
Type cmd and press enter.)
o Navigate to the directory where .hex file is located. For example:
If you have NOT added the avrdude’s path to the system path, execute following
command with your path. Otherwise skip this step.
To burn the hex file enter following command. Consider for example name of my
hex file is io.hex :
> avrdude –c usbasp –p m32 –u –U flash:w:io.hex
-c : Indicates the programmer type. Since we are using the USBasp programmer,
argument “usbasp” is mentioned.
-u : Disables the default behavior of reading out the fuses three times before
programming, then verIfying at the end of programming that the fuses have not
changed. Always use this option. Many times it happens that we forget to switch
on the AVR’s +5V power supply, then at the end of programming cycle, avrdude
detects inconsistent fuses and tries to reprogram them. Since there is no power
supply, fuses gets programmed incorrectly and entire microcontroller gets
screwed up(means becomes useless). Thus always use this option.
-U : memtype:op:filename[:format]
Perform a memory operation. Multiple ‘-U’ options can be specified in order to
operate on multiple memories on the same command-line invocation.
Memtype
Op
Filename
SpecIfy the hex file name. If file is not in current directory specIfy file name with
appropriate path.
Format
Format need not be specIfied, for hex files, avrdude will automatically detect the
format.
It is extremely boring to type such a long command every time you program the
uC. Therefore to avoid this you can create something called as Batch file. It is a
text file which contains series of commands which will be executed by dos
command processor. To create batch file follow these steps :
o Open notepad
o Type our avrdude command. i.e. copy paste following line into notepad.
avrdude –c usbasp –p m32 –u –U flash:w:io.hex
o Save the file with filename “burn.bat” and put it into the directory, which has
the hex file.
Now whenever you recompile your program and want to burn it, simply double
click on burn.bat. This will execute avrdude command that we have typed in it.
found 5 busses
found 5 busses
avrdude: error: could not find USB device “USBasp” with vid=0x16c0 pid=0x5dc
This happens when USBasp is not connected or not recognized by the PC. Try to
connect it to dIfferent USBport. Make sure that “Self programming” jumper of
USBasp is open. Always disconnect AVR from USBasp, before plugging USBasp to
the PC.
found 5 busses
avrdude: error: programm enable: target doesn’t answer. 1
avrdude: initialization failed, rc=-1
Double check connections and try again, or use -F to override this check.
avrdude done. Thank you.
Check the connections of USBasp with uC. Check the power supply of the uC.
Check whether you have connected the crystal and decoupling capacitors. If
everything is fine and still you are getting this error, then it indicates that either
ur uC is dead or its fuse bits have got screwed up.
Observation:
Conclusion:
EXPERIMENT – 9
Interfacing diagram:
#include <avr/io.h>
#include <util/delay.h>
int main (void)
{
DDRB |= 0x01;
While(1)
{
PORTB ^= 0x01;
_delay_ms(1000);
}
return 0;
}
Observation:
Interfacing diagram:
#include <avr/io.h>
#include <util/delay.h>
int main (void)
{
DDRB |= 0xFF;
While(1)
{
PORTB ^= 0xFF;
_delay_ms(1000);
}
return 0;
}
Observation:
#include <avr/io.h>
#include <util/delay.h>
int main (void)
{
DDRB |= 0xFF;
While(1)
{
PORTB = 0x01;
_delay_ms(1000);
PORTB = 0x02;
_delay_ms(1000);
PORTB = 0x04;
_delay_ms(1000);
PORTB = 0x08;
_delay_ms(1000);
PORTB = 0x10;
_delay_ms(1000);
PORTB = 0x20;
_delay_ms(1000);
PORTB = 0x40;
_delay_ms(1000);
PORTB = 0x80;
_delay_ms(1000);
}
return 0;
}
OR
#include <avr/io.h>
#include <util/delay.h>
int main (void)
{
DDRB |= 0xFF;
PORTB = 0x01;
While(1)
{
If(PORTB == 0x00)
PORTB = 0x01;
_delay_ms(1000);
PORTB = 1<<PORTB;
}
return 0;
}
Observation:
Program 9D: Write a C program to display 8-bit binary counter value on 8 LED.
Interfacing diagram:
#include <avr/io.h>
#include <util/delay.h>
int main (void)
{
DDRB |= 0xFF;
Unsigned char count=0;
While(1)
{
PORTB = count;
_delay_ms(1000);
Count++;
}
return 0;
}
Observation:
Conclusion:
EXPERIMENT – 10
Interfacing diagram:
#include <avr/io.h>
#include <util/delay.h>
Else
PORTB |= LED;
}
return 0;
}
Observation:
Program 10B: Write a C program to toggle single LED by pressing the same
switch.
Interfacing diagram:
#include <avr/io.h>
#include <util/delay.h>
DDRB |= LED;
DDRB &= ~SW;
While(1)
{
If (!(PINB & SW))
{
_delay_ms(200);
While(!(PINB & SW));
PORTB ^= LED;
}
}
return 0;
}
Observation:
Program 9C: Write a C program to blink alternate LED by pressing the same
switch.
Interfacing diagram:
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRB |= G_LED;
DDRB &= ~SW;
DDRB |= R_LED;
While(1)
{
If (!(PINB & SW))
{
_delay_ms(20);
If (!(PINB & SW))
{
While(!(PINB & SW));
flag = !flag;
}
}
If (flag)
{
PORTB &= ~R_LED;
PORTB |= G_LED;
}
else
{
PORTB |= R_LED;
PORTB &= ~G_LED;
}
}
return 0;
}
Observation:
Conclusion: