80 likes | 296 Views
/*. Define. register. addresses. */. #define. RBUF. (v. olatile. c. har. *). 0xFFFFFFE0. #define. SCONT. (c. har. *). 0xFFFFFFE3. #define. P. A. OUT. (c. har. *). 0xFFFFFFF1. #define. P. ADIR. (c. har. *). 0xFFFFFFF2. #define. in. t. addr. (in. t. *).
E N D
/* Define register addresses */ #define RBUF (v olatile c har *) 0xFFFFFFE0 #define SCONT (c har *) 0xFFFFFFE3 #define P A OUT (c har *) 0xFFFFFFF1 #define P ADIR (c har *) 0xFFFFFFF2 #define in t addr (in t *) (0x24) v oid in tserv(); v oid main() { /* Initialize the parallel p ort */ *P ADIR = 0xFF; /* Configure P ort A as output */ /* Initialize the in terrupt mec hanism */ in t addr = &in tserv; /* Set in terrupt v ector */ asm ("Mo v e #0x40,%PSR"); /* Pro cessor resp onds to IR Q in terrupts */ = 0x10; /* Enable receiv er in terrupts */ *SCONT /* T ransfer the c haracters */ while (1); /* Infinite lo op */ } /* In terrupt service routine */ v oid in tserv() { *P A OUT = *RBUF; /* T ransfer the c haracter to P ort A */ asm ("ReturnI"); /* Return from in terrupt */ } Figure 9.16. C program for character transfer using interrupts.
/* Define register addresses */ #define RBUF (v olatile c har *) 0xFFFFFFE0 #define SST A T (v olatile c har *) 0xFFFFFFE2 #define P A OUT (c har *) 0xFFFFFFF1 #define P ADIR (c har *) 0xFFFFFFF2 #define PST A T (v olatile c har *) 0xFFFFFFF6 #define BSIZE 80 v oid main() { unsigned c har m buffer[BSIZE]; unsigned c har fin, fout; unsigned c har temp; /* Initialize P ort A and circular buffer */ *P ADIR = 0xFF; /* Configure P ort A as output */ fin = 0; fout = 0; /* T ransfer the c haracters */ while (1) { /* Infinite lo op */ while ((*SST A T & 0x1) == 0) { /* W ait for a new c haracter */ if (fin != fout) { /* If circular buffer is not empt y */ if (*PST A T & 0x2) { /* and output device is ready */ *P A OUT = m buffer[fout]; /* send a c haracter to P ort A */ if (fout < BSIZE 1) /* Up date the output index */ fout++; else fout = 0; } } } m buffer[fin] = *RBUF; /* Read a c haracter from receiv e buffer */ if (fin < BSIZE 1) /* Up date the input index */ fin++; else fin = 0; } } Figure 9.17. C program for transfer through a circular buffer.
RBUF EQU $FFFFFFE0 Receiv e buffer. SST A T EQU $FFFFFFE2 Status register for serial in terface. P A OUT EQU $FFFFFFF1 P ort A output data. P ADIR EQU $FFFFFFF2 P ort A direction register. PST A T EQU $FFFFFFF6 Status register for parallel in terface. MBUFFER Reserv eByte 80 Define the circular buffer. * Initialization ORIGIN $1000 Mo v eByte #$FF,P ADIR Configure P ort A as output. Mo v e #MBUFFER,R0 R0 p oin ts to the buffer. Mo v e #0,R1 Initialize head p oin ter. Mo v e #0,R2 Initialize tail p oin ter. * T ransfer the c haracters LOOP T estbit #0,SST A T Chec k if new c haracter is ready . Branc READ h 0 Compare R1,R2 Chec k if queue is empt y . Branc h=0 LOOP Queue is empt y . T estbit #1,PST A T Chec k if P ort A is ready . Branc h=0 LOOP Mo v eByte (R0,R2),P A OUT Send a c haracter to P ort A. Add #1,R2 Incremen t the tail p oin ter. Compare #80,R2 Is the p oin ter past queue limit? Branc h < 0 LOOP Mo v e #0,R2 W rap around. Branc h LOOP READ Mo v eByte RBUF,(R0,R1) Place new c haracter in to queue. Add #1,R1 Incremen t the head p oin ter. Compare #80,R1 Is the p oin ter past queue limit? Branc h < 0 LOOP Mo v e #0,R1 W rap around. Branc h LOOP Figure 9.18. A generic assembly language program for transfer through a circular buffer.
/* Define register addresses */ #define P A OUT (c har *) 0xFFFFFFF1 #define P ADIR (c har *) 0xFFFFFFF2 #define PBIN (v olatile c har *) 0xFFFFFFF3 #define PBOUT (c har *) 0xFFFFFFF4 #define PBDIR (c har *) 0xFFFFFFF5 #define CNTM (in t *) 0xFFFFFFD0 #define COUNT (v olatile in t *) 0xFFFFFFD4 #define CTCON (c har *) 0xFFFFFFD8 v oid main() { unsigned in t coun ter v alue, total coun t; unsigned in t actual time, seconds, ten ths, h undredths; /* Initialize the parallel p orts */ *P ADIR = 0xFF; /* Configure P ort A */ *PBDIR = 0xF4; /* Configure P ort B */ *P A OUT = 0x0; /* T urn off displa y */ *PBOUT = 0x4; /* and LED */ /* Start the test */ while (1) { /* Infinite lo op */ while ((*PBIN & 0x2) = = 0); /* W ait for Go k ey to b e pressed */ /* W ait 3 seconds and then turn LED on */ *CNTM = 0x11E1A300; /* Set timer v alue to 300,000,000 */ *CTCONT = 0x1; /* Start the timer */ while ((*CTST A T & 0x1) = = 0); /* W ait un til timer reac hes zero */ *PBOUT = 0x0; /* T urn LED on */ /* Initialize the coun ting pro cess */ coun ter v alue = 0; *CNTM = 0xFFFFFFFF; /* Set the starting coun ter v alue */ *CTCONT = 0x1; /* Start coun ting */ while ((*PBIN & 0x1) == 0); /* W ait for the Stop k ey to b e pressed */ /* The Stop k ey has b een pressed - stop coun ting */ *PBOUT = 0x4; /* T urn LED off */ *CTCONT = 0x2; /* Stop the coun ter */ coun ter v alue = *COUNT; /* Read the con ten ts of the coun ter */ Page 1 of Figure 9.20
/* Compute the total coun t */ coun ter v alue); total coun t = (0xFFFFFFFF /* Con v ert coun t to time */ ; actual time = total coun t / 1000000; /* Time in h undredths of seconds */ seconds = actual time / 100; ten ths = (actual time seconds * 100) / 10; h undredths = actual time (seconds * 100 + ten ths * 10); /* Displa y the elapsed time */ *P A OUT = ((seconds << 4) | ten ths); *PBOUT = ((h undredths << 4) | 0x4); /* Keep the LED turned off */ } } Figure 9.20. C program for the reaction timer. Page 2 of Figure 9.20
P A OUT EQU $FFFFFFF1 P ort A output data. P ADIR EQU $FFFFFFF2 P ort A direction register. PBIN EQU $FFFFFFF3 P ort B input pins. PBOUT EQU $FFFFFFF4 P ort B output data. PBDIR EQU $FFFFFFF5 P ort B direction register. CNTM EQU $FFFFFFD0 Initial coun ter v alue. COUNT EQU $FFFFFFD4 Coun ter con ten ts. CTCONT EQU $FFFFFFD8 Con trol register. * Initialization ORIGIN $1000 Mo v eByte #$FF,P ADIR Configure P ort A. Mo v eByte #$F4,PBDIR Configure P ort B. ST AR T Mo v eByte #0,P A OUT T urn the displa y off. Mo v eByte #4,PBOUT T urn the LED off, b y making PB2 = 1. * W ait for Go k ey to b e pressed GKEY T estbit #1,PBIN Go k ey is connected to PB1 pin. Branc h=0 GKEY * Dela y 3 seconds b efore the LED is turned on Mo v e #11E1A300,CNTM Timer v alue is 300,000,000. Mo v eByte #1,CTCONT Start the timer. DELA Y T estbit #0,CTCONT W ait un til timer Branc h=0 DELA Y reac hes zero. Mo v eByte #0,PBOUT T urn the LED on. * Initialize the coun ting pro cess Mo v e #$FFFFFFFF,CNTM Set the starting coun ter v alue. Mo v eByte #1,CTCONT Start coun ting. * W ait for Stop k ey to b e pressed SKEY T estbit #0,PB Stop k ey is connected Branc h=0 SKEY to PB0 pin. * Stop coun ting and read the last coun t Mo v eByte #4,PBOUT T urn the LED o#, b y making PB2 = 1. Mo v eByte #2,CTCONT Stop the coun ter. Mo v e COUNT,R0 Read the coun ter. * Compute the total coun t Mo v e #$FFFFFFFF,R2 Determine the Subtract R0,R2 actual coun t. Page 1 of Figure 9.21
* Con v ert the coun t to actual time in h undredths of seconds, and then to BCD. * Put the 2 most-significan t BCD digits in R4 and the least-significan t digit in R3. Mo v e #1000000,R1 Determine the coun t in Divide R1,R2 h undredths of seconds. Mo v e #100,R1 Divide b y 100 to find the digit that Divide R1,R2 denotes the n um b er of seconds. Mo v e R2,R4 Sa v e the digit in R4. Mo v e R3,R2 Use the remainder as the next dividend. Mo v e #10,R1 Divide b y 10 to find the digit that Divide R1,R2 denotes 1/10th of a second. LShiftL #4,R4 The first 2 BCD digits Or R2,R4 are placed in R4. * Displa y the elapsed time Mo v eByte R4,P A OUT First 2 digits to P ort A. LShiftL #4,R3 Third digit to P ort B Or #4,R3 and k eep the LED Mo v eByte R3,PBOUT turned off. Branc h ST AR T Ready for next test. Figure 9.21. Assembly language program for the reaction timer using polling. Page 2 of Figure 9.21