Search This Blog

Wednesday, July 17, 2013

Atmega16 I2C example and writing to EEPROM


#include
#include
#include
#define high_byte(x) ((x & 0xFF00) >> 8)
#define NO 0x00 // Value representing NO
// I2C Bus functions
#asm
   .equ __i2c_port=0x12 ;PORTD
   .equ __sda_bit=3
   .equ __scl_bit=2
#endasm
#include
#asm
   .equ __lcd_port=0x15 ;PORTC
#endasm
#include
// Declare your global variables here
unsigned char read_byte (unsigned int address);    
void write_byte (unsigned char data_out, unsigned int address);
void READ_FREQ_COUNTER(void);  
void READ_PC_COUNTER(void);  
void WRITE_TO_EEPROM(void);

void WRITE_TO_EEPROM2(void);
unsigned int FREQ_COUNTER=1;
unsigned int PC_COUNTER=3;
void main(void)
{
unsigned char b[20];
// 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=0xff;
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=0xff;

// 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;

// 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: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
PORTD.2=0;
PORTD.3=0;
// I2C Bus initialization
i2c_init();

//unsigned char i2c_start(void);
//unsigned char i2c_read(unsigned char ack);
//unsigned char i2c_write(unsigned char data);
//write_byte((unsigned char)eeprom_address , 1);
//eeprom_address1=  read_byte(1);

//READ_PC_COUNTER();
delay_ms(200);
READ_FREQ_COUNTER();

sprintf(b,"FREQ - %u ,PC - %u",FREQ_COUNTER,PC_COUNTER) ;
lcd_init(40);
lcd_clear();
lcd_putsf("ETM");
delay_ms(2000);
while (1)
      {
       
        sprintf(b,"FREQ - %u ,PC - %u",FREQ_COUNTER,PC_COUNTER) ;
        lcd_clear();
        lcd_puts(b);        
       
          if (PINB.1 == 0)
          {
             FREQ_COUNTER++;
             WRITE_TO_EEPROM();
             delay_ms(200);
          }
           
         
           if (PINB.2 == 0)
          {
              PC_COUNTER++;
             WRITE_TO_EEPROM();
             delay_ms(200);
          }  
         
           if (PINB.3 == 0)
          {
               READ_FREQ_COUNTER();
              // READ_PC_COUNTER();
                delay_ms(200);
          }  
         
         delay_ms(600);
      };
}


void write_byte (unsigned char data_out, unsigned int address)
{
i2c_start(); // Send start signal
i2c_write(0xA0);       // Send identifier I2C address
i2c_write(high_byte(address)); // Send address to EEPROM
    i2c_write((unsigned char)address);   // Send address to EEPROM
    i2c_write(data_out);             // Send low byte to EEPROM
    delay_ms(100);       // Delay a period of time to write
    i2c_stop();                   // Send I2C Stop Transfer
    delay_ms(100);       // Delay a period of time to write
}

//------------------------------------------------------------------------------
// Procedure: read_byte
// Inputs: address
// Outputs: none
// Description: Reads a byte from the EEPROM given the address
//------------------------------------------------------------------------------
unsigned char read_byte (unsigned int address)
{
unsigned char data_in;

i2c_start(); // Send start signal

i2c_write(0xA0);               // Send identifer I2C address
    i2c_write(high_byte(address));   // Send address to EEPROM
    i2c_write((unsigned char)address);   // Send address to EEPROM
i2c_start();             // Send I2C Start Transer
i2c_write(0xA1);               // Send identifer I2C address
    data_in = i2c_read(NO);       // Read byte
    i2c_stop();                   // Send I2C Stop Transfer

    return data_in;                
}



void WRITE_TO_EEPROM()
{
i2c_start();
i2c_write(0xA0);    
//i2c_write(0x00);
i2c_write(high_byte(0)); // Send address to EEPROM
    i2c_write((unsigned char)0);      
i2c_write((unsigned char)FREQ_COUNTER);
delay_ms(40);
i2c_stop();

delay_ms(100);

i2c_start();
i2c_write(0xA0);
i2c_write(0x10);
//i2c_write(high_byte(0)); // Send address to EEPROM
 //   i2c_write((unsigned char)0);
i2c_write((unsigned char)PC_COUNTER);  
delay_ms(40);
i2c_stop();
}

void READ_FREQ_COUNTER()
{
i2c_start();
i2c_write(0xA0);    
    i2c_write(high_byte(0));   // Send address to EEPROM
    i2c_write((unsigned char)0);
i2c_start();
i2c_write(0xA1);        
FREQ_COUNTER = i2c_read(0);

i2c_stop();

delay_ms(100);

i2c_start();
i2c_write(0xA0);  
i2c_write(0x10);
   // i2c_write(high_byte(0));   // Send address to EEPROM
   // i2c_write((unsigned char)0);  
i2c_start();
i2c_write(0xA1);
PC_COUNTER = i2c_read(0);

//   eeprom_address = i2c_read(0);
//    sprintf(b,"EE - %u , %u",i2c_read(0x00), i2c_read(0x00)) ;
i2c_stop();
}



void WRITE_TO_EEPROM2()
{
i2c_start();
i2c_write(0xB0);    
i2c_write(0x00);    
i2c_write((unsigned char)PC_COUNTER);  
i2c_stop();
}



void READ_PC_COUNTER()
{
i2c_start();
i2c_write(0xB0);    
i2c_write(0x00);
i2c_start();
i2c_write(0xB1);        
//  eeprom_address1 = i2c_read(0);
PC_COUNTER = i2c_read(0);
//    sprintf(b,"EE - %u , %u",i2c_read(0x00), i2c_read(0x00)) ;
i2c_stop();
}

No comments:

Post a Comment