Danh mục

Programming Tools P1

Số trang: 30      Loại file: pdf      Dung lượng: 1.43 MB      Lượt xem: 9      Lượt tải: 0    
tailieu_vip

Hỗ trợ phí lưu trữ khi tải xuống: 10,000 VND Tải xuống file đầy đủ (30 trang) 0
Xem trước 3 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Many programs that access the parallel port do many of the same things, including reading and writing to the port registers and finding and testing ports on a system . Another common task is reading, setting, clearing, and toggling individual bits in a byte. This chapter introduces tools to perform these functions in any Visual-Basic program.
Nội dung trích xuất từ tài liệu:
Programming Tools P1 4 Many programs that access the parallel port do many of the same things, including reading and writing to the port registers and finding and testing ports on a system . Another common task is reading, setting, clearing, and toggling individual bits in a byte. This chapter introduces tools to perform these functions in any Visual-Basic program.Routines for Port Access Listing 4-1 is a set of subroutines and functions that simplify the tasks of reading and writing to the port registers and performing bit operations . You can add the file as a .bas module in your parallel-port programs (use Add Module) and call the routines as needed in your code . The individual routines are very short. The reason to use them is convenience. For the port-write subroutines, you pass the base address of a port and a value to write to the port. The routines automatically calculate the register address from the base address and invert the appropriate bits, so the value passed matches the value that appears at the connector. You dont have to worry about calculating an address and inverting the bits every time you write to a port . For the port-read functions, you pass a base address and the function returns the value at the port connector. For the bit operations, you pass a variable and bit number, and the routine auto-Parallel Port Complete 53Chapter 4Function BitRead% (Variable%, BitNumber%)Returns the value (0 or 1) of the requested bit in a Variable .Dim BitValue%the value of the requested bitBitValue -- 2 ^ BitNumberBitRead = (Variable And BitValue) BitValueEnd FunctionSub BitReset (Variable%, BitNumber%)Resets (clears) the requested bit in a Variable .Dim BitValue, CurrentValue%the value of the requested bitBitValue = 2 ^ BitNumberVariable = Variable And (&HFFFF - BitValue)End SubSub BitSet (Variable%, BitNumber%) Sets the requested bit in a Variable .Dim BitValue, CurrentValue% the value of the requested bitBitValue - 2 ^ BitNumberVariable = Variable Or BitValueEnd SubSub BitToggle (Variable%, BitNumber%)Toggles the requested bit in a Variable .Dim BitValue, CurrentValue%the value of the requested bitBitValue = 2 ^ BitNumberIs the current value 0 or 1?CurrentValue = Variable And BitValueSelect Case CurrentValue Case 0 If current value = 0, set it Variable = Variable Or BitValue Case Else If current value - 1, reset it Variable = Variable And (&HFFFF - BitValue)End SelectEnd SubListing 4-1 : Routines for reading and writing to the parallel port registers and forreading, setting, clearing, and toggling individual bits in a byte. (Sheet 1 of 2)54 Parallel Port Complete Programming ToolsFunction ControlPortRead% (BaseAddress%)Reads a parallel ports Control port .Calculates the Control-port address from the portsbase address, and inverts bits 0, 1, & 3 of the byte read .The Control-port hardware reinverts these bits,so the value read matches the value at the connector .ControlPortRead -- (Inp(BaseAddress + 2) Xor &HB)End FunctionSub ControlPortWrite (BaseAddress%, ByteToWrite%)Writes a byte to a parallel ports Control port .Calculates the Control-port address from the portsbase address, and inverts bits 0, 1, & 3 .The Control-port hardware reinverts these bits,so Byte is written to the port connector .Out BaseAddress + 2, ByteToWrite Xor &HBEnd SubFunction DataPortRead% (BaseAddress%)Reads a parallel ports Data port .DataPortRead = Inp(BaseAddress)End FunctionSub DataPortWrite (BaseAddress%, ByteToWrite%)Writes a byte to a parallel ports Data port .Out BaseAddress, ByteToWriteEnd SubFunction StatusPortRead% (BaseAddress%)Reads a parallel ports Status port .Calculates the Status-port address from the portsbase address, and inverts bit 7 of the byte read .The Status-port hardware reinverts these bits,so the value read matches the value at the connector .StatusPortRead = (Inp(BaseAddress + 1) Xor &H80)End FunctionListing 4-1 : Routines for reading and writing to the parallel port registers and forreading, setting, clearing, and toggling individual bits in a byte . (Sheet 2 of 2) matically sets, resets, toggles, or returns the value of the requested bit in the vari- able . Most of the example programs in this book use these routines . The routines require the Inpout DLL described in Chapter 2. Because the routines are funda- mental to accessing the parallel port, Ill explain them in detail .Para ...

Tài liệu được xem nhiều: