C Programming Code Examples
C > Hardware Interaction Through C Code Examples
Program to write data in DMA Mode
/* Program to write data in DMA Mode */
# include<iostream.h>
# include<stdio.h>
# include<conio.h>
# include<stdlib.h>
# include<dos.h>
void main()
{
clrscr();
union REGS regs;
int ans;
char arr[1000];
outp(0x3f2,0x1c); //Motor On
delay(200);
outp(0x3f5,0x0f); //Command Code
delay(200);
outp(0x3f5,0x00); //Command Code
delay(200);
outp(0x3f5,0); //Cylinder no.
delay(200);
outp(0x3f5,0x08); //Sense Interrupt Command
delay(200);
ans=inp(0x3f5); //Reading ST0 in data register
delay(100);
ans=inp(0x3f5); //pcn
cout<<endl<<hex<<ans;
delay(200);
outportb(0x12,0); /*initialization of DMA Mode*/
delay(200);
outportb(0x11,10); /*supplying Mode Byte*/
int ar=FP_OFF(arr);
int ar1=FP_SEG(arr);
outportb(0x10,2); /*supplying channel no. on port 10*/
regs.h.ch = (ar1)&(0x0f00);
regs.x.ax = regs.h.ah+ar;
(regs.h.ch)++;
outportb(0x04,regs.h.al);
delay(200);
outportb(0x04,regs.h.ah);
delay(200);
outportb(0x81,regs.h.ch);
delay(200);
outportb(0x05,1);
// DMA End
//READ command
delay(200);
outportb(0x3f5,46); //Command Code
delay(200);
outportb(0x3f5,0); //Command Code
delay(200);
outportb(0x3f5,0); //Cylinder no.
delay(200);
outportb(0x3f5,0); //Head Addr
delay(200);
outportb(0x3f5,3); //Record
delay(200);
outportb(0x3f5,2); //Sector size
delay(200);
outportb(0x3f5,5); //EOT
delay(200);
outportb(0x3f5,21); //GPL
delay(200);
outportb(0x3f5,10); //DTL
//result
delay(200);
// st0
int st0 = inportb(0x3f5);
printf("
st0 = %x",st0);
if(inportb(0x3f5) & (192)!=0)
{
printf("
st0= %d",st0);
cout<<"
Abnormal termination st0";
delay(200);
outp(0x3f2,0x0c); //Motor off
exit(0);
}
delay(200);
int st1 = inportb(0x3f5);
printf("
st1 = %x",st1);
if((st1=inportb(0x3f5)) !=0)
{
printf("
st1=%d ",st1);
cout<<"
Abnormal termination st1";
delay(200);
outp(0x3f2,0x0c); //Motor off
exit(0);
}
delay(200);
int st2 = inportb(0x3f5);
printf("
st2 = %x",st2);
if((st2=inportb(0x3f5)) != 0)
{
cout<<"st2= "<<st2;
cout<<"
Abnormal termination st2";
delay(200);
outp(0x3f2,0x0c); //Motor off
exit(0);
}
cout<<"
Successful Termination";
delay(200);
cout<<"
c= "<<inportb(0x3f5);
delay(200);
cout<<"
h= "<<inportb(0x3f5);
delay(200);
cout<<"
r= "<<inportb(0x3f5);
delay(200);
cout<<"
n= "<<inportb(0x3f5);
delay(200);
outp(0x3f2,0x0c); //Motor off
getch();
}
In C, the "main" function is treated the same as every function, it has a return type (and in some cases accepts inputs via parameters). The only difference is that the main function is "called" by the operating system when the user runs the program. Thus the main function is always the first code executed when a program starts. main() function is a user defined, body of the function is defined by the programmer or we can say main() is programmer/user implemented function, whose prototype is predefined in the compiler. Hence we can say that main() in c programming is user defined as well as predefined because it's prototype is predefined. main() is a system (compiler) declared function whose defined by the user, which is invoked automatically by the operating system when program is being executed.
Function clrscr() clears the screen and moves the cursor to the upper left-hand corner of the screen. If you are using the GCC compiler, use system function to execute the clear/cls command. clrscr() function is also a non-standard function defined in "conio.h" header. This function is used to clear the console screen. It is often used at the beginning of the program (mostly after variable declaration but not necessarily) so that the console is clear for our output.
Writes the C string pointed by format to the standard output (stdout). If format includes format specifiers (subsequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers. printf format string refers to a control parameter used by a class of functions in the input/output libraries of C programming language. The string is written in a simple template language: characters are usually copied literally into the function's output, but format specifiers, which start with a % character, indicate the location and method to translate a piece of data (such as a number) to characters. "printf" is the name of one of the main C output functions, and stands for "print formatted". printf format strings are complementary to scanf format strings, which provide formatted input (parsing). In both cases these provide simple functionality and fixed format compared to more sophisticated and flexible template engines or parsers,
Write one byte to a 80x86 hardware port. The outp() function writes one byte, determined by value, to the 80x86 hardware port whose number is given by port. A hardware port is used to communicate with a device. One, two or four bytes can be read and/or written from each port, depending on the hardware. Consult the technical documentation for your computer to determine the port numbers for a device and the expected usage of each port for a device.
The exit() function is used to terminate a process or function calling immediately in the program. It means any open file or function belonging to the process is closed immediately as the exit() function occurred in the program. The exit() function is the standard library function of the C, which is defined in the stdlib.h header file. So, we can say it is the function that forcefully terminates the current program and transfers the control to the operating system to exit the program. The exit(0) function determines the program terminates without any error message, and then the exit(1) function determines the program forcefully terminates the execution process.
Delay function is used to suspend execution of a program for a particular time. delay() function requires a parameter which should be a number, defining the milliseconds for the delay. To use delay function in your program you should include the "dos.h" header file which is not a part of standard C library. Here unsigned int is the number of milliseconds (remember 1 second = 1000 milliseconds).
The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program.
The if-else statement is used to perform two operations for a single condition. The if-else statement is an extension to the if statement using which, we can perform two different operations, i.e., one is for the correctness of that condition, and the other is for the incorrectness of the condition. Here, we must notice that if and else block cannot be executed simiulteneously. Using if-else statement is always preferable since it always invokes an otherwise case with every if condition.
Display the content from the same file you are writing the source code. Open the file you are currently writing using statement fopen and assign it to the pointer fp. Scan the every
File I/O for Binary files in C. Pointers for both binary files. Open for binary2.exe file in rb mode. Test logic for successful open. Open file binary2.exe in "wb" mode for writing and