PIC18F14K50: Memoria de Datos E2PROM (Data E2PROM)

Page 1

Omar Gurrola

04/19/13

http://www.proprojects.wordpress.com

PIC18F14K50: Memoria de Datos E2PROM (Data E2PROM) El PIC18F14K50 cuenta con 256 Bytes (00h-FFh) de memoria E2PROM. Esta memoria se utiliza para almacenan datos del usuario, es una memoria no volátil, lo que permite que los datos se mantengan a pesar de apagar el uC. Esta memoria se trabaja como un periférico más del uC, por lo tanto se debe acceder a ella de forma indirecta a través de varios registros de funciones especiales para su control:    

EEADR: En este registro se pone la dirección que deseamos acceder para leer o escribir. EEDATA: En este registro se ponen los datos que deseemos escribir o los datos leídos. EECON1: Controla el acceso a la memoria de programa y E2PROM. EECON2: No es un registro físico y se utiliza únicamente cuando se escribe a la memoria.

Si deseas escribir datos preestablecidos durante la programación del uC se pueden añadir esos datos en el código fuente utilizando la dirección virtual 0xF00000 que corresponde a la memoria E2PROM de la siguiente manera: // This data will be writen in to the E2PROM #pragma romdata eedata_scn=0xF00000 rom s8 eedata_string[] = "Hello World =)"; rom u8 eedata_values[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; #pragma romdata

No se pueden leer los datos utilizando los arreglos creados, se deben utilizar los registros de funciones especiales para realizar esta tarea. Para escribir un byte en la E2PROM seguir estos pasos: 1. 2. 3. 4. 5.

EEADR = Dirección a escribir (00h-FFh). EECON1<7>EEPGD = 0 (Acceso a memoria de datos). EECON1<6>CFGS = 0 (Acceder a E2PROM). EECON1<0>RD = 1 (Inicia lectura). EEDATA (Dato leído).


Omar Gurrola

04/19/13

http://www.proprojects.wordpress.com

Para escribir un byte seguir estos pasos: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.

EEADR = Direcci贸n a escribir (00h-FFh). EEDATA = Valor a escribir. EECON1<7>EEPGD = 0 (Acceso a memoria de datos). EECON1<6>CFGS = 0 (Acceder a E2PROM). EECON1<2>WREN = 1 (Habilitar escritura). INTCON<7>GIE = 0 (Deshabilitar interrupciones). EECON2 = 55h (Obligatorio) EECON2 = AAh (Obligatorio) EECON1<1>WR = 1 (Iniciar escritura). INTCON<7>GIE = 1 (Habilitar interrupciones). Esperar a que EECON1<1>WR == 0 (Finalizo la escritura). EECON1<2>WREN = 0 (Deshabilitar escritura).

1.1.Ejemplo Sencillo Sin Utilizar Interrupciones En este ejemplo vamos a escribir dos cadenas de caracteres a la memoria E2PROM, las leemos y las mostramos en el display. main.c /* * * * * * * * * * * * * * * * * * * * * * */

Copyright (c) 2011-2013, http://www.proprojects.wordpress.com All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1.- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2.- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

/********************************************************************************** * Author: Omar Gurrola * Site: http://www.proprojects.wordpress.com * Processor: PIC18 * Compiler: C18 v3.45 * File Name: main.c * Description: Main program * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Rev. Date Comment * 1.0 04/15/13 Initial version *********************************************************************************/ /** INCLUDES *******************************************************/ #include <p18f14k50.h> #include "pic18f14k50_cbits.h" #include "pic18f14k50_io.h" #include "pic18f14k50_int.h" #include "pic18f14k50_e2prom.h" #include "stdvars.h" #include "wait.h" #include "HD44780-STP.h" /** DECLARATIONS ***************************************************/ /** VARIABLES ******************************************************/


Omar Gurrola

04/19/13

http://www.proprojects.wordpress.com

/** PROTOTYPES *****************************************************/ void HighPriorityISR(void); void LowPriorityISR(void); /** Interrupt Service Routines (ISR)********************************/ #pragma code HP_ISR=0x0008 void HighInterruptVector(void){ _asm goto HighPriorityISR _endasm } #pragma code LP_ISR=0x0018 void LowInterruptVector(void){ _asm goto LowPriorityISR _endasm } #pragma code // Forces the code below this line to be put into the code section #pragma interrupt HighPriorityISR void HighPriorityISR(void){ //Check which interrupt flag caused the interrupt. //Service the interrupt //Clear the interrupt flag //Etc. } //This return will be a "retfie fast", since this is in a #pragma interrupt section #pragma interruptlow LowPriorityISR void LowPriorityISR(void){ //Check which interrupt flag caused the interrupt. //Service the interrupt //Clear the interrupt flag //Etc. } //This return will be a "retfie", since this is in a #pragma interruptlow section void main(void){ s8 StringDemo[] = "Hello World!!"; s8 StringDemo2[] = "Text from EEPROM"; s8 String[21]; SetIntClockTo32MHz(); lcd_initialize(); lcd_goto(1,1); lcd_write_pgm(" Pro Projects lcd_goto(2,1); lcd_write_pgm(" E2PROM Testing

"); ");

E2PROMWriteString(0,&StringDemo[0]); E2PROMWriteString(14,&StringDemo2[0]);

// Write String // Write String

E2PROMReadString(0,&String[0]); lcd_goto(3,5); lcd_write(&String[0]); E2PROMReadString(14,&String[0]); lcd_goto(4,3); lcd_write(&String[0]);

// Read String

while(true){ ; } // end while } // end main()

// Show on LCD // Read String // Show on LCD


Omar Gurrola Diagrama esquemรกtico:

Circuito armado:

04/19/13

http://www.proprojects.wordpress.com


Omar Gurrola

04/19/13

http://www.proprojects.wordpress.com

1.2.Ejemplo Sencillo Utilizando Interrupciones En este ejemplo utilizaremos interrupciones, lo único que aremos en la ISR será mostrar cada carácter que es escrito en la E2PROM. main.c /* * * * * * * * * * * * * * * * * * * * * * */

Copyright (c) 2011-2013, http://www.proprojects.wordpress.com All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1.- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2.- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

/********************************************************************************** * Author: Omar Gurrola * Site: http://www.proprojects.wordpress.com * Processor: PIC18 * Compiler: C18 v3.45 * File Name: main.c * Description: Main program * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Rev. Date Comment * 1.0 04/15/13 Initial version *********************************************************************************/ /** INCLUDES *******************************************************/ #include <p18f14k50.h> #include "pic18f14k50_cbits.h" #include "pic18f14k50_io.h" #include "pic18f14k50_int.h" #include "pic18f14k50_e2prom.h" #include "stdvars.h" #include "wait.h" #include "HD44780-STP.h" /** DECLARATIONS ***************************************************/ /** VARIABLES ******************************************************/ u8 Address; /** PROTOTYPES *****************************************************/ void HighPriorityISR(void); void LowPriorityISR(void); /** Interrupt Service Routines (ISR)********************************/ #pragma code HP_ISR=0x0008 void HighInterruptVector(void){ _asm goto HighPriorityISR _endasm } #pragma code LP_ISR=0x0018 void LowInterruptVector(void){ _asm goto LowPriorityISR _endasm } #pragma code // Forces the code below this line to be put into the code section #pragma interrupt HighPriorityISR void HighPriorityISR(void){ //Check which interrupt flag caused the interrupt. //Service the interrupt


Omar Gurrola

04/19/13

http://www.proprojects.wordpress.com

//Clear the interrupt flag //Etc. s8 c[2]; c[1] = '\0'; if(EEWFlag == 1){ c[0] = E2PROMReadChar(Address); lcd_write(&c[0]); EEWFlagClear(); } } //This return will be a "retfie fast", since this is in a #pragma interrupt section #pragma interruptlow LowPriorityISR void LowPriorityISR(void){ //Check which interrupt flag caused the interrupt. //Service the interrupt //Clear the interrupt flag //Etc. } //This return will be a "retfie", since this is in a #pragma interruptlow section void main(void){ s8 String[] = " Hello World!! "; s8 String2[] = " Using Interrupts "; u8 chars; SetIntClockTo32MHz(); // EE Interruption Configuration EEWEnable(); // Enable Write interrupt EEWFlagClear(); // Clear flag INTPDisable(); // Disable priority PEIEnable(); // Enable peripheral interrupt GIEnable(); // Enable global interrupt lcd_initialize(); lcd_goto(1,1); lcd_write_pgm(" Pro Projects lcd_goto(2,1); lcd_write_pgm(" E2PROM Testing 2

"); ");

Address = 0; lcd_goto(3,1); chars = 0; do{ E2PROMWriteChar(Address,String[chars]); while(EEWriteFlag == 1); Address++; chars++; }while(String[chars-1] != '\0'); lcd_goto(4,1); chars = 0; do{ E2PROMWriteChar(Address,String2[chars]); while(EEWriteFlag == 1); Address++; chars++; }while(String2[chars-1] != '\0'); while(true){ ; } // end while } // end main()


Omar Gurrola Diagrama esquemรกtico:

Circuito armado:

04/19/13

http://www.proprojects.wordpress.com


Omar Gurrola

04/19/13

Referencias 

Microchip, “PIC18F/LF1XK50 Data Sheet”, 2010, http://ww1.microchip.com/downloads/en/DeviceDoc/41350E.pdf

http://www.proprojects.wordpress.com


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.