Date post: | 02-Jan-2016 |
Category: |
Documents |
Upload: | carol-gill |
View: | 11 times |
Download: | 0 times |
Training program: Mechanic - electrotechnician
Program name: Digital processing - microprocessorsIII. classMicrocontrollers
Elaborated by: Vlastimil Vlček
Projekt Anglicky v odborných předmětech, CZ.1.07/1.3.09/04.0002
je spolufinancován Evropským sociálním fondem a státním rozpočtem
České republiky.
Do we start programming?
In what language to program microcontrollers?
Assembler – the lowest level of programming.
•Advantages - the most compact code, absolute control over both HW and SW.
•Disadvantages – more laborious program generation, necessity of creating proprietary libraries of subroutines.
Higher programming languages (C, C++ ...)
•Advantages – more synoptic, compact and therefore also faster program writing, use of standard libraries of functions.
•Disadvantages – more extensive final code, arduous control over HW, in most cases a necessary investment in a good-quality translator.
Illustration of writing in an assembler
Let´s try to add up (in a binary manner) two simple numbers: 20 + 10 = 30:
20 in a decimal manner = 10100 in a binary manner10 in a decimal manner = 01010 in a binary manner30 in a decimal manner = 11110 in a binary manner
Writing of the program adding up these numbers in an assembler:MOVLW b'00010100' ;b = binary representation of the numberADDLW b'00001010'
Or also:MOVLW .20 ;.20 decimal representation of the numberADDLW .10
An assembler describes each instruction by means of an abbreviation based on the English description of the meaning of individual instructions.
See e.g. the above-stated instruction:MOVLW has its origin in the word Move (transfer) and transfers the L data constant to the W register
ADDLW is derived from the word Add (count up) and really adds up the L constant to the contents of the W register
The result is saved in the W register.
Illustration of the same writing in a higher language
A = 20B = 30C = A + BPrint C
Higher programming language:•We do not have to know the inner structure of the processor for which the program is destined. Easier, faster and clearer writing, but at the expense of a bigger result file taking up a bigger memory space.
Assembler:•More complicated and slower way of writing, we have to know the inner structure of the processor and the data storage place exactly.•The result file is, however, smaller (when programmed correctly, the smallest possible). If we want, we can have an absolute control over the behaviour of the program.
Comparison of both ways of writing
Principles of writing a source text in an assembler
• It is a plain text file.
• It must not contain any characters but for text characters and the tabulator character.
• It is possible to use an arbitrary text editor if we keep the above-stated conditions.
• It is necessary to keep the prescribed writing format (determined by the translator which is used).
• It is recommended to use specialized text editors embedded in development systems (they can, to a high degree, “watch over” the correctness of the writing).
Recommended writing format (Microchip - MPASM)
Translation of a program source text
MPASM is a translator from an assembler into the machine code for the Microchip microcontrollers. It is integrated to the MPLAB IDE development environment, but can also be used as an independent application.
Program source text before translation
#include <p16f883.inc>
EQU 0x20COUNTER_1 EQU RAM+1COUNTER_2 EQU RAM+2
GOTO START
INIT NOP BANKSEL ANSEL CLRF ANSEL CLRF ANSELH BANKSEL TRISA
MOVLW .0MOVWF TRISCBANKSEL PORTCMOVLW b'11111111' ; switching off of all LED diodes at the C port
MOVWF PORTC RETURN
WAIT MOVLW D'100' MOVWF COUNTER_1 ;outside loopWAIT_A MOVLW D'255' MOVWF COUNTER_2 ;inner loopWAIT_B DECFSZ COUNTER_2,f ;readout of the inner loop counter, zero test GOTO WAIT_B ;is not zero - back DECFSZ COUNTER_1,f ;readout of the outside loop counter, zero test GOTO WAIT_A ;is not zero - back return ;both loops reset - return
START CALL INIT ;inicialization of the microcontroller START1 BCF STATUS,C ;resetting of the carry bitSTART3 RLF PORTC CALL WAIT GOTO START3
END
The same program after the translation into the machine code
:020000040000FA
:10000000182800008316031788018901831603133B
:100010000030870083120313FF308700080064302C
:10002000A100FF30A200A20B1328A10B1128080089
:0A00300001200310870D0F201A288D
:00000001FF
Only after the creating of this machine code, it is possible to save the program in the memory of the microcontroller, it is possible to start the program, or use debugging means (simulation, emulation, step-by-step operation etc.).
It is not possible to work directly with the original source text file!
When working with ICD2 and with the MPLAB IDE development environment, we have an option whether to use a software simulation or debugging at the chip of the circuit. Both ways have their advantages and disadvantages, it depends on the actual situation which of them we will use.
Summary of the subject matter
• What are the advantages of a higher programming language and when is it, by contrast, more suitable to use an assembler?
• What do we need a translator for?• What format is the translated source text in?
Summary of the subject matter
• For what reasons is it recommended to use specialized text editors for writing a program source text?
• What are the main principles of writing a program source text?• Why is it necessary to keep the recommended writing format of a
program source text?
Datasheet Microchip PIC16F882/883/884/886/887 DS41291E (http://www.microchip.com)
Microchip.com: Getting Started with PICmicro MCUs Microchip.com: MPLAB IDE User’s Guide Microchip.com: Quick Guide to Microchip Development Tools
References