| EEPROM Write 
for PIC12F509 ;EEPROMwrite.asm
;How fast is EEPROM write?  Use this program to listen to the 
;frequency that is produced with 2 EEPROM writes in each cycle. 
;Our PIC12F629 produced a frequency of 133Hz
;  11-3-2010 
;*******************************
;
;       --+------------ +5v     
;		  |                  
;         |
;         |Vdd ---v---   
;         +---|1   Gnd|-------------+   
;             |       |             |
;          ---|GP5 GP0|--           |
;             |       |             |        
;            -|GP4 GP1|--	    |	
;             |       |     |  |    |
;           --|GP3 GP2|-----|[]|----+
;             -------       |  |    |
;            PIC12F629      piezo   |
;                                   |   
;                                   |
;    -------------------------------+-- 0v
	list	p=12F629
	radix	dec
	include	"p12f629.inc"
	
	errorlevel	-302	; Don't complain about BANK 1 Registers during assembly
	__CONFIG	_MCLRE_OFF & _CP_OFF 
                     & _WDT_OFF & _INTRC_OSC_NOCLKOUT  ;Internal osc.
;_MCLRE_OFF  - master clear must be off for gp3 to work as input pin 
;****************************************************************
; variables - names and files
;****************************************************************
temp1		equ 20h	
;****************************************************************
;Equates
;****************************************************************
status		equ 0x03
rp1		equ 0x06
rp0		equ 0x05
gpio 		equ 0x05
			
status		equ 03h
option_reg		equ 81h
		; bits on gpio
				
pin7		equ	0	;GP0  
pin6	 	equ	1	;GP1  
pin5		equ	2	;GP2  Piezo between pin and 0v rail
pin4		equ	3	;GP3    
pin3		equ	4	;GP4  
pin2		equ	5	;GP5     
 
		;bits
				
rp0		equ	5	;bit 5 of the status register
;****************************************************************
;Beginning of program
;****************************************************************
	org	0x00
	nop
	nop
	nop
	nop
	nop			
SetUp	bsf	status, rp0 	;Bank 1			
       	movlw	b'11111000'	;Set TRIS  GP2 out   
	movwf	TRISIO	   	    ;		
	bcf	status, rp0		;bank 0
	movlw       07h         		;turn off Comparator ports
        	movwf       CMCON       	;must be placed in bank 0  
	clrf 	gpio       		;Clear gpio of junk			
	goto 	Main	
		
;****************************************************************
;* Sub Routines 			*
;****************************************************************
		
	;read takes the value 0 or 4 from EEPROM location 127 and puts it in w
		
read		
	movlw	.127
	bsf	status,rp0			
	movwf	EEADR						
	bsf	EECON1,0     ;starts EEPROM read operation. Result stored in EEDATA
	movf	EEDATA,w     ;move read data into w
	bcf	status,rp0				
	movwf	temp1	     ;temp1 has the value 0 or 4 to activate piezo on GP2
	retlw	00
		
		
		
write	
	bsf	status,rp0	;select bank1	
	movwf	EEDATA						
	bcf	status,rp0	;select bank0	
	movlw	.127
	bsf	status,rp0	;select bank1	
	movwf	EEADR			
	bsf	eecon1,wren	;enable write		
	movlw	55h 		;unlock codes
	movwf	eecon2
	movlw	0aah
	movwf	eecon2
	bsf	eecon1,wr	;write begins
	bcf	status,rp0	;select bank0		
writeA	btfss	pir1,eeif	;wait for write to complete
	goto	writeA
	bcf	pir1,eeif
	bsf	status,rp0	;select bank1
	bcf	eecon1,wren	;disable other writes
	bcf	status,rp0	;select bank0		
	retlw	00			
		
						
;****************************************************************
;* Main 					*
;****************************************************************
Main	movlw	b'00000100'		
M1	call	write
	call	read
	movf	temp1,w
	movwf	gpio
	movlw	b'00000000'		
	call	write
	call	read
	movf	temp1,w
	movwf	gpio
	goto	Main		
	
				
;***************************************************************
;*EEPROM     location 127 will be used by the program 
;****************************************************************
							
	org		2100h			
					
							
	end |