home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 15 / 15.iso / s / s220 / 2.ddi / EXAMPLE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-09-14  |  4.0 KB  |  114 lines

  1. /* EXAMPLE.C:  Some sample code for the Archimedes C-51 compiler kit.
  2.  * See the file EXAMPLE.DOC and the Tutorial Introduction to the 
  3.  * manual for more information.  Revised 9-14-87, VC.  */
  4.  
  5. #include <stdio.h>      /* required header for printf, putchar */
  6. #include <io51.h>       /* required header for output, bit_set, etc. */
  7.  
  8. #define TRUE 1
  9. #define FALSE 0
  10. #define BELL 7          /* ASCII terminal bell */
  11.  
  12. /* The following is a function prototype for the assembly
  13.  * language routine, pulse.  See the source file PULSE.S03 */
  14.  
  15. extern int pulse(int count, char value);
  16.  
  17.  
  18. /* ------------------------------------------------------------ */
  19. void set_timer()   /* initialize Timer 0 to generate interupts */
  20.   output(TMOD,1);       /* Timer-0: mode 1 (16-bit timer) */
  21.   output(TH0,0x4C);     /* load 4C00h for interrupt every 50 ms */
  22.   output(TL0,0);
  23.   set_bit(TR0_bit);        /* set Timer 0 run control bit */
  24.   set_bit(ET0_bit);        /* set interrupt mask bit ET0 */
  25.   set_bit(EA_bit);        /* enable interrupts bit EA */
  26. }
  27.  
  28.  
  29. /* ------------------------------------------------------------ */
  30. void ring_bell()   /* Timer-0 interrupt handler, rings the CRT
  31.                     * bell approximately every 3 seconds; called
  32.                     * from vector in CSTARTUP module */
  33. {
  34.   static int intr_ctr = 0;  /* counter for number of interrupts */
  35.  
  36.   output(TH0,0x4C);     /* reload timer again, hi byte */
  37.   output(TL0,0);        /* low byte */
  38.   if(++intr_ctr == 60)  /* only do every 60 interrupts */
  39.   {
  40.     putchar(BELL);      /* ring the bell, or do some useful task */
  41.     intr_ctr = 0;
  42.   }
  43. }
  44.  
  45.  
  46. /* ------------------------------------------------------------ */
  47.  
  48. #define BIT_VAR 0    /* 1st bit address in internal RAM (20.0 hex) */
  49.  
  50. void do_io()      /* demonstrate 8051 I/O functions */
  51.   char c;
  52.  
  53.     /* write and read Port 1 */
  54.   output(P1,0x0F);
  55.   printf("\nPort 1: %02X\n",input(P1));
  56.     /* Write and read an absolute external data address,
  57.      * such as a UART or other memory-mapped I/O device.  
  58.      * "(char *)" casts the int constant to a char pointer. */
  59.   *(char *)0xE000 = c;      /* write */
  60.   c = *(char *)0xE000;   /* read */
  61.   c = read_XDATA(0xE00);    /* another way, for SMALL model */
  62.   printf("Data at E000h = %02Xh\n",c);
  63.     /* read a byte of CODE memory */
  64.   printf("Location 0 in code memory is: %02X",read_CODE(0));
  65.     /* show use of bit-addressable RAM variables */
  66.   if(read_bit(P1_0_bit) || read_bit(P1_1_bit))
  67.     set_bit(BIT_VAR);    
  68. }
  69.  
  70. /* ------------------------------------------------------------
  71.  * Eratosthenese Sieve program from BYTE, 1/83  */
  72.  
  73. #define SIZE 8190        /* size of array for sieve routine */
  74. char flags[SIZE+1];     /* array for sieve */
  75.  
  76. void sieve()  
  77. {
  78.   register int i,k;     /* register class NOT supported -- ignored */
  79.   int prime,count,iter;
  80.  
  81.   printf("\nSieve: 10 iterations...\n");
  82.   for (iter = 1; iter <= 10; iter++)        /* do program 10 times */
  83.   {
  84.     count = 0;          /* initialize prime counter */
  85.     for (i = 0; i <= SIZE; i++)   /* set all flags true */
  86.       flags[i] = TRUE;
  87.     for (i = 0; i <= SIZE; i++)
  88.     {
  89.       if (flags[i])     /* found a prime */
  90.       {
  91.         prime = i + i + 3;        /* twice index + 3 */
  92.         for (k = i + prime; k <= SIZE; k += prime)
  93.           flags[k] = FALSE;       /* kill all multiples */
  94.         count++;        /* primes found */
  95.         }
  96.       }
  97.   }
  98.   printf("%d primes, sieve done.\n",count); /* found in 10th pass */
  99. }
  100.  
  101. /* ------------------------------------------------------------ */
  102. void main()   /* Main EXAMPLE.C program */
  103. {
  104.   printf("EXAMPLE.C sample program.\n");
  105.   set_timer();          /* turn on timer and interrupts */
  106.   printf("Timer enabled, bell should ring every 3 seconds.\n\n");
  107.   do_io();               /* display RAM, Port1, etc. */
  108.   printf("\nPulsing Port 1.7 (8051 pin 8)\n");
  109.   pulse(200,0xA5);      /* call assembler routine pulse P1 */
  110.   sieve();              /* do the Sieve benchmark */
  111. }
  112.