Thursday, January 28, 2010

8086MICROPROCESSOR INTERRUPTS

#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
#include<stdlib.h>

void showbits(int);
int flag=0;
struct Mouse    //Construct a new structure
{
    int x;        //The x coordinate of mouse
    int y;        //The y coordinate
    int button;    //The button status
};

void start_show_mouse()
{
    _AX=0;
    geninterrupt(0x33);    //Start mouse
    _AX=1;
    geninterrupt(0x33);    //Show mouse
}
Mouse GetMouse()
{
    Mouse t;
    _AX=3;
    geninterrupt(0x33);    //Get Button and coordinates
    t.x=_CX;
    t.y=_DX;
    t.button=_BX;
    return t;
}
void main()
{
int i,ch,f,ah,al;
union REGS in,out;
clrscr();
gotoxy(12,3);
textcolor(YELLOW);
cprintf("HARDWARE IMPLEMENTATION OF A SYSTEM USING C PROGRAMMING");
gotoxy(25,12);
printf("1.keyboard services");
gotoxy(25,13);
printf("2.mouse services");
gotoxy(25,14);
printf("3.exit");
gotoxy(25,15);
printf("enter choice\t");
scanf("%d",&ch);
switch(ch)
          { case 1:   clrscr();
                      gotoxy(24,12);
                      printf("1.read keyboard character");
                      gotoxy(24,13);
                      printf("2.get shift status");
                      gotoxy(24,14);
                      printf("3.keyboard enable/disable");
                      gotoxy(24,15);
                      printf("4.return to main menu");
                      gotoxy(24,16);
                      printf("enter choice\t");
                      scanf("%d",&f);
                     switch(f)
                      {
                    case 1: clrscr();
                            in.h.ah=0;            //Service No. to Read Next Character from Keyboard
                            gotoxy(25,12);
                            printf("Hit any key.......");
                            int86(22,&in,&out);   //Interrupt for Keyboard Service
                            ah=out.h.ah;          //Returned Scan Code
                            al=out.h.al;          //Returned ASCII Code
                            gotoxy(25,14);
                            printf("BIOS Scan code= %d ",ah);
                            gotoxy(25,16);
                            printf("ASCII code= %d ",al);
                            gotoxy(25,18);
                            printf("Press any key to continue.......");
                            getch();
                            clrscr();
                            main();
                    case 2: clrscr();
                            in.h.ah=2;            //Service No. to Get Shift Status
                            int86(22,&in,&out);   //Interrupt for Keyboard Service
                            al=out.h.al;          //Returned Shift Status
                            clrscr();
                            gotoxy(5,3);
                            printf("Shift status is= ");
                            flag=1;
                            showbits(al);
                            gotoxy(5,6);
                            printf("From Least Significant Bit: ");
                            gotoxy(5,8);
                            printf("Bits");
                            gotoxy(5,9);
                            printf("Bit 0 = 1:Right shift depressed");
                            gotoxy(5,10);
                            printf("Bit 1 = 1:Left shift depressed");
                            gotoxy(5,11);
                            printf("Bit 2 = 1:Ctrl depressed");
                            gotoxy(5,12);
                            printf("Bit 3 = 1:Alt depressed");
                            gotoxy(5,13);
                            printf("Bit 4 = 1:Scroll Lock On");
                            gotoxy(5,14);
                            printf("Bit 5 = 1:Num Lock On");
                            gotoxy(5,15);
                            printf("Bit 6 = 1:Caps Lock On");
                            gotoxy(5,16);
                            printf("Bit 7 = 1:Insert On");
                            gotoxy(5,18);
                            printf("Press any key to continue.......");
                            getch();
                            clrscr();
                            main();
                    case 3: clrscr();
                            outportb(0x64,0xAD);    /*Code for disabling the KEYBOARD*/
                            gotoxy(24,13);
                            printf("Keyboard Disabled\n");
                            delay(2000);
                            for(i=30;i>=0;i--)
                            {
                            clrscr();
                            gotoxy(24,13);
                            printf("  your keyboard will be enabled in %d seconds"  ,i);
                            delay(1000);
                            }
                            clrscr();
                            outportb(0x64,0xAE);    /*Code for enabling the KEYBOARD*/
                            gotoxy(24,13);
                            printf("keyboard enabled");
                            delay(2000);
                            main();

                    case 4:main();
                    }

case 2:clrscr();
       Mouse m;
       start_show_mouse();
       gotoxy(1,25);
       printf("Press left mouse button to quit");
       while(1)
       {
        m=GetMouse();
        gotoxy(1,13);
        printf("the position of the cursor is[%3d,%3d]",m.x,m.y);
        if(m.button==1)    //If left button is pressed
        main();            //then quit
       }
case 3:exit(1);
}
getch();
}
void showbits(int n)
{
    int i,k,andmask;
    if(flag!=1)
    {
        for(i=15;i>=0;i--)
        {
            andmask=1<<i;
            k=n & andmask;
            k==0?printf("0"):printf("1");
        }
    }
    else
    {
        for(i=7;i>=0;i--)
        {
            andmask=1<<i;
            k=n & andmask;
            k==0?printf("0"):printf("1");
        }
        flag=0;
    }
}

FOLLOWERS