// Use the Timer Interrupt to implemenet a 4-digits 7-Segment Clock, only minutes and seconds are shown // Use Common Anode 7-Segs, LED segment On when LOW, Digits On when LOW/HIGH (comment out the appropriate statements) // The statements within the Timer Interrupt also consume CPU times, therefore, it cannot be used to // calculate time and do 1~50ms 7-Seg refreshing at the same time without flickering. int Dig[10]; // digits 0..9 int buf[4]; // display buffer int nxt; // index to buf[] main() { int m, s; // pinhigh(8); pinhigh(9); pinhigh(10); pinhigh(11); // switch all Digits OFF, for Digits On when LOW pinlow(8); pinlow(9); pinlow(10); pinlow(11); // switch all Digits OFF, for Digits On when HIGH init_digits(); buf[0] = Dig[m/10]; buf[1] = Dig[m%10]; buf[2] = Dig[s/10]; buf[3] = Dig[s%10]; loop { delay(100); // need to tune here for accuracy! s++; buf[2]=Dig[s/10]; buf[3]=Dig[s%10]; if(s >= 60) { s=0; m++; buf[2]=Dig[0]; buf[0]=Dig[m/10]; buf[1]=Dig[m%10]; if(m >= 60) { m=0; buf[0]=Dig[0]; }} } } time0() timer 1 { pinlow(8+nxt++); // switch selected Digit Off, LOW // pinhigh(8+nxt++); // switch selected Digit Off, HIGH nxt %= 4; writeb(0, buf[nxt], 0); // pinlow(8+nxt); // switch selected Digit On, LOW pinhigh(8+nxt); // switch selected Digit On, HIGH } init_digits() { Dig[0] = 0xC0; Dig[1] = 0xF9; Dig[2] = 0xA4; Dig[3] = 0xB0; Dig[4] = 0x99; Dig[5] = 0x92; Dig[6] = 0x82; Dig[7] = 0xF8; Dig[8] = 0x80; Dig[9] = 0x90; }