Arduino/C3/Assembly-programming-through-Arduino/English

From Script | Spoken-Tutorial
Jump to: navigation, search
Visual Cue Narration
Slide 1: Welcome to the spoken tutorial on Assembly programming through Arduino.
Slide 2:

Learning objectives

In this tutorial, we will learn to:
  • Interface a seven-segment Display to Arduino board
  • Write an assembly program to display a digit on seven-segment display and
  • Display a digit on the seven segment display using 7447 IC.
Slide 3:

Pre-requisites


To follow this tutorial, you should have basic knowledge of :
  • Electronics and
  • Assembly language

Refer to the Additional reading material link of this tutorial to know more about:

  • Arduino - Assembly code reference and
  • Arduino ATmega328 Pin mapping.
Slide 4:

System Requirement

To record this tutorial, I am using:
  • Arduino UNO Board
  • Ubuntu Linux operating system version 14.04
Slide 5:

External Devices

We also require some external devices such as:
  • Breadboard
  • Arduino UNO Board
  • Common Anode Seven-Segment Display
  • 220 ohm Resistor
  • Decoder 7447 IC
  • Jumper Wires
Show the image:

Seven-segment display

In this experiment we will use the common anode seven segment display.

Please refer to the basic level tutorial of this series to know more about seven segment display.

Let us see the connection circuit details.

The Dot pin of the Seven Segment Display is connected to the pin 13 of Arduino.

Any one of the COM pins is connected to the +5V through a 220 ohm resistor.

Refer the pin mapping for Arduino and microcontroller.
Show the live connection This is the live setup of the connection.
Point to the Dot in the diagram Now we will write an assembly program to turn on the Dot LED on the seven segment display.
Slide: Software Setup We need to install an assembler AVRA and AVRDUDE.

AVRA is an assembler that will generate a hex file.

AVRDUDE is used to upload any hex file to the Arduino board.

Press Ctrl + Alt + T keys Open the terminal by pressing Ctrl + Alt + T keys together.
Type

> sudo apt-get install avra dude

To install avra and avrdude, type

sudo space apt hyphen get space install space avra space avrdude

Enter the administrative password if prompted and press Enter.
Press ‘Y’ We can see the installation process has begun.

Press 'Y' wherever there is a prompt during the installation, to confirm the configuration.

Installation will take some time to complete depending upon the internet speed.

Point to the prompt We can see that the installation has been completed successfully.

Let us clear the screen.

Now let us connect the Arduino board to the computer.
Installing AVRA To check the port number of Arduino, I'll type

ls space forward slash dev forward slash ttyACM asterisk

Highlight ttyACM0 We can see the output as

forward slash dev forward slash ttyACM0

Here ttyACM0 represents the port number of Arduino.

You may get a different port number. Make a note of your port number.

Slide 6:

Def files for ATmega328P

Download m328Pdef.inc file from the Code files link of this tutorial.

We need to include this library file in the first line of the source code.

This allows the user to use Register and Bit name when writing assembly programs.

Let us write an assembly program and upload it to the microcontroller.
Open gedit text editor Open any text editor and type the following.
Highlight according to narration:

;program to turn on the Dot LED

Semicolon represents comment statements.

These statements are ignored by the assembler and are not executed.

.include "/home/spoken/Assembly/m328Pdef.inc" This line tells the assembler to include the m328Pdef.inc file.

We had downloaded this file earlier and in my computer this is the path.

Note that we have to specify the entire file path.

; Configure pin 13 on Arduino as Output 5:10 ,8:18

ldi r16, 0b00100000


ldi stands for "load immediate"

This sets pin PB5, that is digital pin 13 as output.

It tells the assembler to take a working register r16 and load a binary number into it.

out DDRB, r16 This line tells the compiler to copy the contents of the register r16 into the DDRB register.
; Making pin 13 LOW

ldi r17, 0b00000000

This sets all the pins to 0 volts except pin PB5, (i.e digital pin 13) which is set to 5 volts.
out PortB, r17 This line copies the same binary number from our storage register r17 to PortB.
Start:

Rjmp Start

Relative jump statement executes the program in an infinite loop.

The program has to just keep running, in order for the LED to remain ON.

Save the code as dot hyphen blink.asm file in the home slash spoken slash Assembly folder.
This code is available in the Code files link of this tutorial, as well.

You can download and use it.

Switch to the terminal. Switch to the terminal.
Type,

> cd /home/spoken/Assembly

> avra dot-blink.asm

Go to the folder where dot hyphen blink.asm file is saved.

Type, avra space dot hyphen blink.asm and press Enter.

Point to the /home/spoken/Assembly folder This will assemble the code and create a file dot hyphen blink.hex.

Let us see the dot hyphen blink.hex file that is generated in the same folder.

Next we need to upload the code to Arduino.
Switch back to the terminal.
Type,

avrdude -p atmega328p -c arduino -b 115200 -P /dev/ttyACM0 -U flash:w:dot-blink.hex

3:45

7:06,2:00,6:22

For this, type

avrdude space hyphen p space atmega328p space hyphen c arduino space hyphen b space 115200 space hyphen capital P space forward slash dev forward slash ttyACM0 space hyphen capital U space flash colon w colon dot hyphen blink dot hex

Here ttyACM0 represents the port number of Arduino.

dot hyphen blink.hex is the file that is generated.

And press Enter.

Point to the output Now you can see that the Dot LED in the seven segment is glowing.
Slide 7:

Assignment 1

Pause the tutorial and do the below assignment.
  • Modify the same code to turn off the Dot LED.
Next, we will display the digit 2 on the seven segment display.
Point to the diagram


To display '2', a,b,d,e,g segments should be high and the other LEDS should be low.
Connect Pins a, b, c, d, e, f and g of the seven-segment display to pins 2, 3, 4, 5, 6, 7 and 8 of Arduino.

The two common pins are connected to +5V through resistors.

show the connection Let us see the live connection setup.
Let us see the source code for this program.
open gedit Open any text editor and type the following code.
.include "/home/spoken/Assembly

/m328Pdef.inc"

Highlight 4 lines

As seen earlier, we should first configure the pins as output first.

Then make it as high or low according to our requirement.

;Configure pins 2-7 on Arduino as Output

ldi r16, 0b11111100

out DDRD, r16

These two lines makes the bits 2 to 7 on PORT D as output.
; Configure pin 8 on Arduino as Output

ldi r16, 0b00000001

out DDRB, r16

These two lines makes bit 0 on PORT B as output.

These bits corresponds to the digital pins 2 to 8 on Arduino.

;Writing number 2 on Seven Segment Display Here, we are making the pins as high or low corresponding to whatever number we want to display.

In our case, it is digit two.

Highlight the code

ldi r17, 0b10010000

out PortD, r17

ldi r17, 0b00000000

out PortB, r17


Start:

Rjmp Start

Here, we are writing 0100100 to digital pins 2 to 8 of Arduino.

This will make all the segments of SSD low, except c and g.

save the file as two.asm Let us save this as two.asm file.
Switch back to the terminal. Switch back to the terminal.

Let us clear the screen.

Type, avra two.asm and press Enter. Type, avra space two.asm and press Enter.
type

avrdude -p atmega328p -c arduino -b 115200 -P /dev/ttyACM0 -U flash:w:two.hex

To upload, press the up arrow to get the previous command.

Now change the filename as shown and press Enter.

Point to the output Now you can see that the digit 2 in the seven segment is glowing.
Slide 8:

Assignment 2

Pause the tutorial and do the below assignment.
  • Modify the above code to display any other digit from 0 to 9.
Next, we will use a Decoder to display the number 5 on the Seven Segment Display.
Slide 9:

Show the image of the decoder:

We are using 7447 IC.

Slide 10:

Show the pin image of 7447 IC

The a complement to f complement pins of the decoder connect to the a to f pins of Seven Segment Display.

Vcc and the GND pins of the decoder are connected to +5V and the GND pins of Arduino.

Connect the A, B,C,D pins of the Decoder to pins 2,3,4,5 of Arduino.

Note that this decoder is compatible only with common anode seven segment display.

Show the setup Let us see the live connection setup.
Show the image of the table:

D C B A Decimal

0 1 0 1 5

The input pins of the decoder are A,B,C and D.

A being the lowest significant bit (LSB) and D being the most significant bit (MSB).

For example, the number 5 is visible on the display when the A,B,C and D inputs are as shown here.

open gedit Let us see the source code for this program.

Open any text editor and type the following code.

.include "/home/user/m328Pdef.inc"

;Configure pins 2,3,4 and 5 on Arduino as Output

ldi r16, 0b00111100

out DDRD, r16

The first two lines makes the bits 2 to 5 on PORT D as output.

This means we are making digital pins 2 to 5 in Arduino as output pins.

2 to 5 pins in the Arduino are connected to A, B, C, D pins of decoder.

;Writing number 5 on Seven Segment Display

ldi r16, 0b00010100

out PORTD, r16

The next two lines are giving input of 0101 to decoder inputs.

This will then be decoded as 5 in the seven segment display.

Start:

Rjmp Start

Save it as decoder.asm file. Save it as decoder.asm file.
Switch back to the terminal. Switch back to the terminal.
Type, avra decoder.asm and press Enter. Type, avra space decoder.asm and press Enter.


type

avrdude -p atmega328p -c arduino -b 115200 -P /dev/ttyACM0 -U flash:w:decoder.hex

To upload, press the up arrow to get the previous command.

Now change the filename as shown and press Enter.

Point to the output Now we can see that the digit five in the seven segment is glowing.
This brings us to the end of this tutorial. Let us summarize.
Slide 11:

Summary

In this tutorial, we learnt to:
  • Interface a Seven-segment Display to Arduino board and
  • Write an assembly program to display a digit on seven-segment display.
  • Display a digit on the seven segment display using 7447 IC.
Slide 12:

About Spoken Tutorial project

The video at the following link summarizes the Spoken Tutorial project.

Please download and watch it.

Slide 13:

Spoken Tutorial workshops

The Spoken Tutorial Project Team:
  • conducts workshops and gives certificates.

For more details, please write to us.

Slide 14:

Forum for specific questions

Please post your timed queries in this forum.
Slide 15:

Acknowledgement

Spoken Tutorial project is funded by NMEICT, MHRD, Government of India.

More information on this mission is available at this link.

This tutorial has been contributed by FOSSEE and Spoken Tutorial Project, IIT Bombay.  

This is Priya from IIT Bombay signing off.

Thanks for joining.

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat