C Programming Code Examples
C > Hardware Interaction Through C Code Examples
Program to Implement Seek Command
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* Program to Implement Seek Command */
# 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,0x0b); //Cylinder no.
delay(200);
outp(0x3f5,0x08); //Sense Interrupt Command
delay(200);
ans=inp(0x3f5); //Reading ST0 in data register
// cout<<endl<<hex<<ans;
delay(100);
ans=inp(0x3f5); //pcn
cout<<endl<<hex<<ans;
outportb(0x12,0); /*initialization of DMA Mode*/
outportb(0x11,10); /*supplying Mode Byte*/
clrscr();
int ar=FP_OFF(arr);
int ar1=FP_SEG(arr);
printf(" %x",ar1);
regs.h.ch = (ar1)&(0x0f00);
printf(" %x",(regs.h.ch));
regs.x.ax = regs.h.ah+ar;
(regs.h.ch)++;
printf(" %x",regs.x.ax);
int z = (regs.x.ax) & (0x0f00);
cout<<"
"<<z;
getch();
outportb(0x04,regs.h.al);
outportb(0x04,regs.h.ah);
outportb(0x81,regs.h.ch);
outportb(0x05,1);
// dma end
outp(0x3f2,0x0c); //Motor off
return;
}
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.
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).
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.
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.
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.
A union is a special data type available in C that allows to store different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multiple-purpose. To define a union, you must use the union statement in the same way as you did while defining a structure. The union statement defines a new data type with more than one member for your program. The format of the union statement is as follows:
Program to generate Fibonacci Sequence up to a certain number. The Fibonacci sequence is a series where the Next term is the Sum of Pervious 2 terms. The First Two terms of the
First give a meaningful name to the recursive function to print even odd numbers. Let's say printEvenOdd(). This function can print both even as well as Odd Numbers in given range.
Program to display the decimal equivalent of an input hexadecimal number (Hexadecimal to Decimal). How many digits in input? Digits of your i/p no. must not exceed entered limit
Write a C program to input elements in array and print all negative elements. Display all negative elements in array using loop in C. Displaying negative, positive, prime, even...
C program find Perfect numbers between 1 to n. Input Upper Limit to find Perfect numbers. Store it in a variable say end. Run a loop from 1 to end, increment 1 in each iteration. The...