You are on page 1of 10

CENTRE OF DIPLOMA STUDIES

COMPUTER ADDED DESIGN LABORATORY

LABORATORY INSTRUCTION SHEET

Subject Code and Name

DEK 3133
MICROCONTROLLER

Experiment Code

03

Experiment Title

Introduction to functions, conditions and


Hardware Timer using TMR0

Course Code

DET/DEE/DEX

Document Reference
No.

RPP-05

Document Title

LABORATORY
PRACTICUM

Page. Number
Edition

Page |1
1

Revision No.

Effective Date

27/7/2010

Amendment Date

27/7/2010

SUBJECT INFORMATION
SUBJECT

: DEK 3133 MICROCONTROLLER

TOPIC

: Lab 3 Introduction to functions, conditions and Hardware Timer using TMR0

AIM

: Learn the further programming technique using C language

OBJECTIVES
1.1

To understand the concept of programming in C using functions and conditions

1.2

To learn how to use TMR0 as a hardware timer

EQUIPMENT

2.1

PIC Development Board PICDEV

2.2

MPLAB IDE Program

2.3

Mikro C

2.4

Proteus

2.5

The PIC Development Board User manual

2.6

Power supply 9V

Document Reference
No.

Document Title

RPP-05
LABORATORY
PRACTICUM

Page. Number
Edition

Page |2
1

Revision No.

Effective Date

27/7/2010

Amendment Date

27/7/2010

THEORY
3.1

Functions

3.2

Conditions

3.3 Using TMR0


The special file register 01, Timer Zero (TMR0), which can be used as a counter or timer which, once
started, run independently of the program execution. This mean it can count inputs or clock pulses

Document Reference
No.

Document Title

RPP-05
LABORATORY
PRACTICUM

Page. Number
Edition

Page |3
1

Revision No.

Effective Date

27/7/2010

Amendment Date

27/7/2010

concurrently with the program. The counter/timer can also be set up to generate an interrupt when it has
reached its maximum value, so that the main program does not have to keep checking it to see if a
particular count has been reached.
3.4

REGISTERS ASSOCIATED WITH TIMER0

TMR0
o 8 bit TMR0 Module Register
o Count from 0 to 255 (00h to FFh)

INTCON

o GIE - Global Interrupt Enable bit - controls all possible interrupt sources
simultaneously.
1 - Enables all unmasked interrupts.
0 - Disables all interrupts.
o

T0IE - TMR0 Overflow Interrupt Enable bit controls interrupt enabled by TMR0 overflow.
1 - Enables the TMR0 interrupt.
0 - Disables the TMR0 interrupt.
T0IF - TMR0 Overflow Interrupt Flag bit registers the timer TMR0 register overflow, when
counting starts from zero.
1 - TMR0 register has overflowed (bit must be cleared in software).
0 - TMR0 register has not overflowed.

Document Reference
No.

Document Title

RPP-05
LABORATORY
PRACTICUM

Page. Number
Edition

Page |4
1

Revision No.

Effective Date

27/7/2010

Amendment Date

27/7/2010

OPTION
o TOCS Clock Select bit (Bit 5)
1 Pulses are brought to TMR0 timer/counter input through the RA4 pin
0 Internal cycle clock (Fosc/4)
o

TOSE Source Edge Select bit (Bit 4)


1 Increment on high to low transition on TMR0 pin
0 increment on low to high transition on TMR0 pin
PSA Prescaler Assignment bit (Bit 3)
1 Prescaler is assigned to the WDT
0 Prescaler is assigned to the TMR0 timer/counter
PS2 (Bit 2), PS1(Bit 1), PS0 (Bit 0) Prescaler Rate Select bit

PS2

PS1

PS0

TMR0

WDT

1:2

1:1

1:4

1:2

1:8

1:4

1:16

1:8

1:32

1:16

1:64

1:32

1:128

1:64

1:256

1:128

PreparedBy:

Signature:
Name:MohamadBinMd.Som
Date:27July2010

Approvedby:

Signature:
Name:ShamsulB.Mohamad
Date:27July2010

Document Reference
No.

Document Title

LABORATORY
PRACTICUM

Page. Number
Edition

Page |5
1

Revision No.

Effective Date

27/7/2010

Amendment Date

27/7/2010

ATTENTION
4.1

RPP-05

Do not move any IC or device inside the board without any order from your instructor.

EXPERIMENT PROCEDURE
5.1
5.1.1

Functions and Conditions


Baseonthecircuitbelow(circuit1),keyinthegivenCcodeandsimulateitusing
Proteus.Writeyourobservations.

Circuit 1

Document Reference
No.

Document Title

#define
#define
#define
#define

BUTTON1
BUTTON2
BUTTON3
BUTTON4

RPP-05
LABORATORY
PRACTICUM

Page. Number
Edition

Page |6
1

Revision No.

Effective Date

27/7/2010

Amendment Date

27/7/2010

PORTA.F0
PORTA.F1
PORTA.F2
PORTA.F3

void pattern1(void)
{
PORTB = 0b11111111;
}
void pattern2(void)
{
PORTB = 0b10101010;
}
void pattern3(void)
{
PORTB = 0b11110000;
}
void pattern4(void)
{
PORTB = 0b00001111;
}

//function for pattern 1

//function for pattern 2

//function for pattern 3

//function for pattern 4

void main(void)
{
ADCON1 = 0b00000110; //Set ADCON1 for Port A as a digital input
TRISA = 0b11111111; //Port A as input port
TRISB = 0b00000000; //Port B as output port
PORTB = 0b00000000; //Clear PORTB at start up
while(1) //endless loop
{
if (BUTTON1 == 1) //Test button at PORTA bit 0 if pressed.
pattern1();
//call function pattern1
else if (BUTTON2 == 1)
pattern2();
//call function pattern2
else if (BUTTON3 == 1)
pattern3();
//call function pattern3
else if (BUTTON4 == 1)
pattern4();
//call function pattern4
else
PORTB = 0b00000000; //All LED off if no button pressed
} //end of while (endless loop)
}//end of main func
5.2

Assignment 1.

Base on the Circuit 1, Modify the C code so that each button have a different pattern of animated LEDs. Use 1
second delay for each changing pattern. Test your result in Proteus and put your modifying code in the report.

Document Reference
No.

RPP-05
LABORATORY
PRACTICUM

Document Title

5.3

Page. Number
Edition

Page |7
1

Revision No.

Effective Date

27/7/2010

Amendment Date

27/7/2010

Using TMR0 as a Hardware Timer

Circuit 2
5.3.1

Baseonthefigureabove(Circuit2),thePICuse4Mhzforitsclockspeed.Writethe
programtoblinktheLEDeveryonesecondbyusingTMR0.Thecodeisshownbelow.

#define
#define
#define
#define
#define
#define
#define

LED1 PORTB.F0
TOCS OPTION_REG.F5
TOSE OPTION_REG.F4
PSA OPTION_REG.F3
PS2 OPTION_REG.F2
PS1 OPTION_REG.F1
PS0 OPTION_REG.F0

#define GIE INTCON.F7


#define TMR0IE INTCON.F5
#define TMR0IF INTCON.F2
//Public Variable
unsigned int overflow;
void interrupt(void)
//Interrupt subroutine
{
if (TMR0IE == 1 && TMR0IF == 1) //TMR0 made interrupt

Document Reference
No.

Document Title

RPP-05
LABORATORY
PRACTICUM

Page. Number
Edition

Page |8
1

Revision No.

Effective Date

27/7/2010

Amendment Date

27/7/2010

{
overflow++;
TMR0IF = 0;
TMR0IE = 0;

//clear interrupt flag


//Disable TMR0 interrupt

if (overflow == 15) //TMR0 is overflow about 15 times ~~ 1 second.


{
LED1 = ~LED1;
overflow = 0;
//reset overflow to 0
}
TMR0IE = 1;
//enable TMR0 interrupt
TMR0 = 0;
//restart TMR0 value to 0
}
}
void main(void)
{
overflow = 0;
//port setup
TRISB = 0; //port B is output
PORTB = 0; //initial value for PORTB is 0
//TMR0 setup
TOCS = 0; //Internal cycle clock (Fosc/4)
PSA = 0;
//Prescaler is assinged to the TMR0 timer
PS2 = 1;
PS1 = 1;
PS0 = 1;
//Prescaler rate is selected to 1:256
TMR0 = 0; //Initial value for TMR0 is 0
//Interrupt Setup
GIE = 1;
//Enable all interrupt
TMR0IE = 1; //Enable the TMR0 interrupt
while(1);

//infinite loop

5.3.2

5.4
5.4.1

Burnthe*.hexfileintoPICandtestthePICatthedevelopmentboard.Writeyour
observationinthereport.
Assignment 2
WriteaCprogramsothattheLEDisblinkingusinghardwaredelaywhichisblinking
every1second.AssumePICclockspeedis1MHzandprescalerusedis1:256.Simulate
yourresultusingProteus.

Document Reference
No.

Document Title

RPP-05
LABORATORY
PRACTICUM

Page. Number
Edition

Page |9
1

Revision No.

Effective Date

27/7/2010

Amendment Date

27/7/2010

REPORT PREPARATION AND SCHEMA.

(1)

2 persons for 1 report.

(2)

Due date to send report is 1 weeks after lab date.

(3)

Report schema following below requirements:


Lab report cover sheet for 1st page.
Objective, theory, equipments for the 2nd page. (5)

(5M)

Observations. (20)
1. Observations from 5.1.1 (Proteus Simulation)

(10 M)

2. Observations from 5.3.2 (TMR0 run on development board)

(10 M )

Result. (35)
1. Assignment 1 source code & Flow Chart

( 15 M )

2. Assignment 2 source code & Proteus Simulation

( 20 M )

Discussion. (25)
1. List the Registers related to TMR0 and describes the procedure to setup the TMR0 (15M)
2. If the PIC is supplied with 1 MHz clock speed, calculate time taken for TMR0 to
overflow. Assume that a prescaler 1:256 is used. Show the calculation in your report
(10 M)

Conclusions. (15)

You might also like