PROGRAMMING PIC MICROCONTROLLERS IN C:ACCESSING THE EEPROM MEMORY

ACCESSING THE EEPROM MEMORY

clip_image001Some PIC microcontrollers (e.g. PIC16F84) have EEPROM memories which can be used to store nonvolatile data. PICC Lite provides instructions for reading and writing to this memory. The EEPROM WRITE command is used to write a data byte to the EEPROM memory. For example, the following command writes 0x2E to address 2 of the EEPROM memory:

Programming PIC Microcontrollers in C-0141clip_image002

clip_image001[1]Similarly, the EEPROM READ command is used to read a data byte from the EEPROM memory. For example, the following command reads a byte from address 5 of the EEPROM memory and loads it into the variable k:

Programming PIC Microcontrollers in C-0142clip_image003

INTERUPTS IN C PROGRAMS

Interrupts are very important in real-time programs. Most PIC microcontrollers offer internal and external interrupt facilities. Internal interrupts are usually timer-generated interrupts, while the external interrupts are triggered by activities on the I/O pins. When an interrupt occurs the processor jumps to the interrupt service routine where the interrupt is serviced. Medium-range microcontrollers (such as the PIC16F84) have only one ISR, which is at address 0x04 of the program memory. If interrupts from multiple sources are expected then the interrupting device can be found by inspecting the INTCON register bits.

clip_image004Interrupts can be used in PICC Lite programs in the form of special functions. When an interrupt occurs the program jumps to this special function where the interrupt is handled. ISR function must have the name interrupt followed by a user-selected function name, and the function must not return any value and must not have any arguments. For example, an ISR function named my int is given below:

Programming PIC Microcontrollers in C-0143

Interrupts must be enabled before they can be recognized by the microcontroller. The ei() and di() instructions enable and disable global interrupts, respectively. Additionally, the interrupt control bit of each interrupt source must be enabled in the appropriate SFR register (e.g. T0IE bit in INTCON must be set to 1 to enable timer interrupts).

The PIC microcontroller only saves the program counter on stack when an interrupt occurs. The PICC Lite compiler determines which registers and objects are used by the interrupt function and saves these appropriately.

Leave a comment

Your email address will not be published. Required fields are marked *