/*********************************************

Project : EE476 Final Project--Digital Mirror
Message Machine
Date : 4/11/2001
Author : Brian Ellyson & Dee-Pong Lu
Company : Cornell University 

Comments: Variation of our final code which
displays a counter that counts up
every second (four digits).

Chip type : AT90S8515
Clock frequency : 4.000000 MHz
Memory model : Small
Internal SRAM size : 512
External SRAM size : 0
Data Stack size : 128
*********************************************/

#include <90s8515.h>
#include <delay.h>

// Declare your global variables here
unsigned char index, i, j;
unsigned int count;
unsigned int counter;
unsigned int display;
unsigned int initDelay, rastorDelay;
unsigned char setlights;
unsigned char message[4]={0, 0, 0, 0};
unsigned char rastor[80]={
0x83, 0x7D, 0x7D, 0xD, 0x83, 0xFF, 0xFF,//0
0xFF, 0xBD, 0x01, 0xFD, 0xFF, 0xFF, 0xFF,//1
0xBD, 0x79, 0x75, 0x6D, 0x9D, 0xFF, 0xFF,//2
0x7B, 0x7D, 0x5D, 0x3D, 0x73, 0xFF, 0xFF,//3
0xE7, 0xD7, 0xB7, 0x01, 0xF7, 0xFF, 0xFF,//4
0x1B, 0x5D, 0x5D, 0x5D, 0x63, 0xFF, 0xFF,//5
0xC3, 0xAD, 0x6D, 0x6D, 0xF3, 0xFF, 0xFF,//6
0x7F, 0x71, 0x6F, 0x5F, 0x3F, 0xFF, 0xFF,//7
0x93, 0x6D, 0x6D, 0x6D, 0x93, 0xFF, 0xFF,//8
0x9F, 0x6D, 0x6D, 0x6B, 0x87, 0xFF, 0xFF}; //9

interrupt [EXT_INT0] void external_interrupt(void)

    initDelay=(19*(count/10))/32;
    count=0;
    setlights=1;
}

interrupt [TIM1_COMPA] void cmpA_match(void)
{
    count++;
    counter++;
}

void lights(void)
{
    delay_ms(initDelay);
    for (i=0; i<4; i++)
        {
            for (j=0; j<7; j++)
                {
                    PORTA=rastor[message[i]*7+j];
                    delay_us(90);
                }
        } 
    PORTA=0xFF;
}

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port A
DDRA=0xff;
PORTA=0xff;

// Port B
DDRB=0xff;
PORTB=0x00;

// Port C
DDRC=0x00;
PORTC=0x00;

// Port D
DDRD=0x00;
PORTD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Output Compare
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Output Compare
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
TCCR1A=0x00;
TCCR1B=0x09;
TCNT1H=0x00;
TCNT1L=0x00;
OCR1A=400;
OCR1BH=0x00;
OCR1BL=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
GIMSK=0x40;
MCUCR=0x03;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x40;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
count=600;
counter=0;
display=0;
#asm
sei
#endasm

while (1)
{
    if (setlights==1)
        {
            setlights=0;
            lights();
        }

if (counter>10000)
    {
        counter=0;
        if (message[3]==9)
            {
                message[3]=0;
                if (message[2]==9)
                    {
                        message[2]=0;
                        if (message[1]==9)
                            {
                                if (message[0]==9)
                                message[0]=0;

                            }    

                        else
                            {
                                message[1]=0;
                                message[0]++;
                            }
                       }
                else
                    message[1]++;
                }
            else 
                message[2]++;
        } 
        else
        message[3]++;
    }


};
}