Arduino/C3/AVR-GCC-programming-through-Arduino/English

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

Learning objectives

In this tutorial, we will learn to:
  • Interface a seven segment display to Arduino board.
  • Write an AVR-GCC program to display a digit on seven segment display and
  • Display digits 0 to 9 on the seven segment display.
Slide 3:

Pre-requisites

To follow this tutorial, you should have basic knowledge of :
  • Electronics
  • C Programming and
  • AVR-GCC
Slide 4:

System Requirement

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

AVR -GCC

  • GCC stands for GNU Compiler Collection.
  • It is a compiler which supports various programming languages.
  • AVR-GCC is a part of GCC and supports compiling C programs for AVR microcontrollers.
  • Since Arduino uses ATMEGA328P, this is a suitable compiler.
Slide 5:

External Devices

We also require some external devices such as:
  • Seven-Segment Display
  • 220 ohm Resistor
  • Breadboard
  • Arduino UNO Board
  • Jumper Wires
Show the image: ssd.png

Seven-segment display

Show the image: In this experiment we will be using the common anode seven-segment display.

Please refer to the basic level tutorials of this series to know more about Seven segment display.

Refer the pin mapping for Arduino and microcontroller.
Show the circuit connection(image) Do the circuit connection as shown here.

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

Any one of the common pins is connected to the +5V through a resistor.

Show the real connection This is the live setup of the connection.
Point to the Dot in the diagram Now we will write an AVR-GCC program to blink the Dot LED on the seven segment display.
Slide: Software Setup
  • We need to install AVR-GCC assembler and a AVR-LIBC library.
  • AVR-GCC will generate a hex file and upload it to Arduino board.
  • AVR-LIBC contains the required library files that can be used in the program.
Press Ctrl + Alt + T keys Open the terminal by pressing Ctrl + Alt + T keys together.
Type

> sudo apt-get install avr-libc gcc-avr

Type,

sudo space apt hyphen get space install space avr hyphen libc space gcc hyphen avr

And press Enter.

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 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 me clear the terminal.

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

ls space forwardslash dev forwardslash ttyACM asterisk

and press Enter.

Highlight ttyACM0 We can see the output as shown.

Here ttyACM0 represents the port number of Arduino.

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

Slide 6:

Make file for ATmega328P

Download the file Makefile from the Code files link of this tutorial.

Makefile enables us to create a dot hex file and upload it to Arduino.

Save the Makefile in the folder where you will be saving the C program.

Let us write the avr-gcc program to blink the Dot LED and upload it to the microcontroller.
Open text editor Open any text editor and type the following.
Highlight according to narration:

// include library files

#include <avr/io.h>

#include <util/delay.h>

We have to include the libraries required for our program.

avr slash io dot h contains all the basic libraries required to perform the input and output operations.

util slash delay dot h contains the libraries for the delay function.

// Turns LED on and off

int main (void)

{

DDRB |= ((1 << DDB5));

while(1)

{

PORTB = ((0 << PB5));

_delay_ms (1000L);

PORTB = ((1 << PB5));

_delay_ms (1000L);

}

return 0;

}

Arduino board has an LED at PB5.

Set PB5, that is pin 13 of Arduino as output.

Sending 0 to PB5 turns on the LED.

Sending 1 to PB5 turns off the LED.

These two steps will run in an infinite while loop making the LED blink.

Textbox for the narration Source code that are used in this tutorial are available in the Code Files link of this tutorial.

You can download and use it.

Save the file I’ll save the code as dot hyphen blink dot c in the Downloads folder.
Switch to the terminal. Switch to the terminal.
Type,

> cd /home/spoken/Downloads

Go to the Downloads folder where dot hyphen blink dot c file is saved.
> make FNAME = onoff Type, make space FNAMEin capital =dot hyphen blink and press Enter.

This command creates a dot hex file and uploads it to the Arduino.

Point to the output Now, you can see that the Dot LED in the seven segment is blinking.
Next, we will display 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.
show the connection (Two.jpg) Pins a, b, c, d, e, f and g of the seven segment display are connected to pins 2, 3, 4, 5, 6, 7 and 8 of the Arduino respectively.

The common pin is connected to +5Volts through resistor.

Let us see the live connection setup.
Let us see the source code for this program.
Open any text editor and type the following code.
Highlight according to narration:

// Prints decimal number 2

#include <avr/io.h>

#include <util/delay.h> int main (void)

{

DDRD |= 0xFC;

DDRB |= ((1 << DDB0));

The first two lines of the code in the main function set pins 2 to 8 as output pins.
while(1)

{

PORTB = ((0 << PB0));

PORTD = 0b10010000;

}

return 0;

}

The codes inside the while loop are used to control the state of the respective LEDs.

Sending 0 will cause the LED to glow and sending 1 will turn it off.

Save the file Save it as two.c file.
Switch back to the terminal.

Clear the terminal now.

Type make space FNAME in capital equals two and press Enter.
Point to the output Now you can see that the digit two in the seven segment is glowing.
Slide 7:

Assignment 1

Pause the tutorial and do the below assignment.
  • Modify the above code to display any other digit from 0 to 9.
Next, we will display the numbers 0 to 9 on the Seven Segment Display.
Show the setup Setup remains the same.
Let us see the source code for this program.

Open any text editor and type the following code.

Highlight according to narration:

// Prints decimal numbers from 0 to 9

#include <avr/io.h>

#include <util/delay.h>


void sevenseg(int);

int main (void)

{

DDRD |= 0xFC;

DDRB |= ((1 << DDB0));

while(1)

{

for(int i = 0; i < 10; i++)

{

sevenseg(i);

_delay_ms (1000L);

}

}

return 0;

}

The for loop is used to count from 0 to 9.


Each time, the variable ‘ i ‘ is incremented by one and the value is passed to sevenseg function.

void sevenseg(int dec)

{

switch(dec)

{

case 0:

PORTB = ((1 << 0));

PORTD = 0b00000000;

break;

case 1:

PORTB = ((1 << 0));

PORTD = 0b11100100;

break; case 2:

PORTB = ((0 << 0));

PORTD = 0b10010000;

break;


case 3:

PORTB = ((0 << 0));

PORTD = 0b11000000;

break;


case 4:

PORTB = ((0 << 0));

PORTD = 0b01100100;

break;


case 5:

PORTB = ((0 << 0));

PORTD = 0b01001000;

break;


case 6:

PORTB = ((0 << 0));

PORTD = 0b00001000;

break;


case 7:

PORTB = ((1 << 0));

PORTD = 0b11100000;

break;


case 8:

PORTB = ((0 << 0));

PORTD = 0b00000000;

break;


case 9:

PORTB = ((0 << 0));

PORTD = 0b01000000;

break;

default:

PORTB = ((1 << 0));

PORTD = 0b00000000;

break;


}

}

The sevenseg function receives an integer ranging from 0 to 9.

Based on the input, the case structure is executed.

Thus displaying the digits from 0 to 9.

Save the file Save it as counter.c file.
Switch back to the terminal and clear it.
Type make space FNAME in capital equals counter and press Enter.
Point to the output


Now we can see the digits 0 to 9 is displayed in the seven segment display.
This brings us to the end of this tutorial. Let us summarize.
Slide 8:

Summary

In this tutorial, we learnt to
  • Interface a Seven-segment display to Arduino board
  • Write an AVR-GCC program to display a digit on seven-segment display and
  • Display digits 0 to 9 on the Seven segment display.


Slide 9:

About Spoken Tutorial project

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

Please download and watch it.

Slide 10:

Spoken Tutorial workshops

The Spoken Tutorial Project Team conducts workshops and gives certificates.

For more details, please write to us.

Slide 11:

Forum for specific questions

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

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.

Thanks for joining.

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat