Mikroelektronika MIKROE-742 데이터 시트

다운로드
페이지 532
Library Example
This code demonstrates use of TWI Library procedures and functions. AVR MCU is
connected (SCL, SDA pins ) to 24c02 EEPROM. Program sends data to EEPROM
(data is written at address 2). Then, we read data via TWI from EEPROM and send
its value to PORTA, to check if the cycle was successful. Check the figure below.
program TWI_Simple;
begin
DDRA := 0xFF;        
// configure PORTA as output
TWI_Init(100000);   
// initialize TWI communication
TWI_Start();      
// issue TWI start signal
TWI_Write(0xA2);        // send byte via TWI (device address + W)
TWI_Write(2);           // send byte (address of EEPROM location)
TWI_Write(0xAA);        // send data (data to be written)
TWI_Stop();            // issue TWI stop signal
Delay_100ms();
TWI_Start();     
// issue TWI start signal
TWI_Write(0xA2);    
// send byte via TWI (device address + W)
TWI_Write(2);     
// send byte (data address)
TWI_Start();     
// issue TWI signal repeated start
TWI_Write(0xA3);    
// send byte (device address + R)
PORTA := TWI_Read(0); 
// read data (NO acknowledge)
TWI_Stop();          
// issue TWI stop signal}
end.
HW Connection
Interfacing 24c02 to AVR via TWI
476
MIKROELEKTRONIKA
- SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
Libraries
mikroPASCAL PRO for AVR
CHAPTER 6