Upload
jay-leong
View
137
Download
3
Tags:
Embed Size (px)
DESCRIPTION
chapter 2, microcontroller (dae32203)
Citation preview
Chapter 3
PIC16F887A Instructions Set & Basic Concept of C Language
2
Introduction
Language that microcontroller and man use to communicate is called assembly language.
Programs written in assembly language must be translated into a "language of zeros and ones" in order for a microcontroller to understand it.
Assembly language is a set of rules used in writing a program for microcontroller.
Assembler is a program on the PC which translates assembly language of zeros and ones.
3
Introduction
The process of communication between a man and a microcontroller
PROGRAMMING LANGUAGES
4
The microcontroller executes the program loaded in its Flash memory. This is the so called executable code comprised of seemingly meaningless sequence of zeros and ones. It is organized in 12-, 14- or 16-bit wide words, depending on the microcontroller’s architecture..
All instructions that the microcontroller can recognize are together called the Instruction set. As for PIC microcontrollers the programming words of which are comprised of 14 bits, the instruction set has 35 different instructions in total.
ASSEMBLY LANGUAGE
As the process of writing executable code was endlessly tiring, the first ‘higher’ programming language called assembly language was created. The truth is that it made the process of programming more complicated, but on the other hand the process of writing program stopped being a nightmare. Instructions in assembly language are represented in the form of meaningful abbreviations, and the process of their compiling into executable code is left over to a special program on a PC called compiler. The main advantage of this programming language is its simplicity, i.e. each program instruction corresponds to one memory location in the microcontroller. It enables a complete control of what is going on within the chip, thus making this language commonly used today. 5
6
7
8
9
10
11
PIC16F877A Instructions Set
PIC16F877A INSTRUCTION SET BY FUNCTIONAL GROUPS
F = Any file register (specified by number or label)
W = Working register, W L = Literal value (follows instruction)
12
Instruction ‘Move’
File register
Working register, W
Move data from F to WExample,
MOVF 0Dh,W
8 A1 2
5 F
0Ch
0Dh
0Eh
F FMOVE 0Dh,W
13
Instruction ‘Move’
Working register, W
Move data from F to WExample,
MOVF 0Dh,W
8 A1 2
5 F
0Ch
0Dh
0Eh
8 AMOVE 0Dh,W
File register
14
Instruction ‘Move’
Working register, W
Move data from W to FExample,
MOVWF 0Eh
8 A1 2
F F
0Ch
0Dh
0Eh
3 4MOVWF 0Eh
File register
15
Instruction ‘Move’
Working register, W
Move data from W to FExample,
MOVWF 0Eh
8 A1 2
3 4
0Ch
0Dh
0Eh
3 4MOVWF 0Eh
File register
16
Instruction ‘Move’Move literal into WExample,
MOVLW 12
F FWorking register, W
1 2Working register, Wafter instruction MOVLW 12
17
Register OperationsClear W (reset all bits and value to 0) Example,
CLRW
3 4Working register, W
0 0Working register, Wafter instruction CLRW
18
Register OperationsClear F (reset all bits and value to 0) Example,
CLRF 0Dh
8 A1 2
3 4
0Ch
0Dh
0Eh
File register
0 01 2
3 4
0Ch
0Dh
0Eh
File register after Instruction CLRF 0D
19
Register OperationsDecrement F (Reduce by 1) Example,
DECF 0Dh
8 A1 2
3 4
0Ch
0Dh
0Eh
File register
8 91 2
3 4
0Ch
0Dh
0Eh
File register after Instruction DECF 0Dh
20
Register OperationsIncrement F (Increase by 1) Example,
INCF 0Dh
8 A1 2
3 4
0Ch
0Dh
0Eh
File register
8 B1 2
3 4
0Ch
0Dh
0Eh
File register after Instruction INCF 0Dh
21
Register OperationsSwap the upper and lower four bits in F
Example,
SWAPF 0Dh
8 A1 2
3 4
0Ch
0Dh
0Eh
File register
A 81 2
3 4
0Ch
0Dh
0Eh
File register after Instruction SWAPF 0Dh
22
Register OperationsComplement F value (invert all bits)
Example,
COMF 0Ch
8 A1 2
3 4
0Ch
0Dh
0Eh
File register
8 AE D
3 4
0Ch
0Dh
0Eh
File register after Instruction COMF OCh
23
Register OperationsRotate bits left through Carry flag Example,
RLF 0Dh
8 A1 2
3 4
0Ch
0Dh
0Eh
File register
1 41 2
3 4
0Ch
0Dh
0Eh
File register after Instruction RLF 0Dh
F registerC
C flag isset to ‘1’
24
Register OperationsRotate bits right through Carry flag Example,
RRF 0Dh
8 A1 2
3 4
0Ch
0Dh
0Eh
File register
4 51 2
3 4
0Ch
0Dh
0Eh
File register after Instruction RRF 0Dh
F registerC
C flag is clear to ‘0’
25
Register OperationsClear (reset to zero) the bit specified Example,
BCF 0Dh,03
8 A1 2
3 4
0Ch
0Dh
0Eh
File register
8 21 2
3 4
0Ch
0Dh
0Eh
File register after Instruction BCF 0Dh,03
26
Register OperationsSet (to 1) the bit specified Example,
BSF 0Eh,03
8 A1 2
3 4
0Ch
0Dh
0Eh
File register
8 A1 2
3 C
0Ch
0Dh
0Eh
File register after Instruction BSF 0Eh,03
27
Arithmetic Operations
File register
Working register, W
Add F to WExample,
ADDWF 0Ch,W
8 A1 2
5 F
0Ch
0Dh
0Eh
3 4
28
Arithmetic Operations
File register
Working register, Wafter ADDWF 0C,W
Add F to WExample,
ADDWF 0C,W
8 A1 2
5 F
0Ch
0Dh
0Eh
4 6
29
Arithmetic Operations
File register
Working register, W
Add W to FExample,
ADDWF 0C
8 A1 2
5 F
0Ch
0Dh
0Eh
3 4
30
Arithmetic Operations
File registerafter ADDWF 0C
Working register, W
Add W to FExample,
ADDWF 0C
8 A4 6
5 F
0Ch
0Dh
0Eh
3 4
31
Arithmetic OperationsAdd L to WExample,
ADDLW 15
1 2Working register, W
2 7Working register, Wafter instruction ADDLW 15
32
Arithmetic Operations
File register
Working register, W
Subtract W from FExample,
SUBWF 0Dh
8 A1 2
5 F
0Ch
0Dh
0Eh
3 4
33
Arithmetic Operations
File registerafter SUBWF 0Dh
Working register, W
Subtract W from FExample,
SUBWF 0Dh
5 61 2
5 F
0Ch
0Dh
0Eh
3 4
34
Arithmetic Operations
File register
Working register, W
Subtract W from F, placing result in WExample,
SUBWF 0Dh,W
8 A1 2
5 F
0Ch
0Dh
0Eh
3 4
35
Arithmetic Operations
File register
Working register, Wafter SUBWF 0D,W
Subtract W from F, placing result in WExample,
SUBWF 0Dh,W
8 A1 2
5 F
0Ch
0Dh
0Eh
5 6
If result 0 , C = 1If result < 0 , C = 0
36
Arithmetic OperationsSubtract W from L, placing result in WExample,
SUBLW 13
1 2Working register, W
0 1Working register, Wafter instruction SUBLW 15
If result 0 , C = 1If result < 0 , C = 0
37
Logic Operations
File register
Working register, W
AND the bits of W and F, result in FExample,
ANDWF 0Ch
8 A1 2
5 F
0Ch
0Dh
0Eh
3 4
38
Logic Operations
File registerafter ANDWF 0C
Working register, W
AND the bits of W and F, result in FExample,
ANDWF 0Ch
8 A1 0
5 F
0Ch
0Dh
0Eh
3 4
39
Logic Operations
File register
Working register, W
AND the bits of W and F, result in WExample,
ANDWF 0Ch,W
8 A1 2
5 F
0Ch
0Dh
0Eh
3 4
40
Logic Operations
File register
Working register, Wafter ANDWF 0C,W
AND the bits of W and F, result in WExample,
ANDWF 0Ch,W
8 A1 2
5 F
0Ch
0Dh
0Eh
1 0
41
Logic OperationsAND the bits of L and W, result in WExample,
ANDLW AA
B CWorking register, W
A 8Working register, Wafter instruction ANDLW AA
42
Logic Operations
File register
Working register, W
OR the bits of W and F, result in FExample,
IORWF 0Ch
8 A1 2
5 F
0Ch
0Dh
0Eh
3 4
43
Logic Operations
File registerafter IORWF 0C
Working register, W
OR the bits of W and F, result in FExample,
IORWF 0Ch
1 2
5 F
0Ch
0Dh
0Eh
3 48 A3 6
5 F
0Ch
0Dh
0Eh
44
Logic Operations
File register
Working register, W
OR the bits of W and F, result in WExample,
IORWF 0Ch,W
8 A1 2
5 F
0Ch
0Dh
0Eh
3 4
45
Logic Operations
File register
Working register, Wafter IORWF 0C,W
OR the bits of W and F, result in WExample,
IORWF 0Ch,W
3 68 A1 2
5 F
0Ch
0Dh
0Eh
46
Logic OperationsOR the bits of L and W, result in WExample,
IORLW AB
B CWorking register, W
B FWorking register, W after IORLW AB
47
Logic Operations
File register
Working register, W
Exclusive OR the bits of W and F, result in FExample,
XORWF 0Ch
3 68 A1 2
5 F
0Ch
0Dh
0Eh
48
Logic Operations
File registerafter XORWF 0C
Working register, W
Exclusive OR the bits of W and F, result in FExample,
XORWF 0Ch
3 62 4
5 F
0Ch
0Dh
0Eh
8 A
49
Logic Operations
File register
Exclusive OR the bits of W and F, result in WExample,
XORWF 0Ch,W
8 A1 2
5 F
0Ch
0Dh
0EhWorking register, W
3 6
50
Logic Operations
File register
Working register, Wafter XORWF 0C,W
Exclusive OR the bits of W and F, result in WExample,
XORWF 0Ch,W
2 48 A1 2
5 F
0Ch
0Dh
0Eh
51
Logic OperationsExclusive OR the bits of L and W, result in WExample,
XORLW AB
B CWorking register, W
1 7Working register, W after EORLW AB
52
Test and Skip Test a bit in F and Skip next instruction if it is Clear (= 0) BTFSC 0Ch,3
Test a bit in F and Skip next instruction if it is Set (= 1) BTFSS 0Ch,3
Decrement F and Skip next instruction if it is now Zero DECFSZ 0Ch
Increment F and Skip next instruction if it is now Zero INCFSZ 0Ch
53
PIC16F877A Assembly Language
Representing a number in assemblerIn assembly language MPLAB, numbers can be represented in decimal, hexadecimal or binary form. For Example, number 240 can be represent as:
.240 or d’240’ - Decimal
0xF0, h’F0’ - Hexadecimal
b’11110000’ - Binary
54
PIC16F877A Assembly Language(Mnemonic form)
; Start main loop;...............................................................
reset CLRF 06 ;Clear Port B Data
start BTFSS 05,0 ;Test RA0 input buttonGOTO reset ;and reset port B if pressedBTFSC 05,01 ;Test RA1 input buttonGOTO start ;and run count if pressed
INCF 06 ;increment count at Port BMOVLW 0FF ;Delay count literalCALL delay ;Jump to subroutine 'delay'
GOTO start ;Repeat main oop alwaysEND ;Terminate source code
Label
Command Operand
Comment
C PROGRAMMING
However, programmers have always needed a programming language close to the language being used in everyday life. As a result, the higher programming languages have been created. One of them is C. The main advantage of these languages is simplicity of program writing. It is no longer possible to know exactly how each command executes, but it is no longer of interest anyway. In case it is, a sequence written in assembly language can always be inserted in the program, thus enabling it.
55
COMPILER
a specialized program in a PC called compiler is in charge of compiling
program into machine language. Figures above give a rough illustration of what is going on during the process
of compiling the program from higher to lower programming language.
56
Here is an example of a simple program written in C language:
57
ADVANTAGES OF HIGHER PROGRAMMING LANGUAGES
Support a high level of abstraction: both operations and data Greater portability for a program. As long as there is a translator for
the language on a particular computer architecture, the program can be used on that architecture.
Data representation is hidden. Programmer avoids dealing with details that are not part of his/her problem.
Generally easier to read than machine or assembly language. Programs are generally easier to maintain and modify.
58
Example
59
Programming in C Language Fundamental type of variable
60
61
Using C for programming
62
#include <stdio.h>void main(){}
#include <stdio.h>
void main(){
int number = 10; float weight; weight = 60.00;
printf(“Enter the value of number :”); scanf(“%d”,&number);
number = 50.00;}
Using C for programming
63
SELECTION STATEMENTS
64
ITERATION STATEMENTS
Using C for programming
Using C for programming
65
66
Port Initialization To set Port A as an input;
void main (void){
TRISA = 0b11111111;}
To set Port A as an output;void main (void){
TRISA = 0b00000000;PORTA = 0b00000000; //clear PORTA
}
REGISTER TRISA
1 1 1 1 1 1 1
REGISTER TRISA
0 0 0 0 0 0 0
REGISTER PORTA
0 0 0 0 0 0 0
Delay in Mikro C Delay in milisecond
Delay_ms(1000); //1000ms = 1s Delay in microsecond
Delay_us(1000) ; //1000us = 1ms Delay in 1 minute
void 1minutedelay(void) //function for 1 minute delay
{
unsigned int i;
for (i = 1; i <= 60; i ++) {
Delay_ms(1000);
}
} 67
68
Flowchart
69
A Simple PIC Application (Program1)
PIC
Inputport A
Outputport B
RCclock
+5V
OutputLEDs
Input push buttons(active low)
Clear
Count
CLKIN
MCLR
70
A Simple PIC Application (Program1)
Clockcircuit
10kClear
Count
2n
10k 10k
2200V
+5V
PIC
144
18
17
16
5 6
7
8
9
10
11
12
13
71
Flow Chart for subroutine output
Program 1
Initialize Port
Clear output port
Increment output
again
Using C for programming
void main(void)
{
TRISB = 0x00; //Port B is set as output
while (1)
{
PORTB = PORTB + 1; //increment PORTB
}
}
72
73
Flowchart for Program 1Program 1
Initialize Port
Clear output port
Increment output
start
Reset?
Run?
reset
Yes
No
Yes
No
74
Flowchart for subroutine Delay
Delay
Timer = FF
Decrement Timer
Timer= 0 ?
End
down
Yes
No
75
Flowchart for Program 1 (with Delay) Program 1
Initialize Port
Clear output port
Increment output
start
Reset?
Run?
reset
Yes
No
Yes
No
Delay
76
HEX file of Program 1
:020000040000FA:100000000030660007288C008C0B0428080086014D:10001000051C072885180828860A103003200828A0:00000001FF
View listing file
77
Flowchart of PIC Program Development process
PIC Program
Convert specification into algorithm/flowchart
Edit/write source code
Assemble program
Syntax error?
Test hex code in simulator
LogicalError?
A
A
Download hex code to chip
Test in target hardware
FunctionalError?
Done
YesYes
Yes
No
No
No
MICRO C #define BUTTON PORTA.F4 //Button at PORTA bit 4 #define LED PORTB.F0 //LED at PORTB bit 0 void main() { TRISB = 0b00000000; //All bit at PORTB is output PORTB = 0b00000000; //Clear (give 0V) at all PORTB output ADCON1 = 0b00000110; // Set PORTA as digital input TRISA = 0b00010000; // Set direction of bit 4 PORTA as input while(1) //endless loop { if (BUTTON == 0) //if button is press { LED = 1 //LED at PORTB bit 0 switch ON } else { LED = 0; //LED at PORTB bit 0 switch OFF } }//end of while }//end of main()
78
ADCON1 Register
LITAR SKEMATIK
81
EXAMPLE #define BUTTON PORTA.F4 //Button at PORTA bit 4 #define LED PORTB.F0 //LED at PORTB bit 0 void main() { TRISB = 0b00000000; //All bit at PORTB is output PORTB = 0b00000000; //Clear (give 0V) at all PORTB output ADCON1 = 0b00000110; //Set PORTA as digital input while(1) //endless loop { if (BUTTON == 0) //if button is press { LED = 1 //LED at PORTB bit 0 switch to ON } else { LED = 0; } }//end of while }//end of main()
82