#include <tiny2313.h>  
#include <delay.h>
/// 1 Wire Bus functions
#asm
   .equ __w1_port=0x12 ;PORTD
   .equ __w1_bit=0
#endasm
#include <1wire.h>
#include <ds18b20.h>     //библиотека для работы с датчиком температуры

#define MAX_DS18B20 3
unsigned char ds18b20_devices;
unsigned char ds18b20_rom_codes[MAX_DS18B20][9];
int temp;
int temp1;

 int chislo[12]={ 
  0xAF,    //0
  0x21,    //1
  0x9D,   //2 
  0xB5,    //3
  0x33,    //4
  0xB6,    //5
  0xBE,    //6 
  0x25,    //7
  0xBF,    //8   
  0xB7,    //9
  0x17,    //градус
  0x40     //  точка
       };

int A,B,C;
  int n;


  
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Reinitialize Timer 0 value 
TCCR0B=0x00;
TCNT0=0x64;            
TCCR0B=0x03;
// Place your code here
  n++;  
  if(n==4) n=1;   
  PORTB=0x00;
  PORTD.3=1;
  PORTD.4=1;
  PORTD.5=1;
  
 switch (n) {
      case 1:  {PORTD.5=0 ;PORTB=chislo[A];};            break;   
      case 2:  {PORTD.4=0 ;PORTB=chislo[B];};           break;
      case 3:  {PORTD.3=0 ;PORTB=chislo[C];};           break;
    }; 

 if(n==4) n=1;


}

// Declare your global variables here

void main(void)
{
// Declare your local variables here


#pragma optsize-
CLKPR=0x80;
CLKPR=0x00;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

// 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 
PORTB=0x00;
DDRB=0xFF;
// Port D initialization
// Func6=In Func5=Out Func4=Out Func3=Out Func2=In Func1=In Func0=In 
// State6=T State5=1 State4=1 State3=1 State2=T State1=T State0=T 
PORTD=0x38;
DDRD=0x38;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 15,625 kHz
// Mode: Normal top=FFh
// OC0A output: Disconnected
// OC0B output: Disconnected
TCCR0A=0x00;
TCCR0B=0x03;
TCNT0=0x64;
OCR0A=0x00;
OCR0B=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x02;

// 1 Wire Bus initialization
//w1_init();
// Global enable interrupts      
w1_init();
ds18b20_devices=w1_search(0xf0,ds18b20_rom_codes);
ds18b20_init(&ds18b20_rom_codes[0][0],-55,125,DS18B20_9BIT_RES);

//devices=w1_search(0xf0,rom_code);
#asm("sei")
 
while (1)
      {
  //#asm("cli");
 temp=ds18b20_temperature(&ds18b20_rom_codes[0][0]);    
 //#asm("sei")   
 if(temp!=-9999){
   temp1=temp;  

  if(temp1<100)  {  A=temp1/10;   B= temp1%10;      C=10;              delay_ms(200); };    
  if(temp1>=100){  A=temp1/100; B=(temp1-100)/10; C= temp1%10; delay_ms(200); };
  
            };                          
      };
}
