;***************************************************************************
;* File Name		:i2c_br.asm
;* Title		:Software I2C Byte Read Test
;* Author		:C.Kuehnel
;* Date	 		:10/12/97
;* Version 		:1.0
;* Target MCU		:AT90S8515
;* 
;* DESCRIPTION
;* Simple software I2C driver for AVR processors. Compatible with all AVR
;* processors: AT90S1200, AT90S8515, etc..
;***************************************************************************	.

;***** Directives
.device	 at90s8515
.nolist
.include "8515def.inc"
.list

; Registers used by I2C
; define in calling program as needed (must be >= r16)
;---------------------------------------------------------------------------
.def	bitcntr 	= r20 	; bit counter
.def	i2c_del 	= r21 	; delay loop variable
.def	i2c_buffer	= r22	; async serial data byte transmit/received
.def	bus_check	= r23	; check for bus idle
;
; Main Program Register variables
;---------------------------------------------------------------------------
.def 	temp 		= r16
.def	byte		= r17
.def	dly		= r18
.def	delcnt		= r19	; Loop Counter

.equ	device_id	= $A	; Device Identification for 24C16 EEPROM
.equ	page_addr	= 1	; Page address
.equ	word_addr	= 0	; Memory location


;***** Interrupt vector table

	rjmp	RESET		; Reset handle
	reti			; External Interrupt0 handle
	reti			; External Interrupt1 handle
	reti			; T/C1 Capture Event Interrupt handle
	reti			; T/C1 CompareA Interrupt handle
	reti			; T/C1 CompareB Interrupt handle
	reti			; T/C1 Overflow Interrupt handle
	reti			; T/C0 Overflow Interrupt handle
	reti			; SPI Transfer Complete Interrupt handle
	reti			; UART Rx Complete Interrupt handle
	reti			; UART Data Register Empty Interrupt handle
	reti			; UART Tx Complete Interrupt handle
	reti			; Analog Comparator Interrupt handle

;***** Includes

.include "i2c.inc"

;***** Subroutines

DELAY:	clr	delcnt		; Init Loop Counter
loop1:	dec	delcnt
	nop
	brne	loop1
	dec	dly
	brne	loop1
	ret

;***** Main


RESET:	ldi	temp, LOW(RAMEND)
	out	SPL, temp		; Initialize SPL
	ldi	temp, HIGH(RAMEND)
	out	SPH, temp		; Initialize SPH
	ser	temp
	out	DDRB, temp
	out	PORTB,temp		; Setup PortB to output

	rcall 	i2c_setup		; Initialize UART
	rcall	i2c_start		; I2C Start Condition
	ldi	i2c_buffer, (device_id<<4)|(page_addr<<1) ; Slave address =b10100010
	rcall	i2c_write		; Write Slave address
	ldi	i2c_buffer, word_addr
	rcall	i2c_write		; Write Word address
	

	rcall	i2c_start		; I2C Start Condition
	
	ldi	i2c_buffer, (device_id<<4)|(page_addr<<1) | 1; Slave address =b10100011
	rcall	i2c_write		; Write Slave address
	rcall	i2c_read		; Read Data byte
	rcall	i2c_stop		; I2C Stop  Condition
	out	PORTB, i2c_buffer	; Output Read byte


loop:	sbi	PORTB, PB0
	ldi	dly, $FF		; Initialize delay of about 65 ms
	rcall	DELAY			; Number of cycles = 1026 * del + 4
	cbi	PORTB, PB0
	ldi	dly, $FF		; Initialize delay of about 65 ms
	rcall	DELAY			; Number of cycles = 1026 * del + 4
	rjmp 	loop			; Repeat forever
	