Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Thread Tools Display Modes
Old 17th February 2006, 02:25 AM   (permalink)
Default eeprom internal pic16f628

dear all
m trying to use internal eeprom of pic16f628 to read and write data .this is the code below .m not getting the correct output .please help me find the mistake.
Code:
LIST	p=16F628a		;tell assembler what chip we are using
	include "P16F628a.inc"		;include the defaults for the chip
	ERRORLEVEL	0,	-302	;suppress bank selection messages
	__CONFIG _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT & _PWRTE_ON & _LVP_OFF & _BODEN_OFF & _MCLRE_OFF




		cblock	0x20			;start of general purpose registers
			count			;used in looping routines
			count1			;used in delay routine
			counta			;used in delay routine
			countb			;used in delay routine

			prom_ADDR
			prom_DATA

	
		endc



		org	0x0000

		movlw	0x07
		movwf	CMCON			;turn comparators off (make it like a 16F84)

Initialise	clrf	count
		clrf	PORTA
		clrf	PORTB
		clrf	prom_ADDR
		clrf	prom_DATA


SetPorts	bsf 	STATUS,		RP0	;select bank 1
		movlw	0x00			;make all pins outputs
		movwf	TRISA
		movwf	TRISB
		bcf 	STATUS,		RP0	;select bank 0

	


		call	Delay255
		call	Delay255
		call	Delay255
		call	Delay255


		movlw 	0x50
		movwf	prom_ADDR
		movlw 	0x12
		movwf	prom_DATA
		call	write_eeprom
		
		call 	Delay20




		movlw	0x50
		movwf	prom_ADDR
		call	read_eeprom

		movf	prom_DATA, w
		movwf	PORTB

stop	goto	stop

;Subroutines

write_eeprom

		BSF STATUS, RP0 ;Bank 1
		
		movlw 	prom_ADDR
		movwf	EEADR
		movf	prom_DATA, w
		movwf	EEDATA	
		
		BSF EECON1, WREN ;Enable write
		;BCF INTCON, GIE ;Disable INTs.
		MOVLW 0x55 ;
		MOVWF EECON2 ;Write 55h
		MOVLW 0xAA ;
		MOVWF EECON2 ;Write AAh
		BSF EECON1,WR ;Set WR bit
						;begin write
		;BSF INTCON, GIE ;Enable INTs.

		BCF 	STATUS, RP0 ;Bank 0
		retlw	0x00	








read_eeprom
		BSF 	STATUS, RP0 ;Bank 1
		MOVLW 	prom_ADDR 	;
		MOVWF 	EEADR ;Address to read
		BSF 	EECON1, RD ;EE Read
		MOVF 	EEDATA, W ;W = EEDATA
		movwf	prom_DATA
		BCF 	STATUS, RP0 ;Bank 0
		
		return




Delay255	movlw	0xff		;delay 255 mS
		goto	d0
Delay100	movlw	d'100'		;delay 100mS
		goto	d0
Delay50		movlw	d'50'		;delay 50mS
		goto	d0
Delay20		movlw	d'20'		;delay 20mS
		goto	d0
Delay5		movlw	0x05		;delay 5.000 ms (4 MHz clock)
d0		movwf	count1
d1		movlw	0xC7			;delay 1mS
		movwf	counta
		movlw	0x01
		movwf	countb
Delay_0
		decfsz	counta, f
		goto	$+2
		decfsz	countb, f
		goto	Delay_0

		decfsz	count1	,f
		goto	d1
		retlw	0x00



		end
neelam29 is offline   Reply With Quote
Old 17th February 2006, 03:18 AM   (permalink)
Default

You have a couple of problem in your code. The main one is that your variables (prom_ADDR & prom_DATA) are in bank 0 but you are accessing them in bank 1.

This can be fixed a number of ways.
1. Move the variables to a location that appears in both banks. These are 0x70 to 0x7f.
2. Switch banks when accessing them.
I.E.

Code:
; assumed to be in bank 0
      movlw    prom_ADDR 
      BSF STATUS, RP0 ;Bank 1        
      movwf   EEADR 
      BCF STATUS, RP0 ;Bank 0
      movf   prom_DATA, w 
      BSF STATUS, RP0 ;Bank 1        
      movwf   EEDATA
Do the same in the read EEPROM.

3. Use a subroutine to setup address. You won't need the variables then.
Code:
      movlw 0x50h
      call  setaddress
      movlw 0x12
      call write_eeprom
      .
      .

setaddress
      BSF STATUS, RP0 ;Bank 1        
      movwf   EEADR 
      BCF STATUS, RP0 ;Bank 0
      return

write_eeprom
      BSF STATUS, RP0 ;Bank 1        
      movwf   EEDATA    
     .
     .

The other thing that may cause a problem is that you don't wait for the write to be completed. After you set the WR bit you should do something like.
Code:
                bsf    EECON1,WR
                bcf    STATUS,RP0
waitEEdone      btfss  PIR1,EEIF
                goto   waitEEdone
                bcf    PIR1,EEIF
                bsf    STATUS,RP0
                bcf    EECON1,WREN
You should also clear the WREN bit as shown above.

And lastly, it's a good idea to disable interrupts where indicated. If you don't then a very hard to find bug could come along later.

HTH


Mike.
Pommie is online now   Reply With Quote
Old 17th February 2006, 03:23 AM   (permalink)
Default

Thanks for your great help. Actually i'm very new to PIC programming, if you can please provide me a complete example of this (even a small one will do), i'll be really very thankful to you.

Thanks once again
neelam29 is offline   Reply With Quote
Old 17th February 2006, 03:48 AM   (permalink)
Default

If you want complete examples, then have a look at Nigel's Tutorials.

Mike.
Pommie is online now   Reply With Quote
Old 17th February 2006, 03:55 AM   (permalink)
Default

Speaking of tutorials, you may want to check out the Elmer 160 Tutorial for a decent A-to-Z tutorial series that also helps reinforce good programming practices...

Have fun... Regards, Mike
Mike, K8LH is online now   Reply With Quote
Old 17th February 2006, 08:58 AM   (permalink)
Default

Thanks a lot everyone. I was able to do it.
neelam29 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes




All times are GMT. The time now is 04:52 PM.


Electronic Circuits  |  Electronics Wiki
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.