/*****************************************************
This program was produced by the
CodeWizardAVR V2.04.4a Advanced
Automatic Program Generator
© Copyright 1998-2009 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project : 
Version : 
Date    : 31.05.2010
Author  : NeVaDa
Company : Microsoft
Comments: 


Chip type               : ATtiny2313
AVR Core Clock frequency: 8,000000 MHz
Memory model            : Tiny
External RAM size       : 0
Data Stack size         : 32
*****************************************************/

#include <tiny2313.h>
#include <delay.h>

unsigned char razrad=0b00100000;     
bit start=0;      
unsigned char cloc[6]={0,0,0,0,0,0};

unsigned char tik=0;
unsigned char des_tik=0;
unsigned char sec=0;
unsigned char des_sec=0;
unsigned char min=0; 
unsigned char des_min=0;

unsigned char x=0;

unsigned char znak [10] = {0b00111111, 0b00000110, 0b01011011, 0b01001111, 0b01100110, 0b01101101, 0b01111101, 0b00000111, 0b01111111, 0b01101111};//знакогенератор.
//unsigned char znak [10] = {0b11000000, 0b11111001, 0b10100100, 0b10110000, 0b10011001, 0b10010010, 0b10000010, 0b11111000, 0b10000000, 0b10010000};//знакогенератор.




//******************************  динамическая индикация  **********************************
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
        PORTD = razrad^0xFF ;
          // PORTD = razrad;        
        PORTB = znak [cloc[x]];
        
        if ((x==2) || (x==4))  {PORTB.7 =1;}   else  PORTB.7 =0;
        
        razrad>>= 1;  x++; 
        if (x==6)  {x=0;  razrad=0b00100000;} 
               // if (x==4)  {x=0;  razrad=0b0001000;}    



}

//********************************  отсчет времени  ************************************
interrupt [TIM1_COMPA] void timer1_compa_isr(void)
{

if (start==0)  goto m1;

tik++;
if (tik==10)  {tik=0; des_tik++;}
if (des_tik==10)  {des_tik=0; sec++;}
if (sec==10)  {sec=0; des_sec++;}
if (des_sec==6)  {des_sec=0; min++;}
if (min==10)  {min=0; des_min++;}
if (des_min==10)  {des_min=0;}

cloc[0] = tik;
cloc[1] = des_tik;
cloc[2] = sec;
cloc[3] = des_sec;
cloc[4] = min; 
cloc[5] = des_min; 

m1:
}



void main(void)
{
#pragma optsize-
CLKPR=0x80;
CLKPR=0x00;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

PORTA=0x00;DDRA=0x00;
PORTB=0x00;DDRB=0xFF;
PORTD=0x40;DDRD=0x3F;


// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 125,000 kHz
// Mode: Normal top=FFh
// OC0A output: Disconnected
// OC0B output: Disconnected
TCCR0A=0x00;
TCCR0B=0x03;
TCNT0=0x00;
OCR0A=0x00;
OCR0B=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 125,000 kHz
// Mode: CTC top=OCR1A
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: On
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x0B;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x04;
OCR1AL=0xE2;
OCR1BH=0x00;
OCR1BL=0x00;


TIMSK=0x42;
ACSR=0x80;
#asm("sei")

while (1)
      {
      while (PIND.6!=0) {};
      start++;  delay_ms(200);
      while (PIND.6!=1) {};
      };
}
