/*  Filename     PKIT1.C
    Description  PKIT-1 DICE,H-K,COUNTER,TIMER 4 IN 1 Kid
    Hardware     PKIT-1 BOARD
    Compiler     CCS PCM C Complier V 3.173
    Engineer     Pojchara B.
    Company      Sila Research Co.,Ltd. */

#include <16f676.h>
#fuses intrc_io,nowdt,protect,nobrownout,put,nomclr
#use delay(clock=1000000)
#define CLK PIN_A0
#define STR PIN_A1
#define DATA PIN_A2
#define OUTP PIN_A4
#define SOUND PIN_C0
#define SW1 PIN_C1
#define SW2 PIN_C2
#define DIP1 PIN_A3
#define DIP2 PIN_C5
#define DIP3 PIN_C4
#define DIP4 PIN_C3

BYTE DIS[2];

void outsegment(byte ds1,byte ds2){
const unsigned char SEGTAB [15] = {
0x3f,0x06,0x5B,0x4F,0x66,0x6D,0x7D,
0x07,0x7f,0x6f,0x00,0x63,0x63,0x5c,0x5c};
BYTE i;
    DIS[0] = SEGTAB[ds1];
    DIS[1] = SEGTAB[ds2];
    output_low(CLK);
    output_low(STR);
    for(i=1;i<=16;++i) {             
        if((*(DIS+1)&0x80)==0)
            output_low(DATA);
        else
            output_high(DATA);
        shift_left(DIS,2,0);
        output_high(CLK);
        output_low(CLK);
    }
    output_high(STR);
}
void snd (int8 freq,int16 time) {
unsigned char i;
    while (time>0) {
        output_high(SOUND);         
        for(i=1;i<=freq;i++)
            time--;
        output_low(SOUND);          
        for(i=1;i<=freq;i++)
            time--;
    }
}
void delay (long count){         
unsigned char i;
    while (count) {
        for (i=1;i<=50;i++);
        count--;
    }
}
void dice(){
int1 flag1,flag2;
long del=1;
int dat,i=0,j=0,a,b,c,sel;
    a = 0;flag2 = 1;
    while(!input(DIP1)){
        if(!input(SW2))sel = 1;
        if(!input(SW1))sel = 0;
        if(!input(SW2)|!input(SW1)){
            if(flag1){i = b;j = c;flag1 = 0;}
            del = 30;
            i++;j++;
            snd(10,100);
            if((i&0x0f)==0x07)i=1;
            if((j&0x0f)==0x07)j=1;
            delay(del);
            a = 40;
            flag2 = 1;
        }else{
            if(a){
                if(a>15){
                    del += 6;
                }else{
                    del += 20;
                }
                a--;
                i++;j++;
                snd(10,100);
                if((i&0x0f)==0x07)i=1;
                if((j&0x0f)==0x07)j=1;
                delay(del);
            }else {if(flag2)snd(10,100);flag1 = 1;flag2 = 0;}
        }
        if(flag1){
            b++;
            if((b&0x0f)==0x07){
                b=1;c++;
                if((c&0x0f)==0x07)c=1;
            }
        }
        if(sel==0){
            j = 0x0a; 
            outsegment(i,j);
            j = 0;
        }else outsegment(i,j);
    }
}
void headk(){
int1 flag1,flag2;
long del;
int dat,i=0,a,b,c,j,k;
    del = 1;a = 0;flag2 = 1;
    while(!input(DIP2)){
        if(!input(SW1)|!input(SW2)){
            if(flag1){i = b;flag1 = 0;}
            del = 30;
            i++;
            snd(10,100);
            if((i&0x0f)==0x02)i=0;
            delay(del);
            a = 40;
            flag2 = 1;
        }else{
            if(a){
                if(a>15){
                    del += 6;
                }else{
                    del += 20;
                }
                a--;
                i++;
                snd(10,100);
                if((i&0x0f)==0x02)i=0;
                delay(del);
            }else {if(flag2)snd(10,100);flag1 = 1;flag2 = 0;}
        }
        b++;
        if((b&0x0f)==0x02)b=0;
        if((i&0x0f)==0x00){j = 12;k = 11;}
        else {j = 14;k = 13;}
        outsegment(j,k);
    }
}
void counter(){
byte i=0,j=0;
    while(!input(DIP3)){
        if(!input(SW2)){
            i++;
            if(i==0x0a){
                i=0;
                j++;
                if(j==0x0a)j=0;
            }
        }while(!input(SW2))delay(100);
        if(!input(SW1)){
            i--;
            if(i==0xff){
                i=9;
                j--;
                if(j==0xff)j=9;
            }
        }while(!input(SW1))delay(100);
        outsegment(i,j);
    }
}

void timer(){
int1 startt=0,flash,end=1;
int8 i=0,j=0,cf1=0,cf2=0;
int16 dl,dl2;
    while(!input(DIP4)){
        if(startt){
            i--;
            if(i==0xff){
                i=9;
                j--;
                if(j==0xff)j=9;
            }
            if(i==0 && j==0){
                output_low(OUTP);
                outsegment(i,j);
                snd(14,80);
                end = 1;
                startt = 0;
                output_high(OUTP);
            }
        }
        outsegment(i,j);
        for(dl=0;dl<=44200;dl++){
            do{
                if(!input(SW1)){       //start/stop
                    if(!(cf1==0 && cf2==0)){
                        if(end){
                            i = cf1;
                            j = cf2;
                            end = 0;
                        }
                        startt = ~startt;
                        snd(10,200);
                    }
                    delay(10);
                    while(!input(SW1))delay(100);
                }
                if(!startt){
                    if(flash){
                        for(dl2=0;dl2<=100;dl2++){
                            if(!input(SW1)){       //start/stop
                                if(!(cf1==0 && cf2==0)){
                                    if(end){
                                        i = cf1;
                                        j = cf2;
                                        end = 0;
                                    }
                                    startt = ~startt;
                                    snd(10,200);
                                }
                                delay(10);
                                while(!input(SW1))delay(100);
                            }
                            if(end){
                                if(!input(SW2)){
                                    cf1++;
                                    if(cf1==0x0a){
                                        cf1=0;
                                        cf2++;
                                        if(cf2==0x0a)cf2=0;
                                    }
                                    dl = 0;
                                    snd(10,200);
                                    delay(10);
                                    while(!input(SW2))delay(100);
                                }
                            }if(end)outsegment(cf1,cf2);
                            else outsegment(i,j);
                        }flash = 0;
                    }else{
                        for(dl2=0;dl2<=100;dl2++){
                            if(!input(SW1)){       //start/stop
                                if(!(cf1==0 && cf2==0)){
                                    if(end){
                                        i = cf1;
                                        j = cf2;
                                        end = 0;
                                    }
                                    startt = ~startt;
                                    snd(10,200);
                                }
                                delay(10);
                                while(!input(SW1))delay(100);
                            }
                            if(end){
                                if(!input(SW2)){
                                    cf1++;
                                    if(cf1==0x0a){
                                        cf1=0;
                                        cf2++;
                                        if(cf2==0x0a)cf2=0;
                                    }
                                    dl = 0;
                                    snd(10,200);
                                    delay(10);
                                    while(!input(SW2))delay(100);
                                }
                            }
                            if(startt)outsegment(i,j);
                            else outsegment(0x0a,0x0a);
                        }flash = 1;
                    }
                }
            }while(!startt && !input(DIP4));
            if(input(DIP4))break;
        }
    }
}
void main(){
    output_high(OUTP);
    while(1){
        if(!input(DIP1))dice();
        if(!input(DIP2))headk();
        if(!input(DIP3))counter();
        if(!input(DIP4))timer();
        else outsegment(0x0a,0x0a);
    }
}



