12_c-prog

exit open oxxx int main int out char ch int

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Winter 2008 20 5 C-Programming, Part 1 02/26/2008 UCSD: Physics 121; 2008 How does it look In Linux/Unix? #include <stdio.h> #include <unistd.h> #include <asm/io.h> #include #include #include #include // needed for ioperm() // for outb() and inb() #define DATA 0x378 UCSD: Physics 121; 2008 LPT Method on Windows // parallel port memory address // // // // fprintf()... exit() open()... O_xxx int main() { int out; char ch; int main() { int x = 0xAA; out = open("LPT1:", O_WRONLY|O_BINARY); // open parport if(out < 0) { printf("Can't open LPT1\n"); exit(2); // exit with error status } ch = 0x55; write(out, &ch, 1); close(out); //flushes windows OS buffering if (ioperm(DATA,3,1)) { printf("You must be root to run this program\n"); exit(1); } outb(x,DATA); <stdio.h> <stdlib.h> <io.h> <fcntl.h> // sends 1010 1010 to the Data Port return 0; return 0; } // return success } outb() performs direct write to hardware/memory address Winter 2008 21 Winter 2008 thanks to Eric Michelsen for this template UCSD: Physics 121; 2008 22 UCSD: Physics 121; 2008 Description References • Notes on previous program: • Lots of books on C out there – lots of includes to get all the features we want – open port as write only, binary mode – parallel port is assigned to an integer (out) – Kernighan and Ritchie is the classic standard • they wrote the language – the book by K. N. King is exceptionally good • in essence, a temporary address for the program’s use • Also check the web for tutorials – checks that out is not negative (would indicate error) – C-geeks are exactly the kind of people who write webpages, so there is a profusion of programming advice out there! • aborts if so, with an error message – assigns a test value (hex 55) to the one-byte ch • 0x55 is a nice pattern: 01010101 • 0xAA also nice: 10101010 – writes this to the port • the & points to the location of the value to be sent • the 1 indicates one byte to be sent – closes port and exits – any looping must open and close port with each loop!! Winter 2008 Lecture 12 23 Winter 2008 24 6...
View Full Document

Ask a homework question - tutors are online