/*****************************************************
Project : fan controller
Version : 1.0
Date    : 02.02.2009
Author  : Bogdan Sachkovsky
Company : Home
Comments: 


Chip type           : ATmega16
Program type        : Application
Clock frequency     : 11,059200 MHz
Memory model        : Small
External RAM size   : 0
Data Stack size     : 256
*****************************************************/

#include <mega16.h>

// 1 Wire Bus functions
#asm
   .equ __w1_port=0x18 ;PORTB
   .equ __w1_bit=0
#endasm
#include <1wire.h>

// DS1820 Temperature Sensor functions
#include <ds18b20.h>

// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x1B ;PORTA
#endasm
#include <lcd.h>
#include <stdio.h>
#include <delay.h>
#include <math.h>
// Declare your global variables here
unsigned char lcd_buffer[10];
#define MAX_DEVICES 8               // оголошення максимальної кількості датчиків температури
unsigned char rom_code[MAX_DEVICES][9]; // область збереження ром коду 

void main(void)
{
// Declare your local variables here
int temp;
unsigned char i,devices;
// Input/Output Ports initialization
// Port A 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 
PORTA=0x00;
DDRA=0x00;

// Port B 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 
PORTB=0x00;
DDRB=0x00;

// 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=0x00;
DDRC=0x00;

// Port D 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 
PORTD=0x00;
DDRD=0x00;


// 1 Wire Bus initialization
devices=w1_search(0xf0,rom_code);

// LCD module initialization
lcd_init(16);
lcd_putsf(" FAN CONTROLLER");            // вивід заставки
delay_ms(1000);                         // пауза 1000 мс
lcd_clear();                            //очищення LCD
while (1)
      {
      // Place your code here
            for (i=0;i<devices;)
            {
            temp=ds18b20_temperature(&rom_code[1][0]);
            sprintf(lcd_buffer,"t%u=%i\xdfC",i+1,temp); // вимірювання температури
            lcd_gotoxy(0,0);
            lcd_puts(lcd_buffer);                        // відображення виміряної температури на LCD
                       
            };
      };
}
