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

From Script | Spoken-Tutorial
Revision as of 07:56, 18 April 2018 by Nancyvarkey (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Visual Cue Narration
Slide 1: Welcome to the Spoken Tutorial on Interfacing LCD through AVR-GCC programming.
Slide 2:

Learning objectives

In this tutorial, we will learn to:
  • Interface a LCD to Arduino board.
  • Write an AVR-GCC program to display a digit on LCD.
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
  • Ubuntu Linux operating system version 14.04
Slide 5:

External Devices

We also require some external devices such as:
  • Breadboard
  • LCD 16 by 2
  • Arduino UNO Board
  • Potentiometer
  • Jumper Wires and
  • Pin headers
Show the image: In this experiment, we will be using a 16 by 2 LCD.

Please refer to the basic level tutorial of this series to know more about LCD.

Show the image: Make use of the given table to connect the Arduino pins to the LCD.

This table is available in the Additional reading material link of this tutorial.

Show the circuit connection(LCD_circuit.jpg) This is the circuit connection.
Point to the pins in the image Connect one extreme lead of the potentiometer to pin 1 of the LCD.

The other extreme end is connected to pin 2 of the LCD.

The middle pin is connected to pin 3 of the LCD.

Live setup:

LCD-Setup.JPEG

This is the live setup of the connection.
Now we will write an AVR-GCC program to display the digit 5 on the LCD.
Open text editor Open any text editor and type the following.
Highlight according to narration:


#include <avr/io.h>

#include <util/delay.h>

#include <stdlib.h>

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

Util slash delay.h contains the libraries for the delay function.

stdlib.h contains the libraries for defining variable types and other commonly used functions.

typedef uint8_t byte;

#define ClearBit(x,y) x &= ~_BV(y)

#define SetBit(x,y) x |= _BV(y)

ClearBit(x comma y) is equivalent to cbi(x comma y). This is used to clear the yth bit of PORTx.

SetBit(x comma y) is equivalent to sbi(x comma y). This is used to set the yth bit of PORTx.

#define LCD_RS 0

#define LCD_E 1

#define DAT4 2

#define DAT5 3

#define DAT6 4

#define DAT7 5

#define CLEARDISPLAY 0x01

All the numerical pins of the LCD are replaced with the names using hash define.

This is done to make the program easier to understand.

void PulseEnableLine()

{

SetBit (PORTB, LCD_E);

_delay_us(40);

ClearBit(PORTB, LCD_E);

}

The PulseEnableLine function is used to toggle the enable pin of the LCD.

SetBit will set to high and Clearbit will set to low, with a delay in between.

void SendNibble(byte data)

{

PORTB &= 0xC3;

if (data & _BV(4)) SetBit(PORTB,DAT4);

if (data & _BV(5)) SetBit(PORTB,DAT5);

if (data & _BV(6)) SetBit(PORTB,DAT6);

if (data & _BV(7)) SetBit(PORTB,DAT7);

PulseEnableLine();

}

The SendNibble function is used to send data of one nibble to the PORTB.
void SendByte(byte data)

{

SendNibble(data);

SendNibble(data<<4);

ClearBit(PORTB, 5);

}

The SendByte function is used to send data of one byte to the PORTB.
void LCD_Cmd (byte cmd)

{

ClearBit(PORTB, LCD_RS);

SendByte(cmd);

}

The LCD underscore Cmd function is used to send a command to the LCD.
void LCD_Char (byte ch)

{

SetBit(PORTB, LCD_RS);

SendByte(ch);

}

The LCD underscore Char function is used to send a character data to the LCD.
void LCD_Init()

{

LCD_Cmd(0x33);

LCD_Cmd(0x32);

LCD_Cmd(0x28);

LCD_Cmd(0x0C);

LCD_Cmd(0x06);

LCD_Cmd(0x01);

_delay_ms(3);

}

The LCD underscore Init function is used to initialise the LCD.
void LCD_Clear()

{

LCD_Cmd(CLEARDISPLAY);

_delay_ms(3);

}

The LCD underscore Clear function is used to clear the display.
void LCD_Message(const char *text)

{

LCD_Char(*text++);

}

The LCD underscore Message function is used to send a string of data to be displayed on the LCD.
void LCD_Integer(int data)

{

char st[8] = "";

itoa(data, st, 10);

LCD_Message(st);

}

The LCD underscore Integer function is used to send an integer data to the LCD.
int main(void)

{

DDRB = 0xFF;

LCD_Init();

while(1)

{

LCD_Clear();

LCD_Integer(5);

_delay_ms(600);

}

}

This is the main function where we pass the value 5 to the display.
Text box to display the message. This code is available in the Code files link of this tutorial. You can download and use it.
Save the file. I’ll save the code as lcd.c file in the Downloads folder.

Ensure that the Makefile is also present in the same folder.

Switch to the terminal.
Type,

> cd /home/spoken/Downloads

> make FNAME = lcd

Go to the folder where lcd.c file is saved.

Type, make space FNAME in capital = lcd and press Enter.

Highlight the command This command creates a .hex file and uploads it to Arduino.
Point to the output

LCD-Output.jpeg

Now you can see that the digit 5 is displayed on the LCD.
This brings us to the end of this tutorial. Let us summarize.
Slide 6:

Summary

In this tutorial, we learnt to:
  • Interface a LCD to Arduino board and
  • Write an AVR-GCC program to display a digit on LCD.
Slide 7:

Assignment

As an assignment-
  • Modify the above code to display any other digit from 0-9.
  • Modify the above code to build a counter which counts from 0 to 9.
Slide 8:

About Spoken Tutorial project

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

Please download and watch it.

Slide 9:

Spoken Tutorial workshops

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

For more details, please write to us.

Slide 10:

Forum for specific questions

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

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

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat