EEPROM interfacing with pic microcontroller using I2C protocol

In: Project Code: ES000005
Advertisements
Share

In this project we will control led delay using timer interrupt implementation with an interrupt service routine(ISR) with PIC16F877A 8-bit microcontroller.If you are new to PIC microcontroller programming we suggest you to execute the basic led blinking project before trying this project. If you don't have access to your own hardware for this project, you can and execute it remotely.

course thumb

Project Content


Required hardware and software


The required hardware and software for this project is as below:

  • A breadboard
  • 1 x 4mmLED
  • 1 x resistor between 220 Ohm to 1 kOhm
  • Hookup cables
  • Development board hosting PIC16F877A 8-bit microcontroller
  • MPLAB IDE(v5.05) from Microchip
  • PICKIT3 Programmer, connecting cable from PICKIT3 to your computer.
  • Power supply for the development board

  • Schematic diagram


    The schematic diagram for this project is as shown below:

    Required Components

    PIC16f877A Timer description


    Timer hardware is a crucial component of most embedded systems. In some cases, a timer measures elapsed time (counting processor clock ticks). In others, we want to count or time external events. An 8-bit timer can only count up to 256 (8-bit timer) where as an 16-bit counter can count up to 65535 (16-bit timer).

    In this project we will use timer0 to create a delay and use the delay to control the led blinking frequency.

    Prepare source code, building program source code, creating binary


    Prepare source code
    
    /*
     * File:   dac.c
     * IDE: MPLAB X 5.05
     * Programmer: PICKIT3
     * Compiler: XC8
     */
    #include 
    #pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
    #pragma config WDTE = OFF        // Watchdog Timer Enable bit (WDT enabled)
    #pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
    #pragma config CP = OFF         // FLASH Program Memory Code Protection bits (Code protection off)
    #pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
    #pragma config LVP = OFF        // Low Voltage In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
    #pragma config CPD = OFF        // Data EE Memory Code Protection (Code Protection off)
    #pragma config WRT = OFF        // FLASH Program Memory Write Enable (Unprotected program memory may not be written to by EECON control)
    void msDelay(unsigned int );
    
    #define CS PORTEbits.RE0  //CS PIN
    #define SCK PORTEbits.RE1 //SCK PIN
    #define SDI PORTEbits.RE2 //SDI PIN
    
    void SpiInit();
    void SpiSequencer(unsigned int );
    void main()
    {
        SpiInit();
        unsigned int i = 0,j = 0;
        for(j = 0;j < 50;j++)
        {
            if(i > 4095)
            {
                i = 0;
            }
            SpiSequencer(i & 0xfff);
            i += 200;
        }
        
        while(1);
    }
    
    void msDelay(unsigned int x)
    {
        unsigned int i;
        unsigned char j;
        for(i=0; i < x;i++)
            for(j = 0; j < 165;j++);
    
    }
    
    void SpiSequencer(unsigned int val)
    {
        unsigned int mask = 0x8000;
        unsigned int i;
        SCK = 0;
        CS = 1;
        msDelay(1);
        CS = 0;
        val |= (0x03 << 12);
        for(i = 0;i < 16;i++)
        {
            if(val & mask)
            {
                SDI = 1;
            }else
            {
                SDI = 0;
            }
            msDelay(1);
            SCK = 1;
            msDelay(1);
            SCK = 0;
            msDelay(1);
            mask = mask >> 1;
        }
        CS = 1;
    }
    
    void SpiInit()
    {
        TRISE0 = 0;
        TRISE1 = 0;
        TRISE2 = 0;
    }
    

    Let me explain about the above source code developed for this project. The oscillator is used as a timing source to count the number of clock cycles. We are using OPTION_REG register from PIC16F877A microcontroller which contains various control bits to configure the TMR0. The various control bits of OPTION_REG register is shown below.

    Required Components

    Follow the procedure as followed in to load the .hex file in the microcontroller


    Looking for a new project? Write and upload your project information .
    Share

    Articles


    C Programming

    More Articles Categories

    Technical articles

    Prepare for software jobs


    Test your skills