Page 417 - Programming Microcontrollers in C
P. 417
402 Chapter 8 MCORE, A RISC Machine
The program that calls the delay() function above has some
responsibilities in making this function work. This delay is using the
PIT interrupt. Most of the code necessary to implement the interrupt
is contained in the delay function. There are, however, a couple of
items that have to be taken care of outside the delay routine. The first
is to place the address of the interrupt handler into the fast interrupt
vector. Also, the system fast interrupts must be enabled. You will see
these matters are taken care of in the following test function.
In the previous routine, the semaphore number is saved externally
so that it can be accessed by the interrupt service routine. Next the
PIT is enabled and the ITDR to ITADR write through is enabled so
that the value written to the ITDR is the value to be counted down in
the timer. The count value is next calculated. Remember, the counter
is driven at 8192 Hz. This value is obtained by counting the output
from a crystal-controlled oscillator running at 32768 Hz by 4. The
time per count is approximately 122 microseconds. The delay time
passed to the function is in milliseconds, so to calculate the count
value to be placed into the ITDR/ITADR, the program first converts
the delay time to microseconds by multiplying it by 1000. Then, the
count value is calculated by dividing the microseconds by 122. This
value is written to the ITDR and it is automatically written through
to the ITADR where it is counted down by the hardware.
The remaining code in the delay()routine enables the PIT interrupt
and also enables the fast interrupt, bit 8, that is connected to the output
from the PIT. Control is then passed back to the calling program.
#include “mmc2001.h”
#include “serial.h”
#define FAST_AUTOVECTOR 0x3000002c
/* function prototypes */
void handler(void);
int attach_semaphore(void);
void wait_for_semaphore(int);
main()
{
UWORD count=0;
int semaphore;