// USE LEDREG.BIT #pragma code #include #include unsigned char xdata *ledaddress; // Global values managed by the interrupt routines unsigned int timeHigh, // Copy of count for computing the frequency count; // Counts the number of 250 usec chunks unsigned char timeLow; // usec count // Interrupts should always be short. // This means no loops or other time consuming operations. // // We will use timer 1 as a counter that will interrupt after // N input clocks cycles. We then copy the time information // and reset the time to count the next N input clock cycles. // When this counter interrupts, we can compute the frequency // based on the amount of time for the N input clock cycles // We use timer 1 for this because the T input is a free pin // not connected to the Xilinx chip on the XESS board void counterInterrupt ( void ) interrupt 3 using 1 { timeLow = TL0; TL0 = 0; timeHigh = count; count = 0; if (timeHigh == 0 && timeLow < 16) *ledaddress = 0x6f; // 6 (1us) else if (timeHigh == 0 && timeLow < 106) *ledaddress = 0x6b; // 5 (10us) else if (timeHigh < 4) *ledaddress = 0x3a; // 4 (100us) else if (timeHigh < 40) *ledaddress = 0x5b; // 3 (1ms) else if (timeHigh < 400) *ledaddress = 0x5d; // 2 (10ms) else if (timeHigh < 4000) *ledaddress = 0x12; // 1 (100ms) else if (timeHigh < 40000) *ledaddress = 0x77; // 0 (1s) else *ledaddress = 0xff; // default } // Timer 0 is used just measure time: all we do is increment the // counter if 250 usec have passed. void timerInterrupt ( void ) interrupt 1 using 1 { count++; } void init ( void ) { // Timer1: Counter, mode 2 (auto-reload), with reload value 256- 10 = 246 // Timer0: Timer, mode 2, with reload value 256- 250 = 6 TMOD = 0x62; // 01100010; TCON = 0x50; // 01010000; TH0 = 6; TH1 = 246; // **************************************************************************** // Enable Interruppts // The IE register sets the state of the interrupt vectors. // Every bit in IE is for a different interrupt (see documentation for definition) // //(MSB) (LSB) // EAall NA ET2 ES ET1 EX1 ET0 EX0 // // **************************************************************************** // Enable both timer interrupts IE = 0xCA; // 110001010; msf 1 to enable all } void main() { init(); ledaddress = 0x0f000; // the FPGA is configured to memory map led to this address while (1) { // all programs run forever //*ledaddress = 0x01; } }