/*******************************************************
This program was created by the
CodeWizardAVR V3.04 Evaluation
Automatic Program Generator
© Copyright 1998-2013 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project : 
Version : 
Date    : 30.06.2013
Author  : 
Company : 
Comments: 


Chip type               : ATmega8A
Program type            : Application
AVR Core Clock frequency: 4,000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 256
*******************************************************/

#include <mega8.h>
#include <delay.h>
// Alphanumeric LCD functions
#asm
   .equ __lcd_port=0x18 ;PORTB  
#endasm 
#include <lcd.h>
#include <stdio.h>
//#include <sleep.h>
#define TWI_START 0x08
#define TWI_REP_START 0x10
#define TWI_SLA_ASK 0x18
#define TWI_DATA_ASK 0x28
// Declare your global variables here
signed char reg=47;
unsigned char _menu=0;
bit pause=0;
unsigned char SLA_W=0xA0;
unsigned char SUB_ADR=0x10;
eeprom unsigned char all[8]={0x02, 0x00, 0x2F, 0x00, 0x00, 0x00, 0x00, 0x00};
char string[9];
char c=45;
char p;
bit _mute=1;
//char scal[17]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};

// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
if (_mute)
{_mute=0;}
else 
{_mute=1;};
}

// External Interrupt 1 service routine
interrupt [EXT_INT1] void ext_int1_isr(void)
{
if (PIND.4==1)
{reg++;
if (reg>47)
 {reg=47;};}
else 
{reg--;
if (reg<0)
 {reg=0;};}// Place your code here
}

// Timer1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
pause=0;// Place your code here

}
void error (void)
{
lcd_clear();
lcd_gotoxy(2,0);
lcd_putsf("error");
}
void trans (void)
{
unsigned char x;
#asm ("cli")
TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
while (!(TWCR&(1<<TWINT)));
if ((TWSR&0xF8)!=TWI_START)
{error();};
TWDR=SLA_W;
TWCR=(1<<TWINT)|(1<<TWEN);
while (!(TWCR&(1<<TWINT)));
if ((TWSR&0xF8)!=TWI_SLA_ASK)
{error();};
TWDR=SUB_ADR;
TWCR=(1<<TWINT)|(1<<TWEN);
while (!(TWCR&(1<<TWINT)));
if ((TWSR&0xF8)!=TWI_DATA_ASK)
{error();};
for (x=0;x<8;x++)
{
TWDR=all[x];
TWCR=(1<<TWINT)|(1<<TWEN);
while (!(TWCR&(1<<TWINT)));
if ((TWSR&0xF8)!=TWI_DATA_ASK)
{error();};
};
TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
#asm ("sei")
}
void menu (void)
{    signed char bass=0;
signed char treble=0;
signed char middle=0;
      delay_ms(20);
      if (PIND.1==0)
      while (~PIND.1)
if (_menu>3) {
_menu=0;
};
_menu++;
pause=1;
TCCR1B=(1<<CS12);
switch (_menu) {
    case (1) :
    reg=all[5];
    while (PIND.1&pause)
    {
    if (reg>7)
    {reg=7;};
    lcd_clear();
lcd_gotoxy(1,0);
lcd_putsf("treble");
treble=(-14+(all[5]*2));
sprintf(string,"%i",treble);
//lcd_gotoxy(3,1);
lcd_putchar(0);
lcd_puts(string);
//reg=all[5];
if (reg!=all[5])
{
all[5]=reg;
 trans();
 TCNT1H=0x00;
TCNT1L=0x00;
};}
    break;
    case (2) :
    reg=all[4];
    while (pause&PIND.1)
    {
    if (reg>7)
    {reg=7;};
    lcd_clear();
lcd_gotoxy(1,0);
lcd_putsf("middle");
middle=(-14+(all[4]*2));
sprintf(string,"%i",middle);
//lcd_gotoxy(3,1);
lcd_putchar(0);
lcd_puts(string);
//reg=all[4];
if (reg!=all[4])
{
all[4]=reg;
 trans();
 TCNT1H=0x00;
TCNT1L=0x00;
};}
break;
case (3) :
reg=all[3];
while (pause&PIND.1)
{
if (reg>7)
{reg=7;};
lcd_clear();
lcd_gotoxy(2,0);
lcd_putsf("bass");
bass=(-14+(all[3]*2));
sprintf(string,"%i",bass);
//lcd_gotoxy(3,1);
lcd_putchar(0);
lcd_puts(string);
//reg=all[3];
if (reg!=all[3])
{
all[3]=reg;
 trans();
 TCNT1H=0x00;
TCNT1L=0x00;
};}
break;
    }; 
}

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
 
DDRB=(0<<DDB7) | (0<<DDB6) | (0<<DDB5) | (0<<DDB4) | (0<<DDB3) | (0<<DDB2) | (0<<DDB1) | (0<<DDB0);
PORTB=(0<<PORTB7) | (0<<PORTB6) | (0<<PORTB5) | (0<<PORTB4) | (0<<PORTB3) | (0<<PORTB2) | (0<<PORTB1) | (0<<PORTB0);

 
DDRC=(0<<DDC6) | (0<<DDC5) | (0<<DDC4) | (0<<DDC3) | (0<<DDC2) | (0<<DDC1) | (0<<DDC0); 
PORTC=(0<<PORTC6) | (0<<PORTC5) | (0<<PORTC4) | (0<<PORTC3) | (0<<PORTC2) | (0<<PORTC1) | (0<<PORTC0);

 
DDRD=(0<<DDD7) | (0<<DDD6) | (0<<DDD5) | (0<<DDD4) | (0<<DDD3) | (0<<DDD2) | (0<<DDD1) | (0<<DDD0);
PORTD=(0<<PORTD7) | (0<<PORTD6) | (0<<PORTD5) | (1<<PORTD4) | (1<<PORTD3) | (1<<PORTD2) | (1<<PORTD1) | (0<<PORTD0);

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=(0<<CS02) | (0<<CS01) | (0<<CS00);
TCNT0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 15,625 kHz
// Mode: Normal top=0xFFFF
// OC1A output: Disconnected
// OC1B output: Disconnected
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer Period: 4,1943 s
// Timer1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=(0<<COM1A1) | (0<<COM1A0) | (0<<COM1B1) | (0<<COM1B0) | (0<<WGM11) | (0<<WGM10);
TCCR1B=(0<<ICNC1) | (0<<ICES1) | (0<<WGM13) | (0<<WGM12) | (0<<CS12) | (0<<CS11) | (0<<CS10);
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
// OC2 output: Disconnected
ASSR=0<<AS2;
TCCR2=(0<<PWM2) | (0<<COM21) | (0<<COM20) | (0<<CTC2) | (0<<CS22) | (0<<CS21) | (0<<CS20);
TCNT2=0x00;
OCR2=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=(0<<OCIE2) | (0<<TOIE2) | (0<<TICIE1) | (0<<OCIE1A) | (0<<OCIE1B) | (1<<TOIE1) | (0<<TOIE0);

// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Low level
// INT1: On
// INT1 Mode: Falling Edge
GICR|=(1<<INT1) | (1<<INT0);
MCUCR=(1<<ISC11) | (0<<ISC10) | (1<<ISC01) | (1<<ISC00);
GIFR=(1<<INTF1) | (1<<INTF0);

// USART initialization
// USART disabled
UCSRB=(0<<RXCIE) | (0<<TXCIE) | (0<<UDRIE) | (0<<RXEN) | (0<<TXEN) | (0<<UCSZ2) | (0<<RXB8) | (0<<TXB8);

// Analog Comparator initialization
// Analog Comparator: Off
// The Analog Comparator's positive input is
// connected to the AIN0 pin
// The Analog Comparator's negative input is
// connected to the AIN1 pin
ACSR=(1<<ACD) | (0<<ACBG) | (0<<ACO) | (0<<ACI) | (0<<ACIE) | (0<<ACIC) | (0<<ACIS1) | (0<<ACIS0);
SFIOR=(0<<ACME);

// ADC initialization
// ADC disabled
ADCSRA=(0<<ADEN) | (0<<ADSC) | (0<<ADFR) | (0<<ADIF) | (0<<ADIE) | (0<<ADPS2) | (0<<ADPS1) | (0<<ADPS0);

// SPI initialization
// SPI disabled
SPCR=(0<<SPIE) | (0<<SPE) | (0<<DORD) | (0<<MSTR) | (0<<CPOL) | (0<<CPHA) | (0<<SPR1) | (0<<SPR0);


// Alphanumeric LCD initialization
// Connections are specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTB Bit 0
// RD - PORTB Bit 1
// EN - PORTB Bit 2
// D4 - PORTB Bit 4
// D5 - PORTB Bit 5
// D6 - PORTB Bit 6
// D7 - PORTB Bit 7
// Characters/line: 8
lcd_init(16);

// Global enable interrupts
#asm("sei")
 trans();

while (1)
      {
      // Place your code here
      if (PIND.1==0)
      {
      menu();
      }
      if (_mute)
      {
      TCCR1B=(0<<CS12);
      reg=all[2];
      lcd_clear();
      lcd_gotoxy(1,0);
      lcd_putsf("volume");
      //lcd_gotoxy(3,1);
      lcd_putchar(0);
      if (all[2]==0) {c=0;}
      else {c=45;};
      lcd_putchar(c);
      sprintf(string,"%i",all[2]);
      lcd_puts(string);
      //lcd_gotoxy(0,1);
      //lcd_puts(scal);
      for (p=0;p<((47-all[2])/2.9375);p++)
      {lcd_gotoxy(p,1);
      lcd_putchar(0xFF);};
      //reg = all[2];
      if (reg!=all[2])
      {
      all[2]=reg;
       trans();
      }
      }
            }
}
