/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.6 UL Standard
Automatic Program Generator
© Copyright 1998-2012 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project : 
Version : 
Date    : 01.11.2012
Author  : Sausen Mashinenbau GmbH License1
Company : Porzer Str.70b Koeln D51107     
Comments: 


Chip type               : ATmega1284P
Program type            : Application
AVR Core Clock frequency: 16,000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 4096
*****************************************************/

#include <mega1284p.h>

// I2C Bus functions
#include <i2c.h>

// Standard Input/Output functions
#include <stdio.h>
#include <delay.h>
#include <stdlib.h>

// Declare your global variables here
#define M0_1        660.0   //9028.0
#define M0_2        9028.0  //660.0
#define M100_1      4077    //13933.0
#define M100_2      14000   //2473.0
#define dM_1        34.71
#define dM_2        49.72
char val[2];
int     new, old_1,old_2,value_1,value_2;                                                                                 

void init()
{
// Declare your local variables here

// 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;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=0xFF
// OC0A output: Disconnected
// OC0B output: Disconnected
TCCR0A=0x00;
TCCR0B=0x00;
TCNT0=0x00;
OCR0A=0x00;
OCR0B=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
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: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2A output: Disconnected
// OC2B output: Disconnected
ASSR=0x00;
TCCR2A=0x00;
TCCR2B=0x00;
TCNT2=0x00;
OCR2A=0x00;
OCR2B=0x00;

// Timer/Counter 3 initialization
// Clock source: System Clock
// Clock value: Timer3 Stopped
// Mode: Normal top=0xFFFF
// OC3A output: Discon.
// OC3B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer3 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR3A=0x00;
TCCR3B=0x00;
TCNT3H=0x00;
TCNT3L=0x00;
ICR3H=0x00;
ICR3L=0x00;
OCR3AH=0x00;
OCR3AL=0x00;
OCR3BH=0x00;
OCR3BL=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
// Interrupt on any change on pins PCINT0-7: Off
// Interrupt on any change on pins PCINT8-15: Off
// Interrupt on any change on pins PCINT16-23: Off
// Interrupt on any change on pins PCINT24-31: Off
EICRA=0x00;
EIMSK=0x00;
PCICR=0x00;

// Timer/Counter 0 Interrupt(s) initialization
TIMSK0=0x00;

// Timer/Counter 1 Interrupt(s) initialization
TIMSK1=0x00;

// Timer/Counter 2 Interrupt(s) initialization
TIMSK2=0x00;

// Timer/Counter 3 Interrupt(s) initialization
TIMSK3=0x00;

// USART0 initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART0 Receiver: On
// USART0 Transmitter: On
// USART0 Mode: Asynchronous
// USART0 Baud Rate: 9600
UCSR0A=0x00;
UCSR0B=0x18;
UCSR0C=0x06;
UBRR0H=0x00;
UBRR0L=0x67;

// USART1 initialization
// USART1 disabled
UCSR1B=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
ADCSRB=0x00;
DIDR1=0x00;

// ADC initialization
// ADC disabled
ADCSRA=0x00;

// SPI initialization
// SPI disabled
SPCR=0x00;

// TWI initialization
// TWI disabled
TWCR=0x00;

// I2C Bus initialization
// I2C Port: PORTB
// I2C SDA bit: 0
// I2C SCL bit: 1
// Bit Rate: 100 kHz
// Note: I2C settings are specified in the
// Project|Configure|C Compiler|Libraries|I2C menu.
i2c_init();

}


void mcp_3426_read()
{
    //int value=0;
    i2c_start();
    i2c_write(209);    
    val[0] = i2c_read(1);  //   ACK
    val[1] = i2c_read(0);  //  NACK
    i2c_stop();
    //return value;                    
}


void mcp_3426_init_zelle_1()
{
i2c_start();
i2c_write(208);
i2c_write(24);//56 24
i2c_stop();
}


void mcp_3426_init_zelle_2()
{
i2c_start();
i2c_write(208);
i2c_write(56);//56 24
i2c_stop();
}

float calc_masse_1(int i)
{
    float m = 0.00;    
    m = (i - M0_1) / dM_1;    
    return m;
}

float calc_masse_2(int i)
{
    float m = 0.00;    
    m = (i - M0_2) / dM_2;    
    return m;
}

void read_zelle_1()
{
      mcp_3426_init_zelle_1();        
      mcp_3426_read(); 
      value_1 = (val[0] << 8)+val[1];
      value_1 = value_1 * 0.6 + 0.4 * old_1;
}

void read_zelle_2()
{
      mcp_3426_init_zelle_2();        
      mcp_3426_read(); 
      value_2 = (val[0] << 8)+val[1];
      value_2 = value_2 * 0.6 + 0.4 * old_2;
}

void main(void)
{

float   m     = 0.00;  
char zelle_1[8];
char zelle_2[8];
char wert[8];
init();
      


while (1)
      {    
      //value_1 = read_zelle_2();
      //value_2 = read_zelle_2(); 
      //delay_ms(200); 
      delay_ms(100);
      read_zelle_1();
      delay_ms(100);
      read_zelle_2(); 
      
      ftoa(calc_masse_1(value_1),1,zelle_1);
      ftoa(calc_masse_2(value_2),1,zelle_2);
     // itoa(value_1,zelle_1);
      //itoa(value_2,zelle_2);
      
      printf("%s;%s\n",zelle_1,zelle_2);
      //puts(zelle_2);  
      //itoa(value_2,wert);
      //puts(wert);   
      old_1 = value_1;
      old_2 = value_2;
      //delay_ms(200);
      }
}
