Joon, age 51, is an unmarried, cash basis, calendar year taxpayer. Joon has one 9-year-old qualifying child who lives with him full-time and is claimed on Form 1040. Joon has the following income and deductions for 2016:

RECEIPTS FOR YEAR/Deductions
Salaries and wages from local school district $78,000
Interest from Friendly Bank 1,235
Dividends from Tandem Diabetic Corporation (a publicly traded
U.S. corporation) 36
Allowable itemized deductions 6,970

RENTAL OPERATIONS
Rental income 14,400
Interest expense 7,800
Property taxes 1,450
Insurance 1,260
Depreciation?Building 2,895
Depreciation?Appliances 941

SALE OF RENTAL RESIDENCE
Sales price (October 26, 2016) 210,600
Cost (October 18, 2010)?$40,000 assigned to land 119,600
Depreciation allowed (Straight line MACRS) 17,367
Selling commission 7,440
Purchase costs (recording, other legal) 2,760

SALE OF APPLIANCES WITH RESIDENCE
Sales price (October 26, 2016) 6,600
Cost (September 25, 2014) 9,800
Depreciation allowed (Straight line MACRS) 6,037

Prepare the following forms for Joon for 2016:

Form 1040
Schedule D (Required)?Worksheet Not Required?You may calculate the
tax without completing the worksheet. See October 26 Solutions for an
example.
Schedule E
Form 4562
Form 4797

Provide any information that is not stated.

Accountancy 503?Fall 2016 Tax Return 3b?5 points; Due December 7

Brianna, age 29, is an unmarried, cash basis, calendar year taxpayer. Aileen has no dependents. Aileen has the following income and deductions for 2016:

RECEIPTS FOR YEAR/Deductions
Salaries and wages from local school district $48,000
Interest from Friendly Bank 1,235
Dividends from Tandem Diabetic Corporation (a publicly traded
U.S. corporation) 36
Allowable itemized deductions 6,940

BUSINESS OPERATIONS?PROFESSIONAL SERVICES
Fee income collected 34,400
Interest expense 4,800
Office supplies 1,450
Insurance 1,260
Rental expense 12,895
Transportation 2,300
Continuing Education 975
Purchase of computer and peripherals 1,880

Prepare the following forms for Aileen for 2016:

Schedule C
Schedule SE

Provide any information that is not stated.

1.

Choose a company about 15 pager Suggested Project Outline 1. Executive Summary 2. Firm, Industry, and Environment (not to exceed 2 pages) 1. Summary description the firm including a brief description of the key product or service, the relevant market, and analysis of demand including the principle factors that influence demand, and the implications for revenue. 2. Brief description of the principal inputs, production processes, indicating any significant changes in technology and analysis of trends in productivity. 3. Summary of the behavior of costs, short-run and long run, indicating the principal factors that influence costs and their impacts as indicated by cost trends. 4. Summary of the competitive environment, analysis of the key measures through which firms compete (pricing, quality, brand, innovation, etc.) and discussion of potential changes in the level of competition. 3. Company Performance and Managerial Effectiveness 1. Through-time (trend) analysis of the firm?s performance (using the Dupont framework as an organizing template for your discussion) 2. Cross-sectional analysis and comparison of the firm with benchmarks: industry competitor and industry benchmarks 3. Discuss the effect of the firm?s accounting choices on the financial statements 4. Future Prospects: 1. Integrative assessment of current company performance, focusing on principal sources of competitive advantage, investment potential, and credit-worthiness. 2. Integrative overall outlook for future performance 5. Appendix 6. Footnotes 7. Bibliography1.

2.

HRM 300: Equal Employment Opportunity and Employee Rights Review.

For each selected law or issue, locate a present-day court case that has challenged your selected law or issue A): Equal Employment Opportunity Act of 1972, and Family and Medical Leave Act of 1993. B): Whistle-Blowing. There are a total of three issues to address in the paper Please be original, include APA format, and cite references.

3.
coding using MPLAB

4 Lab 4 ? Timer0 module

5,This laboratory practical will introduce you to the Timer0 module which is one of three timer/counter
peripherals located on the PIC16F877A MCU. Timer/counter modules allow the creation of precise
and deterministic timing intervals which is essential in many applications e.g. frequency
generation/measurement, timekeeping, control systems, communications.??This laboratory practical
will focus on the proper configuration and usage of the Timer0 module in timer mode only.
4.2 Learning Outcomes
? Understand the structure of the Timer0 module on the PIC16F877A
? Be able to configure the Timer0 module using the appropriate special function registers
? Be able to develop projects to utilise the Timer0 module in a range of applications.
4.3 Background
4.3.1 Counter/Timers
The only practical difference between a counter and a timer is the nature of the source. A counter
simply increments a stored ?count? value when a logic state change (either a rising or falling edge) is
received at its input. To operate a counter as a timer, a synchronous pulse train (clock) of a known
frequency is applied to the input. Figure 4-1 shows a generic representation of a counter configured
as a timer. The current count value can be read from the counter register, and the register can also be
written to or ?preloaded?.
Figure 4-1 Generic representation of a hardware timer module
If the frequency of the synchronous pulse train, f is known then the elapsed time, T may be
determined from number of counts, N recorded by the timer as follows,
N T
f ? ??
4.3.2 PIC16F877A Timer0 module
The Timer0 module on the PIC16F877A MCU is an 8 bit pre-scalable timer/counter. In timer mode, the
TMR0 special function register (SFR) is automatically incremented on every instruction clock cycle. In
counter mode, the TMR0 SFR is incremented either on rising or on falling edge (configuration
dependant) of the external pin RA4/T0CKL. In this laboratory practical we will only be considering the
use of Timer0 in timer mode.
33
The 8 bit Timer0 module on board the PIC16F877A can store 8 2 256 ? independent values from 0-
255. The current count value is stored in the TMR0 SFR may be read at any time without affecting the
operation of the Timer0 module. It may also written to (preloaded) by writing the appropriate value
to the TMR0 SFR.
When the Timer0 module count value reaches its maximum value (255), the counter overflows back
to zero and continues counting. This timer overflow results in the setting of a bit (TMR0IF) in the
INTCON SFR. Figure 4-2 show the structure of the INTCON SFR which we will be looking at in more
detail in future laboratory practicals. For the moment just remember that when a Timer0 overflow
occurs then the TMR0IF bit is SET.
Figure 4-2 The INTCON register
A single bit which signifies an occurrence of an event, in this case a Timer0 overflow, is called a flag.
We can detect when the overflow Timer0 has occurred by regularly checking the status of the TMR0IF
bit (flag) in a process known as polling. By using polling in conjunction with loopsit is possible to create
accurate delays.
Timer0 receives its synchronous pulse train (clock) at the instruction cycle rate of the PIC16F877A
MCU, which is one quarter of the frequency of the quartz crystal oscillator used to clock the MCU. For
the 3.2768 MHz crystal oscillators used on the E-block development systems, this yields an instruction
cycle rate, CY f of
6 3.2768 10 819.2 kHz
4 CY f ? ? ?
The instruction cycle period, 1.221 ?s TCY ? ??is then simply the reciprocal of the instruction cycle rate,
CY f .
Along with the basic operation as a timer/counter, the Timer0 module also has a prescaler associated
with it. A prescaler is a logical device that reduced the frequency of the incoming clock by integer
division. The prescaler may be set to divide down the incoming clock by a range of preconfigured ratios
from 1:2 to 1:256.??
For example, setting the Timer0 prescaler to 1:128 will increment the timer at a now-reduced rate of
/128 6.4 kHz CY f ? . It is important that the prescaler is configured correctly so that the required
timings are obtained for the application in question.
4.3.3 Timer0 module configuration overview
A schematic representation of the timer module is shown in figure 4-3 which shows the various
elements which need to be correctly configured including the programmable prescaler module and
two multiplexers, which select the clock source and enable the prescaler respectively.??
34
Figure 4-3 Schematic of the Timer0 module (adapted from PIC16F877A MCU datasheet)
The Timer0 module is configured using the OPTION_REG SFR which contains configuration bits to set
the clock source, prescaler value etc. Figure 4-4 shows the location of the configuring bits in the
OPTION_REG SFR. Note carefully at how the bit names relate to the schematic elements in figure 4-3.
Figure 4-4 The OPTION_REG SFR configuration bits (adapted from PIC16F877A datasheet)
Only bits 0 to 5 of the OPTION_REG SFR are needed to configure the Timer0 module, bits RBPU and
INTEDG are used other peripheral configuration purposes that we need not concern ourselves with
for the moment. In order to use the Timer0 module as in timer mode the properties of the relevant
configuration bits must be set up as shown in Table 4-1.
Table 4-1 Correct OPTION_REG configuration of using Timer0 in timer mode
Bit Name Bit location in
OPTION_REG
MPLAB XC8 syntax Configuration requirements
T0CS 5 INTCONbits.TOCS Set to 0 to use Timer0 in timer mode
T0SE 4 INTCONbits.TOSE No effect in timer mode (don?t care state)
PSA 3 INTCONbits.PSA Set to 0 in timer mode to utilise the prescaler
PS2:0 2:0 INTCONbits.PS Define as required by application
The divide down value of the prescaler is selected by setting the three bits, PS2:0 according to table
4-2, 8 discrete options are available.??
35
Table 4-2 Timer0 prescaler configuration
PS2, PS1, PS0 Value TMR0 Rate
000 0 1:2
001 1 1:4
010 2 1:8
011 3 1:16
100 4 1:32
101 5 1:64
110 6 1:128
111 7 1:256
Note that a number of related bits in a SFR (in this case PS2:0) are termed a ?bit field?.
To summarise, the bitwise configuration of the OPTION_REG SFR (see figure 4-4) for operation of timer
0 in timer mode may be considered as follows:
OPTION_REG = XX0X 0UUU
where X = a don?t care state and U = a user defined state.
4.3.4 Calculation of timer counts and intervals
The prescaler and instruction clock rate are crucial parameters in determining/creating delays using
the Timer0 module. The total delay created by a given number of Timer0 counts is
4 prescaler 0count delay
osc
TMR
f
? ? ? ??
where OSC f is the frequency of the quartz crystal used to clock the PIC16F877A. This formula takes
into account the prescaler ratio and the fact that the instruction clock rate is a quarter of the crystal
frequency (see section 4.3.2). Rearranging the above equation in order to determine the number of
TMR0 counts to create a specific delay yields,
delay 0count
4 prescaler
osc f TMR
? ? ? ??
Remember that the maximum number of countsthe 8 bit Timer0 module can store before overflowing
is 256.
For example, if a 10 ms delay is required, with the prescaler divide down ratio set to 1:256, then the
required number of TMR0 counts is as follows.
3 6 delay 10 10 3.2768 10 0count 32
4 prescaler 4 256
osc f TMR
? ? ?? ? ?? ? ? ? ??
When 32 counts of the Timer0 module have occurred, we know that 10 ms have elapsed. We can use
this information to design a delay loops.
36
4.3.5 Timer0 configuration procedure
With the information shown in the previous section it is possible to derive a configuration procedure
for Timer0 as follows:
? Set TOCS = 0 to enable timer mode
? If using the prescaler, set PSA = 0. If not set PSA = 1 and skip the next step.
? Calculate required prescaler divide down ratio and write the appropriate 3 bit value to the
PS bit field.
4.4 Procedure
4.4.1 Exercise 1 ? Creating a simple delay using the Timer0 module
Create a new project in the MPLAB X IDE, create an empty source file, and populate it with
the following code listing.
// Filename: Lab4Ex1.c
// Version: 1.0
// Date:? ?


// Author:


//

// Description: Simple demonstration of a timer delay??

#include

?????????????// Required for all MPLAB XC8 source files

void main(void)

{

????unsigned char preload = 0x00;??// TMR0 preload variable

????// PORTD setup

????TRISD = 0x00;???????????????// Set PORTD all outputs

????PORTD = 0x00;???????????????// Clear PORTD

????

????//Timer0 setup

????OPTION_REGbits.T0CS = 0;????// Set clock source to internal (timer mode)

????OPTION_REGbits.PSA = 0;?????// Set prescaler to Timer 0

????OPTION_REGbits.PS = 7;??????// Set prescaler bit field to 111 for 1:256

?????

????while(1)????????????????????????// Infinite loop

????{????????????????????????

????????TMR0 = preload;?????????????// Preload timer0

????????while(!INTCONbits.TMR0IF);????// Delay loop: Wait until TMR0 overflows

????????INTCONbits.TMR0IF = 0;????????// Reset overflow flag, TMR0IF

????????PORTD++;????????????????????// Increment PORTD

????}

}

Build the project and upload the HEX file onto the PIC16F877A MCU.

Study the code listing carefully and observe what is happening to the LEDs on PORTD. You

should see PORTD incrementing from 0 to 255 in binary. Sketch a flow chart in your logbook

describing the sequence of events occurring in the executing code.

Calculate the length of the implemented delay using the appropriate formulae from section

4.3.4. Keep in mind that TMR0 is preloaded with 0x00 and the delay loop repeats until TMR0

37

overflows from 0xFF to 0x00 i.e. the loop condition becomes FALSE. In the general case for

the code listing above:

TMR0count 256 preload ? ? ??

Using a stopwatch, time how long it takes for the LEDs on PORTD to count up to 256 i.e. turn

from all OFF to all ON. Calculate the delay time for one individual count by dividing the total

time by 256.

Compare you calculated result from the empirical result obtained using a stopwatch. Does it

agree, taking into account any possible experimental error?

What happens if you increase the value of the preload variable in the code listing above?

4.4.2 Exercise 2 ? Creating longer delays

In the previous exercise you created the longest possible delay using Timer0 i.e. with the prescaler set

to a maximum divide down ratio of 1:256, and the full count of TMR0 used i.e. from zero (0x00) until

it overflowed. However, there are many occasions when a longer delay than is possible using a single

timer overflow is required. One possible approach to this is to cascade several shorter delays, in a

loop, to create a longer overall delay.

Create a new project in the MPLAB X IDE, add an empty source file and add the following

code listing to it.

// Filename: Lab4Ex2.c

// Version: 1.0

// Date:? ?


// Author:


//

// Description: Looping of a short timer delay to create a longer one??

#include

?????????????// Required for all MPLAB XC8 source files

void main(void)

{

????unsigned char preload = 192;????// TMR0 preload variable

????unsigned char i;????????????????// Loop index variable

????// PORTD setup

????TRISD = 0x00;???????????????// Set PORTD all outputs

????PORTD = 0x00;???????????????// Clear PORTD

????

????//Timer0 setup

????OPTION_REGbits.T0CS = 0;????// Set clock source to internal (timer mode)

????OPTION_REGbits.PSA = 0;?????// Set prescaler to Timer 0

TIP!

When a Timer0 overflow occurs and the TMR0IF bit is SET and remains in that state until it is

manually RESET in software. This can be done using the following syntax:

INTCONbits.TMR0IF = 0;

It is important to remember to do this when you are polling for timer overflows.

38

????OPTION_REGbits.PS = 7;??????// Set prescaler bits to 111 for 1:256

?????

????while(1)????????????????????// Infinite loop

????{????????????????????????

????????for(i=0;i<8;i++)???????// Loop to cascade several short delays together

????????{

????????????TMR0 = preload;?????????????// Preload timer0

????????????while(!INTCONbits.TMR0IF);????// Delay loop: Wait until TMR0 overflows

????????????INTCONbits.TMR0IF = 0;????????// Reset overflow flag, TMR0IF

????????}

????????PORTD++;???????????????// Increment PORTD

????}

}

Calculate the length of the delay implemented using bearing in mind that the value of the

preload variable is now set to 192 and the shorter delay loop is now repeated 8 times.

Using a stopwatch, time how long it takes for the LEDs on PORTD to count up to 256 i.e. turn

from all OFF to all ON. Calculate the delay time for one individual count by dividing the total

time by 256.

Compare you calculated result from the empirical result obtained using a stopwatch. Does it

agree, taking into account any possible experimental error?

Modify the code to create a one second delay. You should approach this by adjusting the

preload value so that the short delay loop has a length that divides exactly into one second.

This short delay should then be repeated the required number of times to produce a one

second delay.

Confirm your results using a stopwatch.

4.4.3 Exercise 3 ? Creating a one second delay function

A delay is a useful and often used action in any embedded system. Having gone to the effort of

developing a one second delay it would make sense to package the code into a function so that it could

be easily reused whenever required.???

The following code listing shows one possible template for implementing the one second delay as a

function call one_sec_delay(). The operation of this code is exactly the same as that produced in

exercise 3, the only difference is that all the code that had previously been required to create the

delay has now been packaged inside a function which is called from the main() function.

Create a project and enter the following listing. You will need to replace the two ???? symbols

in the listing below with the values you derived in the previous exercise in order for it to build

successfully.

// Filename: Lab4Ex3.c

// Version: 1.0

// Date:? ?


// Author:


//

// Description: Implementing a Timer0 delay function??

#include

?????????????// Required for all MPLAB XC8 source files

39

void one_sec_delay (void);??// Function prototype

void main(void)

{

????// PORTD setup

????TRISD = 0x00;???????????????// Set PORTD all outputs

????PORTD = 0x00;???????????????// Clear PORTD

?????

????while(1)????????????????????// Infinite loop

????{????

????????one_sec_delay();????????// Call delay function

????????PORTD++;????????????????// Increment PORTD?????

????}

}

void one_sec_delay (void)

{

????unsigned char preload = ??;?????// TMR0 preload variable (derived from Ex2)

????unsigned char i;????????????????// Loop index variable

?????

????//Timer0 setup

????OPTION_REGbits.T0CS = 0;????// Set clock source to internal (timer mode)

????OPTION_REGbits.PSA = 0;?????// Set prescaler to Timer 0

????OPTION_REGbits.PS = 7;??????// Set prescaler bits to 111 for 1:256

?????

????for(i=0;i
????????????????????????????// Number of loops derived from Ex2

????{

????????TMR0 = preload;?????????????// Preload timer0

????????while(!INTCONbits.TMR0IF);????// Delay loop: Wait until TMR0 overflows

????????INTCONbits.TMR0IF = 0;????????// Reset overflow flag, TMR0IF

????}?????

}

Build the project and validate that the operation is as anticipated.

4.4.4 Exercise 4 ? Creating a variable delay function

In this exercise we will develop a generic variable delay function that will allow the implementation of

any user defined delay of an integer number of seconds

Create a new project based upon the code developed in exercise 3.

Modify the fixed one second delay function, one_sec_delay() developed in exercise 3 so that

it becomes a generic delay function taking the form,

void delay(unsigned char delay_length)

where the parameter delay_length sets the length of the delay implemented by the function.

The parameter will need to modify the number of cascaded short delays to generate the

variable delay.

4.5 Further reading

Microchip MPLAB X Timer0 (Part 1) tutorial sheet. Note the code listings show is not directly

compatible with the XC8 compiler, but the information about configuration is still relevant.








32
4 Lab 4 ? Timer0 module
4.1 Aim
This laboratory practical will introduce you to the Timer0 module which is one of three timer/counter
peripherals located on the PIC16F877A MCU. Timer/counter modules allow the creation of precise
and deterministic timing intervals which is essential in many applications e.g. frequency
generation/measurement, timekeeping, control systems, communications.??This laboratory practical
will focus on the proper configuration and usage of the Timer0 module in timer mode only.
4.2 Learning Outcomes
? Understand the structure of the Timer0 module on the PIC16F877A
? Be able to configure the Timer0 module using the appropriate special function registers
? Be able to develop projects to utilise the Timer0 module in a range of applications.
4.3 Background
4.3.1 Counter/Timers
The only practical difference between a counter and a timer is the nature of the source. A counter
simply increments a stored ?count? value when a logic state change (either a rising or falling edge) is
received at its input. To operate a counter as a timer, a synchronous pulse train (clock) of a known
frequency is applied to the input. Figure 4-1 shows a generic representation of a counter configured
as a timer. The current count value can be read from the counter register, and the register can also be
written to or ?preloaded?.
Figure 4-1 Generic representation of a hardware timer module
If the frequency of the synchronous pulse train, f is known then the elapsed time, T may be
determined from number of counts, N recorded by the timer as follows,
N T
f ? ??
4.3.2 PIC16F877A Timer0 module
The Timer0 module on the PIC16F877A MCU is an 8 bit pre-scalable timer/counter. In timer mode, the
TMR0 special function register (SFR) is automatically incremented on every instruction clock cycle. In
counter mode, the TMR0 SFR is incremented either on rising or on falling edge (configuration
dependant) of the external pin RA4/T0CKL. In this laboratory practical we will only be considering the
use of Timer0 in timer mode.
33
The 8 bit Timer0 module on board the PIC16F877A can store 8 2 256 ? independent values from 0-
255. The current count value is stored in the TMR0 SFR may be read at any time without affecting the
operation of the Timer0 module. It may also written to (preloaded) by writing the appropriate value
to the TMR0 SFR.
When the Timer0 module count value reaches its maximum value (255), the counter overflows back
to zero and continues counting. This timer overflow results in the setting of a bit (TMR0IF) in the
INTCON SFR. Figure 4-2 show the structure of the INTCON SFR which we will be looking at in more
detail in future laboratory practicals. For the moment just remember that when a Timer0 overflow
occurs then the TMR0IF bit is SET.
Figure 4-2 The INTCON register
A single bit which signifies an occurrence of an event, in this case a Timer0 overflow, is called a flag.
We can detect when the overflow Timer0 has occurred by regularly checking the status of the TMR0IF
bit (flag) in a process known as polling. By using polling in conjunction with loopsit is possible to create
accurate delays.
Timer0 receives its synchronous pulse train (clock) at the instruction cycle rate of the PIC16F877A
MCU, which is one quarter of the frequency of the quartz crystal oscillator used to clock the MCU. For
the 3.2768 MHz crystal oscillators used on the E-block development systems, this yields an instruction
cycle rate, CY f of
6 3.2768 10 819.2 kHz
4 CY f ? ? ?
The instruction cycle period, 1.221 ?s TCY ? ??is then simply the reciprocal of the instruction cycle rate,
CY f .
Along with the basic operation as a timer/counter, the Timer0 module also has a prescaler associated
with it. A prescaler is a logical device that reduced the frequency of the incoming clock by integer
division. The prescaler may be set to divide down the incoming clock by a range of preconfigured ratios
from 1:2 to 1:256.??
For example, setting the Timer0 prescaler to 1:128 will increment the timer at a now-reduced rate of
/128 6.4 kHz CY f ? . It is important that the prescaler is configured correctly so that the required
timings are obtained for the application in question.
4.3.3 Timer0 module configuration overview
A schematic representation of the timer module is shown in figure 4-3 which shows the various
elements which need to be correctly configured including the programmable prescaler module and
two multiplexers, which select the clock source and enable the prescaler respectively.??
34
Figure 4-3 Schematic of the Timer0 module (adapted from PIC16F877A MCU datasheet)
The Timer0 module is configured using the OPTION_REG SFR which contains configuration bits to set
the clock source, prescaler value etc. Figure 4-4 shows the location of the configuring bits in the
OPTION_REG SFR. Note carefully at how the bit names relate to the schematic elements in figure 4-3.
Figure 4-4 The OPTION_REG SFR configuration bits (adapted from PIC16F877A datasheet)
Only bits 0 to 5 of the OPTION_REG SFR are needed to configure the Timer0 module, bits RBPU and
INTEDG are used other peripheral configuration purposes that we need not concern ourselves with
for the moment. In order to use the Timer0 module as in timer mode the properties of the relevant
configuration bits must be set up as shown in Table 4-1.
Table 4-1 Correct OPTION_REG configuration of using Timer0 in timer mode
Bit Name Bit location in
OPTION_REG
MPLAB XC8 syntax Configuration requirements
T0CS 5 INTCONbits.TOCS Set to 0 to use Timer0 in timer mode
T0SE 4 INTCONbits.TOSE No effect in timer mode (don?t care state)
PSA 3 INTCONbits.PSA Set to 0 in timer mode to utilise the prescaler
PS2:0 2:0 INTCONbits.PS Define as required by application
The divide down value of the prescaler is selected by setting the three bits, PS2:0 according to table
4-2, 8 discrete options are available.??
35
Table 4-2 Timer0 prescaler configuration
PS2, PS1, PS0 Value TMR0 Rate
000 0 1:2
001 1 1:4
010 2 1:8
011 3 1:16
100 4 1:32
101 5 1:64
110 6 1:128
111 7 1:256
Note that a number of related bits in a SFR (in this case PS2:0) are termed a ?bit field?.
To summarise, the bitwise configuration of the OPTION_REG SFR (see figure 4-4) for operation of timer
0 in timer mode may be considered as follows:
OPTION_REG = XX0X 0UUU
where X = a don?t care state and U = a user defined state.
4.3.4 Calculation of timer counts and intervals
The prescaler and instruction clock rate are crucial parameters in determining/creating delays using
the Timer0 module. The total delay created by a given number of Timer0 counts is
4 prescaler 0count delay
osc
TMR
f
? ? ? ??
where OSC f is the frequency of the quartz crystal used to clock the PIC16F877A. This formula takes
into account the prescaler ratio and the fact that the instruction clock rate is a quarter of the crystal
frequency (see section 4.3.2). Rearranging the above equation in order to determine the number of
TMR0 counts to create a specific delay yields,
delay 0count
4 prescaler
osc f TMR
? ? ? ??
Remember that the maximum number of countsthe 8 bit Timer0 module can store before overflowing
is 256.
For example, if a 10 ms delay is required, with the prescaler divide down ratio set to 1:256, then the
required number of TMR0 counts is as follows.
3 6 delay 10 10 3.2768 10 0count 32
4 prescaler 4 256
osc f TMR
? ? ?? ? ?? ? ? ? ??
When 32 counts of the Timer0 module have occurred, we know that 10 ms have elapsed. We can use
this information to design a delay loops.
36
4.3.5 Timer0 configuration procedure
With the information shown in the previous section it is possible to derive a configuration procedure
for Timer0 as follows:
? Set TOCS = 0 to enable timer mode
? If using the prescaler, set PSA = 0. If not set PSA = 1 and skip the next step.
? Calculate required prescaler divide down ratio and write the appropriate 3 bit value to the
PS bit field.
4.4 Procedure
4.4.1 Exercise 1 ? Creating a simple delay using the Timer0 module
Create a new project in the MPLAB X IDE, create an empty source file, and populate it with
the following code listing.
// Filename: Lab4Ex1.c
// Version: 1.0
// Date:? ?


// Author:


//

// Description: Simple demonstration of a timer delay??

#include

?????????????// Required for all MPLAB XC8 source files

void main(void)

{

????unsigned char preload = 0x00;??// TMR0 preload variable

????// PORTD setup

????TRISD = 0x00;???????????????// Set PORTD all outputs

????PORTD = 0x00;???????????????// Clear PORTD

????

????//Timer0 setup

????OPTION_

Leave a Reply

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

Joon, age 51, is an unmarried, cash basis, calendar year taxpayer. Joon has one 9-year-old qualifying child who lives with him full-time and is claimed on Form 1040. Joon has the following income and deductions for 2016:

RECEIPTS FOR YEAR/Deductions
Salaries and wages from local school district $78,000
Interest from Friendly Bank 1,235
Dividends from Tandem Diabetic Corporation (a publicly traded
U.S. corporation) 36
Allowable itemized deductions 6,970

RENTAL OPERATIONS
Rental income 14,400
Interest expense 7,800
Property taxes 1,450
Insurance 1,260
Depreciation?Building 2,895
Depreciation?Appliances 941

SALE OF RENTAL RESIDENCE
Sales price (October 26, 2016) 210,600
Cost (October 18, 2010)?$40,000 assigned to land 119,600
Depreciation allowed (Straight line MACRS) 17,367
Selling commission 7,440
Purchase costs (recording, other legal) 2,760

SALE OF APPLIANCES WITH RESIDENCE
Sales price (October 26, 2016) 6,600
Cost (September 25, 2014) 9,800
Depreciation allowed (Straight line MACRS) 6,037

Prepare the following forms for Joon for 2016:

Form 1040
Schedule D (Required)?Worksheet Not Required?You may calculate the
tax without completing the worksheet. See October 26 Solutions for an
example.
Schedule E
Form 4562
Form 4797

Provide any information that is not stated.

Accountancy 503?Fall 2016 Tax Return 3b?5 points; Due December 7

Brianna, age 29, is an unmarried, cash basis, calendar year taxpayer. Aileen has no dependents. Aileen has the following income and deductions for 2016:

RECEIPTS FOR YEAR/Deductions
Salaries and wages from local school district $48,000
Interest from Friendly Bank 1,235
Dividends from Tandem Diabetic Corporation (a publicly traded
U.S. corporation) 36
Allowable itemized deductions 6,940

BUSINESS OPERATIONS?PROFESSIONAL SERVICES
Fee income collected 34,400
Interest expense 4,800
Office supplies 1,450
Insurance 1,260
Rental expense 12,895
Transportation 2,300
Continuing Education 975
Purchase of computer and peripherals 1,880

Prepare the following forms for Aileen for 2016:

Schedule C
Schedule SE

Provide any information that is not stated.

1.

Choose a company about 15 pager Suggested Project Outline 1. Executive Summary 2. Firm, Industry, and Environment (not to exceed 2 pages) 1. Summary description the firm including a brief description of the key product or service, the relevant market, and analysis of demand including the principle factors that influence demand, and the implications for revenue. 2. Brief description of the principal inputs, production processes, indicating any significant changes in technology and analysis of trends in productivity. 3. Summary of the behavior of costs, short-run and long run, indicating the principal factors that influence costs and their impacts as indicated by cost trends. 4. Summary of the competitive environment, analysis of the key measures through which firms compete (pricing, quality, brand, innovation, etc.) and discussion of potential changes in the level of competition. 3. Company Performance and Managerial Effectiveness 1. Through-time (trend) analysis of the firm?s performance (using the Dupont framework as an organizing template for your discussion) 2. Cross-sectional analysis and comparison of the firm with benchmarks: industry competitor and industry benchmarks 3. Discuss the effect of the firm?s accounting choices on the financial statements 4. Future Prospects: 1. Integrative assessment of current company performance, focusing on principal sources of competitive advantage, investment potential, and credit-worthiness. 2. Integrative overall outlook for future performance 5. Appendix 6. Footnotes 7. Bibliography1.

2.

HRM 300: Equal Employment Opportunity and Employee Rights Review.

For each selected law or issue, locate a present-day court case that has challenged your selected law or issue A): Equal Employment Opportunity Act of 1972, and Family and Medical Leave Act of 1993. B): Whistle-Blowing. There are a total of three issues to address in the paper Please be original, include APA format, and cite references.

3.
coding using MPLAB

4 Lab 4 ? Timer0 module

5,This laboratory practical will introduce you to the Timer0 module which is one of three timer/counter
peripherals located on the PIC16F877A MCU. Timer/counter modules allow the creation of precise
and deterministic timing intervals which is essential in many applications e.g. frequency
generation/measurement, timekeeping, control systems, communications.??This laboratory practical
will focus on the proper configuration and usage of the Timer0 module in timer mode only.
4.2 Learning Outcomes
? Understand the structure of the Timer0 module on the PIC16F877A
? Be able to configure the Timer0 module using the appropriate special function registers
? Be able to develop projects to utilise the Timer0 module in a range of applications.
4.3 Background
4.3.1 Counter/Timers
The only practical difference between a counter and a timer is the nature of the source. A counter
simply increments a stored ?count? value when a logic state change (either a rising or falling edge) is
received at its input. To operate a counter as a timer, a synchronous pulse train (clock) of a known
frequency is applied to the input. Figure 4-1 shows a generic representation of a counter configured
as a timer. The current count value can be read from the counter register, and the register can also be
written to or ?preloaded?.
Figure 4-1 Generic representation of a hardware timer module
If the frequency of the synchronous pulse train, f is known then the elapsed time, T may be
determined from number of counts, N recorded by the timer as follows,
N T
f ? ??
4.3.2 PIC16F877A Timer0 module
The Timer0 module on the PIC16F877A MCU is an 8 bit pre-scalable timer/counter. In timer mode, the
TMR0 special function register (SFR) is automatically incremented on every instruction clock cycle. In
counter mode, the TMR0 SFR is incremented either on rising or on falling edge (configuration
dependant) of the external pin RA4/T0CKL. In this laboratory practical we will only be considering the
use of Timer0 in timer mode.
33
The 8 bit Timer0 module on board the PIC16F877A can store 8 2 256 ? independent values from 0-
255. The current count value is stored in the TMR0 SFR may be read at any time without affecting the
operation of the Timer0 module. It may also written to (preloaded) by writing the appropriate value
to the TMR0 SFR.
When the Timer0 module count value reaches its maximum value (255), the counter overflows back
to zero and continues counting. This timer overflow results in the setting of a bit (TMR0IF) in the
INTCON SFR. Figure 4-2 show the structure of the INTCON SFR which we will be looking at in more
detail in future laboratory practicals. For the moment just remember that when a Timer0 overflow
occurs then the TMR0IF bit is SET.
Figure 4-2 The INTCON register
A single bit which signifies an occurrence of an event, in this case a Timer0 overflow, is called a flag.
We can detect when the overflow Timer0 has occurred by regularly checking the status of the TMR0IF
bit (flag) in a process known as polling. By using polling in conjunction with loopsit is possible to create
accurate delays.
Timer0 receives its synchronous pulse train (clock) at the instruction cycle rate of the PIC16F877A
MCU, which is one quarter of the frequency of the quartz crystal oscillator used to clock the MCU. For
the 3.2768 MHz crystal oscillators used on the E-block development systems, this yields an instruction
cycle rate, CY f of
6 3.2768 10 819.2 kHz
4 CY f ? ? ?
The instruction cycle period, 1.221 ?s TCY ? ??is then simply the reciprocal of the instruction cycle rate,
CY f .
Along with the basic operation as a timer/counter, the Timer0 module also has a prescaler associated
with it. A prescaler is a logical device that reduced the frequency of the incoming clock by integer
division. The prescaler may be set to divide down the incoming clock by a range of preconfigured ratios
from 1:2 to 1:256.??
For example, setting the Timer0 prescaler to 1:128 will increment the timer at a now-reduced rate of
/128 6.4 kHz CY f ? . It is important that the prescaler is configured correctly so that the required
timings are obtained for the application in question.
4.3.3 Timer0 module configuration overview
A schematic representation of the timer module is shown in figure 4-3 which shows the various
elements which need to be correctly configured including the programmable prescaler module and
two multiplexers, which select the clock source and enable the prescaler respectively.??
34
Figure 4-3 Schematic of the Timer0 module (adapted from PIC16F877A MCU datasheet)
The Timer0 module is configured using the OPTION_REG SFR which contains configuration bits to set
the clock source, prescaler value etc. Figure 4-4 shows the location of the configuring bits in the
OPTION_REG SFR. Note carefully at how the bit names relate to the schematic elements in figure 4-3.
Figure 4-4 The OPTION_REG SFR configuration bits (adapted from PIC16F877A datasheet)
Only bits 0 to 5 of the OPTION_REG SFR are needed to configure the Timer0 module, bits RBPU and
INTEDG are used other peripheral configuration purposes that we need not concern ourselves with
for the moment. In order to use the Timer0 module as in timer mode the properties of the relevant
configuration bits must be set up as shown in Table 4-1.
Table 4-1 Correct OPTION_REG configuration of using Timer0 in timer mode
Bit Name Bit location in
OPTION_REG
MPLAB XC8 syntax Configuration requirements
T0CS 5 INTCONbits.TOCS Set to 0 to use Timer0 in timer mode
T0SE 4 INTCONbits.TOSE No effect in timer mode (don?t care state)
PSA 3 INTCONbits.PSA Set to 0 in timer mode to utilise the prescaler
PS2:0 2:0 INTCONbits.PS Define as required by application
The divide down value of the prescaler is selected by setting the three bits, PS2:0 according to table
4-2, 8 discrete options are available.??
35
Table 4-2 Timer0 prescaler configuration
PS2, PS1, PS0 Value TMR0 Rate
000 0 1:2
001 1 1:4
010 2 1:8
011 3 1:16
100 4 1:32
101 5 1:64
110 6 1:128
111 7 1:256
Note that a number of related bits in a SFR (in this case PS2:0) are termed a ?bit field?.
To summarise, the bitwise configuration of the OPTION_REG SFR (see figure 4-4) for operation of timer
0 in timer mode may be considered as follows:
OPTION_REG = XX0X 0UUU
where X = a don?t care state and U = a user defined state.
4.3.4 Calculation of timer counts and intervals
The prescaler and instruction clock rate are crucial parameters in determining/creating delays using
the Timer0 module. The total delay created by a given number of Timer0 counts is
4 prescaler 0count delay
osc
TMR
f
? ? ? ??
where OSC f is the frequency of the quartz crystal used to clock the PIC16F877A. This formula takes
into account the prescaler ratio and the fact that the instruction clock rate is a quarter of the crystal
frequency (see section 4.3.2). Rearranging the above equation in order to determine the number of
TMR0 counts to create a specific delay yields,
delay 0count
4 prescaler
osc f TMR
? ? ? ??
Remember that the maximum number of countsthe 8 bit Timer0 module can store before overflowing
is 256.
For example, if a 10 ms delay is required, with the prescaler divide down ratio set to 1:256, then the
required number of TMR0 counts is as follows.
3 6 delay 10 10 3.2768 10 0count 32
4 prescaler 4 256
osc f TMR
? ? ?? ? ?? ? ? ? ??
When 32 counts of the Timer0 module have occurred, we know that 10 ms have elapsed. We can use
this information to design a delay loops.
36
4.3.5 Timer0 configuration procedure
With the information shown in the previous section it is possible to derive a configuration procedure
for Timer0 as follows:
? Set TOCS = 0 to enable timer mode
? If using the prescaler, set PSA = 0. If not set PSA = 1 and skip the next step.
? Calculate required prescaler divide down ratio and write the appropriate 3 bit value to the
PS bit field.
4.4 Procedure
4.4.1 Exercise 1 ? Creating a simple delay using the Timer0 module
Create a new project in the MPLAB X IDE, create an empty source file, and populate it with
the following code listing.
// Filename: Lab4Ex1.c
// Version: 1.0
// Date:? ?


// Author:


//

// Description: Simple demonstration of a timer delay??

#include

?????????????// Required for all MPLAB XC8 source files

void main(void)

{

????unsigned char preload = 0x00;??// TMR0 preload variable

????// PORTD setup

????TRISD = 0x00;???????????????// Set PORTD all outputs

????PORTD = 0x00;???????????????// Clear PORTD

????

????//Timer0 setup

????OPTION_REGbits.T0CS = 0;????// Set clock source to internal (timer mode)

????OPTION_REGbits.PSA = 0;?????// Set prescaler to Timer 0

????OPTION_REGbits.PS = 7;??????// Set prescaler bit field to 111 for 1:256

?????

????while(1)????????????????????????// Infinite loop

????{????????????????????????

????????TMR0 = preload;?????????????// Preload timer0

????????while(!INTCONbits.TMR0IF);????// Delay loop: Wait until TMR0 overflows

????????INTCONbits.TMR0IF = 0;????????// Reset overflow flag, TMR0IF

????????PORTD++;????????????????????// Increment PORTD

????}

}

Build the project and upload the HEX file onto the PIC16F877A MCU.

Study the code listing carefully and observe what is happening to the LEDs on PORTD. You

should see PORTD incrementing from 0 to 255 in binary. Sketch a flow chart in your logbook

describing the sequence of events occurring in the executing code.

Calculate the length of the implemented delay using the appropriate formulae from section

4.3.4. Keep in mind that TMR0 is preloaded with 0x00 and the delay loop repeats until TMR0

37

overflows from 0xFF to 0x00 i.e. the loop condition becomes FALSE. In the general case for

the code listing above:

TMR0count 256 preload ? ? ??

Using a stopwatch, time how long it takes for the LEDs on PORTD to count up to 256 i.e. turn

from all OFF to all ON. Calculate the delay time for one individual count by dividing the total

time by 256.

Compare you calculated result from the empirical result obtained using a stopwatch. Does it

agree, taking into account any possible experimental error?

What happens if you increase the value of the preload variable in the code listing above?

4.4.2 Exercise 2 ? Creating longer delays

In the previous exercise you created the longest possible delay using Timer0 i.e. with the prescaler set

to a maximum divide down ratio of 1:256, and the full count of TMR0 used i.e. from zero (0x00) until

it overflowed. However, there are many occasions when a longer delay than is possible using a single

timer overflow is required. One possible approach to this is to cascade several shorter delays, in a

loop, to create a longer overall delay.

Create a new project in the MPLAB X IDE, add an empty source file and add the following

code listing to it.

// Filename: Lab4Ex2.c

// Version: 1.0

// Date:? ?


// Author:


//

// Description: Looping of a short timer delay to create a longer one??

#include

?????????????// Required for all MPLAB XC8 source files

void main(void)

{

????unsigned char preload = 192;????// TMR0 preload variable

????unsigned char i;????????????????// Loop index variable

????// PORTD setup

????TRISD = 0x00;???????????????// Set PORTD all outputs

????PORTD = 0x00;???????????????// Clear PORTD

????

????//Timer0 setup

????OPTION_REGbits.T0CS = 0;????// Set clock source to internal (timer mode)

????OPTION_REGbits.PSA = 0;?????// Set prescaler to Timer 0

TIP!

When a Timer0 overflow occurs and the TMR0IF bit is SET and remains in that state until it is

manually RESET in software. This can be done using the following syntax:

INTCONbits.TMR0IF = 0;

It is important to remember to do this when you are polling for timer overflows.

38

????OPTION_REGbits.PS = 7;??????// Set prescaler bits to 111 for 1:256

?????

????while(1)????????????????????// Infinite loop

????{????????????????????????

????????for(i=0;i<8;i++)???????// Loop to cascade several short delays together

????????{

????????????TMR0 = preload;?????????????// Preload timer0

????????????while(!INTCONbits.TMR0IF);????// Delay loop: Wait until TMR0 overflows

????????????INTCONbits.TMR0IF = 0;????????// Reset overflow flag, TMR0IF

????????}

????????PORTD++;???????????????// Increment PORTD

????}

}

Calculate the length of the delay implemented using bearing in mind that the value of the

preload variable is now set to 192 and the shorter delay loop is now repeated 8 times.

Using a stopwatch, time how long it takes for the LEDs on PORTD to count up to 256 i.e. turn

from all OFF to all ON. Calculate the delay time for one individual count by dividing the total

time by 256.

Compare you calculated result from the empirical result obtained using a stopwatch. Does it

agree, taking into account any possible experimental error?

Modify the code to create a one second delay. You should approach this by adjusting the

preload value so that the short delay loop has a length that divides exactly into one second.

This short delay should then be repeated the required number of times to produce a one

second delay.

Confirm your results using a stopwatch.

4.4.3 Exercise 3 ? Creating a one second delay function

A delay is a useful and often used action in any embedded system. Having gone to the effort of

developing a one second delay it would make sense to package the code into a function so that it could

be easily reused whenever required.???

The following code listing shows one possible template for implementing the one second delay as a

function call one_sec_delay(). The operation of this code is exactly the same as that produced in

exercise 3, the only difference is that all the code that had previously been required to create the

delay has now been packaged inside a function which is called from the main() function.

Create a project and enter the following listing. You will need to replace the two ???? symbols

in the listing below with the values you derived in the previous exercise in order for it to build

successfully.

// Filename: Lab4Ex3.c

// Version: 1.0

// Date:? ?


// Author:


//

// Description: Implementing a Timer0 delay function??

#include

?????????????// Required for all MPLAB XC8 source files

39

void one_sec_delay (void);??// Function prototype

void main(void)

{

????// PORTD setup

????TRISD = 0x00;???????????????// Set PORTD all outputs

????PORTD = 0x00;???????????????// Clear PORTD

?????

????while(1)????????????????????// Infinite loop

????{????

????????one_sec_delay();????????// Call delay function

????????PORTD++;????????????????// Increment PORTD?????

????}

}

void one_sec_delay (void)

{

????unsigned char preload = ??;?????// TMR0 preload variable (derived from Ex2)

????unsigned char i;????????????????// Loop index variable

?????

????//Timer0 setup

????OPTION_REGbits.T0CS = 0;????// Set clock source to internal (timer mode)

????OPTION_REGbits.PSA = 0;?????// Set prescaler to Timer 0

????OPTION_REGbits.PS = 7;??????// Set prescaler bits to 111 for 1:256

?????

????for(i=0;i
????????????????????????????// Number of loops derived from Ex2

????{

????????TMR0 = preload;?????????????// Preload timer0

????????while(!INTCONbits.TMR0IF);????// Delay loop: Wait until TMR0 overflows

????????INTCONbits.TMR0IF = 0;????????// Reset overflow flag, TMR0IF

????}?????

}

Build the project and validate that the operation is as anticipated.

4.4.4 Exercise 4 ? Creating a variable delay function

In this exercise we will develop a generic variable delay function that will allow the implementation of

any user defined delay of an integer number of seconds

Create a new project based upon the code developed in exercise 3.

Modify the fixed one second delay function, one_sec_delay() developed in exercise 3 so that

it becomes a generic delay function taking the form,

void delay(unsigned char delay_length)

where the parameter delay_length sets the length of the delay implemented by the function.

The parameter will need to modify the number of cascaded short delays to generate the

variable delay.

4.5 Further reading

Microchip MPLAB X Timer0 (Part 1) tutorial sheet. Note the code listings show is not directly

compatible with the XC8 compiler, but the information about configuration is still relevant.








32
4 Lab 4 ? Timer0 module
4.1 Aim
This laboratory practical will introduce you to the Timer0 module which is one of three timer/counter
peripherals located on the PIC16F877A MCU. Timer/counter modules allow the creation of precise
and deterministic timing intervals which is essential in many applications e.g. frequency
generation/measurement, timekeeping, control systems, communications.??This laboratory practical
will focus on the proper configuration and usage of the Timer0 module in timer mode only.
4.2 Learning Outcomes
? Understand the structure of the Timer0 module on the PIC16F877A
? Be able to configure the Timer0 module using the appropriate special function registers
? Be able to develop projects to utilise the Timer0 module in a range of applications.
4.3 Background
4.3.1 Counter/Timers
The only practical difference between a counter and a timer is the nature of the source. A counter
simply increments a stored ?count? value when a logic state change (either a rising or falling edge) is
received at its input. To operate a counter as a timer, a synchronous pulse train (clock) of a known
frequency is applied to the input. Figure 4-1 shows a generic representation of a counter configured
as a timer. The current count value can be read from the counter register, and the register can also be
written to or ?preloaded?.
Figure 4-1 Generic representation of a hardware timer module
If the frequency of the synchronous pulse train, f is known then the elapsed time, T may be
determined from number of counts, N recorded by the timer as follows,
N T
f ? ??
4.3.2 PIC16F877A Timer0 module
The Timer0 module on the PIC16F877A MCU is an 8 bit pre-scalable timer/counter. In timer mode, the
TMR0 special function register (SFR) is automatically incremented on every instruction clock cycle. In
counter mode, the TMR0 SFR is incremented either on rising or on falling edge (configuration
dependant) of the external pin RA4/T0CKL. In this laboratory practical we will only be considering the
use of Timer0 in timer mode.
33
The 8 bit Timer0 module on board the PIC16F877A can store 8 2 256 ? independent values from 0-
255. The current count value is stored in the TMR0 SFR may be read at any time without affecting the
operation of the Timer0 module. It may also written to (preloaded) by writing the appropriate value
to the TMR0 SFR.
When the Timer0 module count value reaches its maximum value (255), the counter overflows back
to zero and continues counting. This timer overflow results in the setting of a bit (TMR0IF) in the
INTCON SFR. Figure 4-2 show the structure of the INTCON SFR which we will be looking at in more
detail in future laboratory practicals. For the moment just remember that when a Timer0 overflow
occurs then the TMR0IF bit is SET.
Figure 4-2 The INTCON register
A single bit which signifies an occurrence of an event, in this case a Timer0 overflow, is called a flag.
We can detect when the overflow Timer0 has occurred by regularly checking the status of the TMR0IF
bit (flag) in a process known as polling. By using polling in conjunction with loopsit is possible to create
accurate delays.
Timer0 receives its synchronous pulse train (clock) at the instruction cycle rate of the PIC16F877A
MCU, which is one quarter of the frequency of the quartz crystal oscillator used to clock the MCU. For
the 3.2768 MHz crystal oscillators used on the E-block development systems, this yields an instruction
cycle rate, CY f of
6 3.2768 10 819.2 kHz
4 CY f ? ? ?
The instruction cycle period, 1.221 ?s TCY ? ??is then simply the reciprocal of the instruction cycle rate,
CY f .
Along with the basic operation as a timer/counter, the Timer0 module also has a prescaler associated
with it. A prescaler is a logical device that reduced the frequency of the incoming clock by integer
division. The prescaler may be set to divide down the incoming clock by a range of preconfigured ratios
from 1:2 to 1:256.??
For example, setting the Timer0 prescaler to 1:128 will increment the timer at a now-reduced rate of
/128 6.4 kHz CY f ? . It is important that the prescaler is configured correctly so that the required
timings are obtained for the application in question.
4.3.3 Timer0 module configuration overview
A schematic representation of the timer module is shown in figure 4-3 which shows the various
elements which need to be correctly configured including the programmable prescaler module and
two multiplexers, which select the clock source and enable the prescaler respectively.??
34
Figure 4-3 Schematic of the Timer0 module (adapted from PIC16F877A MCU datasheet)
The Timer0 module is configured using the OPTION_REG SFR which contains configuration bits to set
the clock source, prescaler value etc. Figure 4-4 shows the location of the configuring bits in the
OPTION_REG SFR. Note carefully at how the bit names relate to the schematic elements in figure 4-3.
Figure 4-4 The OPTION_REG SFR configuration bits (adapted from PIC16F877A datasheet)
Only bits 0 to 5 of the OPTION_REG SFR are needed to configure the Timer0 module, bits RBPU and
INTEDG are used other peripheral configuration purposes that we need not concern ourselves with
for the moment. In order to use the Timer0 module as in timer mode the properties of the relevant
configuration bits must be set up as shown in Table 4-1.
Table 4-1 Correct OPTION_REG configuration of using Timer0 in timer mode
Bit Name Bit location in
OPTION_REG
MPLAB XC8 syntax Configuration requirements
T0CS 5 INTCONbits.TOCS Set to 0 to use Timer0 in timer mode
T0SE 4 INTCONbits.TOSE No effect in timer mode (don?t care state)
PSA 3 INTCONbits.PSA Set to 0 in timer mode to utilise the prescaler
PS2:0 2:0 INTCONbits.PS Define as required by application
The divide down value of the prescaler is selected by setting the three bits, PS2:0 according to table
4-2, 8 discrete options are available.??
35
Table 4-2 Timer0 prescaler configuration
PS2, PS1, PS0 Value TMR0 Rate
000 0 1:2
001 1 1:4
010 2 1:8
011 3 1:16
100 4 1:32
101 5 1:64
110 6 1:128
111 7 1:256
Note that a number of related bits in a SFR (in this case PS2:0) are termed a ?bit field?.
To summarise, the bitwise configuration of the OPTION_REG SFR (see figure 4-4) for operation of timer
0 in timer mode may be considered as follows:
OPTION_REG = XX0X 0UUU
where X = a don?t care state and U = a user defined state.
4.3.4 Calculation of timer counts and intervals
The prescaler and instruction clock rate are crucial parameters in determining/creating delays using
the Timer0 module. The total delay created by a given number of Timer0 counts is
4 prescaler 0count delay
osc
TMR
f
? ? ? ??
where OSC f is the frequency of the quartz crystal used to clock the PIC16F877A. This formula takes
into account the prescaler ratio and the fact that the instruction clock rate is a quarter of the crystal
frequency (see section 4.3.2). Rearranging the above equation in order to determine the number of
TMR0 counts to create a specific delay yields,
delay 0count
4 prescaler
osc f TMR
? ? ? ??
Remember that the maximum number of countsthe 8 bit Timer0 module can store before overflowing
is 256.
For example, if a 10 ms delay is required, with the prescaler divide down ratio set to 1:256, then the
required number of TMR0 counts is as follows.
3 6 delay 10 10 3.2768 10 0count 32
4 prescaler 4 256
osc f TMR
? ? ?? ? ?? ? ? ? ??
When 32 counts of the Timer0 module have occurred, we know that 10 ms have elapsed. We can use
this information to design a delay loops.
36
4.3.5 Timer0 configuration procedure
With the information shown in the previous section it is possible to derive a configuration procedure
for Timer0 as follows:
? Set TOCS = 0 to enable timer mode
? If using the prescaler, set PSA = 0. If not set PSA = 1 and skip the next step.
? Calculate required prescaler divide down ratio and write the appropriate 3 bit value to the
PS bit field.
4.4 Procedure
4.4.1 Exercise 1 ? Creating a simple delay using the Timer0 module
Create a new project in the MPLAB X IDE, create an empty source file, and populate it with
the following code listing.
// Filename: Lab4Ex1.c
// Version: 1.0
// Date:? ?


// Author:


//

// Description: Simple demonstration of a timer delay??

#include

?????????????// Required for all MPLAB XC8 source files

void main(void)

{

????unsigned char preload = 0x00;??// TMR0 preload variable

????// PORTD setup

????TRISD = 0x00;???????????????// Set PORTD all outputs

????PORTD = 0x00;???????????????// Clear PORTD

????

????//Timer0 setup

????OPTION_

Leave a Reply

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