Monday, 30 September 2013

Communication between two mbeds LPC11U24 via SPI and each have 2 LEDS and 2 switches

 Code for Master mbed:
#include "mbed.h"
SPI ser_port(p11, p12, p13);
DigitalOut red_led(LED1);
DigitalOut green_led(LED2);
DigitalOut cs(p14);
DigitalIn switch_ip2(p7);
DigitalIn switch_ip1(p6);
char switch_word;
char recd_val;

int main() {
    while(1) {
    switch_word=0xa0;
     if(switch_ip1==1)
    switch_word=switch_word|0x01;
    if(switch_ip2==1)
    switch_word=switch_word|0x02;
    cs=0;
    recd_val=ser_port.write(switch_word);
    cs=1;
    wait(0.1);
    red_led = 0;
    green_led = 0;
    recd_val=recd_val&0x03;
    if(recd_val==1)
    red_led=1;
    if(recd_val==2)
    green_led=1;
    if(recd_val==3){
    red_led=1;
    green_led=1;
    }
    }

}

Code for Slave mbed
#include "mbed.h"
SPISlave ser_port(p11, p12, p13, p14);
DigitalOut red_led(LED1);
DigitalOut green_led(LED2);
//DigitalOut cs(p14);
DigitalIn switch_ip2(p7);
DigitalIn switch_ip1(p6);
char switch_word;
char recd_val;

int main() {
    while(1) {
    switch_word=0xa0;
     if(switch_ip1==1)
    switch_word=switch_word|0x01;
    if(switch_ip2==1)
    switch_word=switch_word|0x02;
    if(ser_port.receive()){
    recd_val=ser_port.read();
    ser_port.reply(switch_word);
    }
    red_led = 0;
    green_led = 0;
    recd_val=recd_val&0x03;
    if(recd_val==1)
    red_led=1;
    if(recd_val==2)
    green_led=1;
    if(recd_val==3){
    red_led=1;
    green_led=1;
    }
    }
}


SPI communication

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");
}