Write and Read from internal Memory


Closed Thread
Results 1 to 11 of 11
  1. #1

    Default Write and Read from internal Memory

    Greetings PICperts!
    Can anyone share a code that will write to the internal memory of PIC16F876A the value "1234"
    and display the value when I press a button. I have download an example but I don't really understand how it works.. A simple code will be great to help me get started.

    joe

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358

    Default

    Compiled OK with line...

    pbpw -p16f876a sample -v

    Lots of embedded comments to tell you what's happening...

    Enjoy

    Code:
    	'	Sample.BAS
    	'	==========
    	'	Target Processor PIC16F876A-04
    	'
    	'	Write value '1234' to internal EEPROM
    	'	When Button Pressed Read EEPROM value and Display on LCD
    	'
    	'
    	'	CPU Hardware Layout
    	'	-------------------
    	'
    	'	PortA.0 - Unused - Set to INPUT
    	'	PortA.1 - Unused - Set to INPUT
    	'	PortA.2 - Unused - Set to INPUT
    	'	PortA.3 - Unused - Set to INPUT
    	'	PortA.4 - Unused - Set to INPUT
    	'	PortA.5 - Unused - Set to INPUT
    	'
    	'	PortB.0	- Button - Connect between this PIC Pin and Vss (0v)
    	'		- uses internal PICs pull-up's
    	'	PortB.1 - Unused - Set to OUTPUT
    	'	PortB.2 - LCD - RS
    	'	PortB.3 - LCD - E
    	'	PortB.4 - LCD - DB4
    	'	PortB.5 - LCD - DB5
    	'	PortB.6 - LCD - DB6
    	'	PortB.7 - LCD - DB7
    	'
    	'	PortC.0	- Unused - Set to OUTPUT
    	'	PortC.1 - Unused - Set to OUTPUT
    	'	PortC.2 - Unused - Set to OUTPUT
    	'	PortC.3 - Unused - Set to OUTPUT
    	'	PortC.4 - Unused - Set to OUTPUT
    	'	PortC.5 - Unused - Set to OUTPUT
    	'	PortC.6 - Unused - Set to OUTPUT
    	'	PortC.7 - Unused - Set to OUTPUT
    
    	'
    	'	PIC Defines
    	'	-----------
    	@ DEVICE pic16F876A, XT_OSC		' System Clock Options	
    	@ DEVICE pic16F876A, WDT_ON		' Watchdog Timer
    	@ DEVICE pic16F876A, PWRT_ON		' Power-On Timer
    	@ DEVICE pic16F876A, BOD_ON		' Brown-Out Detect
    	@ DEVICE pic16F876A, LVP_OFF		' Low-Voltage Programming
    	@ DEVICE pic16F876A, CPD_OFF		' Data Memory Code Protect
    	@ DEVICE pic16F876A, PROTECT_OFF	' Program Code Protection
    	@ DEVICE pic16F876A, WRT_OFF		' Flash Memory Word Enable
    
    	'
    	'	Hardware Defines
    	'	----------------
    		'
    		' 	LCD Display
    		'	-----------
    	Define LCD_DREG PORTB			' Port for LCD Data
    	Define LCD_DBIT 4			' Use upper 4 bits of Port
    	Define LCD_RSREG PORTB			' Port for RegisterSelect (RS) bit
    	Define LCD_RSBIT 2			' Port Pin for RS bit
    	Define LCD_EREG PORTB			' Port for Enable (E) bit
    	Define LCD_EBIT 3			' Port Pin for E bit
    	Define LCB_BITS 4			' Using 4-bit bus
    	Define LCD_LINES 2			' Using 2 line Display
    	Define LCD_COMMANDUS 2000		' Command Delay (uS)
    	Define LCD_DATAUS 50			' Data Delay (uS)
    
    		'
    		'	Button Inputs
    		'	-------------
    	ButtonPRESS var PortB.0			' Push Button 
    
    	'
    	'	EEPROM
    	'	------
    	MemoryA		DATA @0,0		' The TWO memory locations for saving WORD variable
    	MemoryB		DATA 0			' Both Preset to Zero
    
    	'
    	'	RAM Assignments & Variables
    	'	---------------------------
    	WORDVariable var WORD			' Just a WORD variable we're going to use
    	
    	'
    	'	Start Program
    	'	=============
    
    	'
    	'	Set-up Hardware
    	'	---------------
    	TRISA=%00111111				' All available PortA is Input
    						' Remember it's still in ANALOG Mode
    	TRISB=%00000001				' All PortB is Output - except
    							' B.0 = Button
    	TRISC=%00000000				' All PortC is Output
    	CMCON=%00000111				' Disable Comparators
    	CVRCON=%00000000			' Disable Reference Module
    	OPTION_REG.7=0				' Enable Weak Pull-Up's
    	Pause 2000				' Wait for LCD to settle
    	'
    	'	Save Value into EEPROM
    	'	-----------------------
    	WORDVariable=1234
    	LCDOut $FE,1,"Writing=",#WordVariable	' Tell user what we're doing...
    	WRITE MemoryA,WORDVariable.HighByte	' Save one Byte from Word to EEPROM Location 0
    	WRITE MemoryB,WORDVariable.LowByte	' Save other Byte from Word to EEPROM Location 1
    	Pause 2000				' Just a pause so you can read what's on the LCD
    	'
    	'	Prompt for Button Press
    	'	-----------------------
    PromptLoop:
    	LCDOut $FE,1,"Press Button..."
    	While ButtonPRESS=1:Wend		' Wait here until Button Pressed
    	READ MemoryA,WORDVariable.HighByte	' Reconstruct Word variable from EEPROM
    	READ MemoryB,WORDVariable.LowByte
    	LCDOut $FE,1,"Reading=",#WORDVariable	' Display Result
    	While ButtonPress=0:Wend		' Wait here as long as finger is on Button
    	Goto PromptLoop
    	'
    	End

  3. #3

    Default

    Melanie,

    Thanks for your fast reply, I will work this out and let you know the outcome later.


    regards,
    joe

  4. #4
    Join Date
    Apr 2006
    Location
    New Hampshire USA
    Posts
    298

    Smile Thanks

    Thank you for the great sample, Melanie.

    The comments make it a real tutorial on reading and writing to EEPROM. The logic and flow are a glimpse into the world of how professionals write code. Very impressive.

    I will be studying this masterpiece for some time to come.

    -Adam-
    Ohm it's not just a good idea... it's the LAW !

  5. #5
    Join Date
    Nov 2007
    Location
    Cebu, Philippines
    Posts
    14

    Default using 16F877A

    Hi Melanie!

    Is the sample.bas that you posted about how to read and write the EEPROM applicable for 16F877A?

    If not, what certain modifications should be done?

    Please help...

    ria

  6. #6
    Join Date
    Jul 2003
    Posts
    2,358

    Default

    Adam - *blush* personally Signed framed Masterpieces are available in exchange for internationally negotiable crinkly drinking vouchers...


    Ria - Yes it is, with a few changes...

    1. Change ALL the PIC defines to 877A instead 876A...

    eg

    @ DEVICE pic16F876A, XT_OSC

    becomes

    @ DEVICE pic16F877A, XT_OSC

    2. Add additional TRIS statements to account for Ports D and E.

    eg

    TRISE=%11111111

    Remembering that parts of PortE will still have Analog on them...

    3. Compile with the command line

    PBPW -p16f877A sample -v

  7. #7
    Join Date
    Nov 2007
    Location
    Cebu, Philippines
    Posts
    14

    Default thank you

    I see..
    Thank you so much Melanie..

    I really learn a lot from this forum.

    Cheers!

    ria

  8. #8
    Join Date
    Nov 2007
    Location
    Cebu, Philippines
    Posts
    14

    Default PBP vs PBPW

    By the way Melanie..why we need to use PBPW instead of PBP in compiling?

    What's the difference between them?

  9. #9

    Default

    Hi Melanie,
    I got your code running, initially I placed '9999' at memory location 0. I'm trying
    to read it from the internal memory, So I try this code below. But when I press the button I got '655535'
    instead the initial value '9999'.



    TRISB=%00010000 ' All PortB is Output - except
    ' B.3 = Button
    TRISC=%00000000 ' All PortC is Output
    CMCON=%00000111 ' Disable Comparators
    CVRCON=%00000000 ' Disable Reference Module
    OPTION_REG.7=0 ' Enable Weak Pull-Up's
    Pause 2000
    ButtonPRESS var PortB.4 ' Push Button
    WORDVariable var WORD

    PromptLoop:
    Serout PortC.1,6, [$FE,1,"Press Button..."]
    While ButtonPRESS=1:Wend ' Wait here until Button Pressed
    READ 0,WORDVariable.HighByte ' Reconstruct Word variable from EEPROM
    READ 1,WORDVariable.LowByte
    Serout PortC.1,6, [$FE,1,"Reading=",#WORDVariable] ' Display Result
    While ButtonPress=0:Wend ' Wait here as long as finger is on Button
    Goto PromptLoop
    '
    End

    Thanks for the time, appreciate it very much,
    joe

  10. #10
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898

    Default

    Make sure your PIC programmer program the EEPROM location, unless.. you may read an empty EEPROM $FFFF

    Now depending of how you wrote your DATA line, this may work.. or not.

    Code:
    data  @0, WORD 9999
    should work.

    Your PICProgrammer software should allow to see the EEPROM section, just compile your code, import the .HEX and look if your data is saved in the EEPROM.
    Last edited by mister_e; - 24th November 2007 at 18:17.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  11. #11

    Question Data in Address 0 keeps overwriting...

    Thanks all, I got it to work. But I've noticed that every time I compile it the data '9999' in Address 0 restarts to 655535. How can I prevent it from overwriting. So that every time I check Address 0 the same data will show.



    Thanks all,
    joe

Similar Threads

  1. write -read problem?
    By turkuaz in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 30th August 2009, 14:06
  2. Write Onewire data toa I2C memory / read ASCI
    By Eugeniu in forum mel PIC BASIC Pro
    Replies: 67
    Last Post: - 16th November 2008, 20:19
  3. PIC16F684 + LCD to use the 256bytes of EEPROM - HELP
    By xnihilo in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 7th March 2008, 15:19
  4. Need the code to write to a memory
    By Hamlet in forum General
    Replies: 0
    Last Post: - 20th August 2007, 01:22
  5. Read / Write , To / From Internal EEPROM
    By charudatt in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 31st October 2003, 03:23

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts