home *** CD-ROM | disk | FTP | other *** search
- /* EXAMPLE.C: Some sample code for the Archimedes C-51 compiler kit.
- * See the file EXAMPLE.DOC and the Tutorial Introduction to the
- * manual for more information. Revised 9-14-87, VC. */
-
- #include <stdio.h> /* required header for printf, putchar */
- #include <io51.h> /* required header for output, bit_set, etc. */
-
- #define TRUE 1
- #define FALSE 0
- #define BELL 7 /* ASCII terminal bell */
-
- /* The following is a function prototype for the assembly
- * language routine, pulse. See the source file PULSE.S03 */
-
- extern int pulse(int count, char value);
-
-
- /* ------------------------------------------------------------ */
- void set_timer() /* initialize Timer 0 to generate interupts */
- {
- output(TMOD,1); /* Timer-0: mode 1 (16-bit timer) */
- output(TH0,0x4C); /* load 4C00h for interrupt every 50 ms */
- output(TL0,0);
- set_bit(TR0_bit); /* set Timer 0 run control bit */
- set_bit(ET0_bit); /* set interrupt mask bit ET0 */
- set_bit(EA_bit); /* enable interrupts bit EA */
- }
-
-
- /* ------------------------------------------------------------ */
- void ring_bell() /* Timer-0 interrupt handler, rings the CRT
- * bell approximately every 3 seconds; called
- * from vector in CSTARTUP module */
- {
- static int intr_ctr = 0; /* counter for number of interrupts */
-
- output(TH0,0x4C); /* reload timer again, hi byte */
- output(TL0,0); /* low byte */
- if(++intr_ctr == 60) /* only do every 60 interrupts */
- {
- putchar(BELL); /* ring the bell, or do some useful task */
- intr_ctr = 0;
- }
- }
-
-
- /* ------------------------------------------------------------ */
-
- #define BIT_VAR 0 /* 1st bit address in internal RAM (20.0 hex) */
-
- void do_io() /* demonstrate 8051 I/O functions */
- {
- char c;
-
- /* write and read Port 1 */
- output(P1,0x0F);
- printf("\nPort 1: %02X\n",input(P1));
- /* Write and read an absolute external data address,
- * such as a UART or other memory-mapped I/O device.
- * "(char *)" casts the int constant to a char pointer. */
- *(char *)0xE000 = c; /* write */
- c = *(char *)0xE000; /* read */
- c = read_XDATA(0xE00); /* another way, for SMALL model */
- printf("Data at E000h = %02Xh\n",c);
- /* read a byte of CODE memory */
- printf("Location 0 in code memory is: %02X",read_CODE(0));
- /* show use of bit-addressable RAM variables */
- if(read_bit(P1_0_bit) || read_bit(P1_1_bit))
- set_bit(BIT_VAR);
- }
-
- /* ------------------------------------------------------------
- * Eratosthenese Sieve program from BYTE, 1/83 */
-
- #define SIZE 8190 /* size of array for sieve routine */
- char flags[SIZE+1]; /* array for sieve */
-
- void sieve()
- {
- register int i,k; /* register class NOT supported -- ignored */
- int prime,count,iter;
-
- printf("\nSieve: 10 iterations...\n");
- for (iter = 1; iter <= 10; iter++) /* do program 10 times */
- {
- count = 0; /* initialize prime counter */
- for (i = 0; i <= SIZE; i++) /* set all flags true */
- flags[i] = TRUE;
- for (i = 0; i <= SIZE; i++)
- {
- if (flags[i]) /* found a prime */
- {
- prime = i + i + 3; /* twice index + 3 */
- for (k = i + prime; k <= SIZE; k += prime)
- flags[k] = FALSE; /* kill all multiples */
- count++; /* primes found */
- }
- }
- }
- printf("%d primes, sieve done.\n",count); /* found in 10th pass */
- }
-
- /* ------------------------------------------------------------ */
- void main() /* Main EXAMPLE.C program */
- {
- printf("EXAMPLE.C sample program.\n");
- set_timer(); /* turn on timer and interrupts */
- printf("Timer enabled, bell should ring every 3 seconds.\n\n");
- do_io(); /* display RAM, Port1, etc. */
- printf("\nPulsing Port 1.7 (8051 pin 8)\n");
- pulse(200,0xA5); /* call assembler routine pulse P1 */
- sieve(); /* do the Sieve benchmark */
- }