Search This Blog

Tuesday, March 16, 2010

I2C EXAMPLE WITH 24C256, ATMEGA16 CODEVISION

STORING DATA IN 24C256 IC,
 SDA  3BIT OF PORTD
SCL  2BIT OF PORTD
 WRITE_TO_EEPROM()  WILL WRITE 2 VAR,
READ_PC_COUNTER() WILL SHOW HOW TO READ

============================


#include  mega16.h
#include  delay.h
#include  stdio.h
// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x15 ;PORTC
#endasm
#include

// I2C Bus functions
#asm
   .equ __i2c_port=0x12 ;PORTD
   .equ __sda_bit=3
   .equ __scl_bit=2
#endasm
#include

#define ADC_VREF_TYPE 0x20

 

// Read the 8 most significant bits of the AD conversion result
unsigned char 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 ADCH;
}

// global variables
void  DELAY(void);
//unsigned int freq_array[]={50,100,150,200,250,300,350,400,450,500,550,600,650,700,750,800,850,900,950,1000} ;
unsigned int no_of_pulses[]={2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40} ;
unsigned char data;

//CAL ON/OFF flag
unsigned int ON_OFF_FLAG=1;
unsigned int pc_array[]={1,2,5,10,20,50,100,200,500,1000} ;
void  PC_OUTPUTS(void);
unsigned char str_buffer[20];

void READ_FREQ_COUNTER(void);   
void READ_PC_COUNTER(void);  
void WRITE_TO_EEPROM(void);
unsigned int FREQ_COUNTER=0;
unsigned int PC_COUNTER=0;

void main(void)
{
// 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=0xFF;
DDRB=0x00;          //input   push buttons

// 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=0xff;           //output         lcd

// 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=0xff;           //output     led

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=FFFFh
// 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=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

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

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

// Analog Comparator initialization
// Analog Comparator:
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0xA6;
SFIOR&=0x1F;

PORTD.2=0;
PORTD.3=0;
// I2C Bus initialization
i2c_init();
//WRITE_TO_EEPROM();    //this will reset both var to 0
READ_FREQ_COUNTER();
READ_PC_COUNTER();

lcd_init(20);
lcd_clear();
lcd_putsf("ETM");
delay_ms(2000);

DELAY();
PC_OUTPUTS();

while (1)
      {      
        if (PINB.4 == 0)
            if (ON_OFF_FLAG==1)
            ON_OFF_FLAG=0;
            else
            ON_OFF_FLAG=1;
         
        if (ON_OFF_FLAG==1)
        {
            data=read_adc(0);                       //outout 0 - 255  read voltage from PORTA.0
            DELAY(); 
            PORTD.7=1;
            DELAY();
            PORTD.7=0;
                    
            if (PINB.0 == 0)
            {
                if (FREQ_COUNTER < 19)
                    {
                           FREQ_COUNTER++;
                           WRITE_TO_EEPROM();
                        } 
            }
                 
            if (PINB.1 == 0)
            {    
                if (FREQ_COUNTER > 0)
                        {
                            FREQ_COUNTER--; 
                            WRITE_TO_EEPROM();
                        } 
            }     
                 
            if (PINB.2 == 0)
            {
                if (PC_COUNTER < 9)
                           {      
                            PC_COUNTER++;
                            PC_OUTPUTS();
                            WRITE_TO_EEPROM();
                        }
            }
                 
            if (PINB.3 == 0)
            {
                if (PC_COUNTER > 0)
                    {
                            PC_COUNTER--;
                            PC_OUTPUTS();
                            WRITE_TO_EEPROM();
                        }  
            }   
        }
      };            
     
     
}


//------------------------------------------------------------------------------
// Procedure:    DELAY
// Inputs:        none
// Return:        none
// Description:    Vary the Delay of signal
//------------------------------------------------------------------------------
void DELAY(void)
{
     switch (no_of_pulses[FREQ_COUNTER])
       {
         case 2:
      //   (int)data /10000 *10     
         delay_ms(500);    //delay_ms(10);
         lcd_gotoxy(0,0);
         lcd_putsf("NO OF PULSES 2");
         break;  
         case 4: 
         delay_ms(600);    //delay_ms(5);
         lcd_gotoxy(0,0);
         lcd_putsf("NO OF PULSES 4");
         break;
         case 6:
         delay_ms(700);    //  delay_us(3333); 
         lcd_gotoxy(0,0);
         lcd_putsf("NO OF PULSES 6");
         break;
         case 8:
         delay_ms(800);    //  delay_us(2500);
         lcd_gotoxy(0,0);
         lcd_putsf("NO OF PULSES 8");
         break;
         case 10:
         delay_ms(900);   // delay_ms(2); 
         lcd_gotoxy(0,0);
         lcd_putsf("NO OF PULSES 10");
         break;   
          case 12:      
         delay_ms(1000);   // delay_us(1667); 
         lcd_gotoxy(0,0);
         lcd_putsf("NO OF PULSES 12");
         break;  
         case 14: 
         delay_ms(1100);   // delay_us(1428);
         lcd_gotoxy(0,0);
         lcd_putsf("NO OF PULSES 14");
         break;
         case 16:
         delay_ms(2010);   // delay_us(1250);   
         lcd_gotoxy(0,0);
         lcd_putsf("NO OF PULSES 16");
         break;
         case 18:
        delay_ms(1300);  //  delay_us(1111);
         lcd_gotoxy(0,0);
         lcd_putsf("NO OF PULSES 18");
         break;
         case 20:
         delay_ms(1400);   // delay_ms(1);  
         lcd_gotoxy(0,0);
         lcd_putsf("NO OF PULSES 20");
         break;   
         //''''''''''''
         case 22:      
         delay_ms(1500);   // delay_us(909);
         lcd_gotoxy(0,0);
         lcd_putsf("NO OF PULSES 22");
         break;  
         case 24: 
         delay_ms(1600);   // delay_us(833); 
         lcd_gotoxy(0,0);
         lcd_putsf("NO OF PULSES 24");
         break;
         case 26:
         delay_ms(1700);   // delay_us(769); 
         lcd_gotoxy(0,0);
         lcd_putsf("NO OF PULSES 26");
         break;
         case 28:
         delay_ms(1800);   // delay_us(714);
         lcd_gotoxy(0,0);
         lcd_putsf("NO OF PULSES 28");
         break;
         case 30:
         delay_ms(1900);  // delay_us(667);
         lcd_gotoxy(0,0);
         lcd_putsf("NO OF PULSES 30");
         break;   
         case 32:      
         delay_ms(2000);  //  delay_us(625);
         lcd_gotoxy(0,0);
         lcd_putsf("NO OF PULSES 32");
         break;  
         case 34: 
         delay_ms(2100);   // delay_us(588);
         lcd_gotoxy(0,0);
         lcd_putsf("NO OF PULSES 34");
         break;
         case 36:
         delay_ms(2500);  // delay_us(556);   
         lcd_gotoxy(0,0);
         lcd_putsf("NO OF PULSES 36");
         break;
         case 38:
         delay_ms(3000);  //  delay_ms(526);
         lcd_gotoxy(0,0);
         lcd_putsf("NO OF PULSES 38");
         break;
         case 40:
         delay_ms(4000);  //  delay_us(500); 
         lcd_gotoxy(0,0);
         lcd_putsf("NO OF PULSES 40");
         break; 
         default:
         delay_ms(4000); //   delay_us(500);
         lcd_gotoxy(0,0);
         sprintf(str_buffer,"ERROR PLS - %u",FREQ_COUNTER)  ;
         lcd_puts(str_buffer);
       };
}


//------------------------------------------------------------------------------
// Procedure:    PC_OUTPUTS
// Inputs:        none
// Return:        none
// Description:    Vary the 5 PC signals
//------------------------------------------------------------------------------

void PC_OUTPUTS(void)
{
       switch (pc_array[PC_COUNTER])
       {
         case 1:      
             PORTD.0=0;
             PORTD.1=0;  
             PORTD.5=1;
             PORTD.6=0;
             PORTD.4=1; 
             lcd_gotoxy(0,1);
             lcd_putsf("1 PC    ");
         break;  
         case 2: 
             PORTD.0=0;
             PORTD.1=1;  
             PORTD.5=0;
             PORTD.6=1;
             PORTD.4=0;
             lcd_gotoxy(0,1);
             lcd_putsf("2 PC    ");
         break;
         case 5:
             PORTD.0=1;
             PORTD.1=0;  
             PORTD.5=1;
             PORTD.6=1;
             PORTD.4=0;
             lcd_gotoxy(0,1);
             lcd_putsf("5 PC   ");
         break;
         case 10:
             PORTD.0=0;
             PORTD.1=0;  
             PORTD.6=1;
             PORTD.5=1;
             PORTD.4=0;
             lcd_gotoxy(0,1);
             lcd_putsf("10 PC   ");
         break;
         case 20:
             PORTD.0=0;
             PORTD.1=1;  
             PORTD.6=1;
             PORTD.5=0;
             PORTD.4=0;
             lcd_gotoxy(0,1);
             lcd_putsf("20 PC   ");
         break;   
          case 50:      
             PORTD.0=1;
             PORTD.1=0;  
             PORTD.6=3;
             PORTD.5=0;
             PORTD.4=0;  
             lcd_gotoxy(0,0);
             lcd_putsf("50 PC   ");
         break;  
         case 100: 
             PORTD.0=0;
             PORTD.1=0;  
             PORTD.6=3;
             PORTD.5=0;
             PORTD.4=0;
              lcd_gotoxy(0,1);
             lcd_putsf("100 PC   ");
         break;
         case 200:
             PORTD.0=0;
             PORTD.1=2;  
             PORTD.5=0;
             PORTD.6=0;
             PORTD.4=0;
             lcd_gotoxy(0,1);
             lcd_putsf("200 PC   ");
         break;
         case 500:
             PORTD.0=1;
             PORTD.1=0;  
             PORTD.6=0;
             PORTD.5=0;
             PORTD.4=0;
             lcd_gotoxy(0,1);
             lcd_putsf("500 PC    ");
         break;
         case 1000:
             PORTD.0=0;
             PORTD.1=0;  
             PORTD.6=0;
             PORTD.5=0;
             PORTD.4=0;
             lcd_gotoxy(0,1);
             lcd_putsf("1000 PC");
         break; 
         default:
             PORTD.0=1;
             PORTD.1=1;  
             PORTD.6=1;
             PORTD.5=1;
             PORTD.4=1;
             lcd_gotoxy(0,1);
             sprintf(str_buffer,"ERROR PC - %u",PC_COUNTER) ;
             lcd_puts(str_buffer);

       };
}

//------------------------------------------------------------------------------
// Procedure:    WRITE_TO_EEPROM
// Inputs:        none
// Return:        none
// Description:    Writes both variable to eeprom
//------------------------------------------------------------------------------
void WRITE_TO_EEPROM()
{
i2c_start();
i2c_write(0xA0);    
i2c_write(0x00);    
i2c_write((unsigned char)FREQ_COUNTER);
i2c_write((unsigned char)PC_COUNTER);   
i2c_stop();
delay_ms(1);
}

//------------------------------------------------------------------------------
// Procedure:    READ_FREQ_COUNTER
// Inputs:        none
// Return:        none
// Description:    Read freq counter var from eeprom
//------------------------------------------------------------------------------
void READ_FREQ_COUNTER()
{
i2c_start();
i2c_write(0xA0);    
i2c_write(0x00);
i2c_start();
i2c_write(0xA1);         
FREQ_COUNTER = i2c_read(0);
//   eeprom_address = i2c_read(0); 
//    sprintf(b,"EE - %u , %u",i2c_read(0x00), i2c_read(0x00)) ;
i2c_stop();
delay_ms(1);
}


//------------------------------------------------------------------------------
// Procedure:    READ_PC_COUNTER
// Inputs:        none
// Return:        none
// Description:    Read pc counter var from eeprom
//------------------------------------------------------------------------------
void READ_PC_COUNTER()
{
i2c_start();
i2c_write(0xA0);    
i2c_write(0x01);
i2c_start();
i2c_write(0xA1);         
//  eeprom_address1 = i2c_read(0);
PC_COUNTER = i2c_read(0); 
//    sprintf(b,"EE - %u , %u",i2c_read(0x00), i2c_read(0x00)) ;
i2c_stop();
delay_ms(1);
}

No comments:

Post a Comment