+ All Categories
Home > Documents > PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line,...

PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line,...

Date post: 10-Sep-2019
Category:
Upload: others
View: 18 times
Download: 7 times
Share this document with a friend
154
PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ! " # ! !$%&# E-mail a friend about this item ’!( !%)* !+!) !&),!- %!! .!! )! .!! Contents:
Transcript
Page 1: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

PIC microcontrollers, for beginners too

on-line, �������������������� author: Nebojsa Matic ������������� ����������������� ��������������� ������� � �������� ��� ��������������������� ���� � ������������������������������� ��������������� � ������������������ ������ ������ ��������������������� ������������ �� � ����������!���������� ���� "��������� ���������� ������������ ������ ����#������������

���������� ���� �!����� � ���� ��� ������������ �!���� �� ��$�%&#�������������� ������ ���

E-mail a friend

������������� �������� about this item

��� �� ��� ��� �������������� ' ����!�(������ ��!�%�)*�!�+ ��!�)�����!�&���)��, �� ��!�- ��������������� ��� ������ �������� � �������� % ���������� ���� !������ �����!��������� ������ �� ���������������� ����������������������������� .�������� �����������������!��� ��������!����� ������� ���� ������ ������� ) �������!������ ��������� ��������� ��������������� ��������������� ��� .�������������!�������������� �������������!���������� ���������� ������� ������ ������������

Contents:

Page 2: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

���� !"���

�# "$%&� �$#� $�

���"$�$# "$��!"��

���������

.������$�������� ���, ������������ �����

�/�/�$ ��������/�0�� �������� ���������/�1�#�� ��/�2����������������/�3�- ���������������/�4���� ������

/�5�6�������/�7�&���������������, �� ��/�8�������

���� !"�������"$�$# "$��!"����'()*+

������������-�!�'�-��&��������������9������������� ���� �����

���� ������� �0�/������� ����������������0�0�' � ��0�1�� �������� ���������0�2�������0�3�$ ��������:����

0�4��� �������0�5�;� ���� ���$'<�0�7�==�'($�)����� ���

���� !"�����

���!���,���#-&�-!��"$-"����#-

��������

1�/�' �� � ������� �������� ��� ��1�0�&�� ����������� � � � ���1�1�6������������� ��������1�2��������� ���, ��1�3�;�� ���� �� �������� ����������������������

���� !"��.�������

���������2�/������������ �$�%&#�������������� ��2�0�6 ��� ���$�%&#�

2�1�) ����������� ���2�2��� ������� ��&�� ��� ����� �2�3�6���������������2�4�����������2�5�$�-�$���������

���� !"�.����"$���#%��&��"$-"����

���������3�/�$�����3�0�-����������3�1�$������� ������ � ���� ��

���� !"�.��

!/����!��)$"��&��,� !���0� ��#����"$�$# "$��!"�

���������4�/�6������������ ���������

==�'($�4�0���� ������� ���������� ��������� �������'#2�'#5�4�1���� ������� ���������� ��������� �����'#<�4�2���� ������� ���������� �����, ��������� ���$'<�

4�3���� ������� ���������� �����, �������$'<�� �� ���� � ���������>�(�+�?�

���� !"�.���!/����!�

���������5�/��� ���������� ���� ���������5�0�%=)���� ��5�1������������

5�2�(������ �����5�2�/�(������ ���������� ���5�2�0�(������ ����������� �5�3�' ����5�4�@ ����������5�5�-������ ���� �����5�5�/������������� ���� ��

��5�5�0�(������������� ���� ��5�7�5�� �� ����������>������� ��?�5�8�%�)���������5�/<�-����� �-������������

Page 3: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

����������������������������

������ �������� � ��������

���������.������

$�������� ���, ������������ ������/�/�$ ��������/�0�� �������� ���������

/�1�#�� ��/�2����������������/�3�- ���������������/�4���� ������

/�5�6�������/�7�&���������������, �� ��/�8�������

Introduction

���������� �������� �������� �, ������������� ��� �������������� ��������� ���� ���������� �� , ��� ����� ���������� ���� ������������������ , ��� ��������� ���������� ������ ����� ����������������������������

�������������������� � "����� ��������������������� �����!������ ������������ ���� � ���� ���������� � ����� ���� �������������� ���!�������������� �!���� �������� ���;���� ����� ��������� �,��� ����� ������� �� ���� ����

�� �������� ���� �������������� � ��� ���� ��������������� ��������� �������� ���� ������������������� ������������������������������ ��!���������������� ��� ������������������ ����� �������

History

�������� ���/848!������� �����A��� � � �� ��������� �#B-��($�����������, ����B�� ��-��� ���������� "� ����������� ���� ���� �������������������������

� ���� �������� ������ ������� �������������� �����C�=%!����$������.�������� ������ ������ ���� ����-�� �� ������� � ������������ � �� � ��������������������� ��>��?��)�7!�������� �������������� ���������� ���������� � ����������� ������� ����� �� ������������������������� ��� �������

�� ����������� ��� ���� �������������� � ��� ����������������� �������������� ������������������������ ��� ������ !������������������� "��� ������� �� ���������� ���� ���������������� �����A��� � � �� ��������� "��� ��

&�� �������� !�������A��� � � �� ������ ���������� ��� �������!�$�����*���� ���!������ �������������� ���������������������������� �������

Page 4: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

� ������� ��������!�;� � ����;����������������� ������C�=%��. ������ �� �����C�=%!���������8�������������� � ���������������������������������

�� ������C�=%����� ���� ����������� ���������� ������������/85/��;����!��� ���������� ���� � ������� �#B-��($����������������� ��������� ���� ��� �������)����������� ��!��� � ���� �� ����� ����� ����������� �������� ��2<<2������������� �������2������������ ����������� ��� ����4�<<<�� ������� ��

� ����C�������� ������!�&� ���������������� "� �� �������C�=%����� ��������� �������� ���7������������ ��������� ���� ��������=, �������������, ����������� ������ � �!��� ������ ��������� ���� ������������ �������� ���������&�������/850!�������7������������ ������� ������� �

���� ���� ������ �7<<7������������ ������� ���/4+����� ���!�����������23���������������� ��� ����1<<�<<<�� ������� ��� ���������������� ���������� ��� � � ��������������*��������� �������� ��� ����� ���� , ��� ������

��&�������/852!������ ��������� ����� ���� �7�������� ������ ������ �7<7<�������������� ������� ���42+����� ���!��������������53����������!������ ����� �� ������D14<���

����� ��&� �����������$����!��� ��� ���: ��"�������������������� ��!����� ������������ ����� ����7������������ ����47<<����� ���������������������� ��� !��������������� ���� ������� ��!�$����������� ���������������

��� ��� ��� ���� �������������470<����473<��&���������� ���������� ��� ���: ���� �� ��������� ���������� ��������� ����� ������ , ��� ����������� ��� �� �, ��$����������$(-�� ���������� ����������� ��, ����� , ������������ ������

�&���� �6=-�(C� ��������B�� ��-��� ����/853!������������ , ��������� ����� ����������������� �������� �$(-�� ��������� ������������� ����������� �����43</����43<0����D03� ���!���������� ��������������� �

��� ���� ���������������� �������������������������������� �������������!����� ������������ ������� � �� �����7<7<����47<<����D/58� �����&������� ����������� ����!������� �����$������� � ���� ������� ����� ��������������� �

�����������D48�83�� ��������� �����$�����"��������������������������$(-�� ���������������� ��� ������������ ���� �� ��47<<��$(-�� ���������� ��������43</!������ �����������43<0���� �43<0�������7������������ ���������34�������������������������������� ��������� �����42+����

� �����)� ����������!�43<0�� �� ��, ���������!���������������� ���������� ������������+�$�/!�&��� ��!�&��� ���!�&����!����� !�&��!�(���!�@�� �!�(��!�B����!���������� ����-���� �� ��� , ������� �����43<0�>'��� ��!�

-: �� �!�@�=!�C�'!�'���!�������� ���� ��, ��$(-�� �����?���������������� ���� ����������� ����������������� ���/3���������� �������� ��E��(�� ���� � �����,�������������;� � ����;������ �, ���� �!�����������������

F����������/854�F������� ���� �F7<��)������� ��������������������� ���!�;�������� �����,����� ������+������������� ���� ���������������, �� ���� ����� , �� �����7<7<!�;������ ���: �������������������������������������

������� ����� ���� ����� ��� � ����� �������� �������������� ��������������� �������������� �� ��� ��������� ����� ����������� ��������� ������7<7<!������������������ ������� ���� ��������������� �������������������

��� ����� ������ ����7<7<��# ��� ��� � �������� �������!����� �� ����, �� ���� �!��������F7<�������, ����� ������������ ������������� ������������ ������� ������ �����42�+����� ���!��������/54����������!������� ���� ����� ���� ��!������������������� �� ������� ��������'&$�� ���!����� �������!�

Page 5: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

�� �� ���� �������� ����F7<��������� ������� ������ , �������, �� ������7<7<���F7<����������� �����������F7<���������������������� ������������

���� ������7������������ �������������� ��# ��� ��F���!��� �� ����������� ������ �$�� �!�C=�!�-.&'�!����-@-�������� �� ���F7<������� �� ��������������� ������ �-� �����!����� �!��'-5<1!�F�1�����

��/854!��� ����� ���������������, ��, ������7������������ ������ ��7<73��.� , �!�F7<������������� �� ��������� ���������� ������ ��&��������� ���� ���� �������� �� ����� ����� ��>47<8!�043<!�-�9$�� ���?!� , ����������������������� ����� ��� ����� � �� � *������� ��� �������, � �������� �

��������� ����, ������� ����� �!���43<0����F7<����������47<<�� ��� ���������� �� � ����, ������ �7������������ ��������������� ��

Microcontrollers versus Microprocessors

$�������� ������ ���������������� ����������������;���������� ������������������������������������ �������������� ������� ��� �!��� ��

��� ������������ ���!������ ������� � �,������� ��������������� ���� ��������������������� ��������������� ��������� �, ���� �������� ������ ���(��� ��� �����!���������� ������ ��� ����� �������������� ��C��� �� � ������� ����� � � �������������������� ���� ����� � ������

� ���� ������� ���� ���������������������!�� ���, ��� ���� �������� � � ������������� ,�� ���

1.1 Memory unit

$ ���������������� ���������� ����� ��������������� ���������� � ��� ��������� �������������� ����� ������� �������� ����������������� �������

� ������ ������� ����� ���� ����� ����������������������� �������� ����� �!�������� ����� ���������� �� � ��������� ����� �������� ����������� �� ����������� ����� ������������� ��������� ��������������� ��

Page 6: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

$ ������� ����� � ��������� �������;����� ����������� �� ���� ��� �������� ��������� �� ��� �����������������*���������� ���� ������ ����������

�������� ��������� �����������$ ������������������ ����������!�������� ������������������ � ����� ����� ��������� ��������� � ����� � ����� �� ��� ��� ����������� ����!������� ��� ������� � ������������

�� ��� �����������������# ��� �� ������������ ���������!�� ���������������,�� �������������������������� �������������������������� ����� ��������� ��6 ������� ����� �������� ����'96�>� ��9���� ?��������� ������ ������ �����������������9�G/!�� ��������� !������������ ������� ��� �����������

� ���� �� �����������$ ��������� ������� � � �!����� � ����� ���� ������������������� ����

1.2 Central Processing Unit

% ������1��� �� ����������������� �����������������������, ��������������������������������!���,�� !���������!�����, ������� ������� �� ��������������� ����� ������� ��������� ����������� ��H� �������� ��������H�>��B?������� ������������� ����� ��� ���� ����

Page 7: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

' ���� ����� ��� � �� �� �������������� ��� ������� ��������� �������,���������� ��������� ������������� ��� ������������������ � , ����������� ������%������� ����� �����������6 ���, ������ � � �� ���� ��>� ���������B?��������� ��� �� �� �!������������ ���� �����������

��� � �!����� ������������������������!���� ���� !�� �������������� ��� ��������� ��������������� ������ �� ������������������ ���!�� ������ ����� ����� �� �� ���������B��-���������� �!�� ��������, ��� �H���H�������������� ������ ����������� ���

1.3 Bus

�����H���H�������� ��H���H������������!����� �� � ������������7!�/4!����� ���� ����� � ��� ������� ������� ������� ������������������ ������� ������������������� ������� ��������� ����� ������������ ��!������ ��� �� ���������� ��������!��������� �7���������� �� ������ ��;����� �� �, ������������

���� ���������B�� ���!������ �� ������ ����������������� ��� ���������� ���

Page 8: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

&�������������������!��� �����������������, �!������� ������ ������������� �� ���� ���, �����������*�������� ��������������� ��!������������ ����

��, ����������������� ������ �����!����������E����� ����� �, ������� ���� ��!�� �*�������������������������� , ����� �������������� � � ������ �� ������ ���������!������ ��� ������� ����������� ��������� ����� ���������� ������������ �� ������������ � � ��������� ���

1.4 Input-output unit

��� ��������� *, ��������� ���� ����� ��H����H���� � ��� �� , ������� ���������

������!������������� ������������6� ����������������!������������������� � ����������� ������������ � ������������!������ ���� ��������!������ ���������� ������

6� ����������������� ������������� ���� �����������-� ���������������� �������� ������� ���������!������������� ���� ����� ��������� ���������� ���

1.5 Serial communication

# ��� ����� ����, �� *, ���� ������ ���� ���� ������������ ��������������

���������������������� �������.� , �!������������������������������������������( ����� ���������������������� ���� ������ �������� ����� ��� ������ ��������� ��������6�������������� �������� �� ������������ ���� , �������� � ��I��� ���� ������ ����� ����� �������� � ���� �*������� ��� �

�������� ���� �������� �, �������,������ ��� ��� ���� ������ ���������������������� ��*��� �� �����������������-���� �� ��� ��������������� ��� ����!��������� ��� ������ ������ ��������!��� ������ � �,��!������ �������

������ �������� � � � ��� ���������� ����������� ���������� ������ ���������������!�� � ����� ���� ���� ���� ���� ����������� � ���� ���� ����� �������������������� � �� �� �� ������,�� ����� � �����*��� ��������� ��������� �� ��� ���� ��������� ����������������� ������ ���;��

���� !���� ��������� �������;� ��!������ ��� ����=�����!������������������ ���������� �������"���������� �� ���, ����� ������ ������ ���% �*������� �� ���, ��� ����������������� ������������H/H����� �������� �������������� �

���������� ��� �����(� ��� ������ ��������!�� ��� ���� ������������� ����������H<H������� ��������� �>������� ������� ����� �����?!����� �� � �,������ ��������������������� � �,�������!������������������,�� ������ ����������� � �����

Page 9: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

% �*��������������� �������������� ����������������������: ������ ������ ��������� ���� ����� ��� ���������������� ��� ���,��� ������������� �

���� ���,��� ��% �� ���������������� ��������� �� ������������� "�������!�������� � �!������ ���� �7������!�� ������������ ������������H/H��������� ��� ������������������� � ������ ������������� ��������� �������� *, ������� ����� ��������� ������� ��������� ����� �C'F�>C�' ������F �?��

&��� ���, �� ����� ��� ������ � �,������� ���!������������� ���� � �, ����� �������>���?������ ���� ���� ��-����� ����������� ��� ������������ ��� ��

���������������������������� ����� ���������������������B��� ��� ������� �����������!�������, ��� � �����������!�������� �� ��������������� �� ���� �� ���� ����������������� �������&�� ���� �� � ������������ � ����� ������������ �� � �,��������������� ������� ���������� ����� ������ � ��� �

��� ������� , �� ���)����� ������� ������������� ��������� �� ���������!������� ����� �� � �,������������������� ��������

1.6 Timer unit

-�� �� ���, ��� �� ��������������� ���� �!�� ����� � �, !�� �������� ���������

.� , �!����� ���������: ��������������� � ����� ���������������������( ������ ������ ���� �������������������������������� ���� ���������, �������������������� !�������!������� ������ ��������������� ���� ��������� ������� ��������������������� ���� ����� ��� ����,��� ���� � ������ ��� , ��� �,���!�

��������������������,��� �������� ������/�����0������� ����������� ������� � � �� ����� � ��� ������������ ����� ���� �������������, �������������������� ���������� ����� ��� ��������� "��� �������������� ��

1.7 Watchdog

Page 10: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

( ��� ���������� "������������ ������������� ��������������� ���������� ���

���������������� ��-���� ������������ ��������� ��� �� � � �>�������� �� ����������������?������������� ������� �������� �������!������ !��������������������� ������

(������ !��� ���������� �������������� �!�� ��������� � ����������������� ���������.� , �!��� � ������ � �������� ������������ ���������� �����

�������, �������� �����, ��� ������������ !�� � ���������� � ��� ���������� ���������������������������������� ���� �������� ���� � ����������� ��������� ���: ���� , ������ ���� ��� ����� ����������� �������������� ���H�����H!�: ���������� ������ ��!������� ���� ������� � ���� �

��������� ��������� ,��������� �����,��� ������������� ������� �������� ������������!������� ������������� ������������������������� � � ���� , ������������� �� ����� ����������*����� �,����

1.8 Analog to Digital Converter

&���� �� ���� ��������������������� ������������������ � �������� � ���������������� ������� ������>: ����� ?!��� ����, ���� ��, �� �������

���� ������������ ����� � � ���������������� ����������������� ���� �������������������������������, �����������&)����������������� ������ �����, ���������������������� ������,��� �������������� �����������������������������B���������������B�������������� ����� �������

;����!��� ���������� ����������� � �!��������� � ����������������� ��� ��������� � ��������� ���� � ������������ ���� ����������������� ������ �������� ������� �� ������������������������ ��������� ����� �

Page 11: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

����� ��� ������������������������������� � �������

������ ��������� ��������� �� � ����������� ���� ������ ���������� ��� �� � ����� ��� ������ ��������������� �������� ����������� ���������� �������� ��������� ������������ ����������� �� �� � ����� �� � ��� ����������������� ���

Page 12: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

�� � ���������������1������������ ����������������������� ��� ����

;����� ������������!������������ ���� ������ �����# ��� ������������ �!�� � ���������������������� � ��� �!������� ���� � � � ������������ �������� ���� ��������������� � � � ������ �������>������������ �������� ������� ������� ��?��

1.9 Program

��������������������� ������� ����������������������� ������������� ��

H���������H������������ �������������������������� ������� ��������� ������� �, ������������� ������� ��� ���������� ����

� �" �

"!-�� !"'2�!�$",��$�� �$#3��

"!-�� !"42�!�$",��$�� �$#3��

Page 13: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

�$" �2"!-�� !"'�5�"!-�� !"4�

!#%���� ���������������� ��� ��������� ����������!����,� ����� ������������&���� ��������� ����� ������������������,����� ��� ������ ����

������H&H���� ����� �� ���� ������ �������� ����������&��� � ���� ��� ����������� ��!�� �����������, ��������� ��� ��� ���� ������ �� �������� ������������ � ���������������������� �� �������� ������������������ ��� �������� ����� ���� ������� ����� ������������&!�������������������������������

� �,����� ����� ������ �������;������ ����� ����� �!����������������������������������� ����� ������������������ �� ���� , ���������� ����������&�� ��� �!������#�����

�������� ������������� �������� ���&�� ��� ��� �������� ��� , �������� ��������� �������� �������!�������� ������ �� ��������������� ���� ���������, ���� �� ���� �������� � ��� ��� ����������� ���������� � ���&���������� ������������� �������� �������������

��������� ������������ �������� ��������� ������� ��������������������� ��� � ��� ����� ������ !� ��� ����� ��� ����!������� ���� ���� ������������� ��� �����������#���������� � ��� ��� ���� ��!�������������������� �

�� �������*��������� ����!�������� ������������������� �������������� ��������� ��� ����������� !�� �� ������� ����������������� ����� � ������� ����� �������� ����� �������� �� �������� ������� �!������ ���: ���� ������������ ����������� ��,������ ����������� ������

&�� ���� ���������������� !�� ���������������� ���������� �������� ,�� ��������������� ������������ � ����������� ���� � � ������� ��� � ������������������;������ ��������, ���� �������������� ������ �������������� ���������>�� �� � ������ ����������� � ������������ ��?�������������

��� ��� ���������������� ��� ������� ������������������������#�� ���������������������� �� ��� ������������������������&������ � �, ������������������ �������� ��������������� ���������� ��!��������� �� ��������� �

���������������� ����������.���� �� ,�� ����������� � ������������� � ��!��� �������������������������� ��������� ������� �� , �� ����������� !������������ �*�� � ���� ���� ������� ��� ������������ �� ,�� ������������������

Page 14: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

����������������������������

�� � �����������'()*+

���������

���-�!�'�-��&��������������9������������� �

��� ��������� ������� �

0�/������� ����������������0�0�' � ��0�1�� �������� ���������0�2������

0�3�$ ��������:����0�4��� �������0�5�;� ���� ���$'<�0�7�==�'($�)����� ���

������ ���

���'()*+�� ����������������7�������������� �����'�-�������� ���� ������

� ������������ ����������� ������������� �� � �������������������������������>;%&-.?������������������ ����������

-�� �� �������� ���;%&-.�� ���������� �������� ������� �� ���� ������ !������� ���������������� ��������� ����� ,�� �� , ��� �����!!�"$���������� ��������� ������ ���, ���� ��� � �������������

���������������� ������������������������������������� ���������� ������������� ���������;������� !� ��������������������� ��� �� ����� ���� �� ����� �� ���������������������������� ��������������������������!�� ������

��, ������ ��� �������� ��� ���������� ����������������������� ,�� ��� ���� ���� ���� ����"����������� ������ ������������������������ ��������'&$��� ���� �������� ��� ��������� ������������������������ �

�$" �������$" ���� ����������� ������ �� ��� ���������� ������� ������ �����������&�������, !��������#����� ������������)"!!6"&#� ��!"������7������ ���� ������ ������������ ������������

�� � � �������� ���������(� , ������������������ ���������������� � �������

Page 15: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

,��� ���������� ��� ���� ��� �����>033?!������ �����������������, �����������: ���&��� ������ � ����������� �� � ���������� � ������� ���� ��

�� ��!���� ������ ��� ������ ���������� ����������, ����� ����������� �� ,�� ������!# "����"$�!���#-�&#� ��������� ���� ���, � � � ��� �� ��� ��

���������� ���������� ������������ ���� ��������� ����������� ��� ���� ��� ���������

����7�"���

���������� ����� ��������������/4;72�������'�-�������� ���� �������� �������� ������������ ����� ����� !������� ������ � ���� ��� � ����� �� ������

.��,���������� ���� ������ � ���� �������,�C ���*�������� �������� � ������ ������� ������������������ �����.��,���������� ���� !�������������

Page 16: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

���� ��������� �� ����� ����������� �� ���������������������� ���������� �� �������� ��������!���������� !����� �� ���� ���������- ���������������������

������ ������� ���������� �������� ��������������������, ���� �7��������������/4;72��� ��/2��������������������������������������������������� � �������������������������������������.��,���������� ���� �����, �� � ����������������,�C ���*�!��������, ������������������� ��� ���� �

���� ����$�������� ��������.��,���������� ���� ��� ��������� ��H'�-����������� ��H��'�-�����������' ��� �����������- ������� ���$�������� ��������,�

C ���*�������� ���� ��� ����� ��*��-����������� ��*������ ���-��������������� ����������- ������� ��

-�� ����/4;72������'�-����������� �!������� ������������������ ��� ��� �������������!��� ��� ��� ���13�������������> ���� �*�����$����*��

��������� �����, �, ������ �����������?�&������� � ������������� � ��� ���� ����� � � �������������������������������&��������������������� ������!����/4;72���������� ��� ��� ��������0�/����� ����� �������2�/����� ����� ��������� ��7�������������� ���������������

����� �����

���/4;72�� �� ���������������� �!�����������, �������� ��������������� �������� �������������������� ��!�� �� �� ���!� � ���������������������� ���� ,�� ��������������� ���������������������� ������������� ��������� ��� ,�� ��� ���� ������������������

==�'($�� ������� ����� ��� ������������������� ������ ,�� ���� � �� ��� ������� ���,����������� � ������ � ��>�� ������������� ��!������� �!�� � �, ���� "� �� �!� ���?��%�����!�����������!� ���������������� ����������� ����/4;72���������� � , ����� ����� � ���������� ���������

�� ,������� ����� � ��> ���� ����� ��������!��� ���� �� ���� � �������� ������ ��!����� ��������������!� ���?�

��-��� �����������������������������>������������������������������������ �?���� �������� ��� ��� ������������������!���� ����� ���������� �������, �� ����� � ����������������������� ��� ������ �� ���� ������� �

�������!������ �����������������,������ ������� �������� ����!���������� ��� ��������, ��������������� ����������

�� �8������� ���� � ��

����������������� �*����������� �!����������� �������� � �������� ��

���� ����H��������H������ ������������ ������������ ������������ �����!����H����H�������� �� ��������������� �� ��������� ���� ����������������� !���������������� ������ ����������������������������� �������������&��!���� ��� ���������� ���� ���������� ������ ������ � �����������������

������������ ���������� � �������������� ��,���(-�/������ � ��� ������������������������� ����,�� ���� ������������� , �������J/!�J0!�J1!����J2�������

Page 17: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

����, �������� � �������������� ���� �������������� �>�������� ������� ����� ?������������� ������������� ��� ��

= ���������������������������������������������������� ��������������������������� �������������� ����� , ���J/������������ ������������

� ���� ���J2��) �������� ������������������ �� �� �� ��� � ��J/����J2����� ���(��� ����������������� ����� ��� �� ���������� �� �������������� ������������� ����������>(-�/?����� ��������������� ����������

J/�J2������������ ��>��?��������������������� ����� ������� � �����������

����������

������������� �������������� ��J/!�J0!�J1����J2������ �������������� ������������������ �� �� ������������������������ ������� �������!� �

������������� ���� � �!���� ��� ���� � ������ �������� ������.� , �!��� ������ ����!� ���������������� �� ���, ��� ��� ���� ����� ������������������ �������� ������������ �!�������� �*���������� �

��������������� ��� ������ ���>���������� ��� ���� ������������������������������������?!�������� ���� � � ����� ����������������������������� ���� ���������������� ���� �� ������!������������� ������� ����������� �������� ����������� ���������J/�����!������������������������ ���� ��>�'?��) �������� ������� ���������J0!�J1����J2�������

Page 18: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

�,9�� ���������������$(K%6�33��>���� �*������ �������������������������� ��� �!�� ���� ��� � ������ ����� ������� ����� �����?�� �,'� ��� �����������$(K%6�33������ ������$(K6;��('�#��

�,4� ��� ��$(K6;��('�#����� �������&%%�-B#L/�� �,:� ��� ������������������������&%%�-B#L/!����� ���������������#-;��('�&!�#��1��&����������������������� � �� � �!���������� ������������������������������-B#L/���� � �������� ������ �!���������������� �� �����

������������������� ���� �������������� ������ ����� ����� ��� �,+�������������� ������������� ��������� ��������� ��������������������������������������� ���-B#L/��

�,;� ��� ���� ��������������������������������-B#L/����� �������� � �� �

������� ������

���/4;72��������������/7������������������ "� ������������)��/7���� ������ ������������� �������-$)���� ��������������� ��������)����)��������

���� ,��������)������������ ��-$)���������� ,��������-����� �$���) ,�� ������ ������������ ���������������������� ������!��� *�� � ����������� ������������ �������� ��

Page 19: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

��������/4;72���������� ����, ��� ��������� ������

����/�"�4�- �����������&��.�����������������������0�"�:���������������&��.������������������������1�"�+�;�������������&���(�+/���������������������� �����������������������

����2����"�' � ����������K�������������,���� �������������� ������3�.���@�������� ��������������4�"�9�F ����������#���� ������������������������������������5�"�'�;�������������#��C�����������������

����7�"�4�- �����������#��C����������������������8�"�:���������������#��C����������������������/<�"�+�;�������������#��C�����������������

����//�"�;�;�������������#��C���������������������/0�"�(�-� �����������#��*����*��� ������������ ������/1�"�<�- , �����������#��*)���*��� ������������ ������/2�.��������, ��� ����������� ��

����/3�$��4��������� ������ �������������������������/4�$��'��������� ������ �������������������������/5�"�4�- �����������&��C��������������������/7�"�'�;�������������&��C�����������������

Page 20: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

4='��� ����������6�� ������

(���������������������� �������,���������������� ����������������������� � ��

���������������� ������� ��� �������������������������������

��� ������������������/4;72��������������������� � ���������������������������-�� �

������������������������������������� ���������������>'�?��� ��� � ��������� ��� �������� "� ���!��� � ��� ��� ���� ��� ������� ���� � ��$�������� ����� ������������������������������������ �������M�!��������������� �������� ��������������������������� �������'�������������������� ���� ���� ����

� ����� ���� �������������� ������������������ �����

M��(��������

��������������������� ������ ������������������������ � ������, ������ ������ ��� "� ��������������������������� ���( �� ���������������

��1<�;���� ��� �� ������ �� ������ ������ ������ �� �� ������� �������

(�������������������������� ����� ��

��������� ��������� ������-����

� � ��������� ��� ������� ������������ �� � � �������������� ��� � �� ����� � ���������� � � � ������� �

����!����� � �������� �� �� �������(-�/����(-�0�������� ���������� ���6� �� ��������� ,�� !��� ���� ���������� ������������

�� ������������� �!���������,�������� �� � � ���� ������������������ ������ � �,����������

'��(�������� ����������������� � ��� ������ ��� ���������� � �����!�'������������� ��������������,����������������� ��' ������ "� �����'������������ � ����

�������,���� ���� !�� ������ �'!���������������������� �� ����� ������������ �� �� ��� � ������� ������ "� �������������� � ����������,������������� �������� � ��!������ ��� ��� � ����'��������� ��!� ���

Page 21: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

&�, �����������������'��������������� �� ����������/4;72��6����,��� ���� ������'�� ���� ���0�0�!�������������� �� ������� !��������� , ������� �

����������6����, ��������,��� ���'�> �/$?����������� �� ��, ���� ����, ����� ��������������������� ��� � �������,��� ���� ������'�������� �� �� �1����/<<���=, ���������������������������������� � �������������>�G<�;?!�

�����������, �0<�;�������������� ��� ������� ���������������C����� ��������������������� ����� �!����� ����� ����������������������� ���������!������������ ���������������� ���,�� �����2��(����������������,�� �����2��������� ����� ���(-�09�%+(B����!�������� ��� ������ �������������:����� ������������������

;��������������!����������������������������(������������������������������ �� ��������������� !�������� ���� �� ��������� ����� �� ���������: ��

Page 22: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

���� , ��������������� �������������� ������������� �*��� ������ !�� � ����� ���� ���������� ����� � ������ ��������������:��������������*��

������)���������, �������������������� �������������������������� ��� ��������� �"����:����������

4=4�"����

' � ������� �������������� ���������� �������*��*�������������������������� ����������������� ������ ��, ����� ���������� ����� ��� ������� ������ ������������� �������� �������� �������������������� �� � �!�� ��������

� ���� ��������� ����� ���������������������' � ������������ ���� ���������� ��� �*��� ��, ��� ������ ���������!������������� ��� ���� ������������� ,�� �������� ��������������� ����!������ ������������� ��� ������ ����������������

���� ������ , ����������������������: ����

$�%'��������� ������>�� ���, ����� ��������� � ���������,�� ��������������: �?!�$�%'�������� �� �� ��,���� ���������� ������, ��������

�� ��' ������������� �� �� �3����/<+�������������� ��������� ������������� ����� ������� ����������� �������� , ��, !�������� ������������

$�������� �����/4;72������ , �������� ����� � ����

�?�' � ���������� ��!��('�>�� ��(�' � �?��?�' � ��������� ���������������������������: ����$�%'���������� �*�������?�' � ��������-%==��� ��� ��?�' � ���������������� ��>6)�?�, �����

?�' � �����������6)��, �����������-%==������� ��� ����� �������������� � ������ ���� ��?�����?���� ������� ������� ������� ���

�� ����������������������� ���������� ������ �, �������������� ���� �������������������������������� ���� �� ��� �������������������� ������������������������: ����$�%'���������������� ��������� ���������� ��������� ��� ������ ��� ������������ , ��� ����

�)�������� � �!�'&$�� ������������� ���� ���� � ����� ���� ��������������� ���������� ������� ��������� � ���B��� ��� � !�-;'�� ���� ����� �� � ��

����������������������������� ��( ����� ������������� �� ��������� � ������ ����������������� ��>��?���: ��><<<<�?�!������� ��� ���� ���������������� ������������ ������������ �������������

Page 23: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

' � ������������,���� ������ ����� �� �������� �>#������' � �? ��

������ ����� � �����������,���� �,���� �������� ��� �������������� ����� ����� ����� � ��������� �� ����������K���>������� �����/�0K���/�7K?������������� �������50������������ ������� ������������������ ���������: ����� � �

50����� ���,�� ��������� �����6'����� ������������������'������������$�������� ���������� � ���� �����������6'���������, ��.� , �!����� ,�� ���������!����� ������ ���� ��������� �*��������: ������������� ����� ������������������ ����������� �*����� ������������������������ ������ ���������� !�

�� ������������������� ,��� ���� � ���������� ��������������������������� ��� , ����������� � ������, ���������� ��� � ������� ���� ��������������� ���������� � ������ � ������� ��������������� ����� �����, ���������

��!����������� � ���������� ��������!��� ����� � ����������������������� �����

������������ � ��!��� ����� � ��������� ������� ��� ���������� ������� ���� �������� �� ��� ��� � ���������# ��� �����������!��� �������������������������, ���������,���� �����,���� ������� ����� ���� ��� , �!����������: ���������� ����$�%'����������������� ���������� ����� � ������ ������,���� ������������������������������ �������� �� ������ �

Page 24: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

4=:����������� �������&���

� �������� ���������>��B?������ ������������������� ����������������

� ������ �������������� �������� ���������������������� ������ � ��� �!����� ������������������!������������������ �����

� �������� ���������� ������������������ ���������� ����� ���� ��-�� ��!����������������������������� �� �������������������6� �������� ������ ����������!�������������, ����� ���������� �$(K%6�< 0<��

.� , �!����� ���������������� ������ ����������!������*� �� �*������������������������ �������� �������� �� ����: ������ ��������������� ����*��� *���������������������� �� ������������������ ����������������������

��� ��� �����������>��������������� ��� �?����������������� ��� �������������� ���������� �� �� �������� �������� ����������6 ������ �� � ��������� ����� ��������� �������������� ���������������� ��� �������� �������� �� ���������������&������������������������ �� �, ������������� ���

������� "��� ����� � ������� �������������� �� ���������� �!������ ����������!����� ��� ������������!���B������� �� �� ��������������������� ���������� �������������� ������� ���������������������������� �������

&����� ����������������� ������ ����� �������� �������������!�����������!�

�,���>� �������������������� ���� �?���������� �������$,������������ ���� ���� ���������������*�������*�����/4;72���������7����������� ����������������7���������� ���� ���

��������������������� ����!���������� �� �������������� ���� ��>6�

� ���� �?!������ ��� ����� ����� �� ���� ��������������#��� ����� �� ���� ��� ������������ �� ��������� ���� !������� ���� �������� ����� �@�'���-;'�� ���� ����@�'���������� ,��������*@ ��������� ��' ���� ��*!����-;'����*-� �����;�����' ���� ��*������������������� �� ���!���� �������

��� ��6�� ���� ���� ����� �� ���� ����&������������������ �������������� �����������!�&%B�������������������>�����������-�&�B-�� ���� �?��= ��������� �������������� ���������������!�������� � ������ �� �������� ����) � ������������������������ ��� ��� �!�&%B������� ���,��� ����������>�?!�)�����������>)�?!����F ��>F?��������-�&�B-�� ���� ��

-�&�B-�' ���� �

�������"��>' ���� ��#���- � ������?��

#������ ��� ������� ��� ������������������ �������� ������� ������� ��� ����

Page 25: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

'&$��/�G�����0����1�<�G�����<����/�>����<<����;;�?

��������"�'�"�9�>' ���� ��#���- � �������?��

�� � ����������� ���� ����������� ����� ��������� ������� ������&���������������������� ����� �� ������� �������, ����� , �����!��� �� �� ��� ��������� �������� �������034���� ��������������������� �����/4;72������'�/����������

�� �!��������� �������� ������ � ���������������������� ���</�G�����������<<�G�: �����

����� $���� ������6�������, ������#������� ����� ����������� ����������� ��������%'6)�����-%==��

�����������#������� � ���� ���������� ������� � ���������������, ������������ ��/�G�, ����������������<�G�, �������������

�������%�>�� ��������?�

������������� ���� , ���� �������������������������������� ���������������������!���� �� ����� ������� � �������� �� �����������������%'6)������������-%==��� � �������� ���������� ������������������������ ������

� � �� ��� �������������� �,���� � ��������������� ����������9���- ��������� ������ � ������������������'#<9�C����!����� ��'#����!��������������� ����)&�&�==�'($!���������6��������/�G���� �������������� ���� ���<�G� ������-%==����������

������>�>F �����?�������������: ��� ������������������ ���� ��� �� ��������� ��� �������� ������������� ��������: ����/�G�� ����� "�����: ��<�G�� ������ ���� "����: �

���� �%��>)����������?�)������� ��

#������ �� ������ ��������������!������������B��� ������!����������� �� � �������� �������� �������� ����������� ��������� ������� ������������������� �������� �� ����� ��������� � ������ ��� ����� ���/�G������ ������� ����� ����������������������� ��� ������ �� �����

<�G������ �������������)������������ �� �����&))6;!�&))%6!�-B#%6!�-B#6;�����������

��������>�����?������ ��#�������������� �� ������ ��������������!������������������������

/�G������ ������� �������� ����� ���� ������������<�G������ ������������������������� �� �����&))6;!�&))%6!�-B#%6!�-B#6;�����������

Page 26: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

4=+�����

� ���H���H�� � ������������������������������ ������������ ���� �� ��

������� ����!����������� ����� ���� �� ��� ������������: ������ �!���� ��������� ���� ������������������������!����������� ���� ������ ������������ ������������ �� �������� ������ ������������������ ��������

� �� � ������������ ������� �������� �����B��������������� �������$�������� ���� ���� ������ ������������������ ����� ������ ,�� ���)� �������������!��� �������, ��������� ����� ��&29�(�+���������� !�������������� ���� ���� ��� �����������������&������ � ������������

�� ������� ���- � ������ ����� � ������������������ ��� ����� ������������ ���� ����&������������������������ ������������<�-���(���(C�� ���� ���#��� � ����� ����� ���������� ��� �� ���������� ��

&��������������� �� ����� �����������������!������������� � �������� ,�� �����*��� ���� , �� ������� ����� �� ������������������������!��� �������

����������: ������ �������� ������ ����'�-�� ���� �������� ���������� ��������'�-�� ���� ���������������H/H!��� ���������������������!��������� ������ ������� !���*��������������=, ������������������ ���'�-�� ���� �������!�����

&������'�-&!��������#������'�-#�������� �������� ����� ���������� ����� ��������������������������������������� ��� ������������� � ���������������������� ����� ������('�&�����('�#����� �� ���� ����� ����� ��������<!����� ��'�-&�����'�-#������� ����� ���� ����� ����� ��������/��

�('�#�����'�-#���('�#��������� ��7�������� ���������� �� ���� ������������� ��������'�-#��- ��������������'�-#�� ���� ��� �� ���� ���� ��������������������!����� � ��������������'�-#�� ���� ��� �� ���� ���� ����������������������

Page 27: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

=�����('�#����������� ����� ������������� ������>� ������������� �� ������ ���

����� ?����������� �����,�� ������ � ������� �� , �������'#�B���(���(C�� ���� ����� � �*�������*�� ��������� ��������������� ������ ������� ����������������� ��������������6� ������������ ���������� �!������������ ������� ���

�;��������('�#!�'#5�'#2�������� ����� �������������������� ��� �������������� ������������� �����������: ���������� ��(������������� ����������������� �������� �������������>������'#5�'#2������������� ������������!���

�� �������*��� �� ��� ������� ����� ����������?�������� ��������������������� ������������� ���������� ����� ��� ������, ��������� ���� �������������� ���� ��������� ������������� �� ����������������� �� �������� �� �� ������ � ����!� ������������� ��������� ����� ����� �������&�

��������� �������� � ��� �������� ���������������� ���� ��������� ��������������� ��� � ����� � ��������#������ ���� ���� �������� ���������� ������ �� ��

��� -�&�B-!�'�< �#��/ �,�� < <; �) ���������������������� �,�� �'�-# �6���������'�-#�� ���� � ��� -�&�B-!�'�< �#��< ��� �('�#!�2 ��('�#�N5�2OG< ��� �('�#!�3 � ��� �('�#!�4 � ��� �('�#!�5 �

Page 28: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

�� ���, � ���� �������������<!�/!�0!����1��� �� ����� ������!��������2!�

3!�4!����5���������!���� ���������('�#������������� �� ���� ��

�('�&�����'�-&���('�&�����3��������������� ���� ������� ���� ������������� ��������'�-&�������� ���73���%�� ����������#!�� ��������������'�-&�� ���� ��� �� �������� ���� ��������������������!������ ��������������'�-&�� ���� ��� �� ���� ���� ����������������������

����������������� �������('�&����'&2����� ����������(���������������������� ���� � ��������������� ���$'<��6� �� ��'&2������� ��������������������������������� ��� � �����<�-�����>����������������������������?���������� ��� ���� ���� ���$'<������ � �� ��� �������� ���������������,��� � ���������� ���'&29�<�+�����

������������������� ����������!���������" ��� ������" ���������#���� ��$��������������%&''(&%���������)

�������������&�

��� -�&�B-!�'�< �#��/ �,�� �*//////<<* �) ���������������������� �,�� �'�-& �6���������'�-&�� ���� � ��� -�&�B-!�'�< �#��<

= ���� �������������<!�/!�0!�1!����2��� �� ����� ������!��������3!�4!����5�

�������&�� ������!������������� ���� ����� �����'&0!�'&1!�'&2!������� ���������: ���� �������'&<����'&/�

Page 29: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

4=;������������?����

���/4;72��������� ����� �� ���������!� �������������� ��� �������������

==�'($�� ���������@�'����-;'�� ���� �����'&$�� ������� ������ ����������!����� �;%&-.�� ������� ������� ����������������

�������� �����

�������� ��������� ������ �������;%&-.�� ��������������� ����������� ��������������������� ��������� ��� �� ���*�������� �������� ,�� !���� , ���� ������������� ����� , ��������� ���������������� �������� � ����������������� ���: ����������� �������/<02�������������/2�������������� � �

�������: ����������� �� � �, ������ � ������� ������, ������

)����� �����)����� �������������==�'($����'&$�� ��� ���==�'($�� �������������

42� ������������������� ��� ��������������������������� ����������==�'($���������� ��������� ����� !����������� �� ������ ������������==&)'����==)&�&�� ���� ����&��==�'($�� ������������� �, ������������������������ � ���>��� ���� !�������, �� �� ����� ���� �� ����� �� �������?�!��� � �

��������������� ��� �������������==�'($������������� ����� ������ �����,���

Page 30: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

����� ������������'&$�� ����������������� ������ ����� ������������������< <����< 2;��������� ����47���������%��������'&$�� ������ �

�������� ��@�'�� ���� ������������������ ,��������*���� ��%������������������@�'�� ���� ������� ���� �� ��� ����� ������������������� � �� ������� ��� �����

-;'�� ���� ����' ���� ������������ ����������/0���������������<����/��� �� ���� �����

�� �����: ������������� �������� ��������������� ���������� ����� � ��� ����� ������� ��+������������������

Page 31: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

$ ����#��� ��

# ��� ������*� ���*���,������-;'����@�'�� ���� ��!�� �����������������,�� ����*�����*�>� ��� � �������?�������� ������� ��*����*��- � ����� ����� ���������� �,���'�<�������-�&�B-�� ���� ���

�!@������� bcf STATUS, RP0 �

Page 32: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

����������#�;��� ��������'�<�>'�<G<?���-�&�B-�� ���� ����������� ����������<��

� bsf STATUS, RP0 ��

���������#-;�� ����� �����'�<�>'�<G/?���-�&�B-�� ���� ����������� ���������/����������� ���������� ���������������� ������ �������������� � �� ���% �*������� ������� ���, �� � �� ������<������ �� ��������� �������!���������� �

������������ ���� ������ ���� ������ ��������/!������'�-#��&�������� ��� ���� ���� ��� ����� �� ���� ���'�-#!������������ ������������� ����������<�� ���� ������� ���������� ����� ��!�����������('�#������ ���� �

BANK0 macro Bcf STATUS, RP0 ;Select memory bank 0 endm BANK1 macro Bsf STATUS, RP0 ;Select memory bank 1 endm

#���� � �������� �������� �,������ ���, �� ��������� ���������� ����� �

� ���� ����� ���� �� ������� ���� ������������ �!��� � ����� ����� ���: �������� ���� �����������������

'�� ��������"�(�+"� �������� ��������������������,*%�-�."��"� �������� ������

/�/��$)�0"������ ������1�"�(��+"����2 ��� � ��� �������!�.�� ��� ��$� ������

�"���# ���� /����� ���������2 ����)�3����"���.�����!�."���4���$���.��"����

�����������5��"��*%�����������!��"������������������.���$� �����."��"�� ���.��

�����6�

���������� � ������������ ��>��?������/1������ ���� ��������������� ����� ������� ����������

� ��� ��� ������������������������� ����������������������3������ ���� ����%&�.������ ���, ����� ����������� ����� ��!������ �7������ ���� ����%������ ��� ��7���������� ����� ���

#��������� � ���������� �>�� ������� ��������?���������� �� ��� ��

������������������� ������� ����

-���� �����/4;72�������/1����������������7�� , ��!������� ������!����������7�� ����������!�/1��������� !�������� ���������� �������������� ������� ���� �,��� ���

���������� ����� ��������������� �������������������� ����������������������� ����������������������������������� ������� � ��������� �����!����������� ������ �,��� ��������������� ���������������6� ��,�����������������������������!����������� ������ ������� ������������> ���� �

�����������&%%���������?��6� � ������������������������'=�B'C!�'=�%6���

Page 33: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

'=�;�=�������� � � ��� ������� � ���������������!����������� ��������� ������������������������������������ ��� � ��������� ��� �� ��������

�� ����� ����� � �� �������������������������������������������� ���������� ����� ���B-.�����(�!������ ��� ��������������������������������� ����� ����������� �����

��-��� ����������� ��

���� ��������������������� ���!���������� �������� �� ������ ������������� ��������������$�%'������/1�3K!�����������,���� �K���������� ��������: ��� �� �2�3K���3�3K���������� �������� �������� ��� ������������

���*����9����*������������������ ,������� �� ����� ������� ,�� ��� �!�������� ���������*���� ����������������������

&��� ������� � ��'&$�� ��������������� ���� �� ����� ����������� ������

)�� ���&��� ���� ��)�� ���&��� ��������� ����������8��������� ������������� ���������� ������ �����5������������ ������� �����������������������������>'�/!�'�<?�����-�&�B-�� ���� ��������������� �������������� ��&����� �����-;'�� ���� �������� ���� ������ ������� ������

Bsf STATUS, RP0 ;Bankl movlw 0xFF ;w=0xFF movwf TRISA ;address of TRISA register is taken from ;instruction movwf

Page 34: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

%��� ������������

���� ���&��� ���� ������ ������� ���� ������� ������ ������� ������� ����������������������� ��, ����������'��������-�&�B-����;-'�� ���� ����&��� �� �������������� �� ��

,����C);�� ���� ���������������������� ����� ��������� �������;-'������ ������!��������������������� ���C);��������� ���� ����� ��������� �� ������������� �������;-'�� ���� ���% �*�����!��������� !������ �� ��������� �� ���� ��>@�'?�������� ���<;����������,��� ���0<��#�����������,��� ���<;����;-'�� ���� ��� ������

� ����� ���� ����������������� ���<;�!�������� ����������C);�� ���� �!�� ������� ����,��� ���0<!�������� ��������� ���, �� ��������� �������� ���� ������,��� ����������� ����������� �����>����,���;-'�����C);?�������� ����������������� ������� ������ ������, ������,���� ��, ����� ������� ����!������ ����� ���

�� ������������������������������ ���, ����������������������� ������� �����

���� ������� ��������, ����, � �������������������������������� ����@�'�� ���� �������������� !������� � ���������������: �;-'�� ���� ��������������������� ���

���� ������!������ �� ������� ���������� ���� �� �������� � ������ �;-'�� ���� ��

Page 35: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

-���� ���� ������� �� ������� ���������,���� ��������������!���������������� ���������������>������������ �������� ������� ����������� ������� ���� �?!��� ���������������'&$�� ����>/4�������?�������� ������������� �

' ���������������C);�� ���� ���� ��� ��� �����;-'�� ���� ����� "������: ��� ������� �,��� ���: �!����������������� ��������C(��� �����>�� ����?�

4=(�����������

�� ��������� ���� �������������������� �������� ��� �������� �������� �

, �������� ��� ���� ������!�� ����� ������������������� �������������� ���� ������������, ���������������!�� ���� ������,�� ��� ����� �� ������������ ����� ,��� ��������������������@ �����!� ������ ������

���� ���� �����������!��� ���������������� �� ���������� ����������������>�� ���������� ?�������� �������������� �������

Page 36: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

$�������������������� ��������������������1�������� ������������������

������ ���� �������� ������������� ���C��(C�������� ���� �� ��� ����� �����

�� ������ � �� ��������� �������������������� ���� ������!��������� ��� ���� ������� �!����� ���� ������� ��� ������� "� ������������������������

�C��(C�' ���� �

2�����-�!�>*��� ��3���������7� �������?�#��������� ��� ���������� �������� ��������/�G������� ��������� � ��� ��<�G������� ��������� ������� �

2�����!!�!�>77%�8��0�������/������3���������7� �������?�#��������� ��� ������ ����������� � ����������������� ���==�'($�

/�G��� ������ ��� ��

Page 37: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

<�G��� ������������ �����==�=����==�;�>�����������==�(C/�� ���� �?��� �� ��������� �����!����� ����������������

����� 9�!�>�����84��5��.�3���������7� �������?�#��������� ��� ���� �������������

��� ���$'<�, ������/�G��� ������ ��� ��<�G��� ������������ ������<�=�����<�;��� �� ��������� ����!��� ����������������

������# !�>39��7#���� ��3���������7� �������?�#��������� ��� �� � ������ �������������'#<9�C���/�G� � ������ ������ ��� ��<�G� � ������ ������������ ������C�=�����C�;��� �� ��������� ����!����� ����������������

������"��!�>�2�������" ����3���������7� �������?�=��� ���� ������������������� ����� ����������������2!�3!�4!����5�������#���/�G� ��� ���� ������������ ����� ����������<�G�� ������������� ������� ����� �������������'#�=����'#�;��� �������� ������ �!����� ����������������

������ 9�)�>�����84��5��.�3���������+� �����?�(, ���������� ���$'<��/�G���� ������ �����������������;;����<<��<�G�, ����������������#��������� ��� �� ��������������� ��������� ��������� �� � �� ��

���� ��# )�>39��7#���� ��3���������+� �����?�= � ������ ����������� ���

/�G��� ����������� ��<�G��� ������������������������������������� �� ������ � �� ������'#<9�C�!�>���������� �� ������������C�=)@���(���(C�� ���� �?!������C�;����� ��

������"��)�>�2�%�����" ����3���������+� �����?�#������������������������ ���

����2!�3!�4����5�������#��/�G����� ���� ������������ �������������<�G������ ������ ���������� �����

#���������� ��� �� �������� ������������� ���� ���� ���� � �������� ���� ���������

Page 38: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

���/4;72����������� ���������� ����/��� �����������������������==�'($�0���$'<��� ���������� �������� ��, �����

1���� ��������������� ������'#2!�'#3!�'#4����'#5�����������#��2��= � ������ ����������'#<9�C���������������� ���

@ �������� ����!� ������ ���������� ��������������� ��������( � ��� ���� ������!������ ��� ��� � ������ ��� ��������������� � ���� ������������� ��@�=����������� ��� �������������� ��� ������� �������������� ������������������, ����� ������ ������������������ ���� ������������������� ���������� �

������ �������� ��������� !�������� ��������� ��������������������������������� ��� ����� ���6� ����������������� � ���@�=��������� ��� ��>@�=G<!������� �������������� �?!������ ������������ ��� �����, ��������� �

��� ����� �������������� ��� �����, ������ � ���� �!��� ���� �� ���� �@�=�����>@�=G/!������� ����������� �?������� ��� �� ���6� ��� ������������� � �!�@�=����������� �� ����������������������� ������������� ������� �!�� �������� ����������� ����������������� ���<<<2����������� ����������

��� ���������� �������� ��� ������������ ������� ��E�&�� ���� ������������ �� �!�������� �� ��������� ������ ������������ ��� �� �!����� ���������� ��������������������� ���� �� ��, ���������������� �������� �������������

+ ������ ��� �������������� ���� �� ��(���� ����,��� ������������� �������� ��������������������� ������>���� ����,��� ������������� ��� �� ���� ����� ������� ��������������������

��� � ��� �!��������*��� ���� ��� ����������� �?��+ ��������� �,��� ������������� ������� ��� �����-� �� ���� ����������� ���� �������� ����� �������������������� ����� ����� ���������� ������� ��� � ���� ��� �!�������������������������� ������������ ���������� �� ������ � ������� � ��

,��� ������� �� ���� ��!���������������� ��� �������� ���������( � ���� �

Page 39: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

������������� ������ ������� ������ ���� ��6������ ������ ������������������������������� ���� ��6������ �������� �����!����������������� ��������� �

,��� �����*��������������� ����������������!��� ����� ������������������� �� ������������������������ ��� �,��� �������� ���� ��6��������������� ������ ����� � ���� ��������������

��� ��� ���� ��������������� ���� ���� �� ����������� ���������� �������� ���B-.!����� ��� ���� ��� �������������� ��� ��,��� ������!�������� ���(����B-.�����(���� ������������������ ��� ����������� ���>�� �?!������� ������ ������ �� ������������ �� ���������� ����� ���� ������/4;72�� ������, �������������� ��B-.�����(�!������ ����, ���� �������� ���

�������������A��������A�����1���B�����������������������������������������

)� ������������������� "� ������ !��� � ����������� ������������ ���� ����

��������� ��� �������$������� ���� ����H���������� ����������� H������ �������� ���� !��� �����6����-�&�B-�� ���� ����� ���� ����6L�=$�����-�&�B-L�=$��,������ ����������� ���������� ��&���� �� ��������B-.����� �� � ������ ����� � ����� � �� ������� ���� �6L�=$�����-�&�B-L�=$���� �

����������<��;�� ���� ��������� �� ��� � �� ���� ��!�-6&�;��������������� ����� �����$(K;�� ���� ����� ������� ����� �-�&�B-�� ���� ���������

= ���� ��������� ��� ��������������������� �����/��� ������� ����� ������0��-�����6�� ���� ��� ����� ������� ����� ������

Page 40: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

1��-�����-�&�B-�� ���� ��������<��2��= �������� ���������� ������ ��������� �����>�-'?�

3��' ��� ��-�&�B-�� ���� ��4��' ��� ��6�� ���� �������� � ��� ��� ��� �,������ ����� ���� �������� ����� ���� �!��� ��� �� ��

��� �� ������ ��������-�&�B-�� ���� ��>�� ��1?!����������������� �� �-�&�B-�� ���� ������ ��� ��>�� ��3?��

�� ���� � ���� ����� ������ ���������������!������� ��������� �� ���� ���������$������������ ���� ����� �� ������ ��� ������������ ���������

$�����#&C+/����#&C+<��������� � ���� ����H$ ��������:���H������ ���� ��� �������������*����*����*��*��

Page 41: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

= � ������ �������'#<9�C���������������� � ��= � ������ �������'#<9�C������������� � ����������������� �� �>��������C�=)@G/���(���(CN4O�� ���� �?!���������� �� �>����C�=)@G<?��6� ���� ������������ ����

��C����!��C�;��������� �����C��(C�� ���� ����C�;�����>�C��(CN/O?������� ��� �� ������ ���������� !���������� ����������*���������������� �������������� ���������������������������������������� ��������������������� ��

���������� �!������������������������������ ���������� ���� ���������� ���� ��������� � ������C�=����������>�C��(CN2O?�������� �������������������� ������������ �� ��������� ������� ������������� �� ���!��� ��������������� "� ���������� ������������ �� ���� ����������� �������� ��� �������������

�� !�������������� ����!����������� ���� ������� � �������� ��� �������������� �� ����� ��������� ����� ������� ���� �������������� �� ���� ���� ����� �� ����� �� � ����� ������� ���������

�� ���������������$'<���� ��, ���� ��(, ��������$'<���� ��>����;;����<<�?������� ���<�;�>�C��(CN0O?��������������, ������������� ������� ���� ������ ������� ������� ���, �������������� �������( ����� � ���� �������� �� ���� � ������� ��������������� �

��� �� ������� �������� � � ����� �����<<����;;�!��� ������ ������ ��������������� �������������������� �������� ����� �������� ���� ����� ������ ���������� ��� �,������ ������� ���� � � ����'&$�� ���!�,��� ��������,������ ��������� ������� ����������� ��� ���� �� ������������������

��� ����� !�������� �������� ���� ����� ���� ���������� ���� ��9������� ����9� � ������<�=�>�C��(CN3O?�������

�� ��������������� ������2!�3!�4����5�������# ������ ����������������('�#�N5�2O�� ���'#�;�>�C��(CN<O?������;�������'#5!�'#4!�'#3����'#2�������#!��������� ������ �������������������� ���������

Page 42: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

�� ������ ����������� ��������: �!���,�� �, �����;��������� �� ����, ������������ !��� �������� �� �� ����������������� ����� ������ �� ����������!�

�� �������������� �� ��� ������� ����� ���������������� ���� �� �� ���������!��� ������� ������ ��������� ������ ����,��� �������������� ������� ������� �������������#���

�� ��������������������� �������� ���==�'($ ��

������� ������������������������� �����-�� ���������� �==�'($���������� �������/<���>����������������� ����� ����������������� �?!����� �*���������������������� ��������������������� ���������� ������� ������������� ��

�������������� ���������� �������� � �������� �����������!����� ����������==�'($����� ���� ����� �����������6� ��������������� � �!��� �������������� ���������� ������������������ � ���==�;����!������������������������������ !����������==�(C/�� ���� ���(����� � ������� ���������

� ������� ������ � ������� �==�=��������C��(C�� ���� �����

�� �������������:��� ������ ������ ����� ������� �������������������� �!��� ��� �������������� ����� �� ���� ����� � ���� ��� ���� ������������ ��H�������:���H��#��

�������:����� �� �� ���������� ��������� ���������� �������� ���!���������� ������������� ������ ������ ���� ������������������� ������� �����!��������������� ��� ����� ������������������������������ ��������������, ���� ����������� � !����������, ����� ����

�� ���, � ���� �������������:������ � ������ �������'#<����������������� ���6� � �� �� � �� ���� �!������� ���������� ��������� ��� ���(����� � ����� ���� ���������������� �!������ ��������� ������� ������ �� ��

�����@�=��������� ���� ����� �������� ���� ������������������������������� �����������/4;72�������������� ������������������ ����� ��������� ������������� �������� ��������

������� � ����� � ����������� ����������������>����� ����� ��� ���������� �����,������ ?!������ �� ���� ��� ����������������������������� � ������������ ������

Page 43: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

�������5��/�������������������� ����� ���/����"���.��"���������������7�&�9!��7�'0� ����7�+37)�3�����

����//�������" ���������������7�+37������������ �����" ������������������"�����$�����."��"�

���/ ��� ��$�������"��*37�����."��"�� ���.����.��������������������)

Page 44: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

4=<�)���6���������� �"9

��� ����� ����������� ������������� ��������������������� �!��������� � ��������� ������ ��� ���� ������ ���������� ��������������������� �������������

������������ ��� ���������� ������� �� ���� ������ �����������H��� H������,������ �������� �� � ����������������� ������������������� �������������!���� �������� ���� ����� �,��� ���������������� �������033!������ ���������������, ��������<!�/!�0!�1!�2���033����<!/!�0!�1������ ���

�������� � �������� ����� ������������ , ������������������ ��� ��������������������� ���������������������� ��������� ���,���� ��������

������� �������������� ����( ����� ������������ ������� �,������ �� ������� ��, ���������� ��������������� ������ �� �������� � ����� � ����!��� �������������� �,��� �����,������ ������������ �������� ����� ������������� ���� ����� ���

����/4;72�������7�������� ���C��� ���������� � ��� �������,��� ���� ���������� �� �������������������: ������������ ���� �����7�������� �!��������� ��

Page 45: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

���034��&��������� ����� � ���� ������ �� ������ ��������� ���� ������ �� � � ����� ��� ,��������������� ���� ��������� ������ ����������

��������� ����������,�� ����������������� �� ���������� ����������������� �� ����� ����������C��� ����������,�� ������������� �� ������������������ ��������(���(C�� ���� ����� ����� �����,�������034����������������� �������������� , ���034�������!���� ��,��� ��������� �� ���� ���������,�� ������������ ������������ ���� ��� ����� ��� �����

&�� �� �������������033!���� ��� � �������,��� ���: ������������������� ������ �����������033��)����� ����������������033���: �!��<�;��������C��($�

� ���� ������ �������� ��������� ����� ��������!���������� ���� ���,���� ������ �������� ��������������� ������� ���������� �������������������� ����� � ���<�;��������� ���������� !�������� ���� �����!��� ��, ����������� �� � �� ���# ��� ��� ��� �����������������!���� ����������������� ���� �� �����

�� � � ����������'&29�(�+������������ ����� � ������������� ���(���(C�� ���� ����������<�-�������������������� � �������������� � �� �!���������� ������� ���� �� ��� � �� �����������>��������������?!����������� ���������� �� �����,��� �

Page 46: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

��������� !� ����� ��������� ���� �����������, ��,��� � ����������������� �����

��������������������� ������� �������������� !���� �������� ����� ���������� ��% �*����������� ������� ������ �� ���������� ����� � �������� ��������� �� � ��� �����, �����% �*������ ����� �������, �� �������������� ���

3��������� �� ���������� ���������, �� ��������� ��� ��� �������������� , ������ ��� �� ������� ���� ����������� �������� ���� ����=���������������� �� � �� �������������������!������ ��������������������������� ��������$'<���� ������������� ������� ������������������� ���� ����������������������

��� �������� ���� ���������� ��������������: ���� ����������������� �� ������ � ������������� ���������� ���� ��/�2����� ���������H����H��� �

Page 47: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

��� ���� � ���� ����� ������ ���������������� ���������� ������������

�� ���� ������ ������ �� ��� ����� ���$'<��������������6������������

� ���������������������� ���� ����� � ����� ������������������� ������������&������������ �� � ���������������!������������������� ������������������ !����� ������������ ���B������ ��!���������� �������������������� � ����� ��������������� ����6� ���������� �!���������� �����������

��������������� ��������������� �������� �� � �������# ���� �������!���������� ������������ ������� ���&�� ����� ������ ��������� !���������� � ����� ���������� ��>��������� ���������� � ������ ��?��6���������������

����� �������� �������� ��, ����������!���������� ������ � �!�������������� �������������������, ������������������!�� � ����������������� ���������� ���������� ������������C ���� ������� , ����� � ������� ������ ����������!����������� �����������: ����6)��� ���� ��>����������%'6)�?�

, ������ ���� ��������, ������������������������� , ����� � �������������*�� ��������� ������(� ����� ��������!�: ���������� ������ !�, �������6)����� �������� � ������������������������������ ���������� ������������ ���

������������������ ���� ���������� ������� ���$'<!���������������� ����������-&�������(���(C�� ���� ���#���� ������-&����!��� ���� �������� ������ ������� ���$'<��6� �

�� ���� ���������� ������� ���$'<!���������������������������$'<�� ���� ��>�%';��$'<!�$(K6;��$'<!�#-;��$'<!���?�������� ����� ���� ���6� ��� ���� ���������� ����������������� �!�����%'6)������������������ ������� ���� ���������������� ������� ���� ���� ����� ���� ������ �������� � ����� ��������� �*������!�������� ����� ������ ����������������

�"����������$���������� ���� ���������/��)�:�������������"�������!��"�$� ���

�����������"��������/�������������� �. ��"���)

Page 48: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

(���(C������' ���� �

������"��&�>%8��2�%���(���7� �������?������������������ ������������� ������������#���������/�G�*�������*�� ���������� ���<�G�*�������*�� ���������� ����

�������# !%-�>3���������7��������������?�

�������� � ����� ����������� ��� �!���������������� � ��� ��������� �� ��� �������'#<9�C�����������������/�G������� �� �<�G�������� ��

����� 9���>����������������������������?�

�������� ��� ������ ������� ������� � ������,��� � ��� ���������� ������������!��� �� , ���/92�����������������!���,��� � ���������� ���'&29�<�+������

/�G� � ���������� ��<�G�/92��� ��������

����� 9�!�>������������7��������������?���������� ���$'<����� ��� ������������� ��������'&29�<�+����!���������������

� � ��� ��� �� ������������ ���� ���������������� �� �������������/�G�������� �� �<�G������� ��

����������>%���� ����������/�������?�#������������������ ���� ��� �� ��$'<��������������� ���

/�G��� ���� ���������� ��������������� ���<�G��� ���� ���������� ������ ���� ���$'<

2���������97���'7���4�>%���� ����� �������������?������� ���2$.:���������!� �������������� �>2��� ���������?�������/µ���C��� ������� ������������ ������� ���� �� ������µ��� �� ���� � �����$'���6)��

Page 49: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

4=*�!!�"$��%���������

���/4;72�����42���� ����==�'($�� ���������������� �� ������<<����41����������� ������ ������ ���������� �������������������� ��������������� ���������������� ������ ������� ���������� ��������� ����������)�������

� �� ��� ����==�'($���������� �����������������2<�� ����>������������ �������/4;72���������� ������ �?!���������/����������� ��������������� � ��� �����

��������� !�==�'($�� ��������� ����������������������������� �������� � ����( ����������� � ���������, �� �� ����� !������ ���� �� ����������� �� ����� �� ����������� ���� ���������������������*��� ��� �!���������� � � ��������

�����������, �� �� ����� ���� �� ������������������-�� ���������, ���������������>��� , ���� ���?!���������� �������������� �����, �� ������������ ������ ����� ���==�'($�� �����

�==�'($�� ����������� �������� ������ �������� �������� ���� �� ����������� ������ ���� ������ � �� ���� ����� �

!!%� � .����� ������������������� ������ � !!�%" ������������ �����==�'($�������� ������ �� �� !!�$#' ���������������� !!�$#4 ������ ���� ��� ���� ������������������� �, ������� ���==�'($�

��������� �����������

==�(C/�� ���� ������������� ���� ���������, ����� � � ��������#����3!�4����5��� �

���� �!�������� �������������� �: ����� ��� �������==�(C/�� ���� �������

��������

==�(C/�' ���� �

Page 50: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

����2�!!�)�>77%�8��0�����8��� �����3���������+� �����?�#����� ����������������������������==�'($����� � ���

6� ������������� ����� �!���������������� �� ����������������������� ��������� ���==�;���������������������� ����� � ��� ��� ������������������/�G��������� ����� ��<�G����������� ����� ��� �!�������������� �

����1�0"!""�>0�����77%�8��7�����+� �?�=�������������������==�'($�

�������������� ���������� ���� ����������==�'($������ ���� ����� �������� � �������������������������� �������������� ��>�������,�� �?��/�G� ��������� ��<�G� ��������������

����0�0"!#�>77%�8��0�����7� �������?�=��� �����������==�'($�

������������������� �!���������� �����������������������==�'($��/�G������������ ��<�G��������������� �

����/�0"�>0����������������?��- �����������������������: ������������������==)&�&�� ���� ������ ����� ���

�� ���� ��������==&)'�� ���� ����/�G��������: ���������<�G�� �����������: �������

����<�"%�>�� �������������?�- ���������������������: ������� ����������������� ���� �� ����==&)'���==)&�&�

� ���� ���-�� ���� ��������� �� �������� ���������������������!����������==)&�&������� ����� ��� ������� ������ � ������������/�G��������: ��� �����

<�G�� �����������: �� �������

' ���������==�'($�$ ��� ��- ������� �')������������: ������� ����������������� ���������==&)'�� ���� ����

==)&�&�� ���� ���&����� ����������� ��*�� ������������ ������������!��������� �, ������==)&�&�� ���� �������� ����� ��� ������� ������ � ��������������

-���� ����� ������������������������ �����������==�'($!����������� �������� ��� ���������

&�� ���� ���������������������!��� ���������==�'($����� ���: ������ �������������� ���� ������

Page 51: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

6��������==�'($�$ ��� ������ �������� ��������==�'($������!�������� ����������������� ����� �����

==&)'�� ���� ������������==)&�&�� ���� ���(����� ��������� ������� ��6'������������ ����� ���� �������������6'����������� �� � �!����==�;������ ���������������������������� ��� ������� ������� ��������K��� ��33�����&&���� ��� ������������ �� ���� ����� ����������������� �������������==�'($���������

�� � ����,��� ���� ������ ���==�(C0�������� �, ��������������� !���� � �, ��� � ����,��� ������������ , ���������� �������������==�'($�� �������������� ������ �����/!�0!�1!����2������� � ��� ����������� ���� , ���� �

�� �,������� � �� !�������, ���������������������� ���������������������� ��� ������� � ����� �����������������&�� ��������!��� ����������� � ��� ����������

= ���� ����� ��������������������������� �������< ==�����������������==�'($�� �������������� �������� ��� ���������

3���������//�������" ��0�79������������55��"��."������/���#�����."���.�������� � ����77%�8�!����

�" ������������$��5� ������� ��.�������.��������/���/ �)��

����.����������77%�8��.���� ���/ ��� ��$���� �� ���� ��������������.������� ���.6

Page 52: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

������ ������ ������ ������

����������������������������

��������

1�/�' �� � ������� �������� ��� ��1�0�&�� ����������� � � � ���1�1�6������������� ��������1�2��������� ���, �

• � �� • ����� • ����� • ,������ • � � • "� • �� • � • �� • �� • ��� • ���� • �� • ��� � • ��� � • ����� • �� • �� • � • �� • �(C;�@ • ��� ���

1�3�;�� ���� �� �������� ������������������������

������ ���

�� ������������������� �������� ���������� �������� ����.� , �!����������

������ ���������������������� �������� ���� ������� !��� �������� ���� ���� �������������������B������ � �������� ������������������!�� ��������� �� ����������������������� �� ���������� �����������%����� ��������������� ���������� ����������� �������� ��H��� ����������� H���� �

Page 53: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

���� ���� ��������� � ��� ���!������������ ����� ������ �������� ��!� ��=��������;� ����$� ��� ��� ��!�H��� ����������� H�������������������������

������������� ������ ����������� ������� �������� �������H������ ���: ������ �H����� ���������������� ������ ����������H&�� ����������� H����H��� ��� �H��� �������� � ��������� �������� �� � ������ ������� ���� ���������������������������������� �!������ ��� ����������������� �� �����

����� ��������������� ����� ����������� ������������ ���: ������ ���&����������������������� ������H: ��H����H �H������������ ��H����� ������� H�

����� ������ ����� ��������1������������������ � �������

����������!�H������H�� �� � �������� ���� ������ �������>������ �� �������������� ���������������� �?!������������ ������������� ���� ������� ��� ������ ��� �������� ������������� �������������$������� ������

��� ��� �������� ���������������������� ����������������6� ����������������!�� �������� �������� ����� ������ ����� ������� ��� �� �� ����&� ����������� ��� ��� ������������������ ������ ����������� ������� �� ����: ������ ����������, ���� ��������� ��� ������������� ���������� ���

% ������ ��������� ��� ����������H'=�B'CH����������������� ���� ����� ����������������������6� ��� ���� ��� ��������� ����!�� �� ����/2������ �� ����: ������ ���������� �

��������� ������������� ��� �����!@�������'=�B'C�<<�<<<<�<<<<�/<<<��

-����������� ���, ������ !� ������� ��� ���������������� ��� � �������� ����������� �� ����: ������ ������ ����� ��� � �������������������� ����������� �������!�������� ����H ����H���� ��6 �������� �� ���� ��� �H.=MH���� ��������� ��� ��������

� �� ������� �� � ��������������� !����� ������������ ������ �H� H����� ����� !� ��H� ���� H��(� �������� ��� �!��� � �������� ����� ���������������� ������������������ ����

�&���������������������������������� ���������������� ����� �����> ����?������������� �������������&-������� ���� ������ ������������� �����: ��������������������$�%&#!������������ � ���� ������ � ������� ��

Page 54: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

:='�"��������������������������������

����� ����������� �$�%&#!���� ������� �� �� � � ����� �����!�� �� �������������������6 ��������������� ���������������� ��02<�

�02< � ����� < ;< � �� ����� �*////<<<<* �����

) ��������� ������������������!�� �� �����������< !������������������������������ ���� ����� ����� ��"�� ��*�

:=4�����������������������������

#����� � � �������� ����������� ��� �

• %�� �� • ��������� • (� ���� • )�� ���, � • ��� ��

%�� ����

&������������� ������ �������>� �������� ������� ������?�������� �����������!���� ����������������� � ��� �������������������� , ��� �� �������� ������ ������������������������� ��� ���� ��� ����������������>��������@���������?������ ����������� , ���, ��������������

������ �� ������� �@�������������� � ��� ������������������������� ������������������ �� ������ ������� ������������� ��� �HLH���� �� �������� ���� ������ ������10�������� �������������������������������� ������������� ������������

�����������

Page 55: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

������������ ���� ����� �� ������� ��� ������� ��������������� �!���������� �������������������� ���������������� ����� ������ ����������� ���� �����

� ����� ����������������������� �����������H���� H������ �������� ���� !�� ����� ���: ��������� ����������� ���� ������������,�������������� ��������� ����/4;72���������� ��

(� ������

(� ������� ��� ���������� � � �������� �������������� ��� ��� ����� ���� ���������������������A����������� ���������

��� ����������������� �� ����������������������� ������ ������� ��� ���������� ��� ������� ���� ������������ ����� ������������!����������������������� �����

H�H���

)�� ���, ����&����� ��A�������������������������!��������� �������������������� � � ���

�� ���������� ���� �!����� �� � ������������ ����������� ���� ����������� ���� ����)�� ���, ���� �����������, ������ ����� �����,���,������ ����� ���� ����;�� ���� !�%=K=%����� ���� ������������,������ ���'&$�� ����������� ���<)�������������!��� �,������ ������������� ������� ���� �� ��,���%=K=%�� �������������

������� ��� �������������� ������ �������������������������� � �� ������ ���<)���������������������%=K=%�

Page 56: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

:=:�0����������������������

�� �������� ���� ���������� ��������� ������������� ������ ����������� �� �� ������� ���������� ����

�6� ����������������!�� ��� ������������ �!��� � ��� ������� ���� ��������� �������� �������� ����� ����� ���( ����� ����������� ��� ��� ����� ������������� �� ����!�������� ��������� �!�����, ���!���� ��� ������������� !���� ������������ �������������� ���!������ �������� �*���� �

-�� �������������*��������������� ���� �������������!������������ ����

������������������ �� ������������ ���������� ������������ ��������������� ����� ������ �������������������������������&�� ���� �� ������ ������� ������ !��� ����� ��A�������� ������ �����������

Page 57: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

�������� � ���� ���, ���

���� ������������� ���!�� ������� �� �� , ������������� ������� � ������������������ �����������!����� �� ������������� �������� ��!�������� �� ���� ����� � ������������� ��� ���

&������������ �� ������� ����������� ���, ���_CONFIG _CP_OFF&_WDT_OFF&PWRTE_ON&XT_OSC��6� ������� � � �� � � �����, �� �� �� �!�� ���������������������������;����!������� � ��������� � ��� ������� ��������������� ���������� ��������!������������ ���������������������������>���< <<?��

�� ����� ��������������� ���������������������� ���������������>���< <2?��-�� ���������������� �������!���������� � ��������� ����� ���������� ������ �� �����������������������H�������H������������

��� ������������������� �������� � ���� �������/�>#&C+/?����� ������� ����'�-#�� ���� �!������������#����� �� ���� �������������>�,���< <<!��,����'�-#?���

��� � ���� �������� � ���� ��������<�������� ��������������� ������#�>�,���< ;;!��,����('�#?!����������� �������������������� ���

6 � ������� ���� ������� � ��� ������������ �� ��������� �*��H��� �H������ �����������;������������ !� ������ ���������� ��� � ��� ���������� ��� ������ ��� ������ �� ����� � � ������H �H������ � ���� ������������������� ���� ���������������������� ������������� ����� ��������

:=+����������� ��A��

1�/�P)=;�C=���������= ���� �� ��������� �������� ����

�����@��P� �� N� �O�QN��� ��� �OR��

%�� ��������=������� �N� �O���� �������� ��������!���������� � ���� �����N��� ��� ��O���!@�������

#define turned_�/�

P� �� ���� �_off 0�������������� ��A����PBC)=;�C=!��;)=;!�;C)=;���

1�0��C�%B)=�������������� ��������������� ��������������

�����@��P����� �N��� L�� O��P����� �H��� L�� H��

Page 58: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

�%�� ��������

&��������������������� ���, ������� � �� �������������� � ��� ���� �������� ���������� ��� � ��� �H����� H���� ���, ��������������� ���� ��� �������� ��"��� ������ ��!�� ��� �� ���������������� ����� !����������������� �"�����������!�� ��� �� �������������� ����� ���� ���� ���, �H����� H�������� ������� �� ����������

�� ��������������!@�������#include <regs.h> #include "subprog.asm"��

1�1��(C-�&C�������@�, ������������ ����,��� ����� �� ������ ��������

�����@��������N�� OGN,��� O��%�� ��������

=������� ������N�� O���� ������������!���������� �� ���� �������N,��� O���!@�������Constant MAXIMUM=100 Constant Length=30 �

������������ ��A����-=�!�K&'�&#%=��

1�2�K&'�&#%=��������@�, ����,������ ��� ����,��� ���� ������ �������������@��K������ N�� OGN,��� O��

%�� ��������#��������������� ���, !�� ������ ����������� ������������������,��� ��������� ��������(C-�&C����� ���, ����������� ������������ ���� ���, !��� �,��� ���

� ������ ����������� ����� �����!@�������variable level=20 variable time=13 �������������� ��A����-=�!��(C-�&C��

��

1�3�-=���������) �������� ��� ��,������ �������@��

N�� L,������ O� �N,��� O��

Page 59: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

%�� ������������ �,������ �N�� L,������ O������� �� �� ����N,��� O��-=����� ���, ����

����������=JB!����������-=����� ���, ��� ����� �,������ ����� �� � �� ������������ ��������!@�������level set 0 length set 12 level set 45 �������������� ��A����=JB!�K&'�&#%=��

1�4�=JB�������) �������� ��� ���������

�����@��N�� L�����O� "��N,��� O��

%�� ������������ ��� �����������N�� L�����O������� ��,��� �N,��� O��!@�������five equ 5 six equ 6 seven equ 7 ���������������� ������-=���

1�5�('@������) �� �������� ��������������� �������������� ������������� ��� ����������@��

N��� �O��N,��� O��%�� ��������

���������� ������� "� ������ ����� ���, ��6������ �� ������������� ���, �� �� �� ��� � ��� ����������������������� ����������� ��������� �������!@�������Start org 0×00 movlw 0xFF movwf PORTB ���� ����������������������������� �������*��*���� ���, ��� ���� ���������� ���<<!������ ��� ������������� ���/<���

1�7�=C)������=������������������@�� ��

�%�� ��������&���� � ���� ����������������� � ������������ �* �*���� ���, ����������� �����

Page 60: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

������������������������ � ��� ���� ��������������� �����������

!@�������. . movlw 0xFF movwf PORTB end

1�8��;��������������������������������������@����N�������L� ��O�

�%�� �������������������N�������L� ��O������ �!���������� ���������������������;�

��� ���, ������� � ��� ���&�����������*�!��� ��� �������������=%-=���=C)�;���� ���, ������� � ��� �����!@�������if level=100 goto FILL else goto DISCHARGE endif �������������� ��A����P=%-=!�=C)�;��

1�/<�=%-=������������ ���� ����, ���*�;*��������������������������� �����������@��

=�� ��%�� ��������B� ��������;���� ���, ��������� ����, ������������� ���������� ����

�!@�������If time< 50 goto SPEED UP else goto SLOW DOWN endif ���������������� ������=C)�;!��;��

1�//�=C)�;��������=�������������������� ������������@��

Page 61: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

�����

%�� ��������)�� ���, ��������� ������ � ���������������������������� ���� ���������������������������� � ������ ���������������

!@�������If level=100 goto LOADS else goto UNLOADS endif �������������� ��A����=%-=!��;�

1�/0�6.�%=��������= ��������������� ����������������������� ����

�����@������ N�����O���� ���

�%�� ����������������� ��� �� �6.�%=����=C)6������� � ��� ���������������������� ������������������ ��� ���,����!����������������� � ������

�����������������=C)6��� ��C��� ��������������� �� �6.�%=����=C)6����� �/<<������ ����!������� ���� ������034���

!@�������While i<10 i=i+1 endw ��

1�/1�=C)6���������=��������������������� ���������������@��

����%�� ������������������������� ������ � ������ ���������6.�%=�����!����������� �����

������������������������������ � ������ ���������������!@�������while i<10 i=i+1 endw ������������� ��A����6.�%=�

1�/2��;)=;���������= ������������������ �����������������

Page 62: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

����� �� ���������@����� �N� ������O�

�%�� ������������ �������N� ������O������� ,������� �� ��>�������������P)=;�C=���������?!���������������������������� � ��� �������=%-=���=C)�;�

��� ���, ���� ��������� �� ��� ����!@�������#define test . ifdef test ;how the test was defined ......; instructions from these lines would execute endif �������������� ��A����P)=;�C=!�=%-=!�=C)�;!��;C)=;!�PBC)=;�C=��

1�/3��;C)=;�������= ������������������ ���������������������� �� ���������@����� �N� ������O�

�%�� ������������ �������N� ������O��������� ,������� �� �!����������� ���������� ��� ���������� ���, �PBC)=;�C=!���������������������������� � ��� �������

=%-=���=C)�;���� ���, �������� ��� ��� ����!@�������#define test .......... #undefine test .......... ifndef test ;how the test was undefined ..... .; instructions from these lines would execute endif �������������� ��A����P)=;�C=!�=%-=!�=C)�;!��;)=;!�PBC)=;�C=�

1�/4��#%(�+���������) ����������������� ��� �����������

�����@��������QN� ��OR�����������N��� �OQ�N��� � �OR!�N��� �OQ�N��� � �OR������� ���

�%�� ��������)�� ���, ������ ������, �,��� ������ ����������=������������ ���� � �, ����

,��� ��� �� ����� ����������� ����������N��� � �O������ � �����������, !��� �,��� ���, ���N��� � �O������ � �������� ������ ���������������

Page 63: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

K��� ���N� ��O������ � ������� ���������,��� ���������������, !����������� � ����� �: ����

�!@�������Cblock 0x02 First, second, third ;first=0x02, second=0x03, third=0x04 endc cblock 0x02 first : 4, second : 2, third ;first=0x06, second=0x08, third=0x09 endc �������������� ��A����=C)���

1�/5�=C)����������=���������������� �������������@�� ���

�%�� ��������)�� ���, ������� ������� � ������� ����������������������������� ����������������������������� � ��� ���� ���������

�������������� ��A�����#%(�+�

1�/7�)#������������) ����� ���� �������������@��QN��� �OR���N� ��O�Q!�N� ��O!�����!N� ��OR��

%�� ��������)�� ���, �� � �, ������� ����������� �����6� ��� � ��� ��� �� ���������� ����� ������ ������� � ���!��� �������� ������ �� ���� ����� ����

!@�������db 't', 0×0f, 'e', 's', 0×12 ��

�������������� ������)=!�)���

1�/8�)=������������) ������� �==�'($�� ������� ��������@��

QN� ��OR�� �N� ��O�Q!�N� ��O!�����!�N� ��OR��%�� ��������)�� ���, ������ ������ �����==�'($�� ������� ��=, ��������������������

�� � ��������==�'($�� ���!���������� ��� ���������� �������������� �������!@�������

Page 64: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

org H'2100' de "Version 1.0" , 0 ���������������� ������)#!�)���

1�0<�)������������) ������� ���������� ���

�����@��QN��� �OR����N� ��O�Q!�N� ��O!���������!�N� ��OR��%�� ��������

)�� ���, �� ��� ��'=�%6�� �� �������������!� ����������� �� ����� �����!@�������dt "Message", 0 dt first, second, third �������������� ��A����)#!�)=�

1�0/�L�(C;�@���������- ������� ��������������������

�����@��L�L����N� ��O��L�L����N���� ��O!N� ��O���%�� ��������

(��������!������������ ����������������� ����� � ������������ �� �� ���# �� �������������� ���, !��� ���� ���������� �� �� ��������'(�=--('���� ���, ����

!@�������_CONFIG _CP_OFF&_WDT_OFF&_PWRTE_ON&_XT_OSC �

������������ ��A����L�)%(�-!��'(�=--('��

1�00��'(�=--('����������) �������������� ���� ���������@����� ����N��������� �L��� O�

�%�� ������������������ ����� ���� ������������ ���� � ��������������� ���

!@�������processor 16F84

:=;�)����� ��������������������������������������

&����� ��������� ���� ����������������������������� ������ ��� �������� �� �� ����� ����� ��

Page 65: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

• = ��������� �>������LC�� �.=M?� • ������� �������� �>������LC�� �=''?� • %������� �>������LC�� �%-�?�

�� ���������� �������������� �������������������� �������������� �����

����������������� ����������, ��������������������� �!�������������� ����� � ���������� ������ �� ������ ������������� � ����������� � ���� ����� ���� �����������!�

���������� � ���� �������� �������������������������������� ����=��������� �����, � ������H����H���� ����� ������������ ������ �������� ��������� ������������������,� ������ �*����*���� ���� ���� ������ ���������� ������ ������� ������������� ���$������������������� ������!�

��� �����������������������������������,������ ����� ���!��� ����������:������

= ���� ���*����*���� ������ �������������������� ���������&���� ������ ������� �������� ����������������� ���� ��� !���� ��� ��������������� �!������� ���� ���;���������������������� ������������� ������ � ������������������������������� ���- ����������������,��� ������,������ �� �� �����

����� ���� ���, ����-=�!�=JB!�K&'�&#%=!��(C-�&C�����#%(�+����������������� � �, ������� ���������������� ������������������������ ��������� ��������������������� ��� ���������������������� �*����� ���������� � ������������� ���� �� ���������������� ����������� � ��������� ���

Page 66: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ
Page 67: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

���

&���� � ������ �H����H���� ��� � ���������� ������������ ��������������B� ���� � � ����*����*���� ��������������� ���������:�����&���� �, ��� �!��� � ������ ������������������ �������� ��������� �������������

Page 68: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

����������������������������

�����

���������

�2�/������������ �$�%&#�������������� �2�0�6 ��� ���$�%&#�2�1�) ����������� ���

2�2�) ������ ��&�� ��� ����� �2�3�6���������������2�4�����������2�5�$�-�$���������

������ ���

$�%&#������6������������������ ��������� ������������� , �������������� ��� ������������ ���� �� ����� ������ , ����� ,��� ����������������������

������ ����������� � ���������������������-� �� ������������� � �� ������� ������������ ������������ ���� ��������� � ���������� �����, ������)=�H�� ���� ��) , ��� ��=,��� �H��� ������ � ��� ������������ �$�%&#��

-����!�������� ������ �!��� , �������� �������� ����� � ���� ��������� �������������� ������������������ ���������� !��� ������ ������������ ���� !������� ������� � ��� �����������,������ �

+='�����������������������6�����

$�%&#����������� , ������������

Page 69: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

��@�������� ���� ������� ����� ���� ���>��� ���$��� �?���@ ������������ ��������������>� ��=����?�

��-������������ ������ ���������� ������������������������������� ���������� ����# ��� ���� � !��� � ��� ������������ ������$���������������������������-�����

����������)�>����������) ���� �?��&����������� �����, ���� ��!��� �������� �� �� ����������������$������������� �� "��� � ���������������� �$�%&#��� ��

�S������������ ������ ��274������� ��S�$�������6�����1�/ ���6�����83���� ��, ��������� �6������ ������

���� ��S�K@&��������������S�7$#�� ����>10$#�� ��� � �?�S�0<$#������ ����� ������������

S�$�� ������ ������������ �$�%&#�� � �������������������������������������� �����

������$�%&#���� �������� ��)�������������������������� ����� � ����������� ���� �������������� �������� ���������� ,���� !��� ��������������� � �������� ����� �� ������ ������ � �� � ��������������� ��������������� ���� ��������������������6��������������;��������� ���� �

6 ��� ���� !��� ���������� ��� ���������� ������������������ ��!���!������ � �!����� ���� �� ���� ���������������������� ������������� ��������������-� ���������������$�%&#��

�/��-���������� �$�������6�����0������ ��� �$���������)�����)�'($����, �

1���������-�&'������ ������� ����� ������ ���� ������� ��� �'BC������2���������#'(6-=����� � ����)�'($����, ������������ ���3��;������ ��������� ��$�%&#�������)�'($�4���������$�%&#�,4�1/�=M=������ ��(+���

5��������������(+�������'BC��������

���������� ������� ���� � �� , ��� ������ ������������ �� ������� �������������������� ���&���� �, ���� ����!���������� �������������� �������������������� ���������������� ���

��������?����0����������������0�?���

Page 70: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

�� ������������������������������ ������� ����������������!����� � �������������������������������������� ����

0�� ���� ��������������A� ������6���������������������

C �����-����� �%�� � �&�� � ������������ ������� ��������� ����������!�� ����� �������!�� � ����� �����H��&�� H�����������C=M��

Page 71: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

���1������ ���������������1���1

�� ��������������� ����� �������������� ���B� ����� � �������� ������� ��������������� �� ������� ������!����������� �, ����� �

%������������������������������ ������������

Page 72: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

C ���������� �������� ������� ���� ,����, ���>�?���$�%&#������� ����� ������������������������ ������������� ���� ���������� ��������������� ���� ���

, �����&�����������������������������������������$�%&#!����������� �, �C(�� � �� ������������C=M��

$��������� �����������������������A����A������

����������6�:������������������������������ �� ��������������� ���$�%&#������� �������� ����������C=M������� �����������������

Page 73: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

������������������������������

������� ��������� ��������, �$�%&#������ ����������������� �������� ���������� "� �����6 ����� ������������T ����� !��������C=M������� �����������������

Page 74: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

�����������������������������

' ������������E��������C=M������������������ � � ��������� �����������

� �����������������������

����������� ������� ���!������ ������������������� ����� �������������������� ��������� ������ ���� ��

��������������1�

&�� ��������������, �� ����� � �!��� � ��� ������������� �!� ������ ������

���� ���������� ��������������, ����������� ����!������ ��� ������� �� ��� ���� ������ ����� ��>' ��� �� �?���, �� �!��� ������� ����� ���� ���

Page 75: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

.��1�"!�%�!������C�

���� ������������� �B-#�� ,�� ����, �����$�%&#���)0���� ����������� ���������������������� �������� ����� ������)����0M���������� � ��� ����������������� �� �������������������������� ������������������������� ��� �$�%&#���)0����� �� ������ �B-#�������� ������������� ����� � � � �

#�����������;����!�������������$�%&#��������� ��

Page 76: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

+=4���������� ���������

;�������� �������������� ��� !�������������$�%&#�������� ����������� �� ���������&��������� !�$�%&#������� ���������������6����������������

����� ��>��� ������� ���������������������;�� !�=����� ���?!�������>��������� �����������������������?��������������� �� ����� ��������� ����� � ��������� �����������6��������������� ������� "� ������ �������������������������� ��� ����� �� �!��������!�� ������� ����� ���� �

"������������ ������� ���������� ������!�����������, ����� ����������������, ����� �� ���

Page 77: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

���� ��������������������������

+=:�%�������������D� �

�� �������� ����������������������������� ��������������� ���������� ���

/��) ����������� ���0��6�������� ��������1���, �������: �� ��� ����� � ���� ������������� �!��� ����������

Page 78: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

$�����������1���D� �

���� �� ������ ��!���������� ������'(A=�������� ���������'(A=���6�F&')!������������� ��� ���������������

Page 79: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

�������������1���D� �

�������C=M�������� ��C ������������������� ��� ���������� ���������� ����������� !����������/4;72&�

Page 80: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

������������������������ � �������

C ���� ������ ������� �������������� ���� ��� ����������� �������&�� ��� �!���� ������� � ���������������������� ����� �� ���

Page 81: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

���� ������������������������

&������������ ���������� ������� ������ ��� ��������� �� ����� ������ ������ ������ ������������ ������� ���� ���������� ���������������� ������������������������, ��������� ��� ���������������������� ����� ��� ���� ���� ����������������� ����� �� ���

Page 82: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

#������������D� �

B��������� ���� ��!��������C=M����� ��� ��������������

Page 83: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

�������� �������������������������������

�������;�C�-.����� �� ��� ���� ������ ���������������������� ���� �������� � ���

+=+��������������1���������������

6� �*��� ��*���������� �������� !��� ����������� ���������� ���

#�1���D� �������

C ���� �������������� �������!�������� "��� �� ����� ���� �� ����������;�%=�

O�C=6!�� ����� �� ��������������� �$�%&#��������� ��> �������� �� � ����� ���� �������������� ������ ��?��B��� ����� � ����� !����������� ���, ������� ����U������ ������ �H#�������H!���� �� ����� ����� ����� ��������> ���� �������������� ������������ ������#?�

C ����� !�H#�������H���������� ������ ������� ���� ����'�������������� ����� ���� ����� ������H� ������H������������� �����������������������,������ ����������� ��� ������� !�H&���;�� �H�

Page 84: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

������������1����������������������D� �

���� ����� ������ �������������������� ������ � ����� ���� �H#�������H!����������� ����� �� ���

���� ���������������������

�� ��������� ���� ����������� ���� ���� ��������������� ������ ������������� ����� �� ���

Page 85: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

+=;�0���������������

(������ ���������� ��� � ����� ��������, �� ����� � ��� ��� ���� ������������������������������������ ����������������������� ����� �, ������������� ��� ����������� ���

Page 86: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

T��������� ����� ��� ������������ � ����� �����������������9���� ���������� ������

Page 87: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

�����1���1�1��������=����������

6� ��� �������������� ����H#�������H�����!�� ������ ��'(A=����O�#B�%)�

&%%���������������� ��� ���������� ������ �.=M�������� ������� � � ����� ����������� ������������� !�� ���� ����������� �� ������������������� �����������H#B�%)�-B��==)=)H������� ���� ������������������������������ ���������������� � �� � �� ������

������ ������ ����� ��������!���� �������� �������� ����� ���� ���*(�����*��������������������������������� �������� ���� ��� ��������!����� ��� ��� � ��� � �������� ��� � ��

+=(� ������ ��

�� ������������ �������� ���� ��� ���������� �������������

C ��&�� ��� ����� ���������������� ���������������&�� ��� ����� ���� ��������� ���

(� ��� ������&�� ��� ����� �

-�, ��������>&�� ��� ����� ?

Page 88: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

����� ������������� ��������1������� ����������������� ����� ������&�� , ������������ ���������������� ����� !��� � �������� ��������� ��

����� ���B��� ��� ��� ,���� !���������� �, ���� �� � �� ��� ������ ����� ����������������������

���� �� ���6� ���������� �� �������� �������!���������� ������������!��� � �������� ����� �����,��������������

������ ���� ���������&�� ��� ���������������� ��

. �������� ���� ���� ��������� � ��� �$�%&#�

�� �� ��� ����� ����������������������������������������,������ ��� ���6�:������������� ��� ���� ��������� ��� �������� ����� ���������� ���� ��

(� ��� � ��������� ������� ���� �������������� ��������� ��� ,����� ������

-�, �6������ ��6������ ���, �������������� ��������� ������ � ����������� �����!��������

�� ���*����� ������ ���� � ����������� ������� �������������� ��� ������,����-�, �������� ����� "� ���!� �� ����������������������������� ��������������������������, ��� ������� ��������������

#����������

6� � , ����� �������, �� ����� ���������� ������������� ��!�������� ������ �� ���� ���&������������� ��.=M���� ������������!��������������� �������� ��� ������ ������� ��� ����� ������ �������������������>$�� ?�������� ���� ���� ��������� ������������ �� ���� ���� ���, �������� �.=M���� ������ ����������

-����������� ��� ,������!� � ���������� ���� ���� ������������� �������������� �&�� ��� ����� ���� ��� ������ ����� ��

+=<���������������

-�������������������� �$�%&#� ,��� ����������,�� ����� �� �������������� ��������������������� ��������������������!�� ������������� ��,������ �,��� �!�� ���� ��,��� ���������������������������������!����������� ������, �

�� ���� �,��� ������������������������������������ �>��� ��� � ���, �� � ������ ���� ?!������������������� ���������� �� ���� �� ���������#������������ ������������������������.� , �!�������������� ����� ���� ���������� �

�������� ��������������������� ���� ��!����� � ����������� � ��� ��������� ������� ����������� "��� � ���> �� ���������������� ��������� �����?��-�������!������ ��� ������� ��H������� ���� ������������������ �H��&����������� �� ��� ������������ ���� !��������������� �, ����

������� ���, �������������������� ������� ��>�� ������ ?���������������� ��������������������� ���������� ���6� ��������������� � �!������������������������������ ����������� ��������������������������!������ �����������

� ������������B������ ��!��������������� �����������!����� ������,�������� ��!��� ���� ����' �������������� ��������� �������!����������������������������������

Page 89: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

�����������������������������

-���������������,�� ��������������)=#B@@='�O�-=%=����((%�O�$�%&#�-�$!������������ ����� ���, ��;��� ��������� ������� ���������� ���� �� ��� ���������������!������, ��� ��������� ����

-������� �������� ��������������� ���6� ������ �!���������� ��� ���� �������������H���� �H������ ����� ���>����������������� �� ����)����� �?�

-����������� ��������� ������&�� ��������������� ������ �!�������� ���������� ����� ���� ������� �������������� ��

-� ���������-� ������� ��������� ����������������������� ��� ���� ����� ������������� ����� � ����� ����������������� ��

-�� ������ ��� ,������!� � ������� ���� � ���� ����������������� ��

' � ����� ���������� ����������������������������� ����������� ������ �� ������������������������������� ���

;����������� � �����!���������� ����������!������� � ������������� �������

)=#B@@='�O�'=-=������������������������ �� � ������������������ ���������� ����� ���� ������ ������� �� ��������� �������!�������������� ����%��������� �����: ���������������� �� �������� ��+������������������������

( ����� ��������������� ���� ������� �����������,� ��� ���� �������������������������� ����� � �� ���� ����� ��������� ���� ������������ ���� ��!���-;'�

Page 90: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

� ���� ����6 ����� ��������������-;'�� ���� ���������������K�=6�O�-�=��&%�;BC���(C�'=@�-�='-��

�# ��� �-;'�� ���� ��!��������� ��������, ���������������� �� ���� ����6������������ �� ���� ������� �� ��������������K�=6�O�;�%=�'=@�-�='-�

����� � ��� �,������ ������ �������!������������������� �!����=����,������ ��������� �� ������>0 ��"�0����.�?�������������K�=6�O�6&��.�

���������1�����������������������������������1���1�

6� ������� �,������ ������ ���� ������� � ����� ����� ����� ������������������ �!��������������� ����C ������������ � ��� �������3�����������84��!����� ����������������������� �������-�� ������������ ����� ��,���� �����!������������;5���;7�

���� �-;'�� ���� �������!�� ������ �, ����� ���� ��6�� � �, ��,��� �< ;;����� ��, ������������#�

#�����������;5�� ������!�� ��*������ , ��������� ���� ����������������, ������H����� ���H������� ���������� ���� ������� ���� ������ �� � ����������������������������� ������� ������������ ��������� ������ �>����������� ������������?!������������������� ������ � ��

Page 91: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

����������������������������

�� �����������������

��

��������

3�/�$�����3�0�-����������3�1�$������� ������ � ���� �

������ ���

-�� ������������ "� � ������������������ "� ������ �������������������

&�� ����������� ����, ���� �������������� ������ "��� ������� ���� ��� , ������� �� ������� ����������������!�� ���� ������ ����� ����������������� �������������������� ����������� ��� ��������� �*��������������-�������

��������� ��������� ���� ����� �� ���������������� � �� �����;���������������������������!����������������������� ��� ��

;='��� ��

$�������� �� ���������� ���, ��� ����������� ��� ���������������� � ������ � �����������!�� �������������������� ����� ��� �� ��� ������������ �

�� � ������������� ������6� �������������� ������������� ��� � �!�������� ���� ������������������ �� ������������������ ������ �����*��� ������

����L�� �� �����/!����0!�� � � ������������� � � ������������� � ����

�� ������ ����� �������������� ��������� ����� � ����, ��������������,���

������������ �������&���� ���� !�� �������� ������������ � �������������-;'�� ���� ����������������� ����������� ������������������� ��� ������, ���

�����#&C+/������������������,������ ���: ������������������� �� ���� �� �� ������������������������ ��� ���������<����/��� �� � �� ������ ��������� ���������3�>'�<?����������� ���� �!����� ��� ��������� � ��� ���������5����C��(C�� ���� ���;���������������� ��� ������ � ����������!����� ��� ����� ��� ���������� ��� �������

���< �� � ��$�������< � ����-�&�B-!�'�< ��' � ��'�<�����G�#��< � ���� ��=��������

Page 92: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

� � � ���/ �� � ��$�������/ � ����-�&�B-!�'�< ��- ��'�<�����G�#��/ � ���� ��=�������� � � � ��� �� �� � ���� ��������� ��������� ��� � � �����C��(C!�5 ��- ���� ���� � ���� ��=�������� � � � ������ �� �� � ���� ��������� ��������������� � � �����C��(C!�5 ��' � ���� ���� � ���� ��=��������

�� � ��������� ���� ���, �������� �������� ������ � ����C��>���� ,������C�%B)=���� ?���� ������������ �������� ���� ����������������������������!����<�������/�

� �����2 ���� ���2 �� � �����4���5���������� ���� �����������/�����" ��

�� ���� �!��������������4��� ��������������������"��� /��;��)�<����.�����

2�9=�7'���3�2� ����"��� ������� �������"����3�2����������.���������������)

&������ �� ���, !�����������������������, ������ � ����.� , �!�

����� � ������� ��� ����� � �������������� ���������� ��������� �������������!��� ��������������� ������������������������ ����� ��������������� ���������� ��������� ��>������� ����������������� ���������� �������'�-#�� ���� �!����/?�!��� ���� ���*��������

Page 93: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

���� �� �����/!����0 ��$�������� � ���/ ������ ������� ����'�-�� ���� �� � �������/!����0 ��- ���� ���, �������/�G����� � ���< ��$�������� � ��������< � ���� ��=�������� � � � ����� �� �����/!����0 ��$��������� � ���/ ������ ������� ����'�-�� ���� �� � �������/!����0 ��' � ���� ���, �������<�G������ � ���< ��$�������� � ��������< � ���� ��=��������

$�������������� � ������� ����� ������������������

������ "���7�<�����������'#5���������

6� ������������������������ � ���'�-#���� ������ ����� ������������ � �!�� � !���

����*��� ������������ � ��5���� ������ �������� � �����0!������� �������� ���������� �

����� �'�-#!�5 ��$��������� � ����-�&�B-!�'�< ��- ��'�<�����G�#&C+/ � � ���'�-#!�5 ��) ����� �'#5��������� � � ��-�&�B-!�'�< ��' � ��'�<�����G�#&C+< � ���� ��=��������

&���� ���!���������������� ��������� �������� �� ���� ������ ��� ��$������������������������� ��������� ������ ���� , ������ �������� ���� ��� � ������ �������!��� ���������� ��� ������� �� ����������� �� ���

������ �*�� � ����������, ���� ������� �!������ ���� ���������������� ���: ��� ��������� "� �����������������

����� ������������� ����� ��!��� ����, ���� �� �� ����������������� ���� ���, �� ����&���� ���� !�� �������� ����������������� ��������������� ��$�������-�&�B-�� ���� ������ ������������������ ���� !� ��������������� ����� ��� ��

����� �� ����� � ��$��������� ��� � = �� ��) ������������� ������������

Page 94: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

� �� �= �� ������G<��������= ������ ������� � ������� � ������G/��������������������� � � ������ ������ ������� ����� = �� � ��%������� ������������ � ���� ��=��������

;=4�����������

-���������� �� � ������ ��������������� ��������������� ����� ����������� �����������������������.������������,���� �, ��������������������� ��������������������� ������� ����������������� ������� � ������� � ��� �� , ������ ����������� ����������� />� /����� ��� � ������������

B��� ���������������������!�������� ��������� ������� ��� ����� ������ � ������������������� �������) ���������������������� ����� ������ � ����� �������!�� ����� ������� ��� �������������������� ��

%�� � � ������������������� �������H�����%�� �H � � ������������� � � � ������������� � � � ������������� � � �������������1 �

6���������!��� ���������������������� � ������, ��������������6��������������!��������������� ���� �� ������ � ����������� �����������������

� �� �������������-����!������������ ���� ��� � �� ��,������ �������� ������������������������ � ���

�������� ��� , ��������� ��� �����,������ �!���������� ������������������ ���� �!������ �� ������� �,������ ��������������, �� ����� ������� �����������

�� �������� ���� !� �������) �/���������,������ �!��&'/�����&'0!������� ��

�� �� �������,������ �'=-��&��0���� �,������ ���� ���"� ���!��� ��������� ����� �������� �� �� ����� �������� � ���� ����������� ������"��� ������ ������������������ ����� ����,������ ���&'/�����&'0!��� ������������� ����� ����������� ����� ������� � ���033�>�� ������������� ?������������� ����,������ �'=-.�

2 ������55����������.����/ ���� ����������� /�����" ���"��/ ����

�� ����5���������5�������������,�� ������"������� //���5��/�

������� ���$����-� ���� ��" 4�������.��� � /������."����

�������� /�� 4���/�/��$!������ �����" 4�������.��� � /�����)

Page 95: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

;=:��� ����������������@������

= ���� ����, �������� ��4��� "� ������ ��������5���!��5������!�����$��!����� ���/�!����� � ������� � ���� ����� �������� ��������������������������� � ���� ����������� ������������������� ��������� ��� �!��������

Page 96: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

� � ��������� ����������� �������������� ����&����������� ������ ������ ���� �/������1)������� ��� ��� � � � �

;=:='��E�������������������������

����� �� ������/!����0!����1 � ���� �����/!����0 � �������1 � ����

$����������� ���������������' ���� �!����!���� �

;=:=4��E���������������������� ������

������ �� ������/!����0!����1 � ����������/!����0 � �������1 � ����

$����������� ����������������' ���� �!����!���� �

C �� ���� ������������� ������������<������&������ �� ��������� �!������������������� ������55!��� ���� �������5������� ��� �!���� ������� ������������� ��������

Page 97: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

;=:=:��!@��� ��������7�����������������������A�������

���������� ���������������������������,������ ���%�)���5� ����������

������ �� �����< � � ��� ���< � � ��� ��= ��/ � � ��� ��= ��0 � � ��� �������, � � ��� ��C ����, � � ����)��/ � � ����)��0 � � ����)��1 � �����, � � � �A�����<!�� � � �A1��)��� �� � � �A�1��/<< � �< �� ��)��/ ������������� �������� � ���1��)��� �� � � ���� �-�&�B-!�� �

Page 98: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

� ����< � � �� ��)��/!�� � � ���1��)��� ��!�� � = ��/ �A�1��/< ���������� ������� � �� ��)��0!�� � � ���1��)��� ��!�� � � ���� �-�&�B-!�� � � ���= ��/ � � �� ��)��0!�� � � ���1��)��� ��!�� � = ��0 �A��)��� ��!�� ��������� ������� � �A1��)��1 � � ���� � � � �

$����������� ��������

��A�1��/34 ����G�/34 ��A1��'=- ��'=-�G�� ���������'=- ����) �/N�/!�) �0N�3!�) �1N�4

�� �������� ���� ������������� ����������$�������������&���� �

� ����!�� ���, ���� �� �,������ ������������� �� ����!�:�� !�:���!�:���!����� �������� �������,������ �:����/��

Page 99: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

;=:=+��-������������������������� ����F'G(;;:;��H

����� ��������������������,�� � ������� �� ��������������

����� �� �� �����/ ���� � %�/ � ���� � � ��� � ���� � ) ���/�� � ���� � %�0 � ���� � =� � � �A�1���������/ ��.��� ����� �������� � ��/�� ����.��� � �A1��.��� � � �A�1�������/ ��%� ����� �������� � ��/�� ����%(�� � �A1��%(�� � �%�/ � � � �A��%(��!�� ��) �� �� �.�������%(��� � ����� � ���� �-�&�B-!�F ����� ������� ��������������������) ���/�� � ���� ��� � � ����) ���/�� �

Page 100: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

� �� ��%(��!�� � � ���%�/ � �� ��� � � � �A��.���!�� � � ���� �-�&�B-!�F � � ���=� � � ����) ���/�� � � �� ��.���!�� � � �� ��%(��!�� � � ���%�/ � �) ���/��� � ��) ���/�������� ���� ������ ���� ��� � �A�1��/<< ��/<<V/<��G/�� � �A1��%((��� ��%((���N�/<< �%�0� � � � �� � � �� � � �� � � �� � � �� � � �� � � �� � � �� ��?�%((���!�� � � ���%�0 ����� �� ���� � �������� ��� ����%�0 � ������ �� "�����/<�� �=� � � � ���� �

������������������ ������2$.:�����������;������� !������7$.:���������!����� �

������ ����, ����������, ������ ���� �������������!���������� ��� ������������������� ������������������� ���������������� ������������ ��������

;������ ���� �� ������ ���� ��������� ���/��������������&���� �� ��������� ��������� ���, ���� �� ��� �������,������ ��?3���!�'8���!����'8%����

Page 101: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ
Page 102: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

����������������������������

!@���������������������1�������� � �������

��������

4�/�6������������ ���������==�'($�4�0���� ������� ���������� ��������� �������'#2�'#5�4�1���� ������� ���������� ��������� �����'#<�4�2���� ������� ���������� �����, ��������� ���$'<�

4�3���� ������� ���������� �����, �������$'<�� �� ���� � ���������>�(�+�?

������ ���

=, ������������ �������� ������� ����������� ��������������� �������������� ���� ����������������� � ������ ��� ����==�'($�� ���!�&)��, �� ��!�

� ��������� �����������������!���� ��!��� ������!� ����������������������: �� � � ����� ��� ������������� ����( ����� � ���� , ������������������� �� �������������� �������������������������

(='�0�������������������������!!�"$�

�������H �������H��� ��==�'($�� �������������� �������������� ��

����� � ��������� ���������� �� �'&$����==�'($���������� ��������������������.������������ ���$������.��������� ��� �����,������ �������, ����� ��!����� ����� ��� ������ ���, ����� �����==�'($������� ���� �,��� �����,������ �

$������.��������� ���� ����� �����==&)'�� ���� ������� �,������ ���==)&�&�� ���� �������� ��������� ����������������� ��� ���� ������������ ��� ����

�������:������������������>� �����6'=C�������==�(C/�� ���� �������������������� ��< 33����< &&���==�(C0?�

Page 103: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

;���������� ������������� ����==�'($!�/<���� ������� � ������������������� , ���������������� ���/�������� ��������������� �������� ����� �������� ��!�

���� ������ ���, �������������� ����������������������������������� ���==�'($�

���� �� ������!�,�� � � ���� ) ����������� ����6�������/4;72!��� � ��� �47���� �� � ��==�'($��������������� ������ ���< <<���< 22� A�� C�� ����� �,������ ���� ���� ����=�'($ � � � �� �� ������!�,�� � � ���� ) ����������� ����6�������/4;72!��� � ��� �47���� � � ��==�'($��������������� ������ ���< <<���< 22� A�� C�� ����� �,������ ���������������� �������=�'($������� ���� ��

Page 104: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

= ���� ��K������ �4���/�!����������� ��,���������'&<����'&/!������� ���� ������ ����� ���<���==�'($��&�� ��� ��!��� ��� ���������������� �!����������������� ���������,��� ���,������ �4���/������==�'($�

Page 105: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ
Page 106: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

(=4��� ����������������� ��������� ��������������"�+6"�<

�������H����������H���������� ������� ���������� � ���� ������������������ �������'#2�'#5��B���������������� ������!�������� � ����� ��� ���������� ����� � ��� ��������������� ������ ������������������������

� ������: �������� ������������ ����� ��������!��� � ��� ���������������������������� ���������� ������� ����������� ����������������� ������������������������$�������� �������������������������!���,��� ����������� ������ � ����� ������

!@���������� ����������������� ��������� ��������������"�+6"�<

Page 107: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ
Page 108: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

(=:��� ����������������� ��������� ������������"�9

= ���� �H����<����H�� ������ ���� ����� ������'#<9�C���B��������� �� ����� ������� ��������'#<9�C����!�������������������������������� �����

�� ���������������� ��� �� ��������� ������ ����!��������� ������������ �%=)���� ���('�#!�5�

!@���������� ����������������� ��������� �������������"�9

Page 109: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

(=+��� ����������������� ���������A����1��������� �"9

�������H�����<����H���������� ������� �������$'<����� � ���� ������ �������� ������� ���������� ��)�� �������#��� ������� ���������

Page 110: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

��� ��� ��� , ���� ������ ���������� ��� �� , ���3�<77�������� ���������� �,������ ����������� � � ������ �������/84!������� ���������� ��/�� ���

���� �>3�<77��V/84�������������<�88027�?��������� ������������:�������(���(C�� ���� �������� ��� ��������� ������������� ���$'<�

!@���������� ����������������� ���������A����1��������� �"9

Page 111: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ
Page 112: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

(=;��� ����������������� ���������A����1��� �"9� ��� �������@�������������F $�I�H

��� ���$'<���� � ���������������� �����'&29�(�+����� ���� ������ ����2!�� ����������$'<������� ���� � � ��� , ��������������� ��������� ������������:�������(���(C�� ���� �������� ��� ��������� ������������� ���$'<�>������� ������������ ,�� ������������� ��?�

Page 113: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

!@���������� ����������������� ���������A����1��������� �"9�

��� ������ $�I�

Page 114: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ
Page 115: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

����������������������������

!@������

��������

5�/�-���������� ���������� ��

5�0�%=)���� ��5�1������������5�2�(������ ���5�2�/�(����� ���������� �

�5�2�0�(������ ����������� �5�3�' ����5�4�@ ����������5�5�-������ ���� ���

�5�5�/������������� ���� ���5�5�0�(������������� ���� ��5�7�5�� ����������>������� ��?�

5�8�%�)���������5�/<�-����� �-������������

������ ���

= ���� ����, ������������� �������������������� ����� �������������� ��������� ��� ���� ������� ������ ,�� ���� �� , �����������

��������� ������ ���=���� ���� �������� ���� ��� ����������������� ������ � ������������ ������� ������ ���������&��������������� ���� ���� ����������� �*$���=� ������*��� � ���� � �����

<='����������������� � �������

@ �������� ����!��� ���� ���,���� �������������������������� ������ ���� �������������� ���������� ������ ��������� ������� ������ ����������� ���������� �������������� ���� ��������������������� ���������� ��������������, ��� �������������*����,������������ �� ,��� ���

�;������� ���������������������� �!������� � ����������,�� �������� ����� ���������!������ �� � ���� ���������������������������&���������

� ��������� ��������������� ���������� ����������������� �!��������,���� ��������, �� �� �0�<K���4�<K�������, �������� ������ ������������� ����� �������������������� �,���� ��������: ��%$57<3���������, ������� �W3K�������������( ���������� ������������ ������� �� ����

Page 116: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

���� ������������� ���!������� ������, ������ �3K������ �������>���1?!�

�����,���� �����/���%$57<3�������� �� �� �5K��������02K��) � ��������� ������������� ,�� �� �������� ��� ���������� ���� ���,���� ��������: ��%$57<3���� � ��� �� , ����, �������%$57<3��;������ �����������������/&�

� ��������� ��� �, �������(�00<���� �������� ���������������������������������� �����������������3<�&!�� ������ �57%<3�>�������: ��, �������������(���80����������������� ���������/<<�&?�

<=4��!%������

%=)���� ���� ��� ����� ������������� �� � � ����� � ��������%=)���������� ,��������*%�����=�������)�� *��6� ���������%=)!�� , ��������� � ���

������� ��� ���������� � �!������������������1���3����>������ � ��?!����������� ������������������������/<�&�>������� ����������0�&����%=)������������ ����� ���������������������?!�������������� !����������� �� ������ �������

�� � ��� �������� !���� !�� ��������%=)�������� �� �� ��������� ���� ������!����� ���� ��������������� ����� ����������� ������������ ��� ���� ���,��� ���������� �%=)����������� ������������>, �� �� �?���� ������, ����� �������������� ����� ��� !������ �

����� �� ������ � ����, ������������ ���� ���>�������?������ ������ ����� ����� ��!��� ������ ������ ����� ��� �������� �%=)�H����H���������������������H����H���� ������ ���� ��)�� ������� �������������������� ����������������� �������� ��(�� ���� !������C����������� , �� ����� ��������� ���*���������

�� ����� �����%=)���� ����!���� ������������ ���� ����� �� ����������������� ������������ ����������� ���� !������������ �������������� �,��� ����� �� ���������� � ��� ������� ������������ ������������������������ �%=)��$� ��������� ������������%=)������ �� �������������� ��

Page 117: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

��� � ��� ��� �,��� ����� ����� ��� �����!�� � ��������� �,��� ����� ��������,���� ��;���

������ ������������ �������� �������,���� ���������%=)�������,��� ��������� �����/�0,���/�4,�� � ������� ��������� �%=)���� ���� ������� �,��� ���&�=�B���������,��� ������ ����� ��� �

������������������� �%=)�><�<<0&���<�</&?�� ������������� �,��� ����� �� ������������ ��������"2&�8���

%=)���� �� �� ��������������� �������������( ��������������� ��������

�����: �!������ ������������� ������������� ���� �������������� ��C=@&��K=����������� ��� ��������� ���(-���K=��������� � ��������������������� ����(-���K=�������-�� ��(-���K=��������,�� ����,���� ���W3K����� ���� ����

���� ��� �����!��������� ���������� ������� ������������#������,�� ��������������/���� ��� ������������ ��������� ����W3K������� ��, ���������: ��������� ��

���� ������!%����������$" ���� � �������

�� �������� ���� ��������: ������#���������������� ��� ��������� ���������

%=)���� �� , ���<�3� ���;������ �� ��� �������� ���/�!����������� �� ������ ���� ����� �72����

Page 118: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

<=:������������

Page 119: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

#������� �� ��������� ,�� ���� ���� ��� ����� �������� �� ����� �� ������������ ���� ������� � ����: �������������� � ������� ���

#������������ ��� ��� � ��� ��������� ��H���������H���� ���� ���� � ����� ������������� ������������ ������� � ���������� ����, ���������>������ ���������?���������, ��� ��� ������������������

!@�������� ��� ���������������� � ������������

#������������������ ��6� �� ������������!������������� ��� ���� �� ������ ���������� ��-����!������*���������������� ���� ����� ���� ������ ����� ���,���� ������ � ����������� ��!�������� ���� �� ������� ����������������

�������������!�� �� ������������� ���������!��� � �������������� �� ������ �,�������>��������?��������������� �������� , ������ ��������������!��������� ��������� ����� � ���� ������������������>������ � ����� ������

���� ���� �����?���� �� �����, ���������� � ��������� ��-6���.�>�(C�&��?�)=#(BC�=�������������, ��� ���� ���������������� !��� �����������!����� ���������������� ��� ����� ����������� ���������� ���������������� �����,�������!�� ���������� ���������� ������ �� �� � ����� �����������

����������������� ��� �������� �������������������� ��� � ����������� ����� ���� �� ��� �� ������������ ���������� ������ �� �������� �� ����� � ������ �����!������ ������ ����� ��������� ����� ������ �������������, �����

������������������������� �����!�������� ���� ��� �������������������� �� ����� ����� ������������ ������� ����������� �� � ����������� � ��� ������ ������� �� �������������������������� ���� ������������� ��

Page 120: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

&���������� �, ������� � � ���� � ������!���������� �����������, ������������ � ������� ������������� ���$���������� ����� ����������2����������� , ��������� � ��������� � �, ���������� �������

������� ������!���!����!���� ���������������������� �*������������������������� �� �������� ��������/4;72���������� �!�������� ��('�&����('�#��

����������*��������������� ���������� �� ����������� �*<*���*/*�������� �� � ����� ����� ��� ��� ������������� ��������������� ����������� ���������������� �,�� �������������������������� ��� � , ��>��������� �?�

Page 121: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

!@������'�

������('�&!�1!�/!�#���/

#�����/����� �� �������'&1�������� ����������������������� �����!������� ��� ��������� ����������6� ��� ���������� � �� �!������������������ ���� ��#���/�

!@������4�

������('�&!�0!�<!�#���0

#�����/����� �� �������'&/�������� ����������������������� �����!������� ��� ���������: �����������6� ��� ���������� � �� �!������������������ ���� ��#���0�

�� �������� ���� ���������� ���� ���������������������������#������� �

� �� ������ ����������������������� ����������� ������� �������� ����� ���K������ ��������������� �������#�%=)������������� � � �������������� ������'&<!�������� �� � � �������������� ������'&/�

Page 122: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

����������������� ������������������ ����������� ��������������!��������� ������ ���� �� ���������������� ���C�� ��!���������� ��������� ����� �

��������������� ��� ���� ������������� �������������� � �� �!����������� ���, ��������� �� ������� ����������������������������� ����������� ���� ����������� ��������������� ���� ���� !����� � ����������������� ��� �������� �

Page 123: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

�������-������������ ��� ��� ���������� ���� �����������������!������������������ ������������������� ��� "��� ������ ��� ����� � �� ������ ���������

�� ��� ���������� ��=, ���/<��!���������� ������� ��� �� ������������� ������ ��� ,������������� ������������������� � ������������������ �� ����� �

������������ ���������� ���� ���� !��� � �� � ������ ��������� �������� ������� �����<�����/!������� �� ������ ���������� ����1���2��� ������ ����� ���� �� �����>������� ?!�� ����� ������, �������� ������������� ��

<=+�$�� ��������

(������ ���� � �����, � ����������� ����������������>��� ������ ����������!� � ����������������� ����������?!�������������%=)��������������������� ���� ���� ���� ������ ����������� �������� ����� ����������������������

��������� ��������� ����� ����

• �� �� � � =��������� ���� ���� ����������������������������� �� � � ��

��������� �������������� ���� �������� � ��� �� � � ����� �� ���� ��������������� ���!� �������������������� ��������������������

• -������ ���� ������������ ������������������=��������� ���� ���� �

� ������������ "��� ����� ������ ��������������� ����������,�� ��B������!������� ������� ������ ����������������� ������������� �����������

• ����� ������� ����!������� ���������� ,�� ���������� ������������!�� �������� �� ����������

(������ ������� ��� ����� ��� ���������������� ,�� ����� �������, ��������������������������� ���������������������-������������ ����>�� �����������-������������ ����� ��� ��<���/���������� ������������������������, ��������� ���� �����������,��� �?��(������ ����� ���������� ��������������������

���� ��� ���������=���������� �� ������������ ������ ������������� ������ ��� ������� �������!������� �,���� ��������� ���� !������������ ���������������� !��������

<=+='�$�� ���������������������

Page 124: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

�� ��������������������� ���� �������������, �!��� �%=)��������� ������� �������� ��!��������������� ���� ���� �������������������������� ���� ���� ��

6� ��� �����������������,�� �!��� �,���� �� �� ���� ������� ���� ����������<�5K���� �������� ���������� ��� �����������������: �������'&2�����

�� � ���� �� ���������������� ���� ��������� �!� � � �������������: ������������>�������� �������������������������������� !�� � ������������ �!��������� ���� ����� ,����������� ��!� ��?��6 ��������, �� ���� ��

����� �%=)� , ������ �� ������ ���������� ,������%=)������������*� �*������������� ������������������������������� �������'&2�>�(�+�?��&���� ���� ������ ����/�0�������� ���� !� , ���� ����������������� � ���$'<������ ������������� ���� ������������ ����('�#�%=)��

!@���������� ���������������������

Page 125: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

<=+=4�$�� ���������������������

&�(������ ������ ������� ����� ����� ��� ������������������������ ��%=)����� �� ������������� ����!��������: ���������������,�� ������� ��%=)!����������,������� ����������������������� "� ������������%=)����� ��������� ,�� ��������/0K��%������������� ������������ ���

Page 126: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

!@���������� ������������������

�� ���������������� ���� ��������� ��#��� ��, ������������� ����� ����������������&!��� ���������������� �����,�� ������ ������� �!������������� �%=)����� ��������� ,�� ��������/0K�

<=;�"����

�� �� ��������� � ���� ��������� ,�� !������������������ � ������������������ ���������, � ����������������������������� ����� ����� ������ !������� ����������� ������ ����� ���������6� ����������,���� ������ ��, � ������ ����!����� ������������������� ������ ��������� ������ ��������, ���� �

������� ������ � �� �������������9��� ���� ��� ���6� ��� ������ �, �������� �� ���!��� ���� ������� ����� ����������� ���������� ����������������,���� ����� ������ ���� ����������,���� ��������� ��� ����, �����������

����������� , �� ����� ����� ����� �� ���������� �������H�������H��� ����� ��� ����������

Page 127: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

���� ��������������������� � ��������A������������

�-�� ���������� ��������,�� �������� ��������������� ��������>���� ��/<<W�&����� "��� ������������ ����������,�� ������03�&?!���������������

�� ������������ ������� �!�������� ��������������������� �� ���������6� ���������� ����� ��, � ���������������� !��������������,�� ���� �� ���!��������� !����������������!�� ������ �� � � ������� ��������������� ����� �� ���������

�� ������������� ������� �����������: ������ ����� , ���� �� ������������,�������������� ������� ��� �������������� ��������� ��'&1�����,�� ���� �� ����

Page 128: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

���� ����������� ���������������������� � �������

&�� ������������ �����,�� ��,����������� ������������� ���� ���� �������� ���� �

���� ��� ��� ������ ����������� ���������� �������,�� ���������� �� �����������.�������� �������� ������������������*)������*��������������������,�� ���������������� ��

� �����,����������� ������ ��� � �� �� ������������������� ������������!��� � �� ������� ��� �������������������� �����!����������������

� �� ��!���� �,���� ��������������������� ���������� ���������������� ���� !��� �%=)��������,�� ����� ����� ��������������!��� �� ������������ ���# �������� �������� � ��������,�� ��� �� ���!��������� ���� ����� ���� ����������� ��������

Page 129: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ
Page 130: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

<=(�-��������������

����������� ������ ��!�� � ������� ��������������� ���������� � �!���������

������������������ ���������, ��� �� � ������� �!���� ������ �� ��, � �������������������� �������������!��������� �� �������������������� ����� ������� �� � ����������������� �� �������� �� ������ �����!������ �

�� ������ ���������� ����������������������!����������� ������������ �

&�������������!������� ����������������������� ��, ����#==��'(B��C=������

��������� � � ���$����#==�������������� �����#==���� ����� "�!���������

����J����� "� ������� �������� ����� ����� ������� ������ ���� "� �������������������������.��� ���� ���� �!��� ���� �������

!@������'��#==��< ;;!�< <0���� ������������� ����� ����� "� ����������������0����� ��� ��43�1�-���������, ��/1<�4��-�

�!@�����4��#==��< 8<!�< <3��

�� ��������������� "� �����< 8<�������������3����� ��� ��43�1�-��������� ������ � ��� ��� � ����������� � ���������� � ��� ��������� � ����� ����������� ����������� ��������������� ������������� �#==��$�����������

Page 131: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

�� �������� ���� �������� ��� ������������������������ ������������� ������ ��� ���������� ����� ������� ������/����0��-� ����� ��� ,������������� ���������� ������ ������ ��������

Page 132: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ
Page 133: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

<=<����������������

�� � ��� ������� ����������� ���� �������������������������������������������

� � �, �������������� �!��������7��� ������� �� ������ ������������������� ��������������� ���$������������������������������ ������ ���� ������� ��� � �, �� ����������������H�����H��� ������!��� ������������������ ��������-�����

� ���� ����� �� �������� ���� ������ ���� ����������������� ��������������� ����� ���� �������������� ������ ������!�� ���� ������� ����������� �����, ������� ���� ���������.� , �!��� ����� ��������������� ��������������/4;72����, �����������

<=<='����������������������<+��;K<

������������ ���� ����������������� ����������� ������������������ ����������

��������� ����� ������������"��� ������ ���� � ��� ������� ������� ������ ������������ 7���� �7���������%�����)�������������� ��������� ����������������� ����� ���� �����������*�����*���������� !��������*���*������!�������������� �� ��

������ ������������� ���� ������ �������� ���� �!���������� � �������� ������������ �� ��������������� ��,���*����*����*����*��� ��

&����� ����� �� �������� �������� ���� ��52.�385���������!��������� ���

Page 134: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

���� ��������������� �����������!������������ ��� ������� ������������� ���� ���$����.�385������������� � �����.�385������K��!�K��/�

�.���,������ ��� � ����������������� ���� ������������������� �� ��.��'������� ��

�!@�������.�385�����!���� ���)���������� �������������� �������� ���� �������� ���������,������ ����� �9��� ��

,������ ������ ������������� �����$�����������

Page 135: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

= ���� ���������� ��� �.�385�����������, ����� �����������������������

� � �, ������������������� ����������� �������� ���� ������, ������ ������������ �'M�,������ ����� ���������� ���%=)��� �� ��������#������������ ��� �� ��������� �����������

Page 136: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

<=<=4�$��������������������

Page 137: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

(������������� ���� ������������ ������������������� ��������(� , ��������� �� ����� �����!��� �������� ���� ��� ������ �,��� ������������ !���� �������� �������

� ���� �!������ �� � ������������� �7���� ���(�������������*�����*��� !������������ �������� �������� ���� ���������� ���� �!��������������������� ������� �������������� �������

&����� ����� �52.�383�������� ���� ��� ��������������� ���������� ���

$������ ��������� ���� ����� ���������383������� !����������� ��.�383���$����.�383������������� � �����

.�383������K��!�K��/��.���,������ ���� ��� ����������� �� �������������������� ���� ���

.��'������� ���!@�������.�383�)���!���� ���

Page 138: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

�� ������� ������������ �������� ���������,������ !������� ��,������ ������ ������������� ��

&� ���� ���������� ��� �.�383�����������, ����� ����������������)��������,������ ��M����� ������������ �� ����������� ���� ���%=)��� �� ������ ������� ������������ �������� ���� ������������� ��� ����� ����� ��� ���������� ���� �,��� �< �#�>//<<�/<//?����� ����������� �� , ��!��� ��!������!������!����: ��%=)���� ��������� ��

Page 139: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

<=*���A��6��������%�������F��������@���H

�� �� �� �������5�� �� ������������ ������ �������������� �����������<���;������������ ��������

Page 140: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

6 ����������������������������� ������ ������������������������=, �������%�)������������ ��� ��������� �����������!�5�� �� ������������� ������������������� �������������������� ����� ���� �� ����� ������ ��!�,�������������

��� �,� ������� ��- �� ����� ����� ����������������� �� �����!��!��!��!� !��!��������!��� � ��������� �� ������������� �7�%=)������ � ���������������� ������ ������������������ ��������� ��6��������������� �

�������!��� ���������� ������� �� �� ������ �<K������������ �%=)���� ���� ��������������� �������� �����������������, ��� ������� �� �� ������ �W3K��������� �� �� ����� ���� ���������������: ����� ���: ����������������� ���� ���������� � ��!��� �� ��������� ���������� ���>���� ������!�

������ ������E?��)���������� ��,������ ��������������� �������5!/<!�/1�3!�0<!���03������� � ������ ���� ������� � ������!����������� �!���� !������ �

�� ������ ������������, ��������������,����������������, ����� � ��� ��,������ ���������2�����������&�� ����, ���������������� ����, ��������������� ���������� ����� ������������� "��� �!��� �� ���������,����� ��������� ��H������� ���H

�� ��������� � � �� �� ��� ����� ���������� ���� ����H���, ��� ��H�&�

�� ��������, ������ ����������� �H����H��� ������ ����, ��������������� ��������� �� �� ��������� � ���� ������������������� ��������������� ����, ���������������� �!�5��� �������� � � ������� ������ �� �����������;�� ����

����������������!���� � ������ ���� � ���������� ���2!�3���4��������������!������� �5�� �� ������������� �� �� ���������� ����� ������� �>�� ���������� ��� ?������� ����� ����� ������������� ������� ��������������� ��������� ���������� ����������=������������������ ������������ ���, �/<<���� ��� ��

� ��!��������������� �������������� ������������ ���� �������� ���� ���� ��&�� ������������������ ��!��� ���������� ��������������� �� ��, � ������������������������, ��� ���� ���� ������B����4�������������� ���� �� ����� ��������������� ������� ����� ������������� ������ �� ���=������������������ ���, �����������

��� ����� ���� ������ ��(K�>� ����� � ���,���?������ � ��������� ��������������� ����� ���� ���� ���� � �� !��� �������������� ��� ��� ���� �������!� �� ��� ���� ��������������������������������

Page 141: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

���� ��������� � ����������<6����������������������������@����

�������H5� �����H����������� ������,��� �������� ����� ����,������ �)�

= ���� �

�,����0/ �,���) ����� ��0/������� ����� ���5� ���������

)������������������������ �������������� ��� �������� ���������� �

��������� ����� ��� ��������� ������������ ����������$'<��� ������� �, ������ ����������� �� ���!���������� �������� � ����� ��� ���������� � , ���3������� ������������� �������� ��� ���������� !��������� ������ ������������� �� ��������� ���� ��������� �������� �� �������������� ,������!����������

� ���� ����!�� ���� ������������������ � ���������5� ����������������������3��!��� ��������� � ���� ������

;�� ���������� � ������������� �� �������!����������$�������� ��������� ���� ����� ��������!��� �� �������!������ � �����������,������ ��)��/!�)��0!����)��1���������� !��������� ����!�)��/������ "����<!�)��0������ "����0!����)��1������ "����/�

' ���:�������� �����������, ����� ���������������

Page 142: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

�� �������� ���� �������� ��� ����� ����������������������������������� ���� ��0���������� �����5� ������������������� ��� �

Page 143: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ
Page 144: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

<=K���%�%������

$� ���������� ��� ,�� ���� ������

*������%�)*������������������,����������������� ������������������, ����� �� ������������ �����%�

������������������������� ���%�)����������� ��� �������.������*��%�)�.)2257<����� !��� �� � ��, !� �������� !���������� , ������� �������� �

��� ������������ �7� �7<��� ������� ����������.�������%�)������������, �����������&-����� ����������� ��������A��� � !�@� ��������� ��������

�������

��'(@4���������� ����%++<*9��������

;����7�������������!��� ���������� "��� ����W3K�������������//��9(��� ���;����2��������������������� "��� ���� ���������� �������� , � ������ ���6� ��� �%�)�

������������� ��� �!�������� ���� ��������� �������� ����� ���� ��������� ����������� ��� �>����������� ���� ����� �� �?���������� ����� �������� �� � �������� �� ��������� ���������� ���� ��� ��������������� ���

���� �� ������� �%�)������ "��� ��1�H����H��� �������� ���������� ��

!������F!H �������� ���������� ������� ����������������'96����'-��� ���6� �������� ������!��� �%�)���������� �������� �������������'96����'-��6� �>=?��� ��������!��� �%�)��� ������ ����� ����� ����������� �����

� ��������������� "���80�����

F"80H ������� �� � ��� ���� ���� ������������ �� ��� �%�)������������� ���6� ���������!�������������� ����� �%�)��6� �����������!���������� ��������� �%�)�

"������������ ��

F"�H 6������ �� ������������ !��� �%�)��� ��� ����� ���� ���������������� ���6� ���������!���������������� �������� ����� �%�)��6� �����������!���������� ������ �������� ����� �%�)�

%������������������� ����

!�����<�&�� �����%�)������� ���������/�&�� �����%�)� ��� ���

"80 �<�6�������������%�)��������/�' ��������������%�)��"�����<����������

�������/�������� ���6��������������� �%�)����� ���� , ������ ����

�- ��'96�����������- ��'-������������<���/�>������������������ �?�- ���������������� ��>���������������?�

Page 145: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

- ��=��� ���������- ��=��� �������

' ������������������� ��>���������� ����?��' ���������������� �%�)����� ����� ���� ����!������������ �'96�������� �������6� �� �� ������������� �%�)!���������� � ��������������������������������

����������� ����%�)������������ ����� � ���� ������������������!��������������������������������� �������� ������������6� ��� �%�)�����������: �!�������� ���������� �� � �,���������������������������� � �, ����������� �!������������� ������� �������������, ��� ������� ����� ����� ���������� ��������������� �

���������� � ���������� �������� ������ ��6� �� ����������� ������������������ ��!�������� � ����� ������� ������������� ��!������ �� �� �������� ���������� ��������� ������������ �������� ����������� ���� �����������������>))?�'&$���� ���: ���))'&$����7<���� ��

�� �%�)���������������� �� ��42���� ����������� ��@ �����>�@?�'&$�������� ��������� ������������ ���� �� ������� ��� ���)�������@�'&$����

� �� � � �������7������������ �����������=����������� ����� �����7���� �����@�'&$!����� ��������� ����������� ��!��������� ��� ������ �� ����

���������� ����� ������� �������� �������������� �%�)��������!�� ������������� ���� ��@�'&$����� �������������

����>��������<?!������ ����� ���������� ������������ �� ����������*�� ����*�������� �������, ����� ������� �

# �� �� ���� ���))�'&$���� ��� ��������� ������������ �!��� �������������� ��

�� �))�'&$����� ����6���������� �����������������%�)�� �������� ������� ���������� �������������� ����������� ������ �������������(� ��� ����� �����))�'&$����� �!��� ������� �������� �������� �������� ������� ���������� ����� ��

�� ���� ��B������� �������� ���� �� ������������������ ���������%�)����������� � ����������� �����#������������������ �%�)������ �� ���2<���/0<������ ����>�-?��������������� ������(�� ��� ������������ ������3��-��

)������������� !��� ���������� ���������� ����� �%�)!������������ ���������� ��� �%�)����������6 ������, ����������������

Page 146: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

( ������������ ����� �#B-T����������������� �)5�������������� �� ���� ����

� ���� �%�)*������ �������!������������������ �������� , ����������� ������� �#B-T�������� ��� ����������������� ���� �������� ����������� �� ����������� ���� ��������� �%�)����������� �� ���������� ������������������������

������ �����������%�)�� ������ ��������� ��� ,�������� ���&���� �� ������ �� �� �������� � � ��//��9(��� ������������ ��������%�)��.� , �!�� ������������ ��������%�)����������2��������������������� �

���� ��� ��� ��������� ���������������� ����� , ���� ����������� ����,�����2������������������������� ���������� ����������� ���� �� ��� ���%�)��������������0 /4�������� ��!���� � ��%$/4M0/0����A��� � ���� ��-.&'����� �� ���� �*������� �*��������� ����� ������������������� �����

������� ���*X*����*Y*��� �������� ������� �� ������� ���, ������ ���� �����*����=� ������*�

Page 147: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

���� ����������%��������������� � �������

;�� �� �=�� ��������������������������� ��� ������������%�)����������

Page 148: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ
Page 149: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

&����������� �������%�������

� ����� $������ �����������: ������ �� ����%�)��%�)���������� ����������2������� �

�= ���� � ������ � � � ���@� ���� ���������� �� ���������/4�������� ��!������������ ���� ������������

����� � ���;���������� � ��� � ������ ��� ����������������������������� � ������: �!�� ��������� �������� ����� ��������������

�= ���� � ���� ��/!�H���� � ������H � ���� ��/!�H� �� ����� /H���������� �� ���������������� �/!�������� ��/ � ���� ��0!�H� ��GH���������������������� �� ���������������� �0!�������� ��/ � ���� ��<!�H������H���������������������������� �� ������� ��� �0 � � � � �� - ������������������ � %�)�%' G��

�*<<<<<<</* ��� ����������!���������

� %�)�. G��

�*<<<<<</<* ���������

� %�)�% G���*<<<<</<<*

�$, ��� ����������� �� ��

� %�)�' G���*<<<<<//<*

�$, ��� ����������� ������

� %�)-% G���*<<<//<<<*

�$, ��� ��� ���������������� �� ��

� %�)-' G���*<<<///<<*

�$, ��� ��� ���������������� ������

� %�)%/ G���*/<<<<<<<*

�- � ����� �/

� %�)%0 G���*//<<<<<<*

�- � ����� �0

� � �= ���� � �������%�)�. � � � ����� ������ ���� �,������ ���������� �����: �� �= ���� � ������ ��� �� �����

6� ����������������������� ���� ���� ����� ��� � � �����������������&��

����!��� ������� �������� ������������������*������������ � ������������ ��� �

��� ������������������� �������� ���������� ������ ������ � �������� ������;����������� �,������ �������� ������������ ���, ��� ���� �����������$���������.�����������, ����� ���� ����������������� �������� ���������� ������������ �� ������%�)��$���������������� ������ ���� ���������

�� ���%�)����������&���� ���������*�� � ��������� ���� �,������ ��%�)���!�%�)� ��!�)��� ��!�)��/!�)��0!����)��1��� ������� ����������%�)������������������� � ������������ ��� ���������������� �������%�)����� �� �������������������: ���� �%�)����� ������ ���������� �����7�����,������ �� ���

Page 150: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ
Page 151: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

<='9�������������� ����

-������������ ,��������- ����������������� ���� ���!�������� �����

������� �!���� ������������������� ����6� ����������,������ !��������� ���� ���������/4;72!�������� ��� �� ���������� �

&�������������� ����������!�� ��� ���������C'F�>C�' ������F �?�����������������7�>8?�C�/!���7���8����������!��������������������������� ����������)������������� �� ������� ������������� �����-����������������������������7������� ������������� �?������ ������������������ �����������>�� ���������������� ����

�������������?!������� ���� ������� ����� ��� �������������� ������� ������������ ���������*�*�� � ������ �������������� ������������� �������������� � ������� ������������;���� ������������� ����84<<�����!������/<2��-�

���� ���������'-010�� ��� /���) ,� ������:�����- 0��'M) ,�����4��: � - 1���M) ,�� ��/���: � - 2��)�' ,: � ����/�� ���� �$- 3��@C) ,*�����- 4��)-' ,: � ������� �$- 5��'�- ,��@������������- 7����- ,��� ���������- 8��'� ,�����3���� ���-

���� ����� �������������� ������� �������������������� �!�� � ����

��������� �� , ������ ��������������������������� ����� ���� �������� , ������������/<K���������: �!����W/<K��������� ��-�� ��� �������� , ����� ���������� �����W3K��������� !����<K���������: �!�� � ������ �� ������

���� ������������, ����� �� , ����( �������� �������� ��� ������������������$&M010������������� � �, ��������������/<���W/<K�����, ������ �����<����3K�

�� ������������������� ���� ������������ ���������� ���

Page 152: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

���� ��������� � ���������������A�������/4:4������������� �� ���

;�� �'-010��������������������������� ������ ���������������

Page 153: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ
Page 154: PIC microcontrollers for beginners too on-line · PIC microcontrollers, for beginners too on-line, author: Nebojsa Matic ˘ ˇ

B������ ���������� ���������������

��4:4���� $��������������:����� �����������������������>�M���?� �= ���� � '-010��� � � ����1 - ����&-����,��� ��������������6�� ���� �� �= ���� � �,���*�* � �����- �� � � ��4:4��@� - ����&-����,��� ������� ���� ��� � �= ���� � ��010�H���� � ������H � � ��4:4���� - ����&-����,��� ���� ���������������7�����,������ �= ���� � �,����/01 � �,����M���� � ��010��� ��M��������- ��*/*!�*0*!�*1*

6� ���010������� ������ �!������� � ��������� ���� �,������ ��'�� �������M)������ �� ��������� ������������

= ���� �

&����� ������������� ��������������!�� ���, ��� ���� �������� ���� ������� ��� ����,������ ������� ������ ��,����������010� �!����� �,������ ��������� ��,����������010��� �������� ������ � ������� ������ ��3<<��!������

��� � ������������ ���� ��������� ���� ����� � ����������� ������, �����>�?����� ������ ���� �������


Recommended