; ; Program18_1.asm ; ; Created: 07/27/2017 08:26:08 ; Author : Naimi ; LDI R21,HIGH(RAMEND) ;set up stack OUT SPH,R21 LDI R21,LOW(RAMEND) OUT SPL,R21 CALL I2C_INIT ;initialize TWI module CALL I2C_START ;transmit START condition LDI R27, 0b11010000 ;SLA(1001100) + W (0) CALL I2C_WRITE ;write R27 to I2C bus LDI R27, 0b11110000 ;data to be transmitted CALL I2C_WRITE ;write R27 to I2C bus CALL I2C_STOP ;transmit STOP condition HERE: RJMP HERE ;wait here forever ;********************************************************* I2C_INIT: LDI R21, 0 STS TWSR,R21 ;set prescaler bits to zero LDI R21, 152 ;move 152 into R21 STS TWBR,R21 ;set clock freq. to 50k (16 MHz XTAL) LDI R21, (1<<TWEN) ;move 0x04 into R21 STS TWCR,R21 ;enable the TWI RET ;********************************************************* I2C_START: LDI R21, (1<<TWINT)|(1<<TWSTA)|(1<<TWEN) STS TWCR,R21 ;transmit a START condition WAIT1: LDS R21, TWCR ;read control register into R21 SBRS R21, TWINT ;skip next line if TWINT is 1 RJMP WAIT1 ;jump to WAIT1 if TWINT is 1 RET ;********************************************************* I2C_WRITE: STS TWDR, R27 ;move the byte into TWDR LDI R21, (1<<TWINT)|(1<<TWEN) STS TWCR, R21 ;configure TWCR to send TWDR WAIT3: LDS R21, TWCR ;read control register into R21 SBRS R21, TWINT ;skip next line if TWINT is 1 RJMP WAIT3 ;jump to WAIT3 if TWINT is 0 RET ;********************************************************* I2C_STOP: LDI R21, (1<<TWINT)|(1<<TWSTO)|(1<<TWEN) STS TWCR, R21 ;transmit STOP condition RET