home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_03 / 9n03062a < prev    next >
Text File  |  1991-01-16  |  3KB  |  104 lines

  1. /* 
  2.  * Modulename:  clock.c
  3.  *
  4.  * Description: This module contains the main 
  5.  *      procedure and interrupt handler for the 
  6.  *      demo clock program for the BSO/Tasking 
  7.  *      C-51 compiler.
  8.  *      Compiler option : -C51
  9.  */
  10.  
  11. #include "clock.sys"
  12. #include "clock.h"
  13.  
  14. /* put time array in internal data for fast access */
  15. static data unsigned char       time[4]; 
  16.  
  17. /* put welcome string in rom */
  18. rom char message[] = "Clock demo program for C-51\r\n";
  19.  
  20. /* define new style function prototypes */
  21. #ifdef _CC51
  22. extern void out_char( char );  /* redefined to 
  23.                            putchar if not C-51 */
  24. #endif
  25. extern void reset_serial();
  26. extern void init_timer();
  27. static void out_bcd( char );
  28. static void welcome( rom char * );
  29. void main( void );
  30.  
  31. interrupt( 1 ) using( 1 ) 
  32. tint( void )
  33. {
  34.     TL0 = ( TIME_FACTOR % 256 );  /* load lower 
  35.                                 byte countvalue */
  36.     TH0 = ( TIME_FACTOR / 256 );  /* load upper 
  37.                                 byte countvalue */
  38.     /* crystal: 11.0592 Mhz; 
  39.        timer0 counting up starting at 0xdc00,
  40.        interrupt at overflow,
  41.        countrate 1/12  osc. frequency,
  42.        every 10 ms. an interrupt
  43.      */
  44.     time[ HUN ] = _da( time[ HUN ] + 1 );
  45.     if ( !time[ HUN ] ) /* bcd 99 == 0x255  */ 
  46.     {
  47.          time[ SEC ] = _da( time[ SEC ] + 1 );
  48.          if ( time[ SEC ] == 0x60 ) /* 0x60 because of _da() */
  49.          {
  50.               time[ SEC ] = 0;
  51.               time[ MIN ] = _da( time[ MIN ] + 1 );
  52.               if ( time[ MIN ] == 0x60 )
  53.               {
  54.                    time[ MIN ] = 0;
  55.                    time[ HRS ] = _da( time[ HRS ] + 1 );
  56.                    if ( time[ HRS ] == 0x25 )
  57.                    {
  58.                         time[ HRS ] = 1;
  59.                    }
  60.               }
  61.          }
  62.     }
  63. }
  64.  
  65. void
  66. main( void )
  67. {
  68.     register char   i;
  69.  
  70.     reset_serial();
  71.     init_timer();
  72.     welcome( message );
  73.     while( SBUF != 0x03 )  /* stop when ^C input */
  74.     {
  75.          simulate_timer_int();
  76.          for( i=0; i<=3; i++ )
  77.          {
  78.               out_bcd( time[ i ] );
  79.               if ( i != 3 )
  80.               {
  81.                    out_char( ':' );
  82.               }
  83.          }
  84.          out_char( '\r' );
  85.          RI = 0;  /* clear rcv/xmit interrupt flag */
  86.     }
  87. }
  88.  
  89. static void
  90. out_bcd( char c )
  91. {
  92.     out_char( ( ( c >> 4 ) & 0x0F ) + '0' );
  93.     out_char( ( c & 0x0F ) + '0' );
  94. }
  95.  
  96. static void 
  97. welcome( rom char *p )
  98. {
  99.     while ( *p )
  100.     {
  101.          out_char( *p++ );
  102.     }
  103. }
  104.