/*****************************************************
This program was produced by the
CodeWizardAVR V1.25.8 Professional
Automatic Program Generator
© Copyright 1998-2007 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project : full
Version : 1
Date    : 24.03.2012
Author  : val                             
Company : org                             
Comments: 
voltmetter


Chip type           : ATmega8
Program type        : Application
Clock frequency     : 8,000000 MHz
Memory model        : Small
External SRAM size  : 0
Data Stack size     : 256
*****************************************************/

#include <mega8.h>

// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x12 ;PORTD
#endasm
#include <lcd.h>  
#include <stdio.h>

#include <delay.h>

#define ADC_VREF_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 main(void)
{    
char lcd[16];  
int b=1; //resistor  
long int x=0;
int y=2046; 
int z = 0; 
// Declare your local variables here

// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=Out Func4=Out Func3=Out Func2=In Func1=In Func0=In 
// State7=T State6=T State5=0 State4=0 State3=0 State2=P State1=P State0=P 
PORTB=0x07;
DDRB=0x38;

// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=Out Func1=In Func0=In 
// State6=T State5=T State4=T State3=T State2=0 State1=T State0=T 
PORTC=0x00;
DDRC=0x04;

// 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;



// ADC initialization
// ADC Clock frequency: 125,000 kHz
// ADC Voltage Reference: AVCC pin
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x86;

// LCD module initialization
lcd_init(16);
//PORTB.5=1;
lcd_clear();
PORTB.4=1; 

while (1)
      {
      // Place your code here
       


lcd_gotoxy(0,0);
#pragma rl+   
lcd_putsf("Ток ");  
#pragma rl- 
z=read_adc(3);   
x=read_adc(0); 
x=x-z;  
x=x*100; 
if (x>=y)
PORTB.5=1;
else
PORTB.5=0;
x=x/b;  
sprintf(lcd,"%01d,%01d%01d A ",x/y, (x%y)*10/y,((x%y)*10%y)*10/y); 
lcd_puts(lcd); 
lcd_gotoxy(0,1);   
#pragma rl+
lcd_putsf("Напряжение ");
#pragma rl-   
x=read_adc(1);
x=x*100;                                                                        
sprintf(lcd,"%01d,%01d%01d V ",x/y, (x%y)*10/y,((x%y)*10%y)*10/y);    
lcd_puts(lcd);
delay_ms(500);
    

      }
}
