Tuesday, 3 November 2015

AT89C51 8BIT LCD INTERFACE (ARRAY METHOD)





 #include<reg51.h>
 sbit RS=P0^0;
 sbit RW=P0^1;
 sbit EN=P0^2;
 #define ldata P1
 void lcd_init();
 void cmd(unsigned char c);
 void dat(unsigned char c);
 void delay(unsigned int i);
 void main()
 {
 int p;
 char arr[12]={'E','M','B','E','D','D','E','D',' ','L','A','B'};
  lcd_init();
 for(p=0;p<12;p++)
 dat(arr[p]);
 while(1);

 }

 void lcd_init()
 {
 cmd(0X01);
 delay(10);
 cmd(0X08);
 delay(10);
 cmd(0X0e);
 delay(10);
 cmd(0X38);
 delay(10);
 }
 void cmd(unsigned char c)
 {
  int i;
  ldata=c;
  RS=0;
  RW=0;
  EN=1;
  for(i=0;i<10;i++);
  EN=0;
  return;
 }
 void dat(unsigned char c)
 {
 int i;
 ldata=c;
 RS=1;
 RW=0;
 EN=1;
 for(i=0;i<=10;i++);
 EN=0;
 return;
 }
 void delay(unsigned int b)
 {
 unsigned int i,j;
 for(i=0;i<b;i++)
 for(j=0;j<3000;j++);
 }

AT89C51 LCD(4BIT) INTERFACE





#include<reg51.h>
void cmd(unsigned char c);
void dat(unsigned char c);
void init();

void delay(unsigned int i)
{
while(i--);
}
void string(unsigned char *);
void main()
{
init();
cmd (0x80);
string("EMBEDDED LAB");
cmd (0xc0);
string("WWW.SUMMITBHARDWAJ.BLOGSPOT.IN");
while(1)
{
}
}
void init()
{
cmd(0x02);
delay(1000);
cmd(0x28);
delay(1000);
cmd(0x01);
delay(1000);
cmd(0x0e);
delay(1000);

}
void cmd(unsigned char c)
{
unsigned char l,h;
l=(c&0x0f);
h=(c>>4);
P3=h;
P2=0x04;
delay(1000);
P2=0x00;
P3=l;
P2=0x04;
delay(1000);
P2=0x00;
}
void dat(unsigned char c)
{
unsigned char l,h;
l=c&0x0f;
h=c>>4;
P3=h;
P2=0x05;
delay(1000);
P2=0x01;
P3=l;
P2=0x05;
delay(1000);
P2=0x01;
}
void string(unsigned char *p)
{
while(*p!='\0')
{
delay(100);
 dat(*p);
 p++;
 }

 }

AT89C51 KEYPAD VALUE ON LCD




#include<reg51.h>
#define port P3
sbit rs=P2^0;
sbit rw=P2^1;
sbit e=P2^2;
void delay()
{
unsigned int i;
for(i=0;i<3000;i++);
}
void cmd(unsigned int c)
{
port=c;
rs=0;
rw=0;
e=1;
delay();
e=0;
}
void dat(unsigned int c)
{
port=c;
rs=1;
rw=0 ;
e=1  ;
delay();
e=0;
}
void lcd_init()
{
cmd(0x01);
cmd(0x80);
cmd(0x0c);
cmd(0x38);
}
 void digit(unsigned int i)
 {
  unsigned char a,k=0x82;
   cmd(0x01);
  do
  {
  cmd(k--);
  a=i%10;
  dat(48+a);
  cmd(0x80);
  delay();
  i=i/10;
 }
 while(i>0);
 }
void main()
{
int a,b,c;
lcd_init();
P2=0x00;
while(1)
{
P1=0xf0;
while(P1==0xf0);
a=P1;
P1=0x0f;
b=P1;
while(P1!=0x0f);
c=a|b;
P2=c;
if(c==0xe7)
{
digit(1);
}
else if(c==0xeb)
{
digit(2);
}
else if(c==0xed)
{
digit(3);

}
else if(c==0xee)
{
digit(4);

}
else if(c==0xd7)
{
digit(5);
}
else if(c==0xdb)
{
digit(6);
}
else if(c==0xdd)
{
digit(7);
}
else if(c==0xde)
{
digit(8);
}
else if(c==0xb7)
{
digit(9);
}
else if(c==0xbb)
{
digit(10);
}
else if(c==0xbd)
{
digit(11);
}
else if(c==0xbe)
{
digit(12);
}
else if(c==0x77)
{
digit(13);
}
else if(c==0x7b)
{
digit(14);
}
else if(c==0x7d)
{
digit(15);
}
else if(c==0x7e)
{
digit(16);
}
}
}





AT89C51 KEYPAD VALUE ON LED BAR



#include<reg51.h>
void delay(unsigned int i)
{
while(i--);
}
void main()
{
int a,b,c;
P2=0x00;
while(1)
{
P1=0xf0;
while(P1==0xf0);
a=P1;
P1=0x0f;
b=P1;
c=a|b;
P2=c;
}
}

Tuesday, 20 October 2015

AT89C51 COUNTER




#include<reg51.h>
void main()
{
int x;
TMOD=0x05;
TR0=1;
TH0=0;
TL0=0;
while(1)
{
while(TF0==0)
{
P1=TH0;
P2=TL0;
}
TF0=0;
}
}

AT89C51 ADC




















#include<reg51.h>
#include<stdio.h>
 sbit RS=P0^0;
 sbit RW=P0^1;
 sbit EN=P0^2;
 sbit cs=P2^0;
 sbit rd=P2^1;
 sbit wr=P2^2;
 sbit intr=P2^3;
 #define ldata P1
 void lcd_init();
 void cmd(unsigned char c);
 void dat(unsigned char c);
 void delay(unsigned int i);
 void digit(unsigned int i);
 int adc()
 {
 unsigned int x;
 cs=0;
 wr=0;
 delay(1);
 wr=1;
 rd=1;
while(intr==1);
 rd=0;
 x=P3;
 return x;
 }
 void main()
 {
 unsigned char z[20],i=0,s[]="WWW.SUMMITBHARDWAJ.BLOGSPOT.IN";
  unsigned int v,a,t;
lcd_init();
 cmd(0x80);

 while(s[i]!='\0')
 {
 dat(s[i]);
 i++;
}
 
   while(1)
{
a=adc();
v=((a*5)/255);
t=((a*5)/2.55);
sprintf(z,"ADC=%3d volt=%2dV temp=%3d'c",a,v,t);
 cmd(0xc0);
 i=0;
 while(z[i]!='\0')
 {
 dat(z[i]);
 i++;
}
}
 }
 
 void digit(unsigned int i)
 {
 unsigned char a,c=0x85;

 do
 {
  cmd(c--);
 a=i%10;
 dat(48+a);
 cmd(0x80);
 delay(10);
 i=i/10;
 }
 while(i>0);
 }

 void lcd_init()
 {
 cmd(0X01);
 delay(10);
 cmd(0X08);
 delay(10);
 cmd(0X0c);
 delay(10);
 cmd(0X38);
 delay(10);
 }
 void cmd(unsigned char c)
 {
  int i;
  ldata=c;
  RS=0;
  RW=0;
  EN=1;
  for(i=0;i<10;i++);
  EN=0;
  return;
 }
 void dat(unsigned char c)
 {
 int i;
 ldata=c;
 RS=1;
 RW=0;
 EN=1;
 for(i=0;i<=10;i++);
 EN=0;
 return;
 }
 void delay(unsigned int b)
 {
 unsigned int i,j;
 for(i=0;i<b;i++)
 for(j=0;j<3000;j++);
 }

PIC16F1518 SERIAL COMMUNICATION




#include<pic.h>
#include<htc.h>
#define __PIC16F1518_H
#define _XTAL_FREQ   16000000

__CONFIG( FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF &  WRT_OFF );

void TX(unsigned char Byte);
unsigned int a1;

void interrupt ISR(void)
{
if(RCIF)  // If UART Rx Interrupt
{
if(OERR) // If over run error, then reset the receiver
{
CREN = 0;
CREN = 1;
}

TX(RCREG); // Echo back received char
}
}


void UART(void)
{
TRISC6 = 0;   // TX Pin
TRISC7 = 1;   // RX Pin

SPBRG = 103;
BRGH  = 1;                   // Fast baudrate
SYNC  = 0; // Asynchronous
SPEN  = 1; // Enable serial port pins
CREN  = 1; // Enable reception
SREN  = 0; // No effect
TXIE  = 0; // Disable tx interrupts
RCIE  = 1; // Enable rx interrupts
TXEN  = 0; // Reset transmitter
TXEN  = 1; // Enable the transmitter
}


void TX(unsigned char Byte)  // Writes a character to the serial port
{
while(!TXIF);  // wait for previous transmission to finish
TXREG = Byte;
}

unsigned char RX(void)   // Reads a character from the serial port
{
if(OERR) // If over run error, then reset the receiver
{
CREN = 0;
CREN = 1;
}

        while(!RCIF);  // Wait for transmission to receive
a1 = RCREG;
return a1;
}

void String(unsigned char*p)
{
while(*p)
TX(*p++);
}

void main(void)
{
UART(); // Intialize UART

    String("EMBEDDED_LAB,WWW.SUMMITBHARDWAJ.BLOGSPOT.IN"); // Send string on UART

GIE  = 1;   // Enable global interrupts
        PEIE = 1;   // Enable Peripheral Interrupts

while(1)
{

}
}