A 16 x 2 Liquid Crystal Display (LCD) controlling with C (AVR-003)

AVR-003 microcontroller kit (www.circuits-home.com)

In this page I’ll bring you a tutorial that can help to start learn microcontroller. In this tutorial, I assume that you have knowledge about C programming language. My tutorial will be use C programming language to make microcontroller work.

Ok! ... Let’s start to learn.

Now we’ll learn to use microcontroller to drive a liquid crystal display (LCD). In this project we’ll use AVR-003 microcontroller from Circuits-Home. Before make program, a job that must we do are understand about the hardware specification. Let looks the box below.

The kit specifications:
MCU                  : AVR ATmega8535 (compatible with MEGA16 and MEGA32 series)
Crystal              : 8 MHz
Pin description    :
  • ·         PORTA = connected with 8 units of LED.
  • ·         PORTB = as line for microcontroller programming (MISO, MOSI, SCK).
  • ·         PORTC = connected with 1 units LCD (2 x 16 chr).
  • ·         PORTD = connected with 8 units of tag-switch.
Power               : About 5 volt DC.

Pin description of the LCD:
LCD’s pin
Info
MCU’s pin
Info
1
GND
11
GND
2
+5 Volt
10
Vcc
3
Contrast
-
-
4
RS
22
PC.0
5
RD/WR
23
PC.1
6
EN
24
PC.2
11
D4
25
PC.3
12
D5
26
PC.4
13
D6
27
PC.5
14
D7
28
PC.6

From the specification, we know that the microcontroller chip that uses is AVR family chip from ATMEL corporations with series ATmega8535. The microcontroller hardware use 8 MHz crystal for the external clock source. About the pin connections, on PORTA have connected 8 units LED display, on PORTB used for MCU line programming (interfacing with computer), on PORTC have connected 1 units LCD display, and the last on PORTD connected with 8 units of tag-switch. After we know the hardware specifications, now let’s run our project.

First step:
Run an IDE software CodeVisionAVR.

Second step:
Make a new project with CodeWizard
Third step:
Type the code on the text editor that show after you choose “File>>Generate, save, and exit”.

In the next part of this tutorial I give to you some example code (in C) how to a LCD display. I hope it will make easy for you in microcontroller programming learn. So let’s do it….we’ll mastering it if we “learning by doing!”. So enjoy it now! J

A. How to blazing LED display in a port (PORTx) ?
I have gave my tutorial about how to blazing LED display on AVR-001 tutorial. So check on its page or click here!

B. How to access a LCD in a port (PORTx) ?
When we make program to drive a LCD, CodeVisionAVR have make available some of library function that make our jobs in accessing LCD become so easy. That library function is explain below.

1. Low level LCD access function.

Void _lcd_ready(void)
This function is used to wait until the LCD module is ready to receive data. This function should be called before the function _lcd_write_data done.

Void _lcd_write_data(unsigned char data)
This function is used to write 1-byte data according to the contents of the variable 'data' to the LCD. This function is normally used to make LCD mode settings such as setting the cursor, blinking (blink), and others.

Void lcd_write_byte(unsigned char addr,unsigned char data)
This function is used to write 1-byte data according to the contents of the variable 'data' memory to the Character Generator RAM (CGRAM) to address appropriate content of the variable 'addr'. This function is used if we are going to define a new character / uniquely our own making.

Unsigned char lcd_read_byte(unsigned char addr)
This function is used to read 1-byte data Character Generator RAM (CGRAM) LCD with addresses based on the contents of the variable 'addr'.

2. High level LCD access function.

void lcd_init(unsigned char lcd_columns)
This function is used to initialize the LCD module that includes a sub-routine to clear the LCD display, put the cursor position at row 0 and column 0 and defines the number of LCD column as variable 'lcd_columns'. This function must be called first before the functions of other LCDs used.

Void lcd_clear(void)
This function is used to remove the LCD and put the cursor position back into line 0 and column 0.

Void lcd_gotoxy(unsigned char x,unsigned char y)
This function is used to place the cursor position to the line 'x' and the 'y'respectively according to the contents of the variable 'y' and variable 'x'. Row and column numbering always starts from 0 (top left corner). The more columns to the right means getting bigger, and the bottom line is getting bigger. For example, lcd_gotoxy (4.0) means to put the cursor at row 0 and column 4.

Void lcd_putchar(char c)
This function is used to display the characters according to the contents of the variable 'c' to the LCD with the current cursor position.
Listen
Read phonetically

Void lcd_puts(char *str)
This function is used to display characters in RAM memory at the address designated by the pointer 'str' to the LCD with the current cursor position.

Void lcd_putsf(char flash *str)
This function is used to display characters in FLASH memory at the address designated by the pointer 'str' to the LCD with the current cursor position.

Example code 1:

#include
#asm
.equ __lcd_port=0x15
#endasm
#include

void main(void)
{
 lcd_init(16);
 lcd_gotoxy(0,0);
 lcd_putsf("Circuits-Home");
 lcd_gotoxy(0,1);
 lcd_putsf("CH electronics");
 while (1);     
}

Example code 2:
#include
#include
#asm
   .equ __lcd_port=0x15 ;PORTC
#endasm
// Alphanumeric LCD Module functions
#include


void main(void)
{
PORTA=0xFF;DDRA=0xFF;
PORTB=0x00;DDRB=0xFF;
PORTC=0x00;DDRC=0xFF;  
PORTD=0xFF;DDRD=0x00;

// Alphanumeric LCD initialization
// Connections specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTC Bit 0
// RD - PORTC Bit 1
// EN - PORTC Bit 2
// D4 - PORTC Bit 3
// D5 - PORTC Bit 4
// D6 - PORTC Bit 5
// D7 - PORTC Bit 6
// Characters/line: 16
lcd_init(16);
lcd_gotoxy(1,0);
lcd_putsf("== WELCOME ==");
delay_ms(500);
lcd_clear();
      
while (1)
      {
       if (PIND.0==0)
       {
        lcd_gotoxy(1,0);
        lcd_putsf("Sudah di Tekan");
        PORTA = 0x00;                                  //Led       
       }   
       else
       {
        lcd_gotoxy(1,1);
        lcd_putsf("Nunggu ditekan!");
        PORTA = 0xFF;                                  //Led 
        lcd_clear();
       }
      }
}

To do a practice, you needs hardware & software below:

Hardware:

  1. AVR-003 microcontroller kit
  2. PRG-001 USB programmer kit
Software:
  1. CodeVisionAVR
  2. WinAVR (versi 20100110)
  3. PROGISP versi 172


0 comments:

Post a Comment

Buku Terbaru

Powered by Blogger.

Follower

Calendar

Refresh

Search

Loading...

translate

Time

Label 1

 
Support : CH electronics | /fiq | 2013
Copyright © 2013. Embedded system & Robotics - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger