With this program you will learn how to write to EEPROM and
read EEPROM.
The
program accesses EEPROM location 127 (the chip has 128 EEPROM locations
- 127 is the second from the top location) and writes "4." The program
then reads the location and outputs a HIGH to the piezo.
The program then writes "0" in location 127 then reads the location and
outputs this value to put a LOW to the piezo.
The program loops and thus toggles the piezo to produce a tone.
INSTRUCTIONS FOR
USE
Load the program into a PIC12F629 and
connect a piezo between pin 5 and 0v. It will produce a tone to
show the program is working and the EEPROM is being accessed.
The operation of writing to EEPROM takes time and this creates
the low frequency for the tone. |
The
PROGRAM
This program is part of a course in PIC Programming. See left
index on Talking Electronics website: List of PIC
Projects: for the projects in this course.
The course consists of
building these projects and modifying the programs to learn the art of
programming PIC microcontrollers.
Most of the projects are very simple however they cover many different
routines and enable you to build up your knowledge.
This
program accesses EEPROM and shows the code needed to perform this
function.
If you want to modify the program you will need a programmer, a board to
hold the 8 pin chip during programming and an adapter to connect between
the programmer and PC board.
These are
covered in our article "Pick-A-PIC."
Here are the files you will need for "burning" your chip and/or
modifying the program:
EEPROM.asm
EEPROM.txt
EEPROM.hex
The following program is for viewing. It may contain
spaces or hidden characters that will not compile correctly to produce a
.hex file. Use the .hex file above to burn your chip or the .asm file to
modify the program.
;*******************************
;EEPROM Write.asm
;13-10-2010
;*******************************
;How fast is EEPROM write? Use this program to listen to the
;frequency produced with 2 EEPROM reads and writes in each cycle.
;4 is placed in EEPROM location then read and sent to piezo.
;then 0 is placed in EEPROM, then read and sent to piezo to create a tone.
;Our PIC12F629 produced a frequency of 133Hz
;
;*******************************
;
; --+------------ +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
__CONFIG _MCLRE_OFF & _CP_OFF
& _WDT_OFF & _INTRC_OSC_NOCLKOUT ;Internal osc.
;_MCLRE_OFF - master clear must be off for gp3 to be 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 pin5 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 with result in EEDATA
movf EEDATA,w ;move read data into w
bcf status,rp0
movwf temp1 ;temp1 has 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 ;55h is an unlock code
movwf eecon2 ;along with aah to prevent
movlw 0aah ;glitches writing to EEPROM.
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'
call write
call read
movf temp1,w
movwf gpio
movlw b'00000000'
call write
call read
movf temp1,w
movwf gpio
goto Main
;****************************************************************
;*EEPROM *
;****************************************************************
org 2100h
END |
|