plz can u explain this code
Discussion in "Project Help" started by vinay Sep 15, 2007.
Sat Sep 15 2007, 09:33 pm
i got some code for device control. here i am not able to crck some code,can u plz help me
#include<dos.h>
#include<stdio.h>
#include<conio.h>
#define PORT 0x378 /* This is the parallel port address */
main()
{
char val=0,key=0;
char str1[]="ON ";
char str2[]="OFF";
char *str;
clrscr();
printf("Press the approriate number key to turn on/off devices:\n\n");
printf("Here Device1 is connected to D0 of parallel port and so on\n\n");
printf("Press \"x\" to quit\n\n");
gotoxy(1,8);
printf("Device1:OFF Device2:OFF Device3:OFF Device4:OFF\n");
printf("Device5:OFF Device6:OFF Device7:OFF Device8:OFF");
while(key!='x' && key!='X')
{
gotoxy(1,12);
printf("Value in hex sent to the port:");
key=getch();
switch(key){
case '1':
gotoxy(9,8);
val=(val&0x01)?(val&(~0x01)):val|0x01;
str=(val&0x01)?str1:str2;
printf("%s",str);
outportb(PORT,val);
gotoxy(1,13);
printf("%x",val);
break;
i cant understand that bolded coded. can u plaese help
#include<dos.h>
#include<stdio.h>
#include<conio.h>
#define PORT 0x378 /* This is the parallel port address */
main()
{
char val=0,key=0;
char str1[]="ON ";
char str2[]="OFF";
char *str;
clrscr();
printf("Press the approriate number key to turn on/off devices:\n\n");
printf("Here Device1 is connected to D0 of parallel port and so on\n\n");
printf("Press \"x\" to quit\n\n");
gotoxy(1,8);
printf("Device1:OFF Device2:OFF Device3:OFF Device4:OFF\n");
printf("Device5:OFF Device6:OFF Device7:OFF Device8:OFF");
while(key!='x' && key!='X')
{
gotoxy(1,12);
printf("Value in hex sent to the port:");
key=getch();
switch(key){
case '1':
gotoxy(9,8);
val=(val&0x01)?(val&(~0x01)):val|0x01;
str=(val&0x01)?str1:str2;
printf("%s",str);
outportb(PORT,val);
gotoxy(1,13);
printf("%x",val);
break;
i cant understand that bolded coded. can u plaese help
Sat Sep 15 2007, 11:55 pm
Well it seems simple.. I hope i am correct.. and if i am not please correct me..
This part of code is to display the status of device.. either of or on.. and the first instruction is to set or clear the value..
i mean it checks if its set, then clear it.. if its 0 then make it 1.
because the part of code...
can also be written as..
and after that this value will be sent to the parallel port... as well as displayed on the screen
Hope you understood..
gotoxy(9,8); val=(val&0x01)?(val&(~0x01)):val|0x01; str=(val&0x01)?str1:str2; printf("%s",str);
This part of code is to display the status of device.. either of or on.. and the first instruction is to set or clear the value..
i mean it checks if its set, then clear it.. if its 0 then make it 1.
because the part of code...
val=(val&0x01)?(val&(~0x01)):val|0x01;
can also be written as..
if(val&0x01) val = val&(~0x01); else val = val|0x01;
and after that this value will be sent to the parallel port... as well as displayed on the screen
outportb(PORT,val); gotoxy(1,13); printf("%x",val); break;
Hope you understood..
Sun Sep 16 2007, 01:12 am
i had used these codes before 0x01 is for LSB bit 1 (D0)of parallel port (DB25) ie pin 2 , 0x02 for D1, 0x04 for D2, 0x08 for D3 and so on upto D7 (ie bit 8 of parallel port pin no 9) :-)
Powered by e107 Forum System