Sie sind auf Seite 1von 5

MicroprocessorsandMicrocontrollersLaboratory D.I.C.I.

UNIVERSIDAD DE GUANAJUATO
D. I. C. I. S.

Microprocessors and Microcontollers


Laboratory
Laboratory Session 4:

Use of timer and 7-segment display.

GustavoCerdaVillafaa,SergioEduardoLedesmaOrozco

MicroprocessorsandMicrocontrollersLaboratory D.I.C.I.S

Laboratory Session 4:

USE OF TIMER AND 7-SEGMENT DISPLAY.

Objective:
Programing a microcontroller to use TMR0 timer and a lookup table for operating a 7-segment
display.

Resources:
1 PIC 18f45k50 microcontroller
1 7-segment display
1 5V power supply
1 MPLAB IDE software
1 Laptop
1 PIC Programmer
Various resistors and capacitors

4.1 Introduction
In this sesin a PIC 18F45K50 will be programmed to generate a counter which will be showed by using a 7-
segment display. The counter will be implemented using TIMER0. This timer has the option of using a
prescaler with ratios from 1:2 to 1:256. TIMER0 can operate as either, a timer or a counter in both 8-bit or 16-
bit modes.

TIMER0 timer/counter characteristics:


8 or 16 bits timer/counter
Readable and writable registers
Selectable clock source (internal or external)
Interrupt on overflow (from FFh to 00h)

TIMER0 is configured using T0CON register. All bits in this register are set by default.
Bit 7 Enables TIMER0.
Bit 6 configures TIMER0 as 8 or 16-bit timer/counter.
Bit 5 selects clock source: T0CKI pin (RA4) or internal instruction cycle clock
Bit 4 increments either by high-to-low or low-to-high transitions on T0CKI pin.
Bit 3 assigns prescaler to TIMER0
Bits 2-0 Selects prescaler ratios.

As for this session, if the clock frequency is set at 4 MHz and the prescaler ratio is 1:16, TIMER0 will
activate its interrupt every 1.048 seconds. Each interrupt will increase a register which will hold values from 0
to 9 before it is cleared to start again at 0. TIMER0 interrupt is activated loading value A0h in register
INTCON.
TIMER0 interrupt does not wake up the microcontroller when in Sleep mode because this timer is shut down
in this mode.
For working with a 7-segment display the binary value from the counter should be transformed to an output
value to show a decimal value in the 7-segment display. This is presented in the following table:

Decimal Binary RB6 RB5 RB4 RB3 RB2 RB1 RB0 Hexadecimal
value value A B C D E F G output
0 0000 1 1 1 1 1 1 0 7E
1 0001 0 1 1 0 0 0 0 30

GustavoCerdaVillafaa,SergioEduardoLedesmaOrozco

MicroprocessorsandMicrocontrollersLaboratory D.I.C.I.S

2 0010 1 1 0 1 1 0 1 6D
3 0011 1 1 1 1 0 0 1 79
4 0100 0 1 1 0 0 1 1 33
5 0101 1 0 1 1 0 1 1 5B
6 0110 1 0 1 1 1 1 1 5F
7 0111 1 1 1 0 0 0 0 70
8 1000 1 1 1 1 1 1 1 7F
9 1001 1 1 1 0 0 1 1 73

In order to present this value as an output through PORTB a lookup table generates the conversion using
instructions MULLW 0x02, ADDWF PCL,f and RETLW values. First instruction multiplies W register
times 2 due to each instruction requiring two lines from memory program. Second instruction adds the value
of W register to the Program Counter, making next instruction to be executed be the next one plus the value
added by W register. Last instruction returns from this routine loading the corresponding value to register W.

4.2 Procedure

A. For editing, debugging and simulating code, in order to program the microcontroller, it will be used the
graphical software MPLAB X IDE (Integrated Development Environment). After opening this program,
select, from File in the menu, the option New Project. In the windows that will emerge, we choose the default
options: Microchip Embedded and Standalone Project. In the next window Family: Advanced 8-bit
MCUs (PIC18), and Device: PIC18F45K50. The following window is left with the default values. In the
next window we select mpasm in order to compile using assembler code. In the next window we select the
Project Name and the location where the project and all its files will be saved, and then we press the button
Finish.

Once the new project is initiated, open a new window for writing code. This window should be saved with an
.asm extension. The generated file has to be loaded in the project window under Source Files. This can be
done with the mouse right button applied over Source Files.

The following code will be written in this new window and compiled.

; Program using TMR0 timer and a lookup table for operating


; a 7-segment display

LIST P = 18f45K50
#include<p18f45K50.inc>

CONFIG WDTEN = OFF ; Deshabilita el Watchdog


CONFIG MCLRE = ON ; Se habilita el pin MCLEAR
CONFIG DEBUG = OFF ; Deshabilita el modo Debug
CONFIG LVP = OFF ; Deshabilita Low-Voltage programming
CONFIG FOSC = INTOSCIO ; Habilita el oscilador interno

AUX1 EQU 0x00


AUX2 EQU 0x01

ORG 00000h
GOTO Start ; Goes to instruction after Start tag
ORG 00008h ; Sets next instruction in address 000008h
GOTO Inter

Start:
MOVLB 0x0F

GustavoCerdaVillafaa,SergioEduardoLedesmaOrozco

MicroprocessorsandMicrocontrollersLaboratory D.I.C.I.S

CLRF ANSELB,1
CLRF TRISB ; PORTB pins as outputs
CLRF PORTB ; Clear PORTB
MOVLW b'01010011' ; Configures OSCCON register
MOVWF OSCCON
MOVLW b'10000011'
MOVWF T0CON ; Enables TMR0 with a rate 1:16
MOVLW b'10100000'
MOVWF INTCON ; Enables TMR0 interrupt

MainLoop
MOVF AUX1,0
CALL Display
MOVWF PORTB
GOTO MainLoop

Display
MULLW 0x02
MOVF PRODL,W
ADDWF PCL,F
RETLW 0x7E
RETLW 0x30
RETLW 0x6D
RETLW 0x79
RETLW 0x33
RETLW 0x5B
RETLW 0x5F
RETLW 0x70
RETLW 0x7F
RETLW 0x73

Inter
MOVWF AUX2 ; Saves W register value
INCF AUX1
MOVLW 0x0A ; Verifies AUX1 value only reaches 9
SUBWF AUX1,W
BTFSC STATUS,2 ; Verifies if previous operation produces 0
CLRF AUX1
MOVF AUX2,W ; Returns original value to W register
BCF INTCON,2 ; Clears TMR0 interrupt flag
RETFIE
END

Once the code is written and saved, select option Build All in the menu.

When the program is compiled, select the file with the .hex extension for programming the microcontroller.
Ask for assistance in this step.

B. Once the PIC is programmed, it is placed in the proto-board and should be connected according to the
following diagram. Be careful when handling the PIC because it can be damaged by static electricity. VDD will
be set at 5V and VSS at 0V. Maximum current from each pin is 25 mA. If your LEDs cannot cope with this
ammount of current you should use a 220 resistor in series with the LED (as it is shown in the diagram).

GustavoCerdaVillafaa,SergioEduardoLedesmaOrozco

MicroprocessorsandMicrocontrollersLaboratory D.I.C.I.S

4.3 Activities
1. If you need to use TIMER0 as an exernal event counter using pin TOCKI (RA4) and with a prescaler value
of 1:64, find the value needed to be loaded in T0CON.

2. If you need to use TIMER1 instead of TIMER0, find and report all registers needed and values to be loaded
with. Remember that TIMER0 was configured for generating an interrupt every 1.048 seconds.

GustavoCerdaVillafaa,SergioEduardoLedesmaOrozco

Das könnte Ihnen auch gefallen