Menu

all about electronic and microcontrollers

Tuesday, December 14, 2010

Lesson nr.19-Using Internal EEPROM Memory

Hardware setup:



Circuit Diagram:
For those who want to build it on their own breadboard or other platform, here is the electronic scheme built in Eagle Cad, free version:


Software:
Here is the C program written for MikroC PRO for PIC 2010 (version v4.15).
/*
'*******************************************************************************
'  Lesson nr.19:
'          Using Internal EEPROM Memory.
'  Done by:
'          Aureliu Raducu Macovei, 2010.
'  Description:
'          In this experiment we will work with internal EEPROM Memory
'          and I'll show how to write and read information on it helped by a little
'          menu displayed on the LCD.
'          Communication with LCD will be performed through 4-bits and connections
'          is made as follows: D4 with RB0, D5 with RB1, D6 with RB2, D7 with RB3;
'                              RS with RB4 and EN with RB5.
'          The Push buttons are connected to PORT RA4 (for read), RA6(for write) and RA7
'          (for delete). Of course we need pull-up resistors (4k7 is required).
'  Test configuration:
'    MCU:                        PIC16F628A
'    Test.Board:                 WB-106 Breadboard 2420 dots
'    SW:                         MikroC PRO for PIC 2010 (version v4.15)
'  Configuration Word
'    Oscillator:                 INTOSC:I/O on RA.6, I/O on RA.7
'    Watchdog Timer:             OFF
'    Power up Timer:             Disabled
'    Master Clear Enable:        Enabled
'    Browun Out Detect:          Enabled
'    Low Voltage Program:        Disabled
'    Data EE Read Protect:       Disabled
'    Code Protect:               OFF
'*******************************************************************************
*/
// LCD module connections
sbit LCD_RS at RB4_bit;                 // LCD_RS assigned to PORT RB4;
sbit LCD_EN at RB5_bit;                 // LCD_EN assigned to PORT RB5;
sbit LCD_D4 at RB0_bit;                 // LCD_D4 assigned to PORT RB0;
sbit LCD_D5 at RB1_bit;                 // LCD_D5 assigned to PORT RB1;
sbit LCD_D6 at RB2_bit;                 // LCD_D6 assigned to PORT RB2;
sbit LCD_D7 at RB3_bit;                 // LCD_D7 assigned to PORT RB3;

sbit LCD_RS_Direction at TRISB4_bit;    // LCD_RS assigned to TRIS B4;
sbit LCD_EN_Direction at TRISB5_bit;    // LCD_EN assigned to TRIS B5;
sbit LCD_D4_Direction at TRISB0_bit;    // LCD_D4 assigned to TRIS B0;
sbit LCD_D5_Direction at TRISB1_bit;    // LCD_D5 assigned to TRIS B1;
sbit LCD_D6_Direction at TRISB2_bit;    // LCD_D6 assigned to TRIS B2;
sbit LCD_D7_Direction at TRISB3_bit;    // LCD_D7 assigned to TRIS B3;
// End LCD module connections

// Define Messages
char message1[] = "Switch1: Read";
char message2[] = "Switch2: Write";
char message3[] = "Switch3: Erase";
char message4[] = "EEPROM_Write";
char message5[] = "EEPROM_Read ";
char message6[] = "EEPROM ERASED";
char message7[] = "Internal";
char message8[] = "EEPROM";
char message9[] = "*";
char message10[] = "**";
char message11[] = "***";

char digit[] = "00000000000000";
unsigned short  ii, All ;

void read_eeprom()
{
 if (Button(&PORTA, 4, 1, 0))           // Detect logical one to zero;
 {    
  Lcd_Cmd(_LCD_CLEAR);
  Lcd_Out(1,3,message5);
  Lcd_Out(2,7,message9);
  delay_ms(1000);                       // Delay 1s
  Lcd_Out(2,7,message10);
  delay_ms(1000);                       // Delay 1s
  Lcd_Out(2,7,message11);
  delay_ms(1000);                       // Delay 1s
  for (ii=0; ii<=13; ii++)
  {
   All = EEPROM_Read(0x00+ii);          // Start EEPROM Location,
   digit[ii] = All+65;                  // Look at ascii table
   }
  Lcd_Out(2,2,digit);
  delay_ms(5000);                       // Delay 5s
  }
 }

void write_eeprom()
{
 if (Button(&PORTA, 6, 1, 0))           // Detect logical one to zero
 {
  for (ii=0; ii<14; ii++)
  EEPROM_Write(0x00+ii,ii);
  Lcd_Cmd(_LCD_CLEAR);
  Lcd_Out(1,3,message4);
  Lcd_Out(2,7,message9);
  delay_ms(1000);
  Lcd_Out(2,7,message10);
  delay_ms(1000);
  Lcd_Out(2,7,message11);
  delay_ms(1000);
  }
 }

void delete_eeprom()
{
 if (Button(&PORTA, 7, 1, 0))           // Detect logical one to zero
 {     
  Lcd_Cmd(_LCD_CLEAR);
  Lcd_Out(1,3,message6);
  Lcd_Out(2,7,message9);
  delay_ms(1000);
  Lcd_Out(2,7,message10);
  delay_ms(1000);
  Lcd_Out(2,7,message11);
  delay_ms(1000);
  for (ii=0; ii<=13; ii++)
  EEPROM_Write(0x00+ii,0x0e);
  }
 }

void main() {
CMCON  |= 7;                            // Disable Comparators
TRISA4_bit = 1;
TRISA6_bit = 1;
TRISA7_bit = 1;
TRISB = 0;
PORTB = 0;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);                    // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF);               // Cursor off
Lcd_Out(1,5,message7);
Lcd_Out(2,6,message8);
delay_ms(3000);                         // Delay 3s
do {
    Lcd_cmd(_LCD_Clear);
    Lcd_Out(1,2,message1);              // Write message1 in 1st row
    delay_ms(1000);
    read_eeprom();
    write_eeprom();
    delete_eeprom();
    Lcd_cmd(_LCD_Clear);
    Lcd_Out(1,2,message2);
    delay_ms(1000);
    read_eeprom();
    write_eeprom();
    delete_eeprom();
    Lcd_cmd(_LCD_Clear);
    Lcd_Out(1,2,message3);
    delay_ms(1000);
    read_eeprom();
    write_eeprom();
    delete_eeprom();
    } while(1);
}

7 comments:

  1. Excellent! Good job.
    I have question for u.
    How to generate alphabetical by using button to select words "SUPRA"
    For example..by pressing button to find "S" then next column by pressing button to find "U" and so on.

    ReplyDelete
  2. Thanks for the feedback.
    I understand perfectly what you mean and for that we need to do some more significant changes in the program. I'll do some tests and after that, I will announce you about results. All these may take a while.

    ReplyDelete
  3. what if i wanted to used external either 4, 8,10, or 20Mhz? 4Mhz seemed too slowly. I didn't tried internal osc. I hated to rewired again. As fact as u understood, using same circuit and same buttons setup.
    The eeprom tutorial is good to learned from you.

    Many thanks again.

    ReplyDelete
  4. I will have to used 4MHZ all times. Because of lcd

    ReplyDelete
  5. What command for moving to next column by pressing button?

    ReplyDelete
  6. Hello Supra,
    Sorry for my delay.
    Thank you for your appreciations. The commands to moving your text, from left to the right or vice versa are:
    Lcd_Cmd(_LCD_SHIFT_RIGHT);
    Lcd_Cmd(_LCD_SHIFT_LEFT);
    And a little example is:
    char i; // Loop variable

    void Move_Delay() { // Function used for text moving
    Delay_ms(500); // You can change the moving speed here
    }

    void main(){
    ANSEL = 0; // Configure AN pins as digital I/O
    ANSELH = 0;
    C1ON_bit = 0; // Disable comparators
    C2ON_bit = 0;

    Lcd_Init(); // Initialize LCD

    Lcd_Cmd(_LCD_CLEAR); // Clear display
    Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
    Lcd_Out(1,6,txt3); // Write text in first row

    Lcd_Out(2,6,txt4); // Write text in second row
    Delay_ms(2000);
    Lcd_Cmd(_LCD_CLEAR); // Clear display

    Lcd_Out(1,1,txt1); // Write text in first row
    Lcd_Out(2,5,txt2); // Write text in second row

    Delay_ms(2000);

    // Moving text
    for(i=0; i<4; i++) { // Move text to the right 4 times
    Lcd_Cmd(_LCD_SHIFT_RIGHT);
    Move_Delay();
    }

    while(1) { // Endless loop
    for(i=0; i<8; i++) { // Move text to the left 7 times
    Lcd_Cmd(_LCD_SHIFT_LEFT);
    Move_Delay();
    }

    for(i=0; i<8; i++) { // Move text to the right 7 times
    Lcd_Cmd(_LCD_SHIFT_RIGHT);
    Move_Delay();
    }
    }
    }

    ReplyDelete
  7. Hello. First of all thanks for such beautiful article. I want to use EEPROM to store and retrieve the Password string. I tried using the simple loop structure to EEPROM_Read() with increasing address and character location. The compilation is ok but when I ran it, it does not map the password on EEPROM. I check it in EEPROM Editor while I ran my program. Is it right or wrong? What is the correct and most elegant way to store and retrieve string from EEPROM?

    ReplyDelete

If you do not understand something, or if you make some aplication helped by this blog, let me know.