/*****************************************************
This program was produced by the
CodeWizardAVR V1.25.8 Standard
Automatic Program Generator
© Copyright 1998-2007 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project : Часы
Version : 2.0
Date    : 20.01.2011
Author  : арсений    
Company : дом                               
Comments: 
...


Chip type           : ATmega8535
Program type        : Application
Clock frequency     : 8.000000 MHz
Memory model        : Small
External SRAM size  : 0
Data Stack size     : 128
*****************************************************/

#include <mega8535.h>
#include <delay.h>
// I2C Bus functions
#asm
   .equ __i2c_port=0x15 ;PORTC
   .equ __sda_bit=1
   .equ __scl_bit=0
#endasm
#include <i2c.h>
#include <bcd.h>
// DS1307 Real Time Clock functions
#include <ds1307.h>

// Declare your global variables here
unsigned char hour, min, sec; //переменные для времени.
unsigned char tmp;
unsigned char hour2;
unsigned char min2;
unsigned char sec2;
bit flag;
void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out 
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 

DDRA=0xFF;

// Port B initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out 
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 

DDRB=0xFF;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTC=0xFF;


// Port D initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out 
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 

DDRD=0xFF;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected


// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off


// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected


// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Offtyjhyt

// Timer(s)/Counter(s) Interrupt(s) initialization


// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;


// I2C Bus initialization
i2c_init();

// DS1307 Real Time Clock initialization
// Square wave output on pin SQW/OUT: On
// SQW/OUT pin state: 1Hz
tmp = rtc_read(0x00);
//tmp &=~(1<<7);
tmp=tmp&0b01111111;
rtc_write(0x00, tmp); //включение DS1307, бит 7 ноль
tmp = rtc_read(0x02);
//tmp &=~(1<<6);
tmp=tmp&0b0111111;

rtc_write(0x02, tmp); // бит 6 ноль - 24ох часовой режим
rtc_init(0, 1, 1); // включение генератора импульсов


while (1)
      {
      // Place your code here
       
      
start: rtc_get_time(&hour, &min, &sec); //  читаем из DS-ки время

tmp=bin2bcd(sec);
            PORTA=tmp;                   
            tmp=bin2bcd(min);
            PORTB=tmp;
            tmp=bin2bcd(hour);                  
            PORTD=tmp;
                               
            delay_ms(100);      //задержка
   switch (PINC&0b11111100) {   //проверка кнопок
    case 0b01111100: delay_ms(1000);             //нажата кнопка set
    parametr:                 switch (PINC&0b11111100) {            //проверка кнопок
                     case 0b10111100: delay_us(10);                 //нажата hour
                     
                     
                     hour++;
                     if (hour>=24) {
                     hour=0;
                     };

                     hour2=bin2bcd(hour);
                     PORTD=hour2;
                     delay_ms(500);
                     goto parametr;                                 //перейти к выбору параметра
                     break;
                     case 0b11011100: delay_us(10);                 //нажата кнопка min
                               
                     
                     min++;
                     if (min>=60) {
                     min=0;
                     };

                     min2=bin2bcd(min);
                     PORTB=min2;
                     delay_ms(500);
                     
                     goto parametr;                                 //перейти к выбору параметра
                     break;
                     case 0b11101100:                               //нажата кнопка sec
                     delay_us(10);
                              
                     flag=1;
                     tmp=0;                                           //обнулить k
                     sec=tmp;                                         //записать в переменную секунд
                     sec2=bin2bcd(sec);
                     PORTA=sec2;                                     //вывести на индикацию секунды
                     delay_ms(500);                                 //задержка 
                    
                     goto parametr;                                 //перейти к выбору параметра
                     break;
                     case 0b01111100:delay_us(100);                 //нажата кнопка set
                     if(flag==0){
                     sec2=bin2bcd(sec);
                     }
                     rtc_set_time(hour2, min2, sec2);                  //записать время в ds
                     delay_ms(1000);
                     goto start;                                    //вернуться к началу
                     break;
                     case 0b11111100: goto parametr;                //если ничего не нажато, вернуться к выбору параметра
                     break;
                     }; 
                     
    break;
    case 0b11111100:goto start;                                     //если ничего не нажато, вернуться к началу
    break;

    }; 

        
    }; 

      };

