1. Trang chủ
  2. » Công Nghệ Thông Tin

Interfacing PIC Microcontrollers 7 docx

10 299 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 197,19 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Pseudocode Pseudocode shows the program as a text outline, using higher-level language constructs to represent the basic processes of sequential processing, selection and repetition.. In

Trang 1

Flowcharts are useful for providing a graphical representation of the program, for example, for a presentation, but they are time consuming to create Nevertheless, the flowcharts shown here were drawn just using the drawing tools

in Word, so the creation of flowcharts to a reasonable standard is not difficult for the occasional user Specialist drawing packages are also available, which make the process quicker and easier for the professional software engineer

Pseudocode

Pseudocode shows the program as a text outline, using higher-level language constructs to represent the basic processes of sequential processing, selection and repetition BIN4 is represented in Table 2.6

(a)

(b)

DELAY

Decrement Count = 0?

Return

No

BIN4

Initialise Port B = all outputs

Reset ? All LEDs off

Run?

Increment LED display

DELAY using Count

Yes

No

Figure 2.3 BIN4 flowcharts: (a) main routine; (b) delay subroutine

Trang 2

The program outline uses high level key words such as IF and DO…WHILE to control the sequence It is not an ideal method for a very simple program like this, but is useful for more complex programs In particular, it translates directly into

‘C’, if the high-level language is preferred Note that in this case, the program out-line does not make any assumptions about the hardware implementation

Structure Charts

Structure charts are also more suited to more complex programs, but the con-cept can be illustrated as in Figure 2.4

Each program component is included under standard headings: inputs, processes and outputs, and can be broken down further in more complex pro-grams, so that components can be created independently and then integrated

‘C’ Programming

The ‘C’ programming language allows applications to be written using syntax whose meaning is a little easier to understand than assembly code Programs

MOVWF PORTB Input

Output

or

Function

Table 2.5 Flowchart implementation

DELAY using Count

Reset ? All LEDs off

Initialise Port B = all outputs

BIN4

Trang 3

Project: BIN4 MPB 22-3-06 Ver 1.0

Description: LED binary counter with stop and reset buttons

Declare

Initialise

Inputs (default) Reset, Run

Main

DO

IF Reset pressed

Switch off LEDs DO

Increment LEDS Load Count DELAY using Count WHILE Run pressed

ALWAYS

Subroutine

DELAY

DO

Decrement Count WHILE Count not zero RETURN

Table 2.6 BIN4 pseudocode

BIN4

Reset Run Reset DELAY Inc LEDS

Figure 2.4 BIN4 structure chart

Trang 4

in C are converted into assembly language by a compiler, and assembled into machine code, in a two-stage process

‘C’ is the high-level language of choice for microcontrollers A range of different development systems and compilers are available, but most use the same basic syntax defined as ANSI (American National Standards Institute)

C Assembly language is syntax which is unique to each type of processor, while C provides a common language for all MCU types

BIN4C Program

A version of BIN4, BIN4C, is shown in Figure 2.5 as an example The func-tion is the same as BIN4

It can be seen that the program is simpler, because each C statement is converted into several assembler instructions As a result, the program written

in C will normally occupy more memory than the equivalent assembler version, so microcontrollers with larger memory are typically needed Therefore, the more powerful 18XXXX series of PIC chips are usually used for C applications They also have additional instructions, such as multiply, which makes the conversion more compact

// BIN4C.C ************************ MPB 19-11-05

#include <16F877.h> // Include standard MCU labels

#byte PortB=6 // Output port data type and address

{ set_tris_b(0); // Initialise output port

while(1) // Endless loop between braces {

if (!input(PIN_D0)) // Reset button pressed?

PortB=0; // if so, switch off LEDs

if (!input(PIN_D1)) // Run button pressed?

PortB++; // if so, increment binary display }

Figure 2.5 BIN4C source code

Trang 5

In the BIN4C source code, the header file with the standard register labels for the 16F877 is included in the same way as in the assembler version The output port is declared as an 8-bit variable (PortB), and its address assigned (6)

The main program block starts with the statement ‘void main()’ and is en-closed in braces (curly brackets) The output port is then initialised using a li-brary function provided with the compiler ‘set_tris_b(0)’, where 0 is the data direction code in decimal form An initial value of 0 is output to switch off the LEDs

The control loop starts with the loop condition statement ‘while(1)’, which means repeat the statements between the braces endlessly The buttons are tested using ‘if (condition)’ statements, and the actions following carried out if the condition is true The condition is that the input is low ( ! = not ), and pin labels as defined in the header file are used

BIN4C Assembler Code

The C source code is compiled into assembler code, and then into machine code The list file in Figure 2.6 shows the assembler version of BI4C

It can be seen that some statements are converted into a single instruction, for example,

PortB++; >>> INCF 06,F

Others need several instructions,

if (!input(PIN_D0)) >>> BSF 03.5

The total number of instructions for the C version is 28 The original as-sembler version used 20, giving an increase of 40% for the C version, in this case

We are not going to look at the C language in any further detail here, but this example is given so that the advantages of C programming for microcontrollers can be appreciated When assembly language has been mastered, the developer can then decide if C would be a better choice for given applications For those needing complex mathematical calculations, for example, C is a better choice For simpler programs comprising more bit

Trang 6

CCS PCM C Compiler, Version 3.207 19-Nov-05 15:01

Filename: C:\PIC\bin4c\bin4c.LST ROM used: 28 words (0%)

Largest free fragment is 2048 RAM used: 5 (3%) at main() level

5 (3%) worst case Stack: 0 locations

* 0000: MOVLW 00 0001: MOVWF 0A 0002: GOTO 004 0003: NOP //BIN4C.C

#include <16F877.h>

#device PIC16F877 #list

#byte PortB=6 .

void main() {

0004: CLRF 04 0005: MOVLW 1F 0006: ANDWF 03,F 0007: BSF 03.5 0008: BSF 1F.0 0009: BSF 1F.1 000A: BSF 1F.2 000B: BCF 1F.3 set_tris_b(0);

000C: MOVLW 00 000D: MOVWF 06 PortB=0;

000E: BCF 03.5 000F: CLRF 06

while(1) { if (!input(PIN_D0)) 0010: BSF 03.5

0011: BSF 08.0 0012: BCF 03.5 0013: BTFSS 08.0 PortB=0;

0014: CLRF 06 if (!input(PIN_D1)) 0015: BSF 03.5

0016: BSF 08.1 0017: BCF 03.5 0018: BTFSS 08.1 PortB++;

0019: INCF 06,F } 001A: GOTO 010

} .

001B: SLEEP

Figure 2.6 BIN4C list file

Trang 7

I/O operations and fewer calculations, assembler is generally faster and more compact C and assembler can be mixed in the same program to retain the advantages of both

SUMMARY 2

• The standard development system consists of a source code editor, assem-bler, simulator and programmer

• Machine code instructions can be broken down into operation and operand

• Programs should be well commented and structured for ease of analysis and debugging

• Assembler directives can be used to improve the efficiency and flexibility

of code production

ASSESSMENT 2

1 Describe the advantages of in-circuit programming and debugging over the

2 Refer to the instruction set in the PIC16F877 data sheet State the binary

codes for the operation and operands in the instruction DECFSZ 0C (3)

4 Identify two instructions, one of which must be placed last in the PIC

source code What happens if one of these is not used? (3)

5 Identify two types of label used assembly language programming (3)

6 State three PIC chip options, which are determined by the

8 State the difference between the subroutine and macro, and one

9 Describe the function of the standard header file “P16F877A.INC” (3)

10 State the only assembler directive, which is essential in any program,

Trang 8

11 Identify the five main symbols, which are used in a flowchart (5)

12 Rewrite the BIN4 program pseudocode outline with the delay in-line

ASSIGNMENTS 2

2.1 MPLAB Test

Download and install the current version of the MPLAB development system (if necessary) Enter or download program BIN4 If entered manually, leave out the comments Assemble (V7 Quickbuild) and run the program Set up the input simulator buttons to represent the push buttons at Port D (toggle mode) Set the MCU clock to 40 kHz Display Port B and the Timer register in a suit-able window Demonstrate that the program runs correctly

2.2 MPLAB Debugging

Use the MPLAB debugging tools to single step the program BIN4 and observe the changes in the MCU registers Operate the simulated inputs to enable the output count to Port B Set a break point at the output instruction and run one loop at a time, checking that Port B is incremented Use the stopwatch to meas-ure the loop time Comment out the delay routine call in the source code, re-assemble and check that the delay does not execute, and note the effect on the loop time Re-instate the delay, change the delay count to 03 and note the ef-fect on the loop time

2.3 C Program

Write a minimal ‘C’ program, which will perform the same function as BIN1, and save as a plain text file BIN1.C Discuss the advantages and disadvantages

of programming in ‘C’ and assembler Obtain access to a suitable ‘C’ develop-ment system and test your program Predict the assembler code, which will be produced by the same compiler that was used to produce the list file BIN4C.LST Add comments to explain the meaning of each assembler state-ment produced by the compiler

Trang 10

Circuit Simulation

In the past, the electronics engineers needed to have a fairly comprehensive knowledge of both electronic component operation and circuit analysis, before setting out to design new applications The circuit would be designed on paper and a prototype built to test the design, using a hardware prototyping technique such as stripboard; further refinement of the design would often then be required When the circuit was fully functional, a production version could be developed, with the printed circuit board (PCB) being laid out by hand Further testing would then be needed on the production prototype to make sure that the layout was correct, and that the variation in component values due to tolerances would not prevent the circuit from functioning correctly Learning how electronics systems worked also required a good imagination! Unlike mechanical systems, it is not obvious how a circuit works from simple observation Instruments (voltmeters, oscilloscopes, etc.) must be used to see what is happening, and these also need complex skills to use them effectively

We now have computer-based tools that make the job easier, and perhaps more enjoyable An early ECAD (Electronic Computer-Aided Design) tool was a system of mathematical modelling used to predict circuit behaviour SPICE was developed at University of Berkeley, California, to provide a consistent and commonly understood set of models for components, circuits and signals This system uses nodal analysis to predict the signal flow between each point in a circuit, based on the connections between the components The results would be displayed or printed numerically

The simplest component is the resistor, and the simplest mathematical model

Ohm’s law, V = IR, which relates the current and voltage in the resistor For two

Ngày đăng: 02/07/2014, 04:21

TỪ KHÓA LIÊN QUAN

w