Sunday, 4 August 2013

Keypad and Serial Communication with PC using 8051

#include <reg51.h>

#define COL P2
#define ROW P0



 void MSDelay(unsigned int vaulue);
 void SerTx(unsigned char);
// void lcddata(unsigned char value);

 unsigned char keypad[4][4]= {'0','1','2','3',
                               '4','5','6','7',
      '8','9','A','B',
'C','D','E','F'};
 void main()
 {
 unsigned char colloc, rowloc;
 TMOD=0X20;
TH1=-3;
SCON=0X50;
TR1=1;
  COL=0xFF;
  while(1)
  {
    do
 {
   ROW=0x00;
colloc =COL;
colloc &=0x0F;
}
while(colloc!=0x0F);
    do
{
do
 {
  MSDelay(20);
  colloc=COL;
colloc&=0x0F;
}
while(colloc==0x0F);
MSDelay(20);
  colloc=COL;
colloc&=0x0F;
}
while(colloc==0x0F);
while(1)
{
  ROW=0xFE;
colloc=COL;
colloc&=0x0F;
if(colloc!=0x0F)
{
rowloc=0;
break;
}

ROW=0xFD;
colloc=COL;
colloc&=0x0F;
if(colloc!=0x0F)
{
rowloc=1;
break;
}

ROW=0xFB;
colloc=COL;
colloc&=0x0F;
if(colloc!=0x0F)
{
rowloc=2;
break;
}

ROW=0xF7;
colloc=COL;
colloc&=0x0F;
rowloc=3;
break;
}

if (colloc==0x0E)
SerTx(keypad[rowloc][0]);
  else if(colloc==0x0D)
 SerTx(keypad[rowloc][1]);
else if(colloc==0x0B)
 SerTx(keypad[rowloc][2]);
else
SerTx(keypad[rowloc][3]);
}
}
 
 void SerTx(unsigned char x)
 {
 SBUF=x;
while(TI==0);
TI=0;
 }

 void MSDelay(unsigned int value)
{
 unsigned int x, y;
 for(x=0;x<1275;x++)
 for(y=0;y<value;y++);
 }

Serial Communication with PC using 8051

#include<reg51.h>
sbit MYSW=P2^0;
void main(void)
{
unsigned char z;
unsigned char fname[]=" Abha ";
unsigned char lname[]=" Singh ";
TMOD=0x20;
TH1=0xFD;
SCON=0x50;
TR1=1;
if(MYSW==0)
{
for(z=0;z<10;z++)
{
SBUF=fname[z];
while(TI==0);
TI=0;
}
}
else
{
for(z=0;z<15;z++)
{
SBUF=lname[z];
while(TI==0);
TI=0;
}
}
}

Interfacing LCD and Keypad with 8051

#include <reg51.h>

#define COL P2
#define ROW P0
#define LCD P3
sbit rs = P1^5;
sbit rw = P1^6;
sbit en = P1^7;
sbit refresh = P1^4;

 void MSDelay(unsigned int vaulue);
 void lcdcmd(unsigned char value);
 void lcddata(unsigned char value);

unsigned char keypad[4][4]= {'0','1','2','3',
                               '4','5','6','7',
                 '8','9','A','B',
                'C','D','E','F'};
 void main()
 {
//refresh=1;
 unsigned char colloc, rowloc;
restart:
 lcdcmd(0x38);
 MSDelay(250);
 lcdcmd(0x0E);
 MSDelay(250);
 lcdcmd(0x01);
 MSDelay(250);
 lcdcmd(0x06);
 MSDelay(250);
 lcdcmd(0x80);
  COL=0xFF;
  while(1)
  {
if(refresh==0)
{
goto restart;
     }
    do
 {
   ROW=0x00;
colloc =COL;
colloc &=0x0F;
}
while(colloc!=0x0F);
    do
{
do
 {
  MSDelay(20);
  colloc=COL;
colloc&=0x0F;
}
while(colloc==0x0F);
MSDelay(20);
  colloc=COL;
colloc&=0x0F;
}
while(colloc==0x0F);
while(1)
{
  ROW=0xFE;
colloc=COL;
colloc&=0x0F;
if(colloc!=0x0F)
{
rowloc=0;
break;
}

ROW=0xFD;
colloc=COL;
colloc&=0x0F;
if(colloc!=0x0F)
{
rowloc=1;
break;
}

ROW=0xFB;
colloc=COL;
colloc&=0x0F;
if(colloc!=0x0F)
{
rowloc=2;
break;
}

ROW=0xF7;
colloc=COL;
colloc&=0x0F;
rowloc=3;
break;
}

if (colloc==0x0E)
{ lcddata(keypad[rowloc][0]);
     MSDelay(250); }
else if(colloc==0x0D)
{ lcddata(keypad[rowloc][1]);
     MSDelay(250); }
else if(colloc==0x0B)
 { lcddata(keypad[rowloc][2]);
     MSDelay(250); }
else
{ lcddata(keypad[rowloc][3]);
     MSDelay(250); }
}

}
 
 void lcdcmd(unsigned char value)
 {
 LCD = value;
 rs=0;
 rw=0;
 en=1;
 MSDelay(1);
 en=0;
 return;
 }

 void lcddata(unsigned char value)
 {
 LCD = value;
 rs=1;
 rw=0;
 en=1;
 MSDelay(1);
 en=0;
 return;
 }

void MSDelay(unsigned int value)
{
 unsigned int x, y;
 for(x=0;x<1275;x++)
 for(y=0;y<value;y++);
 }

Interfacing LCD with 8051

//Program to display String on LCD

#include<reg51.h>
#define  lcd_data_pin P1 // data port P2
sbit rs=P2^0; // Register select pin
sbit rw=P2^1; // Read write pin
sbit en=P2^2; // Enable pin
void delay(unsigned int msec)    //delay function
{
int i,j;
for(i=0;i<msec;i++)
for(j=0;j<1275;j++);
}
void lcd_command(unsigned char comm) // function to send command to LCD
{
lcd_data_pin=comm;
en=1;
rs=0;
rw=0;
delay(1);
en=0;
}
void lcd_data(unsigned char disp)    // function to send data on LCD
{
lcd_data_pin=disp;
en=1;
rs=1;
rw=0;
delay(1);
en=0;
}

lcd_dataa(unsigned char *disp)    // function to send string to LCD
{
int x;
for(x=0;disp[x]!=0;x++)
{
lcd_data(disp[x]);

}
}

void lcd_ini()     //Function to inisialize the LCD
{
lcd_command(0x38);  
delay(5);
lcd_command(0x0F);      
delay(5);
lcd_command(0x80);
delay(5);
 

}
void main()
{
lcd_ini();
lcd_dataa("ABHA SINGH");
}

Ethernet


I2C

Introduction

Interrupt

Introduction

Timer

Introduction

RF Module


  • Introduction

RFID


  • Introduction

IR Transmitter-Reciever

Introduction

Android Apps for Project


  • Introduction

GSM Module


  • Introduction

GPS Module


  • Introduction

Xbee (Zigbee) Module

  • Introduction

DC Motor

  • Introduction

Stepper Motor


  • Introduction

LEDs and Switches

We do basic and first program on any microcontroller of LED blinking.

Keypad

LCD (Liquid Crystal Display)

Temperature Sensors: LM35

ADC0804

Celsius scale digital thermometer using AT89C51

Circuit

Code:

//Program to make a digital thermometer with display in centigrade scale

#include<reg51.h>
#define port P3
#define adc_input P1
#define dataport P0
#define sec 100
sbit rs = port^0;
sbit rw = port^1;
sbit e = port^2;
sbit wr= port^3;
sbit rd= port^4;
sbit intr= port^5;

int test_intermediate3=0, test_final=0,test_intermediate1[10],test_intermediate2[3]={0,0,0};

void delay(unsigned int msec )
{
int i ,j ;
for(i=0;i<msec;i++)
  for(j=0; j<1275; j++);
}

void lcd_cmd(unsigned char item)  //Function to send command to LCD
{
dataport = item;
rs= 0;
rw=0;
e=1;
delay(1);
e=0;
return;
}

void lcd_data(unsigned char item) //Function to send data to LCD
{
dataport = item;
rs= 1;
rw=0;
e=1;
delay(1);
e=0;
//delay(100);
return;
}

void lcd_data_string(unsigned char *str)  // Function to send string to LCD
{
int i=0;
while(str[i]!='\0')
{
  lcd_data(str[i]);
  i++;
  delay(10);
}
return;
}

void shape()     // Function to create the shape of degree
{
lcd_cmd(64);
lcd_data(2);
lcd_data(5);
lcd_data(2);
lcd_data(0);
lcd_data(0);
lcd_data(0);
lcd_data(0);
lcd_data(0);
}
     
void convert()    // Function to convert the values of ADC into numeric value to be sent to LCD
{
int s;
test_final=test_intermediate3;
lcd_cmd(0xc1); 
delay(2);
lcd_data_string("TEMP:");
s=test_final/100;
test_final=test_final%100;
lcd_cmd(0xc8);
if(s!=0)
lcd_data(s+48);
else
lcd_cmd(0x06);
s=test_final/10;
test_final=test_final%10;
lcd_data(s+48);
lcd_data(test_final+48);
lcd_data(0);
lcd_data('c');
lcd_data(' ');
delay(2);
}

void main()
{
int i,j;
adc_input=0xff;
lcd_cmd(0x38); 
lcd_cmd(0x0c);  //Display On, Cursor  Blinking
delay(2);
lcd_cmd(0x01);  // Clear Screen
delay(2);

while(1)
{
  for(j=0;j<3;j++)
  {
   for(i=0;i<10;i++)
   {
    delay(1);
    rd=1;
    wr=0;
    delay(1);
    wr=1;
    while(intr==1);
    rd=0;
    lcd_cmd(0x88);
    test_intermediate1[i]=adc_input/10;
    delay(1);
    intr=1;
   }
   for(i=0;i<10;i++)
   test_intermediate2[j]=test_intermediate1[i]+test_intermediate2[j];
  }

test_intermediate2[0]=test_intermediate2[0]/3;
test_intermediate2[1]=test_intermediate2[1]/3;
test_intermediate2[2]=test_intermediate2[2]/3;
test_intermediate3=test_intermediate2[0]+test_intermediate2[1]+test_intermediate2[2];
shape();
convert();
}
}

ADC0804: Introduction

FEATURES
• Compatible with 8080 μP derivatives – no interfacing logic needed – access time 135 ns
• Easy interface to all microprocessors, or operates “stand alone”
• Differential analog voltage inputs
• Logic inputs and outputs meet both MOS and  TTL voltage level specifications
• Works with 2.5V (LM336) voltage reference
• On-chip clock generator
• 0V to 5V analog input voltage range with  single 5V supply
• No zero adjust required
• 0.3" standard width 20-pin DIP package resolution.
• 20-pin molded chip carrier or small outline package
• Operates ratiometrically or with 5 VDC, 2.5 VDC, or analog span adjusted voltage reference
Pin Description



bus stop

Relay

Bluetooth Module

Seven Segment Display

1.Digital Clock using 7-segments on mbed LPC11U24
2.Digital Alarm on mbed LPC11U24 using four 7-segments

Serial Communication with PC, Bluetooth and Xbee(Zigbee)