/*****************************************************
This program was produced by the
CodeWizardAVR V2.03.9 Standard
Automatic Program Generator
© Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project : Тест температурных датчиков
Version : 1.001
Date    : 05.03.2010
Author  : Евгений Панасенко
Company : Resonance
Comments: 
Термометр


Chip type               : ATmega8L
Program type            : Application
AVR Core Clock frequency: 8,000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 256
*****************************************************/

#include <mega8.h>

// 1 Wire Bus functions
#asm
   .equ __w1_port=0x15 ;PORTC
   .equ __w1_bit=2
#endasm
#include <1wire.h>

// DS1820 Temperature Sensor functions
#include <ds1820.h>

// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x12 ;PORTD
#endasm
#include <lcd.h>

#include <delay.h>

unsigned int value;         // Значение, необходимое для засылки в конвертацию
unsigned char OutValue[5];  // Конвертированное число в ANSII
unsigned char LcdBuffer1[16]; // Видеобуффер 1
unsigned char LcdBuffer2[16]; // Видеобуффер 2


#define ADC_VREF_TYPE 0xC0
#define ADC_AVCC_TYPE 0x40

// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}

// Declare your global variables here
// Конвертирование числа
void ConversionValue(void)
{
OutValue[0] = 0x30;
OutValue[1] = 0x30;
OutValue[2] = 0x30;
OutValue[3] = 0x30;
OutValue[4] = 0x30;
while(value > 9999)
    {
    value -= 10000;
    OutValue[4] ++;
    }
while(value > 999)
    {
    value -= 1000;
    OutValue[3] ++;
    }
while(value > 99)
    {
    value -= 100;
    OutValue[2] ++;
    }        
while(value > 9)
    {
    value -= 10;
    OutValue[1] ++;
    }
OutValue[0] += value;
}
    

void main(void)
{
unsigned char a;
// Declare your local variables here

// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=P State4=P State3=P State2=P State1=P State0=P 
PORTB=0x3F;
DDRB=0x00;

// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=Out Func1=In Func0=Out 
// State6=T State5=T State4=T State3=T State2=1 State1=T State0=0 
PORTC=0x04;
DDRC=0x05;

// Port D initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=In Func2=Out Func1=Out Func0=Out 
// State7=0 State6=0 State5=0 State4=0 State3=P State2=0 State1=0 State0=0 
PORTD=0x08;
DDRD=0xF7;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 7,813 kHz
// 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
TCCR1A=0x00;
TCCR1B=0x05;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
MCUCR=0x00;

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

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// ADC initialization
// ADC Clock frequency: 62,500 kHz
// ADC Voltage Reference: AREF pin
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x87;

// 1 Wire Bus initialization
w1_init();
// LCD module initialization
lcd_init(16);
lcd_putsf("HELLOW!!!");
delay_ms(1000);


while (1)
    {       
    
    // ПЕРВЫЙ DS600  
    for(a = 0; a < 64; a++)
    {
    value += read_adc(3);     
    }
    value /= 64;
    value = (((unsigned long int) value)*2695/1024);
    delay_ms(1);            
    value = (((unsigned long int) (value - 509)) * 1000) / 645;
    ConversionValue(); 
    lcd_gotoxy(0,0); 
    lcd_putsf("DS600-1 : ");
    lcd_putchar(OutValue[4]);
    lcd_putchar(OutValue[3]);
    lcd_putchar(OutValue[2]); 
    lcd_putchar(OutValue[1]);
    lcd_putchar(OutValue[0]); 
    
    // ВТОРОЙ DS600
    for(a = 0; a < 64; a++)
    {
    value += read_adc(1);     
    }
    value /= 64;
    value = (((unsigned long int) value)*2695/1024);
    delay_ms(1);
    ConversionValue(); 
    lcd_gotoxy(0,1); 
    lcd_putsf("DS600-2 : ");
    lcd_putchar(OutValue[4]);
    lcd_putchar(OutValue[3]);
    lcd_putchar(OutValue[2]); 
    lcd_putchar(OutValue[1]);
    lcd_putchar(OutValue[0]);    
    
    // DS18B20
    value = ds1820_temperature_10(0);
    ConversionValue(); 
    lcd_gotoxy(2,2);
    lcd_putsf("  18B20:");
    lcd_putchar(0x20);
    lcd_putchar(OutValue[3]);
    lcd_putchar(OutValue[2]); 
    lcd_putsf(".");
    lcd_putchar(OutValue[1]);
    lcd_putchar(OutValue[0]);   
    // Конфигурация АЦП, измерение ИОН
    //вывод на ЖКИ  

    };
}
