Microcontroller Basics

  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Microcontroller Basics as PDF for free.

More details

  • Words: 8,852
  • Pages: 63
INTRODUCTION TO PIC MICROCONTROLLERS • The PIC was originally designed as a Peripheral Interface Controller (PIC) for a 16-bit microprocessor. • It was essentially an I/O controller and was designed to be very fast. • It had a small microcoded instruction set. • The PIC16F84 (or PIC16F84A) microcontroller is unique because its program memory is made using Flash technology. • It can be programmed, tested in-circuit and reprogrammed if necessary in a matter of a few minutes and without the need for a UV EPROM eraser. • It is a small device (I8-pin), readily available to all including hobbyists and students at a cost of $6.00 (at this writing) in single quantity. • Think of the PIC16F84 as a custom I/O handler. It looks at its inputs and, based on what it sees, it sends signals out its outputs. You can customize it to do what you want via programming. • It is not a heavy duty number cruncher or data manipulator. • The MPASM tm assembler from Microchip Technology (manufacturer of PIC family) is the PIC assembler of choice for use in this notes because: • It is free from Microchip. Most people who are into PICs speak "MPASM". • Most examples in magazines and on the Internet are written in the MPASM dialect. • Microchip makes the PICSTART Plus (tm) programmer which will program all PIC microcontrollers. • The PIC16F84, MPASM assembler (either stand alone or as part of MPLAB) and a low cost PIC16F84 programmer make a very inexpensive development system and a great way to get started creating microcontroller based projects. • What you learn using the PIC16F84 is directly applicable to the whole line of PIC microcontrollers. • Learning how PICs work and how to apply them involves study in three areas: • IBM compatible computer use (as needed). • Assembler • PIC itself.

EE313 Microprocessor Applications

1

R Singh

• PICs are not easy to write code for by hand because they are not intuitive. Many instructions require setting or clearing a bit to specify the destination for the result of executing the instruction. • This is OK as an initial learning experience, but would become tedious very soon. So use of "power tools" is essential. This means learning to use an IBM compatible computer if you haven't already done so. It also means learning to use an assembler which converts English-like readable instructions into machine language understood by the PIC itself. • Finally, learning about the PIC's inner workings is possible once use of the power tools is understood. • The object is to make this process as easy and enjoyable as possible. Once you get through this and you have programmed a PIC16F84 for the first time, a whole new-world awaits. You will be able to create more interesting projects and have more fun! • The usual approach to teaching the use of the PIC has been to get into all of the assembler commands and then show advanced examples. There has been no simple example showing how to get started. As usual, only 5 percent of this information is needed to get started, but which 5 percent. • The approach taken in this course will be to give you the 5 percent you need to get going.

PIC16F84 PRODUCT OVERVIEW This course uses the PICl6F84 as examples to get you started. The emphasis is on the mid-range (14-bit core) product. The 12-bit core designates the instruction word length. You really won't care how many bits there are in an instruction word, but it is the basis on which Microchip divides up their product line. The following brief tables are for reference:

EE313 Microprocessor Applications

2

R Singh

1.1 PARTS IS PARTS • As the Microchip microcontroller product line grows, and I am glad that it is, the part numbering convention/system seems to be getting a bit involved. • The microcontroller product line designation has been changed from PIC16/17 TM to PICmicro TM. • The real key to sanity here is to recognize that there are three fundamental product groups in terms of how they function and, to some extent, how program code must be written for them. • The three product groups have 12-bit, 14-bit and 16-bit cores. • We don't really care how many bits are in an instruction word. We just need to know which set of rules and features apply when writing code for a selected chip. Here are some popular examples:

EE313 Microprocessor Applications

3

R Singh

PIC16F84 PINS and FUNCTIONS

• The PICI6F84 is fabricated using CMOS technology. It consumes very little power and is fully static meaning that the clock can be stopped and all register contents will be retained. • The maximum sink or source current for a port at any given time is as follows:

• Supply current is primarily a function of operating voltage, frequency and I/O pin loading and is typically 2 mA or so for a 4MHz clock oscillator. This drops to approximately 40 microamps in the sleep mode. • Because the device is CMOS, all inputs must go somewhere. All unused inputs should be pulled up to the supply voltage (usually +5 VDC). EE313 Microprocessor Applications

4

R Singh

PACKAGE • The PIC16F84 is available in an 18-pin DIP package suitable for the experimenter. The part number is PICI6F84-04/p for the 4 MHz part. CLOCK OSCILLATOR • Four different types of clock oscillators may be used with PICI6F84 parts in general. The clock oscillator types are: • RC -resistor/capacitor. • XT -crystal or ceramic resonator -external clock. • HS -high speed crystal or ceramic resonator -external clock. • LP -low power crystal –external clock. • At programming time, the PIC must be told via configuration bits what type of oscillator it will be connected to. • The details of various oscillator circuits and components are given in the Microchip data book available online. • One possible clock circuit for experimentation is a 4 MHz crystalcontrolled oscillator (type XT). They are accurate, always start and the math required for designing time delay loops is simple. • The external clock frequency is divided by 4 internally, so the internal clock frequency is 1MHz. • Almost all instructions are executed in one instruction (internal clock) cycle or 1microsecond in this case. This is reasonably fast for experimenting purposes.

EE313 Microprocessor Applications

5

R Singh

• Ceramic resonators with built-in capacitors are small and inexpensive while providing good accuracy (+/- 1.3 percent or better). They look a lot like a small ceramic capacitor except for the fact they have three leads. A typical device looks like this:



An RC clock circuit may also be used where cost is an issue and timing accuracy is not. The oscillator type code is RC.

CONFIGURATIONBITS The configuration bits are located in Flash memory outside the main part of Flash memory used for program storage. There are five configuration bits. Two select clock oscillator type, one is the watchdog timer enable bit, one is the power-up timer enable bit and one is the code protection bit. The device programmer accesses these bits during the device programming procedure.

EE313 Microprocessor Applications

6

R Singh

RESET •

The PICl6F84 has built-in power-on reset which works as long as the power supply voltage comes up quickly. Commonly the MCLR pin is merely tied to the power supply. A switch may be used to regain control if things run away.

PORTS • Port A is 5 bits/lines wide • Port B is 8 bits/lines wide or byte-wide. Each port line may be individually programmed as an input line or output line. This is done using a special instruction, which matches a bit pattern with the port lines. A "0" associated with a port line makes it an output, a" 1" makes it an input. The PIC16F84 has port data direction registers as many other microcontrollers do. Using the port data direction registers involves file register bank switching which we will avoid for now. So the initial programming examples will use an obsolete instruction (TRIS) to avoid having to deal with bank switching at this early stage. A benefit of this is backward compatibility. You will need to learn to use this technique if you choose to work with base-line parts (PIC16C5X series). • The Port B lines have weak pullup resistors on them which may be enabled or disabled under software control. • All 8 resistors are enabled/disabled as a group via the RBPU bit in the option register. The pullup resistor on an individual port line is EE313 Microprocessor Applications

7

R Singh

automatically turned off when that line is configured as an output. The pullups are disabled on power-on reset. • Port A, bit 4 is shared with the external timer/counter input called T0CKI. As a digital input line, the input is Schmitt trigger. As a digital output line, it is open drain, so a pullup resistor is required. The output cannot source current, it can only sink current. • All unused port lines should be tied to the power supply (CMOS rule -all inputs must go somewhere). • On reset, all port lines are inputs.

SPECIAL FEATURES Watchdog Timer The watchdog timer is useful in some control applications where a runaway program could cause a safety problem. We will not deal with it except to say that it is important to select "watchdog timer off' when programming the configuration bits. Power-up Timer The power-up timer should be selected "ON" when programming the configuration bits. Sleep Mode The feature of the "sleep mode" is to drastically reduced power consumption achieved by turning off the main clock oscillator.

EE313 Microprocessor Applications

8

R Singh

2.0 The PIC16F84 Microcontroller The block diagram for the PIC16F84 microcontroller is shown in Figure 2.0.

Figure 2.0: PIC16F84 block diagram. (Courtesy of Microchip Technology Inc.)

2.1 Features of PIC16F84 • 8-bit wide data bus CMOS microcontroller • 18-pin DIP, SOIC • 1792 bytes of flash EEPROM program memory subdivided into 14-bit words (0h–3Fh) • 68 bytes of RAM data memory • 64 bytes of nonvolatile EEPROM data memory • 1024 (1 k) instructions capability. • 4 MHz clock speed (max. 10 MHz) • 15 special function hardware registers • 36 general purpose registers (SRAM) • 13 I/O pins (5-PORT A , 8-PORT B) 2.2 The PIC16F84 Architecture PICs use the Harvard architecture since its program memory and data memory are accessed from different memories. This offers an improvement over the von Neumann architecture in which the program and data are

EE313 Microprocessor Applications

9

R Singh

accessed simultaneously from the same memory (accesses over the same bus). 2.3 Memory Organization There are two memory blocks in the PIC16F84. • Program memory • Data memory. Each memory-block has its own bus to allow simultaneous accessing during the same oscillator cycle. This is the Harvard architecture, which improves bandwidth over traditional von Neumann architecture. Program Memory (Flash) Organization The PIC16F8X has eight stack levels that can page each program memory space of 1K so that the total memory space of 8K can be utilized as shown in Figure 2.3. The first 1Kx14 (0000h – 03FFh) are physically implemented as shown. Conceptually, the configuration is similar to layers used in commercial drafting packages. The reset vector is at 0000h and the interrupt vector is at 0004h location. Accessing a location above the physically implemented address will result in wrap around. For example, locations 10h, 410h, 810h, C10h, 1010h, 1410h, 1810h, 1C10h will be the same instructions in each of the eight pages, each being 1K different from the other. This is because 400h = 1024 (1K) 800h = 2048 (2K) C00h = 3072 (3K) 1000h = 4096 (4K) 1400h = 5120 (5K) 1800h = 6144 (6K) 1C00h = 7164 (7K)

EE313 Microprocessor Applications

10

R Singh

Figure 2.3: PIC16F84 program memory organization

Data Memory (RAM) Organization The PIC16F8X data memory is partitioned into two areas as shown in Figure 2.4. • 12 special function registers (SFRs) • 68 general purpose registers (GPRs within the RAM) The special function registers (SFRs) are used by the CPU and peripheral functions to control the device operation (these registers are SRAM) and occupy the first 12 file addresses corresponding to 80h (128) up to 8Bh (139), i.e. (128, 129, .., 138, 139). The general purpose registers (GPRs) commence from 8Ch (140) up till CFh (207), so that there are a total of (207 – 140 + 1) = 68. The unimplemented data memory locations commence from D0h (208) up till FFh (255), so that there are a total of (255 – 208 + 1) = 48. There are two banks: banks 0 and 1. For bank 0, the 12 SFRs are from 00h (0) to 0Bh (11) or 11 – 0 + 1 = 12 addresses. The 68 GPRs are from 0Ch EE313 Microprocessor Applications

11

R Singh

(12) to 4Fh (79) or (79 – 12 + 1) = 68, the unimplemented are from 50h (80) to 7Fh (127) or (127 – 80 + 1) = 48.

Figure 2.4: PIC16F84 data memory organization

EE313 Microprocessor Applications

12

R Singh

Bank 0 and Bank 1 Mapping and Selection The GPR addresses in bank 1 are mapped to addresses in bank 0. For example, address location 4Fh and CFh will access the same GPR.

3.0 Special Features of the PIC CPU • OSC selection • Reset • Interrupts • Watchdog Timer (WDT) • SLEEP • Code Protection • ID locations • In-circuit serial programming

3.1 Working (W) register or accumulator When a program is compiled and downloaded to a PIC, it is stored as a set of binary machine code instructions in the flash program memory. These instructions are sequentially fetched from memory, placed in the instruction register, and executed. Each instruction corresponds to a low-level function implemented with logic circuits on the chip. For example, one instruction might load a number stored in RAM or EEPROM into the working register, which is also called the W register or accumulator; the next instruction might command the ALU to add a different number to the value in this register; and the next instruction might return this summed value to memory. Since an instruction is executed every four clock cycles, the PIC16F84 can do calculations, read input values, store and retrieve information from memory, and perform other functions very quickly. With a clock speed of 4 MHz an instruction is executed every microsecond and 1 million instructions can be executed every second. EE313 Microprocessor Applications

13

R Singh

The microcontroller is referred to as 8-bit, because the data bus is 8 bits wide, and all data processing and storage and retrieval occur using bytes. 3.2 File Registers The RAM, in addition to providing space for storing data, maintains a set of special purpose byte-wide locations called file registers. The bits in these registers are used to control the function and indicate the status of the microcontroller. 3.3 Watch-dog timer A useful special purpose timer, called a watch-dog timer, is included on PIC microcontrollers. This is a count-down timer that, when activated, needs to be continually reset by the running program. If the program fails to reset the watch-dog timer before it counts down to 0, the PIC will automatically reset itself. In a critical application, you might use this feature to have the microcontroller reset if the software gets caught in an unintentional endless loop. 3.4 PORTS The PIC16F84 is packaged on an 18-pin DIP IC that has the pin schematic (pinout) shown in Figure 3.4. The figure also shows the minimum set of external components recommended for the PIC to function properly. Table 3.4 lists the pin identifiers in natural groupings, along with their descriptions. The five pins RA0 through RA4 are digital I/O pins collectively referred to as PORTA, and the eight pins RB0 through RB7 are digital I/O pins collectively referred to as PORTB. In total, there are 13 I/O lines, called bidirectional lines because each can be individually configured in software as an input or output. PORTA and PORTB are special purpose file registers on the PIC that provide the interface to the I/O pins. Although all PIC registers contain 8 bits, only the 5 least significant bits (LSBs) of PORTA are used.

EE313 Microprocessor Applications

14

R Singh

Figure 3.4: PIC16F84 pin-out and required external components

TABLE 3.4

3.5 Interrupt

EE313 Microprocessor Applications

15

R Singh

An important feature of the PIC, available with most microcontrollers, is its ability to process interrupts. An interrupt occurs when a specially designated input changes state. When this happens, normal program execution is suspended while a special interrupt handling portion of the program is executed. In the PIC16F84, pins RB0 and RB4 through RB7 can be configured as interrupt inputs. Power and ground are connected to the PIC through pins Vdd and Vss. The dd and ss subscripts refer to the drain and source notation used for MOS transistors, since a PIC is a CMOS device. The voltage levels (e.g., Vdd = 5V and Vss = 0 V) can be provided using a DC power supply or batteries (e.g., four AA batteries in series or a 9-V battery connected through a voltage regulator). The master clear pin ( MCLR ) is active low and provides a reset feature. Grounding this pin causes the PIC to reset and restart the program stored in EEPROM. This pin must be held high during normal program execution. hence connected with a pull-up resistor. To provide a manual reset feature to a PIC design, you can add a normally open (NO) pushbutton switch as shown in Figure 3.5. Closing the switch grounds the pin and causes the PIC to reset.

Figure 7.8 Reset switch circuit

4.0 Programming a PIC using Assembly Language

EE313 Microprocessor Applications

16

R Singh

Various software development tools can assist in testing and debugging assembly language programs written for a microcontroller. Simulator: This is software that runs on a PC and allows the microcontroller code to be simulated (run) on the PC. Most programming errors can be identified and corrected during simulation. Emulator: This is a hardware that connects a PC to the microcontroller in a prototype control system. It usually consists of a printed circuit board connected to the control system through ribbon cables. The emulator can be used to load and run a program on the actual microcontroller attached to the control system hardware (containing sensors, actuators, and control circuits). The emulator allows the PC to monitor and control the operation of the microcontroller while it is embedded in the control system. Instruction Set The assembly language used to program a PIC16F84 consists of 35 commands that control all functions of the PIC. This set of commands is called the instruction set for the microcontroller. The complete instruction set and brief command descriptions for the PIC16F84 are listed in Table 4.0. Each command consists of a name called the mnemonic and, where appropriate, a list of operands. Values must be provided for each of these operands. The letters f, d, b, and k correspond, respectively, to a file register address (a valid RAM address), result destination (0: W register, 1: file register), bit number (0 through 7), and literal constant (a number between 0 and 255). Note that many of the commands refer to the working register W, also called the accumulator. This is a special CPU register used to temporarily store values (e.g., from memory) for calculations or comparisons.

TABLE 4.0

EE313 Microprocessor Applications

17

R Singh

EE313 Microprocessor Applications

18

R Singh

BCF f, b (read BCF as “bit clear f”) clears bit b in file register f to 0, where the bits are numbered from 0 (LSB) to 7 (MSB) For example, BCF PORTB, 1 makes bit 1 in PORTB go low (where PORTB is a constant containing the address of the PORTB file register). If PORTB contained the hexadecimal (hex) value FF (binary 11111111) originally, the final value would be hex FC (binary 11111101). If PORTB contained the hex value A8 (binary 10101000) originally, the value would remain unchanged. MOVLW k (read MOVLW as “move literal to W”) stores the literal constant k in the accumulator (the W register) For example, MOVLW 0xA8 would store the hex value A8 in the W register. In assembly language, hexadecimal constants are identified with the 0x prefix. RLF f, d (read RLF as “rotate f left ”) shifts the bits in file register f to the left 1 bit, and stores the result in f if d is 1 or in the accumulator (the W register) if d is 0. The value of the LSB will become 0, and the original value of the MSB is lost. For example, if the current value in PORTB is hex 1F (binary 00011111), then RLF PORTB, 1 would change the value to hex 3E (binary 00111110). SWAPF f, d (read SWAPF as “swap nibbles in f”) exchanges the upper and lower nibbles (a nibble is 4 bits or half a byte) of file register f and stores the result in f if d is 1 or in the accumulator (the W register) if d is 0 For example, if the memory location at address hex 10 contains the value hex AB, then SWAPF 0x10, 0 would store the value hex BA in the W register. SWAPF 0x10, 1 would change the value at address hex 10 from hex AB to hex BA.

EE313 Microprocessor Applications

19

R Singh

5.0 FLOWCHATS to represent Programs

Figure 5.0 FLOWCHART symbols

EE313 Microprocessor Applications

20

R Singh

PROGRAMMING EXERCISE 1 You are required to write an assembly program such that PORTA and PORTB of the PIC16F84 IC become output ports. Then output data 01H and 0FH at PORTA and PORTB respectively. Continue this operation in an infinite loop. • We first need to define the worded problem as a flow chart. START

(HEADER) list p=16F84 radix hex

Set PORTA as OUTPUT Set PORTB as OUTPUT

Clear PORTA Clear PORTB

Output 0FH at PORTB Output 01H at PORTA

END

EE313 Microprocessor Applications

21

R Singh

• Next we can attempt to write the assembly program ;============EE313L1.ASM========================10/03/04================================= ; ;This program would teach you the basics of making PORTA and PORTB Ouput ports ;and using it to turn on and off respective LEDs. ; ;---------------------------------------------------------------------------------------; (HEADER) /Assembler Directives/ ; list p=16f84 radix hex ;----------------------------------------------------------------------; (EQUATES) ; porta portb

equ equ

0x05 0x06

;equate "porta" to point to file register (SPR) 0x05 ;equate "portb" to point to file register (SPR) 0x06

;-----------------------------------------------------------------------;(ORIGIN) ; org 0x000 ;program originates at location 0x00 ;--------------------------------------------------------------------------------------------; (MAIN PROGRAM) ; start

go

movlw tris

0x00 porta

;load W register with 0x00 ;copy W to tristate buffer of PRORTA. PORTA set as output

movlw tris

0x00 portb

;load W register with 0x00 ;copy W to tristate buffer of PRORTB. PORTB set as output

clrf clrf

porta portb

;clear all pins of porta to low ;clear all pins of portb to low

movlw movwf movlw movwf

0x0F portb 0x01 porta

;laod W ;output ;laod W ;output

goto

go

;repeat

register with 0x0F W to portb register with 0x01 W to portb

;---------------------------------------------------------------------------------------;(END OF MAIN PROGRAM) ; end ;----------------------------------------------------------------------------------------

EE313 Microprocessor Applications

22

R Singh

PROGRAMMING EXERCISE 2 You are required to write an assembly program such that PORTA and PORTB of the PIC16F84 IC become output ports. Then output data 05H and E0H at PORTA and PORTB respectively. Continue this operation in an infinite loop. • We first need to define the worded problem as a flow chart. START

(HEADER) list p=16F84 radix hex

Set PORTA as OUTPUT Set PORTB as OUTPUT

Clear PORTA Clear PORTB

Output 05H at PORTA Output E0H at PORTB

END

EE313 Microprocessor Applications

23

R Singh

• The assembly program can be written as follows ;============EE313L2.ASM========================10/03/04================================= ; ;This program would teach you the basics of making PORTA and PORTB Output ports ;and using it to turn on and off respective LEDs. ; ;---------------------------------------------------------------------------------------; (HEADER) /Assembler Directives/ ; list p=16f84 radix hex ;---------------------------------------------------------------------------------------; (EQUATES) ; porta portb

equ equ

0x05 0x06

;equate porta to point to file register (SPR) 0x05 ;equate portb to point to file register (SPR) 0x06

;---------------------------------------------------------------------------------------;(ORIGIN) ; org 0x000 ;program originates at location 0x00 ;------------------------------------------------------------------------------------------------; (MAIN PROGRAM) ; start movlw 0x00 ;load W register with 0x00 tris porta ;copy W to tristate PRORTA --> PORTA set as output movlw tris

0x00 portb

;load W register with 0x00 ;copy W to tristate PRORTB --> PORTA set as output

clrf clrf

porta portb

;clear all lines of porta to low ;clear all lines of portb to low

bsf bsf bsf bsf bsf

porta,0 porta,2 portb,5 portb,6 portb,7

;set ;set ;set ;set ;set

goto

go

;repeat

go bit bit bit bit bit

0 2 5 6 7

of of of of of

porta porta portb portb portb

file file file file file

register register register register register

;---------------------------------------------------------------------------------------;(END OF MAIN PROGRAM) ; end ;----------------------------------------------------------------------------------------

EE313 Microprocessor Applications

24

R Singh

PROGRAMMING EXERCISE 3 You are required to write an assembly program such that PORTA (RA0) becomes an input and PORTB (RB0) an output port respectively. Next, connect a SWITCH (Switch 5) to RA0 and a LED (Led1) to RB0. Write an assembly program so that the LED turns on each time the switch is pressed. •

We first need to define the worded problem as a flow chart. START

(HEADER) list p=16F84 radix hex

Set PORTA (RA0) as INPUT Set PORTB (RB0) as OUTPUT

Clear PORTA Clear PORTB

Is SWITCH Pressed ?

N

Y

Turn ON LED

END

EE313 Microprocessor Applications

25

R Singh

• The assembly program can be written as follows ;============EE313L3.ASM========================10/03/04================================= ; ;This program would teach you the basics of making PORTA an input port and PORTB an ;output port. A SWITCH5 would be connected to RA0 which when pressed would turn on LED1 ;connected to RB0. ; ;---------------------------------------------------------------------------------------; (HEADER) /Assembler Directives/ ; list p=16f84 radix hex ;---------------------------------------------------------------------------------------; (EQUATES) ; porta portb

equ equ

0x05 0x06

;equate porta to point to file register (SPR) 0x05 ;equate portb to point to file register (SPR) 0x06

;---------------------------------------------------------------------------------------;(ORIGIN) ; org 0x000 ;program originates at location 0x00 ;---------------------------------------------------------------------------------------; (MAIN PROGRAM) ; movlw 0x01 ;load W register with 0x01 tris porta ;copy W to tristate PRORTA --> PORTA RA0 set as input movlw tris

0xFE portb

;load W register with 0xFE ;copy W to tristate PRORTB --> PORTB RB0 set as output

start

clrf clrf

porta portb

;clear all lines of porta to low ;clear all lines of portb to low

begin

btfss goto

porta,0 ; if switch5 at RBO pressed skip next line else test for ;switch pressed. begin ;goto begin

bsf

portb,0 ;turn on LED1 connected to RB0

goto

start

;repeat

;------------------------------------------------------------------------;(END OF MAIN PROGRAM) ; end ;-------------------------------------------------------------------------

EE313 Microprocessor Applications

26

R Singh

PROGRAMMING EXERCISE 4 You are required to write an assembly program such that PORTA (RA0) becomes an input and PORTB (RB0) an output port respectively. Next, connect a SWITCH (Switch 5) to RA0 and a LED (Led1) to RB0. Write an assembly program so that the LED flickers (on/off) each time the switch is pressed. •

We first need to define the worded problem as a flow chart. START

(HEADER) list p=16F84 radix hex

Set PORTA (RA0) as INPUT Set PORTB (RB0) as OUTPUT

Clear PORTA Clear PORTB

Is SWITCH Pressed ?

N

Y Turn ON LED

DELAY Turn OFF LED DELAY END

EE313 Microprocessor Applications

27

R Singh

• The assembly program can be written as follows ;============EE313L4.ASM========================10/03/04============================== ; ;This program would teach you the basics of making PORTA an input port and PORTB an ;output port and using a delay subroutine to flicker a LED. ;A SWITCH5 would be connected to RA0 which when pressed would flicker LED1 connected ;to RB0. ; ;------------------------------------------------------------------------------------; (HEADER) /Assembler Directives/ ; list p=16f84 radix hex ;----------------------------------------------------------------------; (EQUATES) ; porta portb count1 count2

equ equ equ equ

0x05 0x06 0x0C 0x0D

;equate porta to point to file register (SPR) 0x05 ;equate portb to point to file register (SPR) 0x06

;-----------------------------------------------------------------------;(ORIGIN) ; org 0x000 ;program originates at location 0x00 ;------------------------------------------------------------------------------------; (MAIN PROGRAM) ; movlw 0x01 ;load W register with 0x01 tris porta ;copy W to tristate PRORTA --> PORTA RA0 set as input movlw tris

0xFE portb

;load W register with 0xFE ;copy W to tristate PRORTB --> PORTB RB0 set as output

start

clrf clrf

porta portb

;clear all lines of porta to low ;clear all lines of portb to low, turn off LED1

begin

btfss goto

porta,0 ; if switch5 at RBO pressed skip next line else test for ;switch pressed. begin ; goto begin

bsf call bcf call

portb,0 delay portb,0 delay

;turn on LED1 connected at RB0 ; delay for (256 x 256 micro second) ;turn off LED1 connected at RB0 ;delay for (256 x 256 micro second)

goto

start

;repeat by going to begin

;------------------------------------------------------------------------;(SUBROUTINE PROGRAM) delay movlw 0xFF ;load Decimal FF in W register movwf count1 ;load Count1 with contents of W register loadC2 movlw 0xFF ;load Decimal FF in W register movwf count2 ;load Count2 with contents of W register decC2 decfsz count2,f;decrement counter2 and skip next line if Z=0 goto decC2 ; if count2 not 0 than go to decC2 decfsz count1,f;decrement counter1 and skip next line if Z=0 goto loadC2 ; if count1 not 0 than go to loadC2 return ;else jump out of subroutine

;------------------------------------------------------------------------;(END OF MAIN PROGRAM) ; end ;-------------------------------------------------------------------------

EE313 Microprocessor Applications

28

R Singh

6.0 MINI-PROJECT 1 Using all the experience gained from the four programming exercises covered in class, you are required to complete a small project. First connect a push button switch SW1 to PORTB (RB0) and two LEDs to PORTA i.e. LED1 to RA0 and LED2 to RA1 as shown in Figure 1.0. Write a simple assembly program so that each time SW1 is pressed LED1 turns ON. Once SW1 has been pressed 5 times LED2 should also turn ON. Repeat this process.

Figure 6.0 Circuit diagram for Mini-Project 1

EE313 Microprocessor Applications

29

R Singh

7.0 SWITCH DEBOUNCE When a button is opened or closed, we perceive that it is a clean operation that really looks like a step function. In reality, the contacts of a switch bounce when they make contact, resulting in a jagged signal as shown in Figure 7.0.

Figure 7.0 Oscilloscope picture of switch debounce

When this signal is passed to a PICmicro® MCU, the microcontroller can recognize this as multiple button presses, which will cause the application software to act as if multiple, very fast button presses have occurred. To avoid this problem the “noisy” switch press is “debounced” into an idealized “press,” or the step function as shown in Figure 7.1.

Figure 7.1 Idealized switch operation

Two common methods are used to debounce button inputs.

EE313 Microprocessor Applications

30

R Singh

(i) Software Solution When a switch is pressed the jagged signal would last for less than 20ms. So incorporating a 20ms delay subroutine after reading a switch input and repeating to read the switch input would solve the debounce problem.

START

(HEADER) list p=16F84 radix hex

Set PORTA (RA0) as INPUT Set PORTB (RB0) as OUTPUT

Clear PORTA Clear PORTB

Is SWITCH Pressed ?

N

Y

DELAY for 20ms (avoids debounce)

Turn ON LED

END

EE313 Microprocessor Applications

31

R Singh

(i) Hardware Solution If you don’t want to use the software approach, you can use a capacitor to filter the bouncing signal and pass it into a Schmitt trigger input. Schmitt trigger inputs have different thresholds, depending on whether the signal is rising or falling. For rising edges, the trigger point is higher than falling. Schmidt trigger inputs have the “hysteresis” symbol put in the buffer as shown in Figure 7.2.

Figure 7.2 Debounce switch using a Schmitt trigger

7.1 Switch Scan Subroutine ; (MAIN PROGRAM) call call

call

IsSwtPrs TurnOnLED . . . IsSwtPrs

;subroutine to check if switch pressed ;subroutine to turn on LED

;subroutine to check if switch pressed

;(SUBROUTINE PROGRAM) ;--------------------------------- Switch Scan Subroutine-----------------------------------------------IsSwtPrs btfss portb,0 ; if switch at RBO pressed (close) skip next line else test ; for switch pressed. goto IsSwtPrs; goto IsSwtPrs call Delay IsSwtCls btfsc portb,0 ; if switch at RBO open, skip next line else test for ;switch opened. goto IsSwtCls ;goto IsSwtCls return ;jump out of switch scan subroutine ;-------------------Delay Subroutine----------------------------------------------------Delay movlw 0xFF ;load Decimal FF in W register movwf count1 ;load Count1 with contents of W register loadC2 movlw 0xFF ;load Decimal FF in W register movwf count2 ;load Count2 with contents of W register decC2 decfsz count2,f;decrement counter2 and skip next line if Z=0 goto decC2 ; if count2 not 0 than go to decC2 decfsz count1,f;decrement counter1 and skip next line if Z=0 goto loadC2 ; if count1 not 0 than go to loadC2 return ;else jump out of subroutine

EE313 Microprocessor Applications

32

R Singh

• The flow chart for the problem in Mini Project 1 is as follows

START

Set PORTB (RB0) as INPUT Set PORTA (RA0&RA1) as OUTPUT

COUNT3 = 5

Clear PORTA (LEDs OFF)

SWITCH SCAN SUBROUTINE

Turn ON LED1

DELAY SUBROUTINE

Decrement COUNT3

Is COUNT3=0 ? N

Y Turn ON LED2

DELAY SUBROUTINE

END

EE313 Microprocessor Applications

33

R Singh

;============EE313MINIPROJECT1===============10/03/04========================================== ;Write a simple assembly program so that each time SW1 is pressed LED1 turns ON. Once SW1 has been pressed 5 times LED2 should also ; turn ON. Turn OFF both LEDs and Repeat the process. ;---------------------------------------------------------------------------------------------;(HEADER) /Assembler Directives/ ; list p=16f84 radix hex ;---------------------------------------------------------------------------------------------; (EQUATES) ; porta equ 0x05 ;equate porta to point to special file register (SFR) 0x05 portb equ 0x06;equate portb to point to special file register (SFR) 0x06 count1 equ 0x0C;equate portb to point to general purpose register(GPR) 0x0C count2 equ 0x0D;equate portb to point to general purpose register(GPR) 0x0D count3 equ 0x0D;equate portb to point to general purpose register(GPR) 0x0E ;---------------------------------------------------------------------------------------------;(ORIGIN) ; org 0x000 ;program originates at location 0x00 ;---------------------------------------------------------------------------------------------; (MAIN PROGRAM) ; movlw 0xFC ;load W register with 0xFC tris porta ;copy W to tristate PRORTA --> PORTA RA0 & RA1 set as input movlw tris

0x01 portb

reset movlw

0x05 movwf

;load W register with 0x05 count3 ;count3 = 05

start clrf

porta

;clear all lines of porta to low (LED1 & LED2 OFF)

begin call

IsSwtPrs; call Switch Scan Subroutine bsf call decfsz goto bsf call goto

;load W register with 0xFE ;copy W to tristate PRORTB --> PORTB RB0 set as input

porta,0 ;turn on LED1 connected at RA0 delay ; delay for (~256 x 256 micro second) count3,1; decrement count3: if Z=0, skip next line start; porta,1 ;turn on LED2 connected at RA1 delay ;delay for (~256 x 256 micro second) reset ;repeat by going to reset

;--------------------------------- Switch Scan Subroutine---------------------------------------------------IsSwtPrs btfss portb,0 ; if switch at RBO pressed (close) skip next line else test ; for switch pressed. goto IsSwtPrs ; goto IsSwtPrs call delay ; IsSwtCls btfsc portb,0 ; if switch at RBO open, skip next line else test for ;switch opened. goto IsSwtCls ;goto IsSwtCls return ;jump out of switch scan subroutine ;--------------Delay Subroutine--------------------------------------------------------------delay movlw 0xFF ;load Decimal FF in W register movwf count1 ;load Count1 with contents of W register loadC2 movlw 0xFF ;load Decimal FF in W register movwf count2 ;load Count2 with contents of W register decC2 decfsz count2,f;decrement counter2 and skip next line if Z=0 goto decC2 ; if count2 not 0 than go to decC2 decfsz count1,f;decrement counter1 and skip next line if Z=0 goto loadC2 ; if count1 not 0 than go to loadC2 return ;else jump out of subroutine ;---------------------------------------------------------------------------------------------;(END OF MAIN PROGRAM) ; end

;----------------------------------------------------------------------------------------------------------------------------------------EE313 Microprocessor Applications

34

R Singh

PIC16F877

Figure B : PIC16F877 Pin Diagram

8.0 16F877 MICROCONTROLLER CORE FEATURES • High performance RISC CPU • Only 35 single word instructions to learn • All single cycle instructions except for program branches which are two cycles • Operating speed: DC - 20MHz clock input DC – 200ns instruction cycle

EE313 Microprocessor Applications

35

R Singh

• Up to 8K x 14 words of FLASH Program Memory Up to 368 x 8 bytes of Data Memory (RAM) Up to 256 x 8 bytes of EEPROM Data Memory • Interrupt capability (up to 14 sources) • Eight level deep hardware stack • Direct, indirect and relative addressing modes • Power-on Reset (POR) • Power-up Timer (PWRT) and • Oscillator Start-up Timer (OST) • Watchdog Timer (WDT) with its own on-chip RC oscillator for reliable operation • Programmable code protection • Power saving SLEEP mode • Selectable oscillator options • Low power, high speed CMOS FLASH/EEPROM technology • Fully static design • In-Circuit Serial ProgrammingTM (ICSP) via two pins • Single 5V In-Circuit Serial Programming capability • In-Circuit Debugging via two pins • Processor read/write access to program memory • Wide operating voltage range 2.0v to 5.5V • High Sink/Source Current: 25mA • Commercial, Industrial and Extended temperature ranges • Low-power consumption - < 0.6mA typical @ 3V, 4MHz - 20µA typical @ 3V, 32kHz - < 1µA typical standby current

9.0 16F877 PERIPHERAL FEATURES • Timer0: 8-bit timer/counter with 8-bit prescaler • Timer1: 16-bit timer/counter with prescaler, can be incremented during SLEEP via external crystal/clock • Timer2: 8-bit timer/counter with 8-bit period register, prescaler and postscaler

EE313 Microprocessor Applications

36

R Singh

• Two capture, Compare, PWM modules -Capture is 16-bit, max. resolution is 12.5ns -Compare is 16-bit, max. resolution is 200ns -PWM max. resolution is 10-bit • 10-bit multi-channel Analog-to-Digital converter • Synchronous Serial Port (SSP) with SPITM (Master mode) and I2CTM (Master/Slave) • Universal Synchronous Asynchronous Receiver Transmitter (USART/SCI) with 9-bit address detection • Parallel Slave Port (PSP) 8-bits wide, with external RD , WR and CS controls (40/44-pin only) • Brown-out detection circuitry for Brown-out Reset (BOR) 10.0 PIC16F877 BLOCK DIAGRAM

Figure 10.0: PIC16F877 Block diagram EE313 Microprocessor Applications

37

R Singh

• The 16F877 has 5 PORTS (Port A, Port B, Port C, Port D and Port E) • The 16F877 microcontrollers are highly prone to static damage compared to PIC16F84 so anti-static wrist band must always be worn while handling the microcontrollers.

11.0 PIC16F877 PIN OUT DESCRIPTION

EE313 Microprocessor Applications

38

R Singh

Figure 11.0: PIC16F877 Pin Description

EE313 Microprocessor Applications

39

R Singh

12.0 PIC16F877 PROGRAM MEMORY MAP and STACK

Figure 12.0: Program Memory and Stack

13.0 DATA MEMORY ORGANISATION • The Data Memory is partitioned into multiple banks which contain the Special Function Registers and General Purpose Registers • Each Bank extends up to 7FH (128 bytes). Bits RP1 (STATUS<6>) and RP0 (STATUS<5>) are the bank select bits.

Figure 13.0: Data Memory Organisation

EE313 Microprocessor Applications

40

R Singh

Figure 13.1: PIC16F877 Register File Map

EE313 Microprocessor Applications

41

R Singh

Figure 13.2: Special Function Register Summary

EE313 Microprocessor Applications

42

R Singh

Figure 13.2(cont.): Special Function Register Summary

14.0 16F877 STATUS REGISTER

EE313 Microprocessor Applications

43

R Singh

WHY Migrate from PIC16F84 to 16F877? For a few $$dollars more we get all the features of PIC16F84 plus additional features such as: CORE FEATURES • More I/O pins (PortA, PortB, PortC, PortD & PortE) • 8K Program Memory • Can be operated with 20MHz clock input (200ns instruction cycle) PERIPHERAL FEATURES • 10-bit multi-channel Analog-to-Digital converter • Two Capture, Compare, PWM modules • 3 Built in TIMER (Timer0, Timer1 & Timer2) • Interrupt capability (up to 14 sources) • Eight level deep hardware stack • Synchronous Serial Port (SSP) with SPITM (Master mode) and I2CTM (Master/Slave) • Universal Synchronous Asynchronous Receiver Transmitter (USART/SCI) with 9-bit address detection • Parallel Slave Port (PSP) 8-bits wide, with external RD , WR and CS controls (40/44-pin only)

Because of these features the department ventured into developing “PIC16F877 BOOTLOADER BOARDS”

EE313 Microprocessor Applications

44

R Singh

Implementing PIC16F877 BOOTLOADER The Bootloader can be considered as an internal programmer for the 16F877 PIC Microcontroller. It is a code which is programmed initially using the PIC START PLUS Programmer, once programmed, the chip is ready to use. That is once, the appropriate hardware is designed, the hex file created by MPLAB or PIC C Complier can be sent to the chip using the serial port through TERA TERM (software used for serial file transfer). The Bootloader is split into two sections: • The first is programmed at the initial memory registers (Boot Reset Code), which when activated starts the PIC16F877 microcontroller in programmer mode. Thus it waits for the user to download a new set of code. • The other section is at the end of the memory registers (main boot code), which makes serial programming possible.

Hence a PIC16F877 Bootloader microcontroller programming.

board allows for “hands free”

This avoids PIC16F877 being damaged by static chargers. The best part is that PIC16F877 Bootloader boards are very reliable.

EE313 Microprocessor Applications

45

R Singh

EE313 Microprocessor Applications

46

R Singh

EE313 Microprocessor Applications

47

R Singh

EE313 Microprocessor Applications

48

R Singh

1 Using the CCS PIC C Compiler ‘ Open the PIC C compiler by double clicking on the icon desktop window.

found on the PIC C Compiler.lnk

‘ Use the white box on the toolbar to select the compiler. CCS offers different compilers for each family of Microchip parts. All the exercise in this course are for the PIC16F877 chip, a 14-bit opcode part. Make sure 14 bit is selected in the white box. ‘ The main program compiled is always shown in the lower right corner of the IDE. ‘ Click File > New and create file name Exercise1.C as shown

EE313 Microprocessor Applications

49

R Singh

‘ The first seven lines of this program define the basic hardware environment. The chip being used is the PIC16F877, running at 4Mhz, which will communicate with the bootloader using RS232 serial communication protocol. ‘ The “while (TRUE)” is a simple way to create a loop that never stops. ‘ Note the “output_A(0xFF)” outputs logic high (5V) to all the pins of PORTA and “output_A(0x00)” outputs logic low (0V) to all the pins of PORTA. ‘ The “delay_ms(1000)” is a one second delay (1000milliseconds). ‘ Click Compile > Compile and the following files should be created as shown

‘ Use Tera Term software to download the Exercise1.HEX (Exercise1.ICD) file to the bootloader. ‘ Reset the bootloader and the program starts running. Connect the output pins of all the ports to a LED and verify that they cause the LED to flick On and OFF at 1sec interval. Note: This simple program can always be used to test if all the pins of the microcontroller are working or not.

EE313 Microprocessor Applications

50

R Singh

2 GENERATING OUTPUT ‘ Click File > New and create file name Exercise2.C as shown

‘ Note: the “output_high(PIN_A0)” is used to make a single pin (PIN_A0) output logic high (5V) and “output_low(PIN_A0)” is used to make a single pin (PIN_A0) output logic low (0V). ‘ Once this program is complied, downloaded to the bootloader and run only PIN_A0 output flicks ON and OFF.

3 READING INPUTs & GENERATING OUTPUTs ‘ Click File > New and create file name Exercise3.C as shown on page 52. ‘ Note: the “#define GREEN_LED PIN_A0” is used to enhance readability by referring to GREEN_LED in the program instead of PIN_A0 which is connected EE313 Microprocessor Applications

51

R Singh

to a Green Led and “#define PUSH_BUTTON PIN_D0” is used to enhance readability by referring to PUSH_BUTTON in the program instead of PIN_D0 which is connected to a Push Button Switch. ‘ The function “wait_for_one_press()” will first get stuck in a loop while the input pin is low (not pressed). When ever a button is pressed once, is it common for several very quick connect/disconnect cycles to occur. The100ms delay takes care of this switch debounce. It then waits in another loop while the pin is high. The function returns as soon as the pin goes low again. Note that the loops, since they do not do anything while waiting, do not look like much– they are a simple ; (do nothing).

EE313 Microprocessor Applications

52

R Singh

‘ Once this program is complied, downloaded to the bootloader and run only the green LED should come on first. Press the button and the yellow LED should light and then the red LED when pressed again.

4 ANALOG TO DIGITAL CONVERSION ‘ The PIC16F877 chip has 8 pins that may be used to read an analog voltage. These 8 pins can be configured to certain combinations of analog input and digital pins, but not all combinations are possible. ‘ Click File > New and create file name Exercise4.C as shown.

‘ For this exercise a 5V variable input supply would be required which should be connected to PORTA pin0 (RA0). A variable power supply can be used but if you have a power supply with fixed voltage output then design a voltage divider circuit as shown. Note: INPUT SHOULD NOT EXCEED 5V.

EE313 Microprocessor Applications

53

R Singh

to pin A0

5V fixed power supply

0 to 5V variable output to common PIC ground

‘ The “setup_adc_ports(RA0_RA1_RA3_ANALOG)” function call determine what pins are set to be analog inputs and hence pins RA0, RA1 and RA3 are set as analog inputs. ‘ The “setup_adc(ADC_CLOCK_INTERNAL)” function call determines how fast the conversion is done. The internal clock option uses an internal RC clock. Although the timing is not exact, it is long enough for a accurate conversion. The time can be based off the instruction clock for more precise timing. ‘ The “set_adc_channel(0)” function sets the A/D converter to channel 0 (RA0). This switches an internal mux in the part but does not start an A/D conversion. Even though a conversion has not started, there is a small capacitor in the chip that must charge up after the port switch and before the voltage is read. This is fast with a low impedance input, but for a higher impedance input, a small delay should be put in after the channel is changed. ‘ The call to “read_adc()” starts a conversion, waits for it to complete and returns the result. The conversion time is around 20 us. ‘ By default, the analog to digital converter is 8 bits. Thus, a range of 0 to 5 volts analog is represented by the numbers 0-255. The A/D reading can be converted to volts by the formula: Volts = reading * (5.0/255)

‘ Once this program is complied, downloaded to the bootloader and run, the 0 to 5volts variable voltage would be read in at PORTA pin0 (RA0) and the corresponding voltage displayed on the Tera Term window every 1 second.

EE313 Microprocessor Applications

54

R Singh

EXERCISE: A half wave rectified signal which is an output of a transducer after amplification is shown in Figure 4.0. This amplified signal is to be read in by the A/D converter input PORTA pin0 (RA0) in Exercise4.C. What modification (if any) is needed to read in the output signal and display the correct voltage on the Tera Term window ?

A: rl_2

12.70 V

7.700 V

2.700 V

-2.300 V

-7.300 V

-12.30 V 0.000us

50.00us

100.0us

150.0us

200.0us

250.0us

300.0us

350.0us

400.0us

450.0us

500.0us

FIGURE 4.0

SOLUTION (i) Since the peak voltage Vpeak = 11.5V we need a voltage divider circuit as shown in Figure 4.1 to reduce this voltage to 5V which can be read in by the PIC microcontroller. Vout = Vin x (R1 / (R1 + R2)) where Vout = 5V then

Vin = 11.5V & if R1=1kohm

5 = 11.5 (1 / (1 + R2)

hence R2 = (11.5 – 5) / 5 = R2 = 1.3kohm FIGURE 4.1

(ii) In the program the following line should change to display the correct voltage volt = value * (float)5/255; to volt = value * (float)11.5/255;

EE313 Microprocessor Applications

55

R Singh

5 INTERFACING MATRIX KEYPAD ‘ Figure 5.0 illustrates a common three-row, four-column 12-button keypad. Each key is attached to a normally open (NO) pushbutton switch. When a key is pressed, the switch closes. ‘ Figure 5.1 illustrates the electrical schematic of the keypad as represented on the millennium development board.

ROW1 ROW2

FIGURE 5.0

ROW3 ROW4

COL1 COL2 COL3 FIGURE 5.1

‘ Connect up the row and column outputs of the matrix keypad to the appropriate pins of the PIC 16F877 microcontroller as outlined in the table below Keypad Output ROW1 ROW2 ROW3 ROW4 COL1 COL2 COL3

EE313 Microprocessor Applications

PIC Input RC0 RC1 RC2 RC3 RE0 RE1 RE2

56

R Singh

‘ Click File > Open and open file name Keypad.C as shown which is stored in the Keypad folder.

‘ Once this program is complied, downloaded to the bootloader and run, random key presses should be detected and displayed on the Tera Term window. To correct this error 10k ohms “Pull Down resistors” should be connected from each of the four Row outputs to ground as shown in Figure 5.2.

EE313 Microprocessor Applications

57

R Singh

FIGURE 5.2

EXERCISE: (i) Carefully study the Keypad.C program and use it to draw a flow chart of the program. (ii) Modify the Keypad.C program to read in all the key inputs from the 4X4 keypad. A valid key press is one where the input key switch first “closes” and then “opens”. Save this modified Keypad.C program for future use.

6 INTERFACING LCD Unit ‘ The other common peripheral device we want to highlight is a standard Hitachi 44780-based liquid crystal display. LCDs come in different shapes and sizes that can support different numbers of rows of text and different numbers of characters per row. The standard choices for the number of characters and rows are 8x2, 16x1, 16x2, 16x4, 20x2, 24x2, 40x2, and 40x4. The commonly used 20x2 LCD is illustrated in Figure 6.0. ‘ Applications of LCDs include displaying messages or information to the user (e.g., a home thermostat display, a microwave oven display, or a digital clock) and displaying a hierarchical input menu for changing settings and making selections (e.g., a fax machine display). ‘ For a LCD display with 80 characters or less (all but the 40x4 just listed), the display is controlled via 14 pins. The names and descriptions of these pins are listed in Table 6.0. LCD displays with more than 80 characters (40x4) use a 16pin header with different pin assignments. A 14-pin LCD can be controlled via

EE313 Microprocessor Applications

58

R Singh

four or eight data lines. It is recommended that you use four lines to minimize the number of I/O pins required. ‘ Commands and data are sent to the display via lines DB4 through DB7, and lines DB0 through DB3 (pins 7 through 10) are not used. ‘ The potentiometer connected to Vee is used to adjust the contrast between the foreground and background shades of the display. The RS, R/W, and E lines are controlled automatically by PIC C program when communicating with the display.

FIGURE 6.0

TABLE 6.0

EE313 Microprocessor Applications

59

R Singh

7 INTERFACING I/O Devices Interfacing I/O devices to microcontrollers are very straight forward if enough I/O pins are available on the microcontroller.

(b) 8-bit Switch Input

(a) PIC 16F877 Microcontroller

(b) 8-bit Seven Segment LED output

Problem only arises when there are not enough I/O pins for the number of I/O devices to be interfaced. This situation can be overcome by using Decoders as shown in the figure below:

(b) 3-to-8 Decoder

EE313 Microprocessor Applications

60

R Singh

8 INTERRUPTS PIC16F877 Microcontroller has two different sets of external interrupts available namely: #INT_EXT #INT_RB

External interrupt on B0 Port B any change on B4-B7

‘ Click File > New and create file name Interrupt1.C as shown on page 61

EE313 Microprocessor Applications

61

R Singh

Example Interrupt1.C shows how the two external interrupts are setup using CCS PIC C compiler.

ENABLE_INTERRUPTS() Syntax:

enable_interrupts (level)

Parameters:

level - a constant defined in the devices .h file

Returns:

undefined

Function:

Enables the interrupt at the given level. An interrupt procedure should have been defined for the indicated interrupt. The GLOBAL level will not enable any of the specific interrupts but will allow any of the specific interrupts previously enabled to become active.

Requires

Should have a #int_xxxx, Constants are defined in the devices .h file.

Examples:

enable_interrupts(GLOBAL); enable_interrupts(INT_RB); enable_interrupts(INT_EXT);

_____________________________________________________________ #INT_xxxx Syntax:

#INT_EXT External interrupt #INT_RB Port B any change on B4-B7 Note many more #INT_ options are available on specific chips. Check the devices .h file for a full list for a given chip.

Purpose:

These directives specify the following function is an interrupt function. Interrupt functions may not have any parameters. Not all directives may be used with all parts. See the devices .h file for all valid interrupts. The compiler will generate code to jump to the function when the interrupt is detected. It will generate code to save and restore the machine state, and will clear the interrupt flag. To prevent the flag from being cleared add NOCLEAR after the #INT_xxxx. The application program must call ENABLE_INTERRUPTS(INT_xxxx) to initially activate the interrupt along with the ENABLE_INTERRUPTS(GLOBAL) to enable interrupts.

EE313 Microprocessor Applications

62

R Singh

EE313 Microprocessor Applications

63

R Singh

Related Documents