82
Chapter 3 PIC16F887A Instructions Set & Basic Concept of C Language

Chapter2 2014

Embed Size (px)

DESCRIPTION

chapter 2, microcontroller (dae32203)

Citation preview

Page 1: Chapter2 2014

Chapter 3

PIC16F887A Instructions Set & Basic Concept of C Language

Page 2: Chapter2 2014

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.

Page 3: Chapter2 2014

3

Introduction

The process of communication between a man and a microcontroller

Page 4: Chapter2 2014

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.

Page 5: Chapter2 2014

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

Page 6: Chapter2 2014

6

Page 7: Chapter2 2014

7

Page 8: Chapter2 2014

8

Page 9: Chapter2 2014

9

Page 10: Chapter2 2014

10

Page 11: Chapter2 2014

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)

Page 12: Chapter2 2014

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

Page 13: Chapter2 2014

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

Page 14: Chapter2 2014

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

Page 15: Chapter2 2014

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

Page 16: Chapter2 2014

16

Instruction ‘Move’Move literal into WExample,

MOVLW 12

F FWorking register, W

1 2Working register, Wafter instruction MOVLW 12

Page 17: Chapter2 2014

17

Register OperationsClear W (reset all bits and value to 0) Example,

CLRW

3 4Working register, W

0 0Working register, Wafter instruction CLRW

Page 18: Chapter2 2014

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

Page 19: Chapter2 2014

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

Page 20: Chapter2 2014

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

Page 21: Chapter2 2014

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

Page 22: Chapter2 2014

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

Page 23: Chapter2 2014

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’

Page 24: Chapter2 2014

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’

Page 25: Chapter2 2014

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

Page 26: Chapter2 2014

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

Page 27: Chapter2 2014

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

Page 28: Chapter2 2014

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

Page 29: Chapter2 2014

29

Arithmetic Operations

File register

Working register, W

Add W to FExample,

ADDWF 0C

8 A1 2

5 F

0Ch

0Dh

0Eh

3 4

Page 30: Chapter2 2014

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

Page 31: Chapter2 2014

31

Arithmetic OperationsAdd L to WExample,

ADDLW 15

1 2Working register, W

2 7Working register, Wafter instruction ADDLW 15

Page 32: Chapter2 2014

32

Arithmetic Operations

File register

Working register, W

Subtract W from FExample,

SUBWF 0Dh

8 A1 2

5 F

0Ch

0Dh

0Eh

3 4

Page 33: Chapter2 2014

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

Page 34: Chapter2 2014

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

Page 35: Chapter2 2014

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

Page 36: Chapter2 2014

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

Page 37: Chapter2 2014

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

Page 38: Chapter2 2014

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

Page 39: Chapter2 2014

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

Page 40: Chapter2 2014

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

Page 41: Chapter2 2014

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

Page 42: Chapter2 2014

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

Page 43: Chapter2 2014

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

Page 44: Chapter2 2014

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

Page 45: Chapter2 2014

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

Page 46: Chapter2 2014

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

Page 47: Chapter2 2014

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

Page 48: Chapter2 2014

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

Page 49: Chapter2 2014

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

Page 50: Chapter2 2014

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

Page 51: Chapter2 2014

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

Page 52: Chapter2 2014

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

Page 53: Chapter2 2014

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

Page 54: Chapter2 2014

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

Page 55: Chapter2 2014

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

Page 56: Chapter2 2014

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

Page 57: Chapter2 2014

Here is an example of a simple program written in C language:

57

Page 58: Chapter2 2014

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

Page 59: Chapter2 2014

Example

59

Page 60: Chapter2 2014

Programming in C Language Fundamental type of variable

60

Page 61: Chapter2 2014

61

Using C for programming

Page 62: Chapter2 2014

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;}

Page 63: Chapter2 2014

Using C for programming

63

SELECTION STATEMENTS

Page 64: Chapter2 2014

64

ITERATION STATEMENTS

Using C for programming

Page 65: Chapter2 2014

Using C for programming

65

Page 66: Chapter2 2014

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

Page 67: Chapter2 2014

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

Page 68: Chapter2 2014

68

Flowchart

Page 69: Chapter2 2014

69

A Simple PIC Application (Program1)

PIC

Inputport A

Outputport B

RCclock

+5V

OutputLEDs

Input push buttons(active low)

Clear

Count

CLKIN

MCLR

Page 70: Chapter2 2014

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

Page 71: Chapter2 2014

71

Flow Chart for subroutine output

Program 1

Initialize Port

Clear output port

Increment output

again

Page 72: Chapter2 2014

Using C for programming

void main(void)

{

TRISB = 0x00; //Port B is set as output

while (1)

{

PORTB = PORTB + 1; //increment PORTB

}

}

72

Page 73: Chapter2 2014

73

Flowchart for Program 1Program 1

Initialize Port

Clear output port

Increment output

start

Reset?

Run?

reset

Yes

No

Yes

No

Page 74: Chapter2 2014

74

Flowchart for subroutine Delay

Delay

Timer = FF

Decrement Timer

Timer= 0 ?

End

down

Yes

No

Page 75: Chapter2 2014

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

Page 76: Chapter2 2014

76

HEX file of Program 1

:020000040000FA:100000000030660007288C008C0B0428080086014D:10001000051C072885180828860A103003200828A0:00000001FF

View listing file

Page 77: Chapter2 2014

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

Page 78: Chapter2 2014

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

Page 79: Chapter2 2014

ADCON1 Register

Page 80: Chapter2 2014
Page 81: Chapter2 2014

LITAR SKEMATIK

81

Page 82: Chapter2 2014

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