#include __CONFIG(0xFFF4); __CONFIG(0xFFFF); #define _XTAL_FREQ 16000000 #define BUT_0 PORTAbits.RA6 #define BUT_1 PORTAbits.RA7 #define BUT_2 PORTDbits.RD6 #define BUT_3 PORTDbits.RD7 #define BUT_5 PORTEbits.RE3 #define RELAY PORTCbits.RC4 #define NUM_0 PORTA &= 0xF6; PORTB &= 0x4B; #define NUM_1 PORTA &= 0xF7; PORTB &= 0xFB; #define NUM_2 PORTA &= 0xF7; PORTB &= 0x4D; #define NUM_3 PORTA &= 0xF7; PORTB &= 0x69; #define NUM_4 PORTA &= 0xF6; PORTB &= 0xF9; #define NUM_5 PORTA &= 0xFE; PORTB &= 0x69; #define NUM_6 PORTA &= 0xFE; PORTB &= 0x49; #define NUM_7 PORTA &= 0xF7; PORTB &= 0x7B; #define NUM_8 PORTA &= 0xF6; PORTB &= 0x49; #define NUM_9 PORTA &= 0xF6; PORTB &= 0x69; #define GOTO_DIGIT(num) DIG_##num //#define DIG_1 PORTA &= 0xF9; PORTB &= 0xBE; PORTB |= 0x40; //#define DIG_2 PORTA &= 0xE9; PORTB &= 0xBE; PORTA |= 0x02; //#define DIG_3 PORTA &= 0xE9; PORTB &= 0xBE; PORTA |= 0x04; //#define DIG_4 PORTA &= 0xE9; PORTB &= 0xBE; PORTB |= 0x01; #define DIG_1 PORTA |= 0x06; PORTB |= 0x41; PORTB &= 0xBF; #define DIG_2 PORTA |= 0x06; PORTB |= 0x41; PORTA &= 0xFD; #define DIG_3 PORTA |= 0x06; PORTB |= 0x41; PORTA &= 0xFB; #define DIG_4 PORTA |= 0x06; PORTB |= 0x41; PORTB &= 0xFE; #define LET_R PORTA &= 0xF6; PORTB &= 0x59; #define LET_E PORTA &= 0xFE; PORTB &= 0x4C; #define LET_K PORTA &= 0xF6; PORTB &= 0xD9; #define LET_A PORTA &= 0xF6; PORTB &= 0x59; #define LET_L PORTA &= 0xFE; PORTB &= 0xCF; #define LET_J PORTA &= 0xF7; PORTB &= 0xCB; #define LET_F PORTA &= 0xFE; PORTB &= 0x5D; #define LET_B PORTA &= 0xF6; PORTB &= 0x49; #define LET_D PORTA &= 0xF6; PORTB &= 0x4B; #define LET_S PORTA &= 0xFE; PORTB &= 0x69; void display_number( char number ); volatile bit tick_1_second = 0; volatile unsigned int mscounter = 0; volatile int tick_digit = 0; void interrupt isr( void ) { if((TMR0IE == 1) && (TMR0IF == 1)) { if(mscounter < 992) mscounter++; else { mscounter = 0; tick_1_second = 1; } if(mscounter < 500) RB3 = 1; else RB3 = 0; TMR0IF = 0; TMR0 = 6; } } void main( void ) { static unsigned int years = 2012; static unsigned char months = 0; static unsigned char days = 0; static unsigned char hours = 0; static unsigned char minutes = 0; static unsigned char seconds = 0; static unsigned int workdays = 0; unsigned char alarm_hours = 0; unsigned char alarm_minutes = 0; static bit alarm_every_day = 0; static bit alarm_workdays = 0; unsigned char dig1 = 0; unsigned char dig2 = 0; unsigned char dig3 = 0; unsigned char dig4 = 0; unsigned char temp = 0; unsigned int i = 0; OSCCON = 0x30; INTCON = 0xA0; OPTION_REG = 0xC3; TMR0 = 6; SSPCON = 0x00; ADCON0 = 0x00; ADCON1 = 0x00; ANSELA = 0x00; ANSELB = 0x00; ANSELD = 0x00; ANSELE = 0x00; TRISA = 0xC0; PORTA = 0xff; TRISB = 0x00; PORTB = 0xff; TRISC = 0x00; PORTC = 0xff; TRISD = 0xC0; PORTD = 0xff; TRISE = 0x08; PORTE = 0xff; i = 500; while( i ) { __delay_us(500); PORTA |= 0xFF; PORTB |= 0xF7; LET_R; DIG_1; __delay_us(500); PORTA |= 0xFF; PORTB |= 0xF7; LET_E; DIG_2; __delay_us(500); PORTA |= 0xFF; PORTB |= 0xF7; LET_K; DIG_3; __delay_us(500); PORTA |= 0xFF; PORTB |= 0xF7; LET_A; DIG_4; i--; } i = 500; while( i ) { __delay_us(500); PORTA |= 0xFF; PORTB |= 0xF7; LET_K; DIG_1; __delay_us(500); PORTA |= 0xFF; PORTB |= 0xF7; LET_E; DIG_2; __delay_us(500); PORTA |= 0xFF; PORTB |= 0xF7; LET_L; DIG_3; __delay_us(500); PORTA |= 0xFF; PORTB |= 0xF7; LET_J; DIG_4; i--; } i = 500; while( i ) { __delay_us(500); PORTA |= 0xFF; PORTB |= 0xF7; LET_F; DIG_1; __delay_us(500); PORTA |= 0xFF; PORTB |= 0xF7; LET_E; DIG_2; __delay_us(500); PORTA |= 0xFF; PORTB |= 0xF7; LET_E; DIG_3; __delay_us(500); PORTA |= 0xFF; PORTB |= 0xF7; LET_L; DIG_4; i--; } while(1) { /* handling of time */ if( tick_1_second ) { seconds++; tick_1_second = 0; } if( seconds > 59 ) { seconds = 0; minutes++; } if( minutes > 59 ) { minutes = 0; hours++; } if( hours > 23 ) { hours = 0; days++; workdays++; } if((months == 1) || (months == 3) || (months == 5) || (months == 7) || (months == 8) || (months == 10) || (months == 12)) { if(days > 31) { days = 0; months++; } } if((months == 4) || (months == 6) || (months == 9) || (months == 11)) { if(days > 30) { days = 0; months++; } } if(months == 2) { if(years%4 == 0) { if(days > 29) { days = 0; months++; } } else { if(days > 28) { days = 0; months++; } } } if(months > 12) { years++; months = 1; } /* handling of time */ /* handling of numbers */ temp = minutes; dig4 = temp%10; temp /= 10; dig3 = temp%10; temp = hours; dig2 = temp%10; temp /= 10; dig1 = temp%10; /* handling of numbers */ /* handling of display */ if( BUT_5 == 0 ) // clock mode { __delay_us(500); display_number(dig1); DIG_1; __delay_us(500); display_number(dig2); DIG_2; __delay_us(500); display_number(dig3); DIG_3; __delay_us(500); display_number(dig4); DIG_4; } /* while( BUT_5 == 0 ) // alarm setup mode { if( BUT_0 == 0 ) alarm_minutes++; if( BUT_1 == 0 ) alarm_hours++; if( BUT_2 == 0 ) alarm_every_day = 1; else alarm_every_day = 0; if( BUT_3 == 0 ) alarm_workdays = 1; else alarm_workdays = 0; if(alarm_minutes > 60) alarm_minutes = 0; if(alarm_hours > 23) alarm_hours = 0; temp = alarm_minutes; dig4 = temp%10; temp /= 10; dig3 = temp%10; temp = alarm_hours; dig2 = temp%10; temp /= 10; dig1 = temp%10; switch( tick_digit ) { case 1: { display_number(dig1); GOTO_DIGIT(1); break;} case 2: { display_number(dig2); GOTO_DIGIT(2); break;} case 3: { display_number(dig3); GOTO_DIGIT(3); break;} case 4: { display_number(dig4); GOTO_DIGIT(4); break;} } } */ /* handling of display */ if( BUT_0 == 0 ) minutes++; if( BUT_1 == 0 ) hours++; if( BUT_2 == 0 ) days++; if( BUT_3 == 0 ) months++; /* handling of alarm if((minutes == alarm_minutes) && (hours == alarm_hours)) { if(alarm_every_day) { for(temp = 0; temp < 60; temp++) { __delay_ms(1000); RELAY = !RELAY; GOTO_DIGIT(1); LET_R; GOTO_DIGIT(2); LET_E; GOTO_DIGIT(3); LET_K; GOTO_DIGIT(4); LET_A; __delay_ms(1000); RELAY = !RELAY; GOTO_DIGIT(1); LET_K; GOTO_DIGIT(3); LET_L; GOTO_DIGIT(4); LET_J; __delay_ms(1000); RELAY = !RELAY; GOTO_DIGIT(1); LET_F; GOTO_DIGIT(3); LET_L; if(( BUT_0 == 0 ) || ( BUT_1 == 0 ) || ( BUT_2 == 0 ) || ( BUT_3 == 0 )) break; } } if((!workdays%6) && (!workdays%7)); { for(temp = 0; temp < 60; temp++) { __delay_ms(1000); RELAY = !RELAY; GOTO_DIGIT(1); LET_R; GOTO_DIGIT(2); LET_E; GOTO_DIGIT(3); LET_K; GOTO_DIGIT(4); LET_A; __delay_ms(1000); RELAY = !RELAY; GOTO_DIGIT(1); LET_K; GOTO_DIGIT(3); LET_L; GOTO_DIGIT(4); LET_J; __delay_ms(1000); RELAY = !RELAY; GOTO_DIGIT(1); LET_F; GOTO_DIGIT(3); LET_L; if(( BUT_0 == 0 ) || ( BUT_1 == 0 ) || ( BUT_2 == 0 ) || ( BUT_3 == 0 )) break; } } } */ /*handling of alarm */ } } void display_number( char number ) { // clear segments PORTA |= 0xFF; PORTB |= 0xF7; switch(number) { case 0: {NUM_0; break;} case 1: {NUM_1; break;} case 2: {NUM_2; break;} case 3: {NUM_3; break;} case 4: {NUM_4; break;} case 5: {NUM_5; break;} case 6: {NUM_6; break;} case 7: {NUM_7; break;} case 8: {NUM_8; break;} case 9: {NUM_9; break;} } }