// Handel-C decade counter and test - for the Celoxica RC100 board void increment_decade(unsigned 4 *count, unsigned 1 Hold); unsigned 8 sevenseg(unsigned 4 bcd); set clock = external_divide "A11" 2 with { rate = 40 }; // 80 MHz / 2 void main (void) { // Declarations unsigned 1 stop; unsigned 4 count[2]; unsigned 1 hold; unsigned 24 cycles; unsigned 8 seg1, seg2; // Hardware interface // Seven segment display 1 interface bus_out () display1 (unsigned 8 out = seg1) with { data = {"AA14", "AA20", "W18", "Y18", "V17", "W14", "AB14", "V13"} }; // Segments[8:1] interface bus_out () enable1 (unsigned 1 out = 1) // Enable 1 - active-low with { data = {"AA22"} }; // Seven segment display 2 interface bus_out () display2 (unsigned 8 out = seg2) with { data = {"Y13", "AB20", "AA19", "W17", "AA18", "AA13", "AB13", "W13"} }; // Segments[8:1] interface bus_out () enable2 (unsigned 1 out = 1) // Enable 2 - active-low with { data = {"V20"} }; // Initialisation par { stop = 0; count[0] = 0; count[1] = 0; } par { // Cycle counter - use this to divide the clock while (!stop) par { cycles++; hold = (cycles != 0xffffff); } // Call the counter while (!stop) increment_decade(count, hold); // Seven segment displays (active high) - musn't call these in parallel! while (!stop) { seg1 = sevenseg(count[1]); seg2 = sevenseg(count[0]); } } } // This function describes a two digit BCD counter void increment_decade(unsigned 4 *count, unsigned 1 Hold) { if (Hold) count[0] = count[0]; /* You have to do something! */ else if ( count[0] == 9 ) par { /* "par" ensures one cycle */ count[0] = 0; if ( count[1] == 9 ) count[1] = 0; else count[1]++; } else count[0]++; } // This function describes a BCD to seven-segment encoder. // The display inputs are active high. At 80 MHz, it doesn't matter // that this takes two clocks to execute. unsigned 8 sevenseg(unsigned 4 bcd) { unsigned 8 seg; switch (bcd) { case 0: seg = 0b00111111; break; case 1: seg = 0b00000110; break; case 2: seg = 0b01011011; break; case 3: seg = 0b01001111; break; case 4: seg = 0b01100110; break; case 5: seg = 0b01101101; break; case 6: seg = 0b01111100; break; case 7: seg = 0b00000111; break; case 8: seg = 0b01111111; break; case 9: seg = 0b01100111; break; default: seg = 0b10001000; break; } return seg; }