Wednesday, 19 October 2011

Interfacing 16x2 LCD to 8051 core mcu

How would you interface devices to a microcontroller?
The most important thing is "Data sheet" of the device.And it is not mandatory to read each and every part of data sheet,reading data sheet is also an art form.You must know your expectation of  devices specifications, read through data sheet of the device and make sure it satisfies that to some extend.Electrical specification will matter a lot when you are interfacing a device.You must know the power rating,input voltage supply,input current etc.,
We know that lot of  16x2 LCDs are available in the market so it is your job to find out the data sheet of the LCD your interfacing.But the instruction sets for almost all LCD are same,so below i've put instruction set.



Here is a program for interfacing t JHD162A LCD to 8051 core mcu:

 HEADER FILE


 #ifndef __LCD_H__
#define __LCD_H__

void lcd_init();
void lcd_printc(char a_char);  //print a character
void lcd_prints(char *string);  //print a string
void lcd_clear_row(char row);
void lcd_clear(void);
void lcd_cursor_on(void);
void lcd_cursor_off(void);
void lcd_move_cursor(unsigned char row, unsigned char column);
void lcd_move_cursor_left(void);
void lcd_move_cursor_right(void);


#endif

LIBRARY FILE
#include <reg51.h>
#define LCD_RS P2_4
#define LCD_WR P2_5
#define LCD_EN P2_6

#define LCD_MAX_ROWS    2
#define LCD_MAX_COLS    16

#define LCD_CMD_CLEAR        0x01
#define LCD_CMD_CURSOR_HOME    0x02
#define LCD_CMD_CURSOR_LEFT    0x10
#define LCD_CMD_CURSOR_RIGHT     0x14
#define LCD_CMD_CURSOR_ON    0x0D
#define LCD_CMD_CURSOR_OFF    0x0C

static void lcd_write_data(unsigned char val);
static void lcd_write_cmd(unsigned char cmd);
static char lcd_is_busy();



void lcd_init(void)
{
    delay(15);        // LCD Power on delay
    lcd_write_cmd(0x38);    // 8BIT, 2 LINE , 5x7 DOT Format font
    delay(5);        // Small dealy
    lcd_write_cmd(0x38);
    lcd_write_cmd(0x06);
    lcd_write_cmd(0x0c);
    lcd_write_cmd(0x0E);
    lcd_clear();        // Display ON, Cursor ON, Blink OFF

    lcd_cursor_off();
    lcd_clear();
}

void lcd_printc(char a_char)
{
    lcd_write_data(a_char);
}

void lcd_prints(char *string)
{
    while (*string)
        lcd_printc(*string++);
}

void lcd_clear(void)
{
    lcd_write_cmd(LCD_CMD_CLEAR);
}

void lcd_clear_row(char row)
{
    char i = 0;
    lcd_move_cursor(row, 0);
    while (i < LCD_MAX_COLS) {
        lcd_printc(' ');
        i++;
    }
    lcd_move_cursor(row, 0);
}

void lcd_cursor_on(void)
{
    lcd_write_cmd(LCD_CMD_CURSOR_ON);
}

void lcd_cursor_off(void)
{
    lcd_write_cmd(LCD_CMD_CURSOR_OFF);
}

void lcd_move_cursor(unsigned char row, unsigned char column)
{
    switch (row) {
    case 0:
        lcd_write_cmd(0x80 + column);
        break;
    case 1:
        lcd_write_cmd(0xc0 + column);
        break;
    default:
        break;
    }
}

void lcd_move_cursor_left(void)
{
    lcd_write_cmd(LCD_CMD_CURSOR_LEFT);
}

void lcd_move_cursor_right(void)
{
    lcd_write_cmd(LCD_CMD_CURSOR_RIGHT);
}

void lcd_move_cursor_home(void)
{
    lcd_write_cmd(LCD_CMD_CURSOR_HOME);
}

static char lcd_is_busy()
{
    unsigned char c;
    P0 = 0xFF;
    LCD_WR = 1;
    LCD_RS = 0;        // SET LCD_RS for DATA
    LCD_EN = 0;
    LCD_EN = 1;        // Pulse LCD_EN
    c = P0;
    LCD_EN = 0;
    return c & 0x80;    // return busy bit
}

static void lcd_write_cmd(unsigned char cmd)
{
    while (lcd_is_busy());
    LCD_WR = 0;
    LCD_EN = 0;
    LCD_RS = 0;        // Reset LCD_RS for Command
    P0 = cmd;
    LCD_EN = 1;        // Pulse LCD_EN
    LCD_EN = 0;
    LCD_WR = 1;
}

static void lcd_write_data(unsigned char val)
{
    while (lcd_is_busy());
    LCD_WR = 0;
    LCD_EN = 0;
    LCD_RS = 1;        // SET LCD_RS for DATA
    P0 = val;
    LCD_EN = 1;        // Pulse LCD_EN
    LCD_EN = 0;
    LCD_WR = 1;
}




Tuesday, 4 October 2011

Keil tutorials

this video helps you to create a new project and debug,if any error encounters.

Tuesday, 27 September 2011

Keil Uvision 4 on Ubuntu

Hey all,
            i'm here to help you people out with installing keil in ubuntu.

Now you people might think.Can we really install programs with .exe extensions?
Well,here's an answer to you people "yes"?.

Wine is a powerful software(free software!) that aims to allow computer programs written solely for microsoft windows to run on linux environment.

if you want to know more about it.Here is a link on its articles:




Here are few simple steps to install wine :
  1. open terminal Applications>accessories>terminal
    2. Type the following command to install wine (you must be connected to
         internet):
                sudo apt-get install wine
    3. After installing wine to make it create duplicate "c:"drive configure it by 
        typing "winecfg" in terminal.


Install keil in wine by getting into the location where installation file is located in terminal by



                 "wine file_name.exe"
if file extension is other than exe type: " wine start file_name.abc"


hurray! installation is done.

next i'll be teaching how to use keil.

Thursday, 15 September 2011

Windows 8 developers preview!

I'm not sure of availability of win 8 in market.But i've got a video from youtube previewing win8.

This version will be first on tablets to compete with android (which is not obvious at all! and this is what i believe) later on for different machines.

If you have used win 7 in multitouch laptops,you would likely be disappointed because of its fewer functionality like pinch to zoom,two finger scrolling,twist to rotate etc., But win 8 is going to support certain new software layer that helps you  to pinch and rotate multiple objects at the same time, utilize more touch points, and manipulate the Start Screen interface as though your touchpad were a tiny touchscreen proxy, not a mouse substitute.


Happy! hurray! one more thing is software giants have made it clear that this will have backward compatibility with win7 applications!

Anyhow i'm not going to waste my money :-|

Wednesday, 14 September 2011

SPI interface programs for 8051 core microcontroller

HEADER FILE
#ifndef SPI_H
#define SPI_H

#include <8052.h>


#define SPI_SS         P2_7
#define SPI_READ    0x03
#define SPI_WRITE    0x02
#define SPI_WRDI    0x04
#define SPI_WREN    0x06
#define SPI_RDSR    0x05


/* SPI Special Function Registers */

__sfr __at(0XD5) SPCR;
__sfr __at(0XAA) SPSR;
__sfr __at(0X86) SPDAT;

void spi_init();
char spi_tx_rx(char val);
void spi_write_enable();
void spi_write(char val, char addr);
char spi_read(char addr);

#endif


LIBRARY FILE



#include <8052.h>
#include "spi.h"
#include "string.h"

#define SPI_SS P2_7

void spi_init()
{
    SPI_SS = 1;
    SPSR = 0;
    SPCR = 0x5c;
}

char spi_tx_rx(char val)
{
    SPDAT = val;        /* send data out spi port */
    while (!(SPSR & 0x80));    /* wait for transfer to complete */
    SPSR = 0;
    return SPDAT;
}

void spi_write_enable()
{
    SPI_SS = 0;
    spi_tx_rx(SPI_WREN);
    SPI_SS = 1;
    delay(10);
}

void spi_write(char val, char addr)
{
    spi_write_enable();
    SPI_SS = 0;
    spi_tx_rx(SPI_WRITE);
    spi_tx_rx(addr);
    spi_tx_rx(val);
    SPI_SS = 1;
    delay(10);
}

char spi_read(char addr)
{
    char ret;
    SPI_SS = 0;
    spi_tx_rx(SPI_READ);
    spi_tx_rx(addr);
    ret = spi_tx_rx(0);
    SPI_SS = 1;
    return ret;

}