
.include "m168Pdef.inc"


.org 0
	jmp start
.org INT_VECTORS_SIZE

start:
	ldi r16, low(RAMEND)
	out SPL, r16
	ldi r16, high(RAMEND)
	out SPH, r16
;Diod off
	in r16, DDRB
	ori r16, (1<<PB5)
	out DDRB, r16	
	in r16,PORTB
	andi r16, ~(1<<PB5)
	out PORTB, r16
;Delay
    ldi  r18, 5
    ldi  r19, 15
    ldi  r20, 242
L1: dec  r20
    brne L1
    dec  r19
	wdr
    brne L1
	wdr
    dec  r18
    brne L1


;---------------------------------------
; load the Z-pointer with 0x0000
; Fuse Low byte (FLB)
;-------------------------------------	
; load the Z-pointer with 0x0001
; To read the Lock bits
;---------------------------------------
; load 0x0002 in the Z-pointer
; Extended Fuse byte (EFB)
;---------------------------------------
; load 0x0003 in the Z-pointer
; Fuse High byte (FHB)
;---------------------------------------
	clr zl
	clr zh
	clr r17
	clr r18
	ldi r19, (1<<BLBSET)|(1<<SELFPRGEN)
loop_read_fuse:
; It is recommended that the user checks the status bit (EEPE) in the EECR
; Register and verifies that the bit is cleared before writing to the SPMCSR Register.
wait_EEPE:
	sbic EECR,EEPE
	rjmp wait_EEPE
	sts SPMCSR, r19
	lpm r24, z
	call EEPROM_write
	inc zl
	inc r17
	cpi r17,4
	brne loop_read_fuse

;Diod on
	in r16,PORTB
	ori r16, (1<<PB5)
	out PORTB, r16

loop:
	wdr
	jmp loop


EEPROM_write:
	; Wait for completion of previous write
	sbic EECR,EEPE
	rjmp EEPROM_write
	; Set up address (r18:r17) in address register
	out EEARH, r18
	out EEARL, r17
	; Write data (r16) to Data Register
	out EEDR,r24
	; Write logical one to EEMPE
	sbi EECR,EEMPE
	; Start eeprom write by setting EEPE
	sbi EECR,EEPE
ret
