Monday, 15 October 2012

8051 Keypad with Decoding keys

void DelayMs(int k)
{
    unsigned int a;
    for(a=0;a<=k;a++);
}

unsigned char DecodeKeyVal(char num)
{
    unsigned char keyval=255;
    switch(num)
    {
        case 0xee: keyval = 1;break;
        case 0xed: keyval = 2;break;
        case 0xeb: keyval = 3;break;
        case 0xe7: keyval = 4;break;
        case 0xde: keyval = 5;break;
        case 0xdd: keyval = 6;break;
        case 0xdb: keyval = 7;break;
        case 0xd7: keyval = 8;break;
        case 0xbe: keyval = 9;break;
        case 0xbd: keyval = '*';break;
        case 0xbb: keyval = 0;break;
        case 0xb7: keyval = '#';break;
        case 0x7e: keyval = 'A';break;
        case 0x7d: keyval = 'B';break;
        case 0x7b: keyval = 'C';break;
        case 0x77: keyval = 'D';break;
    }
    return keyval;
}

void Key_Scan(void)
{
    for(i=0;i<4;i++)
    {
        DelayMs(50);
        P2 = ~(0x01<<i);
        DelayMs(50);
        UN = (P2 | 0x0f);
        if(UN != 0xff)
        {
            DelayMs(50);
            UN = (P2 | 0x0f);
            if(UN != 0xff)
            {
                KC = UN & (~(0x01<<i));   
                DelayMs(50);
                KeyBuf[wptr] = DecodeKeyVal(KC);
                wptr++;               
                if(wptr > 25)
                    wptr=0;
                while(UN != 0xff)
                    UN = P2 | 0x0f;
                DelayMs(100);
            }
        }
    }
}

Keypad Matrix with 8051

void Key_Scan(void)
{
    for(i=0;i<4;i++)
    {
        DelayMs(50);
        P2 = ~(0x01<<i);
        DelayMs(50);
        UN = (P2 | 0x0f);
        if(UN != 0xff)
        {
            DelayMs(50);
            UN = (P2 | 0x0f);
            if(UN != 0xff)
            {
                KC = UN & (~(0x01<<i));   
                DelayMs(50);
                DispNumber(KC);
                dig++;
                if(dig == 16)
                    dig = 0;
                while(UN != 0xff)
                    UN = P2 | 0x0f;
                DelayMs(100);
            }
        }
    }
}

void DispNumber(char num)
{
    SendCommand(0xC0+dig,1);
    switch(num)
    {
        case 0xee: SendCommand('1',0);break;
        case 0xed: SendCommand('2',0);break;
        case 0xeb: SendCommand('3',0);break;
        case 0xe7: SendCommand('4',0);break;
        case 0xde: SendCommand('5',0);break;
        case 0xdd: SendCommand('6',0);break;
        case 0xdb: SendCommand('7',0);break;
        case 0xd7: SendCommand('8',0);break;
        case 0xbe: SendCommand('9',0);break;
        case 0xbd: SendCommand('*',0);break;
        case 0xbb: SendCommand('0',0);break;
        case 0xb7: SendCommand('#',0);break;
        case 0x7e: SendCommand('A',0);break;
        case 0x7d: SendCommand('B',0);break;
        case 0x7b: SendCommand('C',0);break;
        case 0x77: SendCommand('D',0);break;
    }
}

Thursday, 26 April 2012

Serial EEPROM 24c16 program

#include <reg52.h>
sbit EECS = P2^0;
sbit EECLCK = P2^1;
sbit EEDATA = P2^2;
unsigned char data eecount,eetemp,eeval,wdata,dev_addr=0xa0;
unsigned char data c,d,flag;
unsigned int data u,eeadd,i,q;

void start(void)
{
    EEDATA=1;
    EECLCK=1;
    EEDATA=0;
    EECLCK=0;
}

void stop(void)
{
    EEDATA=0;
    EECLCK=1;
    EEDATA=1;
    EECLCK=0;
}

void add_data(void)
{
    for(eecount=0;eecount<8;eecount++)
    {
        eetemp=(wdata&0x80);
        if(eetemp==0)
            EEDATA=0;
        else
            EEDATA=1;
        EECLCK=1;   
        EECLCK=0;
        wdata=wdata<<1;
    }
    EEDATA=1;
    EECLCK=1;
    EECLCK=0;
}

void eeread(void)
{
    unsigned char data eread,rdata;
    start();
    wdata=dev_addr;
    add_data();
    wdata=eeadd;
    add_data();
    start();
    wdata=(dev_addr | 0x01);
    add_data();
    EEDATA=1;                   
    for(eread=0;eread<8;eread++)
    {
        EECLCK=1;
            if(EEDATA==1)
                rdata|=1;
            else
                rdata&=(0xfe);
            if(eread != 7)
                rdata<<=1;
        EECLCK=0;
    }
    eeval=rdata;
    EEDATA=1;
    EECLCK=1;
    EECLCK=0;
    stop();
}
void eewrite(void)
{
    start();
    wdata=dev_addr;
    add_data();
    wdata=eeadd;
    add_data();
    wdata=eeval;
    add_data();
    stop();    
    for(u=0;u<300;u++);
}

void changeadd()
{
    if(q == 256)
    {
        dev_addr = 0xa2;
        c = 'B';
        SBUF = 'b';while(!TI);TI = 0;
    }
    else if(q == 512)
    {
        dev_addr = 0xa4;
        c = 'C';
        SBUF = 'c';while(!TI);TI = 0;
    }
    else if(q == 768)
    {
        dev_addr = 0xa6;
        c = 'D';
        SBUF = 'd';while(!TI);TI = 0;
    }
    else if(q == 1024)
    {
        dev_addr = 0xa8;
        c = 'E';
        SBUF = 'e';while(!TI);TI = 0;
    }
    else if(q == 1280)
    {
        dev_addr = 0xaa;
        c = 'F';
        SBUF = 'f';while(!TI);TI = 0;
    }
    else if(q == 1536)
    {
        dev_addr = 0xac;
        c = 'G';
        SBUF = 'g';while(!TI);TI = 0;
    }
    else if(q == 1792)
    {
        dev_addr = 0xae;
        c = 'H';
        SBUF = 'h';while(!TI);TI = 0;
    }
}

void main()
{
    PCON=0x00;
    TH1 = 0xfd;
    TL1 = 0x00;
    SCON=0x50;
    TMOD=0x20;
    TCON = 0x40;
    PSW=0x00;
    IP=0x02;
    IE=0x92;
    TH0=0xf2;
    TL0=0x64;
    TMOD=0x21;
    P2 = 0x00;
    TR0 = 1;
    flag = 0;
    c = 'T';
    SBUF = 'R';while(!TI);TI = 0;
    while(1)
    {
        if(flag == 1)
        {
            eeadd = 0;
            dev_addr = 0xa0;
            for(q=0;q<2048;q++)
            {   
                eeval = c;
                eewrite();
                eeadd++;
                changeadd();
                SBUF = '.';while(!TI);TI = 0;
            }
            flag = 0;
        }
        if(flag == 2)
        {
            eeadd = 0;
            dev_addr = 0xa0;
            for(q=0;q<2048;q++)
            {   
                eeread();
/*                SBUF = ((eeadd/100)+48);while(!TI);TI = 0;   
                SBUF = (((eeadd%100)/10)+48);while(!TI);TI = 0;   
                SBUF = (((eeadd%100)%10)+48);while(!TI);TI = 0;   
                SBUF = '-';while(!TI);TI = 0;*/
                SBUF = eeval;while(!TI);TI = 0;
                eeadd++;
                changeadd();
            }
            flag = 0;
        }
    }
}

void timer() interrupt 1 using 1
{
    TR0=0;
    TH0=0xf2;
    TL0=0x64;
    TR0=1;
}

void serial() interrupt 4 using 2
{
    if(RI)
    {
        RI = 0;
        d = SBUF;
        if(d == 'W' || d == 'w')
            flag = 1;
        else if(d == 'R' || d == 'r')
            flag = 2;
    }
}

Serial EEPROM 24c32 Example Program

#include <reg52.h>
sbit EECS = P2^0;
sbit EECLCK = P2^1;
sbit EEDATA = P2^2;
unsigned char data eecount,eetemp,eeval,wdata,dev_addr=0xa0;
unsigned char data c,d,flag;
unsigned int data u,eeadd,eeadd1,i,q;

void start(void)
{
    EEDATA=1;
    EECLCK=1;
    EEDATA=0;
    EECLCK=0;
}

void stop(void)
{
    EEDATA=0;
    EECLCK=1;
    EEDATA=1;
    EECLCK=0;
}

void add_data(void)
{
    for(eecount=0;eecount<8;eecount++)
    {
        eetemp=(wdata&0x80);
        if(eetemp==0)
            EEDATA=0;
        else
            EEDATA=1;
        EECLCK=1;   
        EECLCK=0;
        wdata=wdata<<1;
    }
    EEDATA=1;
    EECLCK=1;
    EECLCK=0;
}

void eeread(void)
{
    unsigned char data eread,rdata;
    start();
    wdata=dev_addr;
    add_data();
    wdata=eeadd1;
    add_data();
    wdata=eeadd;
    add_data();
    start();
    wdata=(dev_addr | 0x01);
    add_data();
    EEDATA=1;                   
    for(eread=0;eread<8;eread++)
    {
        EECLCK=1;
            if(EEDATA==1)
                rdata|=1;
            else
                rdata&=(0xfe);
            if(eread != 7)
                rdata<<=1;
        EECLCK=0;
    }
    eeval=rdata;
    EEDATA=1;
    EECLCK=1;
    EECLCK=0;
    stop();
}
void eewrite(void)
{
    start();
    wdata=dev_addr;
    add_data();
    wdata=eeadd1;
    add_data();
    wdata=eeadd;
    add_data();
    wdata=eeval;
    add_data();
    stop();    
    for(u=0;u<300;u++);
}

void changeadd()
{
    if(q == 256)
    {
        c++;eeadd1++;eeadd = 0;
        SBUF = 'b';while(!TI);TI = 0;
    }
    else if(q == 512)
    {
        c++;eeadd1++;eeadd = 0;
        SBUF = 'c';while(!TI);TI = 0;
    }
    else if(q == 768)
    {
        c++;eeadd1++;eeadd = 0;
        SBUF = 'd';while(!TI);TI = 0;
    }
    else if(q == 1024)
    {
        c++;eeadd1++;eeadd = 0;
        SBUF = 'e';while(!TI);TI = 0;
    }
    else if(q == 1280)
    {
        c++;eeadd1++;eeadd = 0;
        SBUF = 'f';while(!TI);TI = 0;
    }
    else if(q == 1536)
    {
        c++;eeadd1++;eeadd = 0;
        SBUF = 'g';while(!TI);TI = 0;
    }
    else if(q == 1792)
    {
        c++;eeadd1++;eeadd = 0;
        SBUF = 'h';while(!TI);TI = 0;
    }
    else if(q == 2048)
    {
        c++;eeadd1++;eeadd = 0;
        SBUF = 'i';while(!TI);TI = 0;
    }
    else if(q == 2304)
    {
        c++;eeadd1++;eeadd = 0;
        SBUF = 'j';while(!TI);TI = 0;
    }
    else if(q == 2560)
    {
        c++;eeadd1++;eeadd = 0;
        SBUF = 'k';while(!TI);TI = 0;
    }
    else if(q == 2816)
    {
        c++;eeadd1++;eeadd = 0;
        SBUF = 'l';while(!TI);TI = 0;
    }
    else if(q == 3072)
    {
        c++;eeadd1++;eeadd = 0;
        SBUF = 'm';while(!TI);TI = 0;
    }
    else if(q == 3328)
    {
        c++;eeadd1++;eeadd = 0;
        SBUF = 'n';while(!TI);TI = 0;
    }
    else if(q == 3584)
    {
        c++;eeadd1++;eeadd = 0;
        SBUF = 'o';while(!TI);TI = 0;
    }
    else if(q == 3840)
    {
        c++;eeadd1++;eeadd = 0;
        SBUF = 'p';while(!TI);TI = 0;
    }
}

void main()
{
    PCON=0x00;
    TH1 = 0xfd;
    TL1 = 0x00;
    SCON=0x50;
    TMOD=0x20;
    TCON = 0x40;
    PSW=0x00;
    IP=0x02;
    IE=0x92;
    TH0=0xf2;
    TL0=0x64;
    TMOD=0x21;
    P2 = 0x00;
    TR0 = 1;
    flag = 0;
    c = 'A';
    SBUF = 'R';while(!TI);TI = 0;
    while(1)
    {
        if(flag == 1)
        {
            eeadd = 0;eeadd1 = 0;
            for(q=0;q<4096;q++)
            {   
                eeval = c;
                eewrite();
                eeadd++;
                changeadd();
                SBUF = '.';while(!TI);TI = 0;
            }
            flag = 0;
        }
        if(flag == 2)
        {
            eeadd = 0;eeadd1 = 0;
            for(q=0;q<4096;q++)
            {   
                eeread();
                SBUF = eeval;while(!TI);TI = 0;
                eeadd++;
                changeadd();
            }
            flag = 0;
        }
        if(flag == 3)
        {
            eeadd = 200;
            eeval = 'Z';
            for(q=0;q<25;q++)
            {
                eewrite();
                eeadd++;
            }
            eeadd1++;
        }
    }
}

void timer() interrupt 1 using 1
{
    TR0=0;
    TH0=0xf2;
    TL0=0x64;
    TR0=1;
}

void serial() interrupt 4 using 2
{
    if(RI)
    {
        RI = 0;
        d = SBUF;
        if(d == 'W' || d == 'w')
            flag = 1;
        else if(d == 'R' || d == 'r')
            flag = 2;
        else if(d == 'P' || d == 'p')
        {
            flag = 3;eeadd1=0;
        }
    }
}

Wednesday, 25 April 2012

Create a Jar File

Creating a jar File in Command Prompt

  1. Start Command Prompt.
  2. Navigate to the folder that holds your class files:
    C:\>cd \mywork
  3. Set path to include JDK’s bin.  For example:
    C:\mywork> path c:\Program Files\Java\jdk1.5.0_09\bin;%path%
  4. Compile your class(es):
    C:\mywork> javac *.java
  5. Create a manifest file:
    C:\mywork> echo Main-Class: DanceStudio >manifest.txt
  6. Create a jar file:
    C:\mywork> jar cvfm DanceStudio.jar manifest.txt *.class
  7. Test your jar:
    C:\mywork> DanceStudio.jar

Sunday, 25 March 2012

Basic of Network Protocol

ARP

When a device needs to send an IP packet to another device on the local network, the IP software will first check to see if it knows the hardware address associated with the destination IP address. If it founds then the sender simply transmit the data to the destination. However, if the destination system's hardware address is not known then the IP software has to locate  it before any data can be sent. at this point, IP will call on ARP to locate the hardware address of the destination system.
ARP achieves this task by issuing a low level broadcast onto the network, equesting that the system that is using the specified IP address respond with its hardware address. if the destination system is powered up and on the network, it will see this broadcast (As will all  of the other devices on the local network), and it will return an ARP response back to the original system. not that the response is not broadcast back of the network. but is instead sent directly to the requesting device.

ARP structure

ARP packets work at the data-link layer, the same as IP packets. As such, ARP packets are completely separate from IP packets; they even have a different protocol  ID of 0806, instead of 0800 as used with IP.ARP packets contain several fields, although only five of them are actually used to proved ARP's functionality.
Field Usage
Source Hardware address The hardware address of the sender's device
Source IP address The network address of the senders device
Destination Hardware address The hardware address of the receiver's device (when this field is unknown ARP set it to all-zero)
Destination IP address The network address of the reciver's device
Message-Type Indicates whether the current ARP packets is a request or a response to a request

ARP Request

When a device is sending ARP request, it fills in three of the four address-related fields, providing its own hardware and IP address, as well as the IP address of the target. the destination hardware address is yet unknown so that field is filled with zeros. In addition it will set the message type to indicate that the current  packet is an ARP request, and then broadcast the request onto the local network for all devices to see

ARP Reply

All of the local devices should monitor the network for ARP broadcasts, and when ever they see a request for themselves they should generate a response packet and send it back to the requesting system. The response packet will consist of the local device's IP address of the original sender. The response will also be marked as as such, with the message-type field indicating that the current packet is an ARP response. the new ARP packet is then unicast directly to the original requester, where it is received and processed.