CHAPTER 6
Examples for subsystems within microcontroller
6.1 Writing to and reading from EEPROM
6.2 Processing interrupt caused by changes on pins RB4-RB7
6.3 Processing interrupt caused by change on pin RB0
6.4 Processing interrupt caused by overflow on timer TMR0
6.5 Processing interrupt caused by overflow on TMR0 connected to external input (TOCKI)
Every microcontroller comprises a number of subsystems allowing for flexibility and wide range of applications. These include internal EEPROM memory, AD converters, serial or other form of communication, timers, interrupts, etc. Two most commonly utilized elements are interrupts and timers. One of these or several in combination can create a basis for useful and practical programs.
6.1 Writing to and reading from EEPROM
Program "eeprom.asm" uses EEPROM memory for storing certain microcontroller parameters. Transfer of data between RAM and EEPROM has two steps - calling macros eewrite and eeread. Macro eewrite writes certain variable to a given address, while eeread reads the given address of EEPROM and stores the value to a variable.
Macro eewrite writes the address to EEADR register and the variable to EEDATA register. It then calls the subprogram which executes the standard procedure for initialization of writing data (setting WREN bit in EECON1 register and writing control bytes 0x55 and 0xAA to EECON2).
For data to be actually stored in EEPROM, 10ms delay is necessary. This is achieved by using macro pausems. In case that this pause is unacceptable for any reason, problem can be solved by using an interrupt for signaling that data is written to EEPROM.
eewrite
macro addr, var
addr
Destination address. With PIC16F84, there are 68 bytes
of EEPROM for a total address range of 0x00 - 0x44.
var
Name of the variable to be stored to EPROM
eeread
macro addr, var
addr
Destination address. With PIC16F84, there are 68 bytes
of EEPROM for a total address range of 0x00 - 0x44.
var
Name of the variable into which data read from EPROM will be stored.
Example: Variable volume, which is set via buttons RA0 and RA1, will be stored to the address 0 of EEPROM. After reboot, when the program is started, it first loads the last known value of variable volume from EEPROM.
6.2 Processing interrupt caused by changes on pins RB4-RB7
Program "intportb.asm" illustrates how interrupt can be employed for indicating changes on pins RB4-RB7. Upon pushing any of the buttons, program enters the interrupt routine and determines which pin caused an interrupt. This program could be utilized in systems with battery power supply, where power consumption plays an important role. It is useful to set microcontroller to low consumption mode with a sleep instruction. Microcontroller is practically on stand-by, saving energy until the occurrence of interrupt.
Example of processing interrupt caused by changes on pins RB4-RB7
6.3 Processing interrupt caused by change on pin RB0
Example "intrb0.asm" demonstrates use of interrupt RB0/INT. Upon falling edge of the impulse coming to RB0/INT pin, program jumps to subprogram for processing interrupt. This routine then performs a certain operation, in our case it blinks the LED diode on PORTB, 7.
Example of processing interrupt caused by changes on pin RB0
6.4 Processing interrupt caused by overflow on timer TMR0
Program "inttmr0.asm" illustrates how interrupt TMR0 can be employed for generating specific periods of time. Diodes on port B are switched on and off alternately every second. Interrupt is generated every 5.088ms; in interrupt routine variable cnt is incremented to the cap of 196, thus generating approx. 1 second pause (5.088ms*196 is actually 0.99248s). Pay attention to initialization of OPTION register which enables this mode of work for timer TMR0.
Example of processing interrupt caused by overflow on timer TMR0
6.5 Processing interrupt caused by overflow on TMR0 connected to external input (TOCKI)
Counter TMR0 increments upon signal change on pin RA4/TOCKI. Prescaler is set to 4, meaning that TMR0 will be incremented on every fourth impulse. Pay attention to initialization of OPTION register which enables this mode of work for timer TMR0 (this mode is common for devices such as counters).
Example of processing interrupt caused by overflow on timer TMR0 connected to TOCKI