Examples for subsystems within microcontroller Introduction 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 R B0 6.4 Processing interrupt caused by overflow on timer TMR0 6.5 Processing interrupt caused by overflow on TMR0 connected to external input (TOCKI) Introduction Every microcontroller comprises a number of subsystems allowing for flexibility and wide range ofapplications. These include internal EEPROM memory, AD converters, serial or other form ofcommunication, 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 eereadreads the given address ofEEPROM 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).
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
832019 Examples for Subsystems Within Micro Controller
62 Processing interrupt caused by changes on pins RB4-RB763 Processing interrupt caused by change on pin RB0
64 Processing interrupt caused by overflow on timer TMR065 Processing interrupt caused by overflow on TMR0 connected to external input (TOCKI)
Introduction
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 andtimers One of these or several in combination can create a basis for useful and practical programs
61 Writing to and reading from EEPROM
Program eepromasm 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 thencalls 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)
832019 Examples for Subsystems Within Micro Controller
For data to be actually stored in EEPROM 10ms delay is necessary This is achieved by usingmacro 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 variablevolume from EEPROM
832019 Examples for Subsystems Within Micro Controller
62 Processing interrupt caused by changes on pins RB4-RB7
Program intportbasm illustrates how interrupt can be employed for indicating changes on pinsRB4-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 powersupply 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 savingenergy until the occurrence of interrupt
832019 Examples for Subsystems Within Micro Controller
63 Processing interrupt caused by change on pin RB0
Example intrb0asm demonstrates use of interrupt RB0INT Upon falling edge of the impulsecoming to RB0INT 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
832019 Examples for Subsystems Within Micro Controller
generating approx 1 second pause (5088ms196 is actually 099248s) Pay attention toinitialization of OPTION register which enables this mode of work for timer TMR0
Example of processing interrupt caused by overflow on timer TMR0
832019 Examples for Subsystems Within Micro Controller
65 Processing interrupt caused by overflow on TMR0 connected
to external input (TOCKI)
Counter TMR0 increments upon signal change on pin RA4TOCKI Prescaler is set to 4 meaning
that TMR0 will be incremented on every fourth impulse Pay attention to initialization of OPTIONregister which enables this mode of work for timer TMR0 (this mode is common for devices such ascounters)
Example of processing interrupt caused by overflow on timer TMR0 connected to TOCKI
832019 Examples for Subsystems Within Micro Controller
For data to be actually stored in EEPROM 10ms delay is necessary This is achieved by usingmacro 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 variablevolume from EEPROM
832019 Examples for Subsystems Within Micro Controller
62 Processing interrupt caused by changes on pins RB4-RB7
Program intportbasm illustrates how interrupt can be employed for indicating changes on pinsRB4-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 powersupply 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 savingenergy until the occurrence of interrupt
832019 Examples for Subsystems Within Micro Controller
63 Processing interrupt caused by change on pin RB0
Example intrb0asm demonstrates use of interrupt RB0INT Upon falling edge of the impulsecoming to RB0INT 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
832019 Examples for Subsystems Within Micro Controller
generating approx 1 second pause (5088ms196 is actually 099248s) Pay attention toinitialization of OPTION register which enables this mode of work for timer TMR0
Example of processing interrupt caused by overflow on timer TMR0
832019 Examples for Subsystems Within Micro Controller
65 Processing interrupt caused by overflow on TMR0 connected
to external input (TOCKI)
Counter TMR0 increments upon signal change on pin RA4TOCKI Prescaler is set to 4 meaning
that TMR0 will be incremented on every fourth impulse Pay attention to initialization of OPTIONregister which enables this mode of work for timer TMR0 (this mode is common for devices such ascounters)
Example of processing interrupt caused by overflow on timer TMR0 connected to TOCKI
832019 Examples for Subsystems Within Micro Controller
62 Processing interrupt caused by changes on pins RB4-RB7
Program intportbasm illustrates how interrupt can be employed for indicating changes on pinsRB4-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 powersupply 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 savingenergy until the occurrence of interrupt
832019 Examples for Subsystems Within Micro Controller
63 Processing interrupt caused by change on pin RB0
Example intrb0asm demonstrates use of interrupt RB0INT Upon falling edge of the impulsecoming to RB0INT 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
832019 Examples for Subsystems Within Micro Controller
generating approx 1 second pause (5088ms196 is actually 099248s) Pay attention toinitialization of OPTION register which enables this mode of work for timer TMR0
Example of processing interrupt caused by overflow on timer TMR0
832019 Examples for Subsystems Within Micro Controller
65 Processing interrupt caused by overflow on TMR0 connected
to external input (TOCKI)
Counter TMR0 increments upon signal change on pin RA4TOCKI Prescaler is set to 4 meaning
that TMR0 will be incremented on every fourth impulse Pay attention to initialization of OPTIONregister which enables this mode of work for timer TMR0 (this mode is common for devices such ascounters)
Example of processing interrupt caused by overflow on timer TMR0 connected to TOCKI
832019 Examples for Subsystems Within Micro Controller
63 Processing interrupt caused by change on pin RB0
Example intrb0asm demonstrates use of interrupt RB0INT Upon falling edge of the impulsecoming to RB0INT 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
832019 Examples for Subsystems Within Micro Controller
generating approx 1 second pause (5088ms196 is actually 099248s) Pay attention toinitialization of OPTION register which enables this mode of work for timer TMR0
Example of processing interrupt caused by overflow on timer TMR0
832019 Examples for Subsystems Within Micro Controller
65 Processing interrupt caused by overflow on TMR0 connected
to external input (TOCKI)
Counter TMR0 increments upon signal change on pin RA4TOCKI Prescaler is set to 4 meaning
that TMR0 will be incremented on every fourth impulse Pay attention to initialization of OPTIONregister which enables this mode of work for timer TMR0 (this mode is common for devices such ascounters)
Example of processing interrupt caused by overflow on timer TMR0 connected to TOCKI
832019 Examples for Subsystems Within Micro Controller
63 Processing interrupt caused by change on pin RB0
Example intrb0asm demonstrates use of interrupt RB0INT Upon falling edge of the impulsecoming to RB0INT 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
832019 Examples for Subsystems Within Micro Controller
generating approx 1 second pause (5088ms196 is actually 099248s) Pay attention toinitialization of OPTION register which enables this mode of work for timer TMR0
Example of processing interrupt caused by overflow on timer TMR0
832019 Examples for Subsystems Within Micro Controller
65 Processing interrupt caused by overflow on TMR0 connected
to external input (TOCKI)
Counter TMR0 increments upon signal change on pin RA4TOCKI Prescaler is set to 4 meaning
that TMR0 will be incremented on every fourth impulse Pay attention to initialization of OPTIONregister which enables this mode of work for timer TMR0 (this mode is common for devices such ascounters)
Example of processing interrupt caused by overflow on timer TMR0 connected to TOCKI
832019 Examples for Subsystems Within Micro Controller
generating approx 1 second pause (5088ms196 is actually 099248s) Pay attention toinitialization of OPTION register which enables this mode of work for timer TMR0
Example of processing interrupt caused by overflow on timer TMR0
832019 Examples for Subsystems Within Micro Controller
65 Processing interrupt caused by overflow on TMR0 connected
to external input (TOCKI)
Counter TMR0 increments upon signal change on pin RA4TOCKI Prescaler is set to 4 meaning
that TMR0 will be incremented on every fourth impulse Pay attention to initialization of OPTIONregister which enables this mode of work for timer TMR0 (this mode is common for devices such ascounters)
Example of processing interrupt caused by overflow on timer TMR0 connected to TOCKI
832019 Examples for Subsystems Within Micro Controller
generating approx 1 second pause (5088ms196 is actually 099248s) Pay attention toinitialization of OPTION register which enables this mode of work for timer TMR0
Example of processing interrupt caused by overflow on timer TMR0
832019 Examples for Subsystems Within Micro Controller
65 Processing interrupt caused by overflow on TMR0 connected
to external input (TOCKI)
Counter TMR0 increments upon signal change on pin RA4TOCKI Prescaler is set to 4 meaning
that TMR0 will be incremented on every fourth impulse Pay attention to initialization of OPTIONregister which enables this mode of work for timer TMR0 (this mode is common for devices such ascounters)
Example of processing interrupt caused by overflow on timer TMR0 connected to TOCKI
832019 Examples for Subsystems Within Micro Controller
65 Processing interrupt caused by overflow on TMR0 connected
to external input (TOCKI)
Counter TMR0 increments upon signal change on pin RA4TOCKI Prescaler is set to 4 meaning
that TMR0 will be incremented on every fourth impulse Pay attention to initialization of OPTIONregister which enables this mode of work for timer TMR0 (this mode is common for devices such ascounters)
Example of processing interrupt caused by overflow on timer TMR0 connected to TOCKI
832019 Examples for Subsystems Within Micro Controller
65 Processing interrupt caused by overflow on TMR0 connected
to external input (TOCKI)
Counter TMR0 increments upon signal change on pin RA4TOCKI Prescaler is set to 4 meaning
that TMR0 will be incremented on every fourth impulse Pay attention to initialization of OPTIONregister which enables this mode of work for timer TMR0 (this mode is common for devices such ascounters)
Example of processing interrupt caused by overflow on timer TMR0 connected to TOCKI
832019 Examples for Subsystems Within Micro Controller