UMBC CMSC 211 Fall 1999

CSEE | 211 | 211 F'99 | lectures | news | help


The Programmable Timer

The timer has three channels and operate at 1.193Mhz under control of the 8284A generator. The channels are available through ports 40h, 41h, and 42. The command register is available through port 43h.
The Programmable Timer Command Register
Bit Meaning
0 0 - binary data, 1 - BCD
1-3 Number of mode used (000-101)
4-5 Operation Type
  00 - send the value of counter
  01 - read/write high byte
  10 - read/write low/byte
  11 - read/write both high and low bytes
6-7 Number of channel to be programmed

Channel 0 is used by teh system clock to calculate the time of day. It is programmed for 18.2 pulses per second (called ticks) and stored in the BIOS data area at 0040h:006Ch. Every pulse initiates a timer interrupt 8h (IRQ0). Don't mess with this one!

Channel 1 is responsible for refreshing RAM and counts the pulses during disk operations, so that it can reset the timer counter upon completion of an operation. This is also not a good one to mess with.

Channel 2 (port 42h) is connected to the computer's speaker and issues square wave pulses used to make sounds. You can change the sound frequency with this channel.The 8255 chip, (PPI) is also involved in generating sound and that bits 0 and 1 of port 61h also control the speaker.

Example program

;***************************************************************
;  Program Sound ( Chapter 10 )
;
;  The program for outputting a sound of prescribed tone
;
;  Author: A.I.Sopin   Voronezh, Russia   1990 --- 1992
;  ------
;
;  Call from Assembler programs:
;
;       Call SOUND
;
;  Parameters passed through the registers:
;
;  Frequency    - DI register (from   21  to  65535  hertz)
;
;  Duration      -BX register (in hundredth of second)
;
;  Registers  AX, CX, DX, DS, ES, SI  are retained by the program 
;
;
;
;***************************************************************
PUBLIC   SOUND
CODE     SEGMENT
         ASSUME CS:CODE
SOUND    PROC   FAR
         push   ax
         push   cx
         push   dx
         push   ds
         push   es
         push   si
;-----------------------------------------------------------
         in     al,61h            ;  Read current port mode B (8255)
         mov    cl,al             ;  Save current mode
         or     al,3              ;  Switch on speaker and timer
         out    61h,al            ;
         mov    al,0B6h           ;  set for channel  2 (8253)
         out    43h,al            ;  command register  8253
         mov    dx,14h            ;
         mov    ax,4F38h          ;  divisor of frequency
         div    di                ;
         out    42h,al            ;  lower byte of frequency
         mov    al,ah             ;
         out    42h,al            ;  higher byte of frequency
;  Generation of sound delay 
         mov    ax,91             ;  multiplier  -  AX register !
         mul    bx                ;  AX =BX*91 (result in   DX:AX)
         mov    bx,500            ;  divisor, dividend in   DX:AX
         div    bx                ;  result in   AX, remainder in DX 
         mov    bx,ax             ;  save result
         mov    ah,0              ;  read time
         int    1Ah               ;
         add    dx,bx             ;
         mov    bx,dx             ;
Cycle:   int    1Ah               ;
         cmp    dx,bx             ;  Has time gone ?
         jne    Cycle             ;
         in     al,61h            ;  Read mode of port B (8255)
         mov    al,cl             ;  Previous mode 
         and    al,0FCh           ;
         out    61h,al            ;  Restore mode
;-----------------------------------------------------------
;  Restoring registers and exit
Exit:    pop    si                ;
         pop    es                ;
         pop    ds                ;
         pop    dx                ;
         pop    cx                ;
         pop    ax                ;
         RETF                     ; exit from subroutine
SOUND    ENDP
CODE     ENDS
         END

  

CSEE | 211 | 211 F'99 | lectures | news | help