-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembedded_systems.html
More file actions
3512 lines (2539 loc) · 259 KB
/
Copy pathembedded_systems.html
File metadata and controls
3512 lines (2539 loc) · 259 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html><html><body><title>Embedded Systems Tutorial</title>
<h1>Embedded Systems Tutorial</h1>
<p>We can broadly define an embedded system as a microcontroller-based, software-driven, reliable, real-time control system, designed to perform a specific task. It can be thought of as a computer hardware system having software embedded in it. An embedded system can be either an independent system or a part of a large system. In this tutorial, we will explain all the steps necessary to design an embedded system and use it.</p>
<h1>Audience</h1>
<p>This tutorial has been designed to help the students of electronics learn the basic-to-advanced concepts of Embedded System and 8051 Microcontroller.</p>
<h1>Prerequisites</h1>
<p>Before proceeding with this tutorial, you should have a good understanding of the concepts of basic electronics such as circuits, logic gates, etc.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>Embedded Systems Overview</title>
<h1>Embedded Systems - Overview</h1>
<h2>System</h2>
<p>A system is an arrangement in which all its unit assemble work together according to a set of rules. It can also be defined as a way of working, organizing or doing one or many tasks according to a fixed plan. For example, a watch is a time displaying system. Its components follow a set of rules to show time. If one of its parts fails, the watch will stop working. So we can say, in a system, all its subcomponents depend on each other.</p>
<h2>Embedded System</h2>
<p>As its name suggests, Embedded means something that is attached to another thing. An embedded system can be thought of as a computer hardware system having software embedded in it. An embedded system can be an independent system or it can be a part of a large system. An embedded system is a microcontroller or microprocessor based system which is designed to perform a specific task. For example, a fire alarm is an embedded system; it will sense only smoke.</p>
<p>An embedded system has three components −</p>
<p>It has hardware.</p>
<p>It has application software.</p>
<p>It has Real Time Operating system (RTOS) that supervises the application software and provide mechanism to let the processor run a process as per scheduling by following a plan to control the latencies. RTOS defines the way the system works. It sets the rules during the execution of application program. A small scale embedded system may not have RTOS.</p>
<p>So we can define an embedded system as a Microcontroller based, software driven, reliable, real-time control system.</p>
<h2>Characteristics of an Embedded System</h2>
<p><b>Single-functioned</b> − An embedded system usually performs a specialized operation and does the same repeatedly. For example: A pager always functions as a pager.</p>
<p><b>Tightly constrained</b> − All computing systems have constraints on design metrics, but those on an embedded system can be especially tight. Design metrics is a measure of an implementation's features such as its cost, size, power, and performance. It must be of a size to fit on a single chip, must perform fast enough to process data in real time and consume minimum power to extend battery life.</p>
<p><b>Reactive and Real time</b> − Many embedded systems must continually react to changes in the system's environment and must compute certain results in real time without any delay. Consider an example of a car cruise controller; it continually monitors and reacts to speed and brake sensors. It must compute acceleration or de-accelerations repeatedly within a limited time; a delayed computation can result in failure to control of the car.</p>
<p><b>Microprocessors based</b> − It must be microprocessor or microcontroller based.</p>
<p><b>Memory</b> − It must have a memory, as its software usually embeds in ROM. It does not need any secondary memories in the computer.</p>
<p><b>Connected</b> − It must have connected peripherals to connect input and output devices.</p>
<p><b>HW-SW systems</b> − Software is used for more features and flexibility. Hardware is used for performance and security.</p>
<h3>Advantages</h3>
<h3>Disadvantages</h3>
<h2>Basic Structure of an Embedded System</h2>
<p>The following illustration shows the basic structure of an embedded system −</p>
<p><b>Sensor</b> − It measures the physical quantity and converts it to an electrical signal which can be read by an observer or by any electronic instrument like an A2D converter. A sensor stores the measured quantity to the memory.</p>
<p><b>A-D Converter</b> − An analog-to-digital converter converts the analog signal sent by the sensor into a digital signal.</p>
<p><b>Processor & ASICs</b> − Processors process the data to measure the output and store it to the memory.</p>
<p><b>D-A Converter</b> − A digital-to-analog converter converts the digital data fed by the processor to analog data</p>
<p><b>Actuator</b> − An actuator compares the output given by the D-A Converter to the actual (expected) output stored in it and stores the approved output. </p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>Embedded Systems Processors</title>
<h1>Embedded Systems - Processors</h1>
<p>Processor is the heart of an embedded system. It is the basic unit that takes inputs and produces an output after processing the data. For an embedded system designer, it is necessary to have the knowledge of both microprocessors and microcontrollers.</p>
<h2>Processors in a System</h2>
<p>A processor has two essential units −</p>
<p>The CU includes a fetch unit for fetching instructions from the memory. The EU has circuits that implement the instructions pertaining to data transfer operation and data conversion from one form to another.</p>
<p>The EU includes the Arithmetic and Logical Unit (ALU) and also the circuits that execute instructions for a program control task such as interrupt, or jump to another set of instructions.</p>
<p>A processor runs the cycles of fetch and executes the instructions in the same sequence as they are fetched from memory.</p>
<h2>Types of Processors</h2>
<p>Processors can be of the following categories −</p>
<p>General Purpose Processor (GPP)</p>
<p>Application Specific System Processor (ASSP)</p>
<p>Application Specific Instruction Processors (ASIPs)</p>
<p>GPP core(s) or ASIP core(s) on either an Application Specific Integrated Circuit (ASIC) or a Very Large Scale Integration (VLSI) circuit.</p>
<h2>Microprocessor</h2>
<p>A microprocessor is a single VLSI chip having a CPU. In addition, it may also have other units such as coaches, floating point processing arithmetic unit, and pipelining units that help in faster processing of instructions.</p>
<p>Earlier generation microprocessors’ fetch-and-execute cycle was guided by a clock frequency of order of ~1 MHz. Processors now operate at a clock frequency of 2GHz</p>
<h2>Microcontroller</h2>
<p>A microcontroller is a single-chip VLSI unit (also called <b>microcomputer</b>) which, although having limited computational capabilities, possesses enhanced input/output capability and a number of on-chip functional units.</p>
<p>Microcontrollers are particularly used in embedded systems for real-time control applications with on-chip program memory and devices.</p>
<h2>Microprocessor vs Microcontroller</h2>
<p>Let us now take a look at the most notable differences between a microprocessor and a microcontroller.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>Embedded Systems Architecture Types</title>
<h1>Embedded Systems - Architecture Types</h1>
<p>The 8051 microcontrollers work with 8-bit data bus. So they can support external data memory up to 64K and external program memory of 64k at best. Collectively, 8051 microcontrollers can address 128k of external memory.</p>
<p>When data and code lie in different memory blocks, then the architecture is referred as <b>Harvard architecture</b>. In case data and code lie in the same memory block, then the architecture is referred as <b>Von Neumann architecture</b>.</p>
<h2>Von Neumann Architecture</h2>
<p>The Von Neumann architecture was first proposed by a computer scientist John von Neumann. In this architecture, one data path or bus exists for both instruction and data. As a result, the CPU does one operation at a time. It either fetches an instruction from memory, or performs read/write operation on data. So an instruction fetch and a data operation cannot occur simultaneously, sharing a common bus.</p>
<p>Von-Neumann architecture supports simple hardware. It allows the use of a single, sequential memory. Today's processing speeds vastly outpace memory access times, and we employ a very fast but small amount of memory (cache) local to the processor.</p>
<h2>Harvard Architecture</h2>
<p>The Harvard architecture offers separate storage and signal buses for instructions and data. This architecture has data storage entirely contained within the CPU, and there is no access to the instruction storage as data. Computers have separate memory areas for program instructions and data using internal data buses, allowing simultaneous access to both instructions and data.</p>
<p>Programs needed to be loaded by an operator; the processor could not boot itself. In a Harvard architecture, there is no need to make the two memories share properties.</p>
<h2>Von-Neumann Architecture vs Harvard Architecture</h2>
<p>The following points distinguish the Von Neumann Architecture from the Harvard Architecture.</p>
<h2>CISC and RISC</h2>
<p>CISC is a Complex Instruction Set Computer. It is a computer that can address a large number of instructions.</p>
<p>In the early 1980s, computer designers recommended that computers should use fewer instructions with simple constructs so that they can be executed much faster within the CPU without having to use memory. Such computers are classified as Reduced Instruction Set Computer or RISC.</p>
<h3>CISC vs RISC</h3>
<p>The following points differentiate a CISC from a RISC −</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>Embedded Systems Tools and Peripherals </title>
<h1>Embedded Systems - Tools & Peripherals </h1>
<h2>Compilers and Assemblers</h2>
<h3>Compiler</h3>
<p>A compiler is a computer program (or a set of programs) that transforms the source code written in a programming language (the source language) into another computer language (normally binary format). The most common reason for conversion is to create an executable program. The name "compiler" is primarily used for programs that translate the source code from a highlevel programming language to a low-level language (e.g., assembly language or machine code).</p>
<h3>Cross-Compiler</h3>
<p>If the compiled program can run on a computer having different CPU or operating system than the computer on which the compiler compiled the program, then that compiler is known as a cross-compiler.</p>
<h3>Decompiler</h3>
<p>A program that can translate a program from a low-level language to a high-level language is called a decompiler.</p>
<h3>Language Converter</h3>
<p>A program that translates programs written in different high-level languages is normally called a language translator, source to source translator, or language converter.</p>
<p>A compiler is likely to perform the following operations −</p>
<h3>Assemblers</h3>
<p>An assembler is a program that takes basic computer instructions (called as assembly language) and converts them into a pattern of bits that the computer's processor can use to perform its basic operations. An assembler creates object code by translating assembly instruction mnemonics into opcodes, resolving symbolic names to memory locations. Assembly language uses a mnemonic to represent each low-level machine operation (opcode).</p>
<h2>Debugging Tools in an Embedded System</h2>
<p>Debugging is a methodical process to find and reduce the number of bugs in a computer program or a piece of electronic hardware, so that it works as expected. Debugging is difficult when subsystems are tightly coupled, because a small change in one subsystem can create bugs in another. The debugging tools used in embedded systems differ greatly in terms of their development time and debugging features. We will discuss here the following debugging tools −</p>
<h2>Simulators</h2>
<p>Code is tested for the MCU / system by simulating it on the host computer used for code development. Simulators try to model the behavior of the complete microcontroller in software.</p>
<h3>Functions of Simulators</h3>
<p>A simulator performs the following functions −</p>
<p>Defines the processor or processing device family as well as its various versions for the target system.</p>
<p>Monitors the detailed information of a source code part with labels and symbolic arguments as the execution goes on for each single step.</p>
<p>Provides the status of RAM and simulated ports of the target system for each single step execution.</p>
<p>Monitors system response and determines throughput.</p>
<p>Provides trace of the output of contents of program counter versus the processor registers.</p>
<p>Provides the detailed meaning of the present command.</p>
<p>Monitors the detailed information of the simulator commands as these are entered from the keyboard or selected from the menu.</p>
<p>Supports the conditions (up to 8 or 16 or 32 conditions) and unconditional breakpoints.</p>
<p>Provides breakpoints and the trace which are together the important testing and debugging tool.</p>
<p>Facilitates synchronizing the internal peripherals and delays.</p>
<h2>Microcontroller Starter Kit</h2>
<p>A microcontroller starter kit consists of −</p>
<p>A big advantage of these kits over simulators is that they work in real-time and thus allow for easy input/output functionality verification. Starter kits, however, are completely sufficient and the cheapest option to develop simple microcontroller projects.</p>
<h2>Emulators</h2>
<p>An emulator is a hardware kit or a software program or can be both which emulates the functions of one computer system (the guest) in another computer system (the host), different from the first one, so that the emulated behavior closely resembles the behavior of the real system (the guest).</p>
<p>Emulation refers to the ability of a computer program in an electronic device to emulate (imitate) another program or device. Emulation focuses on recreating an original computer environment. Emulators have the ability to maintain a closer connection to the authenticity of the digital object. An emulator helps the user to work on any kind of application or operating system on a platform in a similar way as the software runs as in its original environment.</p>
<h2>Peripheral Devices in Embedded Systems</h2>
<p>Embedded systems communicate with the outside world via their peripherals, such as following &mins;</p>
<h2>Criteria for Choosing Microcontroller</h2>
<p>While choosing a microcontroller, make sure it meets the task at hand and that it is cost effective. We must see whether an 8-bit, 16-bit or 32-bit microcontroller can best handle the computing needs of a task. In addition, the following points should be kept in mind while choosing a microcontroller −</p>
<p><b>Speed</b> − What is the highest speed the microcontroller can support?</p>
<p><b>Packaging</b> − Is it 40-pin DIP (Dual-inline-package) or QFP (Quad flat package)? This is important in terms of space, assembling, and prototyping the end-product.</p>
<p><b>Power Consumption</b> − This is an important criteria for battery-powered products.</p>
<p><b>Amount of RAM and ROM</b> on the chip.</p>
<p><b>Count of I/O pins and Timers</b> on the chip.</p>
<p><b>Cost per Unit</b> − This is important in terms of final cost of the product in which the microcontroller is to be used.</p>
<p>Further, make sure you have tools such as compilers, debuggers, and assemblers, available with the microcontroller. The most important of all, you should purchase a microcontroller from a reliable source.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>Embedded Systems 8051 Microcontroller</title>
<h1>Embedded Systems - 8051 Microcontroller</h1>
<h2>Brief History of 8051</h2>
<p>The first microprocessor <b>4004</b> was invented by Intel Corporation. <b>8085</b> and <b>8086</b> microprocessors were also invented by Intel. In 1981, Intel introduced an 8-bit microcontroller called the <b>8051</b>. It was referred as <b>system on a chip</b> because it had 128 bytes of RAM, 4K byte of on-chip ROM, two timers, one serial port, and 4 ports (8-bit wide), all on a single chip. When it became widely popular, Intel allowed other manufacturers to make and market different flavors of 8051 with its code compatible with 8051. It means that if you write your program for one flavor of 8051, it will run on other flavors too, regardless of the manufacturer. This has led to several versions with different speeds and amounts of on-chip RAM.</p>
<h2>8051 Flavors / Members</h2>
<p><b>8052 microcontroller</b> − 8052 has all the standard features of the 8051 microcontroller as well as an extra 128 bytes of RAM and an extra timer. It also has 8K bytes of on-chip program ROM instead of 4K bytes.</p>
<p><b>8031 microcontroller</b> − It is another member of the 8051 family. This chip is often referred to as a ROM-less 8051, since it has 0K byte of on-chip ROM. You must add external ROM to it in order to use it, which contains the program to be fetched and executed. This program can be as large as 64K bytes. But in the process of adding external ROM to the 8031, it lost 2 ports out of 4 ports. To solve this problem, we can add an external I/O to the 8031</p>
<h2>Comparison between 8051 Family Members</h2>
<p>The following table compares the features available in 8051, 8052, and 8031.</p>
<h2>Features of 8051 Microcontroller</h2>
<p>An 8051 microcontroller comes bundled with the following features −</p>
<h2>Block Diagram of 8051 Microcontroller</h2>
<p>The following illustration shows the block diagram of an 8051 microcontroller −</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>Embedded Systems I/O Programming</title>
<h1>Embedded Systems - I/O Programming</h1>
<p>In 8051, I/O operations are done using four ports and 40 pins. The following pin diagram shows the details of the 40 pins. I/O operation port reserves 32 pins where each port has 8 pins. The other 8 pins are designated as V<sub>cc</sub>, GND, XTAL1, XTAL2, RST, EA (bar), ALE/PROG (bar), and PSEN (bar).</p>
<p>It is a 40 Pin PDIP (Plastic Dual Inline Package)</p>
<p><b>Note</b> − In a DIP package, you can recognize the first pin and the last pin by the cut at the middle of the IC. The first pin is on the left of this cut mark and the last pin (i.e. the 40<sup>th</sup> pin in this case) is to the right of the cut mark.</p>
<h2>I/O Ports and their Functions</h2>
<p>The four ports P0, P1, P2, and P3, each use 8 pins, making them 8-bit ports. Upon RESET, all the ports are configured as inputs, ready to be used as input ports. When the first 0 is written to a port, it becomes an output. To reconfigure it as an input, a 1 must be sent to a port.</p>
<h3>Port 0 (Pin No 32 – Pin No 39)</h3>
<p>It has 8 pins (32 to 39). It can be used for input or output. Unlike P1, P2, and P3 ports, we normally connect P0 to 10K-ohm pull-up resistors to use it as an input or output port being an open drain.</p>
<p>It is also designated as AD0-AD7, allowing it to be used as both address and data. In case of 8031 (i.e. ROMless Chip), when we need to access the external ROM, then P0 will be used for both Address and Data Bus. ALE (Pin no 31) indicates if P0 has address or data. When ALE = 0, it provides data D0-D7, but when ALE = 1, it has address A0-A7. In case no external memory connection is available, P0 must be connected externally to a 10K-ohm pull-up resistor.</p>
<h3>Port 1 (Pin 1 through 8)</h3>
<p>It is an 8-bit port (pin 1 through 8) and can be used either as input or output. It doesn't require pull-up resistors because they are already connected internally. Upon reset, Port 1 is configured as an input port. The following code can be used to send alternating values of 55H and AAH to Port 1.</p>
<p>If Port 1 is configured to be used as an output port, then to use it as an input port again, program it by writing 1 to all of its bits as in the following code.</p>
<h3>Port 2 (Pins 21 through 28)</h3>
<p>Port 2 occupies a total of 8 pins (pins 21 through 28) and can be used for both input and output operations. Just as P1 (Port 1), P2 also doesn't require external Pull-up resistors because they are already connected internally. It must be used along with P0 to provide the 16-bit address for the external memory. So it is also designated as (A0–A7), as shown in the pin diagram. When the 8051 is connected to an external memory, it provides path for upper 8-bits of 16-bits address, and it cannot be used as I/O. Upon reset, Port 2 is configured as an input port. The following code can be used to send alternating values of 55H and AAH to port 2.</p>
<p>If Port 2 is configured to be used as an output port, then to use it as an input port again, program it by writing 1 to all of its bits as in the following code.</p>
<h3>Port 3 (Pins 10 through 17)</h3>
<p>It is also of 8 bits and can be used as Input/Output. This port provides some extremely important signals. P3.0 and P3.1 are RxD (Receiver) and TxD (Transmitter) respectively and are collectively used for Serial Communication. P3.2 and P3.3 pins are used for external interrupts. P3.4 and P3.5 are used for timers T0 and T1 respectively. P3.6 and P3.7 are Write (WR) and Read (RD) pins. These are active low pins, means they will be active when 0 is given to them and these are used to provide Read and Write operations to External ROM in 8031 based systems.</p>
<h2>Dual Role of Port 0 and Port 2</h2>
<p><b>Dual role of Port 0</b> − Port 0 is also designated as AD0–AD7, as it can be used for both data and address handling. While connecting an 8051 to external memory, Port 0 can provide both address and data. The 8051 microcontroller then multiplexes the input as address or data in order to save pins.</p>
<p><b>Dual role of Port 2</b> − Besides working as I/O, Port P2 is also used to provide 16-bit address bus for external memory along with Port 0. Port P2 is also designated as (A8– A15), while Port 0 provides the lower 8-bits via A0–A7. In other words, we can say that when an 8051 is connected to an external memory (ROM) which can be maximum up to 64KB and this is possible by 16 bit address bus because we know 216 = 64KB. Port2 is used for the upper 8-bit of the 16 bits address, and it cannot be used for I/O and this is the way any Program code of external ROM is addressed.</p>
<h2>Hardware Connection of Pins</h2>
<p><b>V<sub>cc</sub></b> − Pin 40 provides supply to the Chip and it is +5 V.</p>
<p><b>Gnd</b> − Pin 20 provides ground for the Reference.</p>
<p><b>XTAL1, XTAL2 (Pin no 18 & Pin no 19)</b> − 8051 has on-chip oscillator but requires external clock to run it. A quartz crystal is connected between the XTAL1 & XTAL2 pin of the chip. This crystal also needs two capacitors of 30pF for generating a signal of desired frequency. One side of each capacitor is connected to ground. 8051 IC is available in various speeds and it all depends on this Quartz crystal, for example, a 20 MHz microcontroller requires a crystal with a frequency no more than 20 MHz.</p>
<p><b>RST (Pin No. 9)</b> − It is an Input pin and active High pin. Upon applying a high pulse on this pin, that is 1, the microcontroller will reset and terminate all activities. This process is known as <b>Power-On Reset</b>. Activating a power-on reset will cause all values in the register to be lost. It will set a program counter to all 0's. To ensure a valid input of Reset, the high pulse must be high for a minimum of two machine cycles before it is allowed to go low, which depends on the capacitor value and the rate at which it charges. (<b>Machine Cycle</b> is the minimum amount of frequency a single instruction requires in execution).</p>
<p><b>EA or External Access (Pin No. 31)</b> − It is an input pin. This pin is an active low pin; upon applying a low pulse, it gets activated. In case of microcontroller (8051/52) having on-chip ROM, the EA (bar) pin is connected to V<sub>cc</sub>. But in an 8031 microcontroller which does not have an on-chip ROM, the code is stored in an external ROM and then fetched by the microcontroller. In this case, we must connect the (pin no 31) EA to Gnd to indicate that the program code is stored externally.</p>
<p><b>PSEN or Program store Enable (Pin No 29)</b> − This is also an active low pin, i.e., it gets activated after applying a low pulse. It is an output pin and used along with the EA pin in 8031 based (i.e. ROMLESS) Systems to allow storage of program code in external ROM.</p>
<p><b>ALE or (Address Latch Enable)</b> − This is an Output Pin and is active high. It is especially used for 8031 IC to connect it to the external memory. It can be used while deciding whether P0 pins will be used as Address bus or Data bus. When ALE = 1, then the P0 pins work as Data bus and when ALE = 0, then the P0 pins act as Address bus.</p>
<h2>I/O Ports and Bit Addressability</h2>
<p>It is a most widely used feature of 8051 while writing code for 8051. Sometimes we need to access only 1 or 2 bits of the port instead of the entire 8-bits. 8051 provides the capability to access individual bits of the ports.</p>
<p>While accessing a port in a single-bit manner, we use the syntax "SETB X. Y" where X is the port number (0 to 3), and Y is a bit number (0 to 7) for data bits D0-D7 where D0 is the LSB and D7 is the MSB. For example, "SETB P1.5" sets high bit 5 of port 1.</p>
<p>The following code shows how we can toggle the bit P1.2 continuously.</p>
<h2>Single-Bit Instructions</h2>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="result notranslate">
MOV A,#0FFH ;(comments: A=FFH(Hexadecimal i.e. A=1111 1111)
MOV P0,A ;(Port0 have 1's on every pin so that it works as Input)
</pre>
<h3>Port 1 (Pin 1 through 8)</h3>
<p>It is an 8-bit port (pin 1 through 8) and can be used either as input or output. It doesn't require pull-up resistors because they are already connected internally. Upon reset, Port 1 is configured as an input port. The following code can be used to send alternating values of 55H and AAH to Port 1.</p>
<pre class="result notranslate">
;Toggle all bits of continuously
MOV A,#55
BACK:
MOV P2,A
ACALL DELAY
CPL A ;complement(invert) reg. A
SJMP BACK
</pre>
<p>If Port 1 is configured to be used as an output port, then to use it as an input port again, program it by writing 1 to all of its bits as in the following code.</p>
<pre class="result notranslate">
;Toggle all bits of continuously
MOV A ,#0FFH ;A = FF hex
MOV P1,A ;Make P1 an input port
MOV A,P1 ;get data from P1
MOV R7,A ;save it in Reg R7
ACALL DELAY ;wait
MOV A,P1 ;get another data from P1
MOV R6,A ;save it in R6
ACALL DELAY ;wait
MOV A,P1 ;get another data from P1
MOV R5,A ;save it in R5
</pre>
<h3>Port 2 (Pins 21 through 28)</h3>
<p>Port 2 occupies a total of 8 pins (pins 21 through 28) and can be used for both input and output operations. Just as P1 (Port 1), P2 also doesn't require external Pull-up resistors because they are already connected internally. It must be used along with P0 to provide the 16-bit address for the external memory. So it is also designated as (A0–A7), as shown in the pin diagram. When the 8051 is connected to an external memory, it provides path for upper 8-bits of 16-bits address, and it cannot be used as I/O. Upon reset, Port 2 is configured as an input port. The following code can be used to send alternating values of 55H and AAH to port 2.</p>
<pre class="result notranslate">
;Toggle all bits of continuously
MOV A,#55
BACK:
MOV P2,A
ACALL DELAY
CPL A ; complement(invert) reg. A
SJMP BACK
</pre>
<p>If Port 2 is configured to be used as an output port, then to use it as an input port again, program it by writing 1 to all of its bits as in the following code.</p>
<pre class="result notranslate">
;Get a byte from P2 and send it to P1
MOV A,#0FFH ;A = FF hex
MOV P2,A ;make P2 an input port
BACK:
MOV A,P2 ;get data from P2
MOV P1,A ;send it to Port 1
SJMP BACK ;keep doing that
</pre>
<h3>Port 3 (Pins 10 through 17)</h3>
<p>It is also of 8 bits and can be used as Input/Output. This port provides some extremely important signals. P3.0 and P3.1 are RxD (Receiver) and TxD (Transmitter) respectively and are collectively used for Serial Communication. P3.2 and P3.3 pins are used for external interrupts. P3.4 and P3.5 are used for timers T0 and T1 respectively. P3.6 and P3.7 are Write (WR) and Read (RD) pins. These are active low pins, means they will be active when 0 is given to them and these are used to provide Read and Write operations to External ROM in 8031 based systems.</p>
<table class="table table-bordered">
<tr>
<th width="33%" style="text-align:center;">P3 Bit</th>
<th width="33%" style="text-align:center;">Function</th>
<th width="33%" style="text-align:center;">Pin</th>
</tr>
<tr style="text-align:center;">
<td>P3.0</td>
<td>RxD</td>
<td>10</td>
</tr>
<tr style="text-align:center;">
<td>P3.1 <</td>
<td>TxD</td>
<td>11</td>
</tr>
<tr style="text-align:center;">
<td>P3.2 <</td>
<td>Complement of INT0</td>
<td>12</td>
</tr>
<tr style="text-align:center;">
<td>P3.3 <</td>
<td>INT1</td>
<td>13</td>
</tr>
<tr style="text-align:center;">
<td>P3.4 <</td>
<td>T0</td>
<td>14</td>
</tr>
<tr style="text-align:center;">
<td>P3.5 <</td>
<td>T1</td>
<td>15</td>
</tr>
<tr style="text-align:center;">
<td>P3.6 <</td>
<td>WR</td>
<td>16</td>
</tr>
<tr style="text-align:center;">
<td>P3.7 <</td>
<td>Complement of RD</td>
<td>17</td>
</tr>
</table>
<h2>Dual Role of Port 0 and Port 2</h2>
<ul class="list">
<li><p><b>Dual role of Port 0</b> − Port 0 is also designated as AD0–AD7, as it can be used for both data and address handling. While connecting an 8051 to external memory, Port 0 can provide both address and data. The 8051 microcontroller then multiplexes the input as address or data in order to save pins.</p></li>
<li><p><b>Dual role of Port 2</b> − Besides working as I/O, Port P2 is also used to provide 16-bit address bus for external memory along with Port 0. Port P2 is also designated as (A8– A15), while Port 0 provides the lower 8-bits via A0–A7. In other words, we can say that when an 8051 is connected to an external memory (ROM) which can be maximum up to 64KB and this is possible by 16 bit address bus because we know 216 = 64KB. Port2 is used for the upper 8-bit of the 16 bits address, and it cannot be used for I/O and this is the way any Program code of external ROM is addressed.</p></li>
</ul>
<h2>Hardware Connection of Pins</h2>
<ul class="list">
<li><p><b>V<sub>cc</sub></b> − Pin 40 provides supply to the Chip and it is +5 V.</p></li>
<li><p><b>Gnd</b> − Pin 20 provides ground for the Reference.</p></li>
<li><p><b>XTAL1, XTAL2 (Pin no 18 & Pin no 19)</b> − 8051 has on-chip oscillator but requires external clock to run it. A quartz crystal is connected between the XTAL1 & XTAL2 pin of the chip. This crystal also needs two capacitors of 30pF for generating a signal of desired frequency. One side of each capacitor is connected to ground. 8051 IC is available in various speeds and it all depends on this Quartz crystal, for example, a 20 MHz microcontroller requires a crystal with a frequency no more than 20 MHz.</p></li>
</ul>
<img src="/embedded_systems/images/xtal.jpg" alt="XTAL1, XTAL2 diagram" />
<ul class="list">
<li><p><b>RST (Pin No. 9)</b> − It is an Input pin and active High pin. Upon applying a high pulse on this pin, that is 1, the microcontroller will reset and terminate all activities. This process is known as <b>Power-On Reset</b>. Activating a power-on reset will cause all values in the register to be lost. It will set a program counter to all 0's. To ensure a valid input of Reset, the high pulse must be high for a minimum of two machine cycles before it is allowed to go low, which depends on the capacitor value and the rate at which it charges. (<b>Machine Cycle</b> is the minimum amount of frequency a single instruction requires in execution).</p></li>
<li><p><b>EA or External Access (Pin No. 31)</b> − It is an input pin. This pin is an active low pin; upon applying a low pulse, it gets activated. In case of microcontroller (8051/52) having on-chip ROM, the EA (bar) pin is connected to V<sub>cc</sub>. But in an 8031 microcontroller which does not have an on-chip ROM, the code is stored in an external ROM and then fetched by the microcontroller. In this case, we must connect the (pin no 31) EA to Gnd to indicate that the program code is stored externally.</p></li>
</ul>
<img src="/embedded_systems/images/rst.jpg" alt="RST, EA diagram" />
<ul class="list">
<li><p><b>PSEN or Program store Enable (Pin No 29)</b> − This is also an active low pin, i.e., it gets activated after applying a low pulse. It is an output pin and used along with the EA pin in 8031 based (i.e. ROMLESS) Systems to allow storage of program code in external ROM.</p></li>
<li><p><b>ALE or (Address Latch Enable)</b> − This is an Output Pin and is active high. It is especially used for 8031 IC to connect it to the external memory. It can be used while deciding whether P0 pins will be used as Address bus or Data bus. When ALE = 1, then the P0 pins work as Data bus and when ALE = 0, then the P0 pins act as Address bus.</p></li>
</ul>
<h2>I/O Ports and Bit Addressability</h2>
<p>It is a most widely used feature of 8051 while writing code for 8051. Sometimes we need to access only 1 or 2 bits of the port instead of the entire 8-bits. 8051 provides the capability to access individual bits of the ports.</p>
<p>While accessing a port in a single-bit manner, we use the syntax "SETB X. Y" where X is the port number (0 to 3), and Y is a bit number (0 to 7) for data bits D0-D7 where D0 is the LSB and D7 is the MSB. For example, "SETB P1.5" sets high bit 5 of port 1.</p>
<p>The following code shows how we can toggle the bit P1.2 continuously.</p>
<pre class="result notranslate">
AGAIN:
SETB P1.2
ACALL DELAY
CLR P1.2
ACALL DELAY
SJMP AGAIN
</pre>
<title>Embedded Systems Terms</title>
<h1>Embedded Systems - Terms</h1>
<h2>Program Counter</h2>
<p>The Program Counter is a 16- or 32-bit register which contains the address of the next instruction to be executed. The PC automatically increments to the next sequential memory location every time an instruction is fetched. Branch, jump, and interrupt operations load the Program Counter with an address other than the next sequential location.</p>
<p>Activating a power-on reset will cause all values in the register to be lost. It means the value of the PC (program counter) is 0 upon reset, forcing the CPU to fetch the first opcode from the ROM memory location 0000. It means we must place the first byte of upcode in ROM location 0000 because that is where the CPU expects to find the first instruction</p>
<h2>Reset Vector</h2>
<p>The significance of the reset vector is that it points the processor to the memory address which contains the firmware's first instruction. Without the Reset Vector, the processor would not know where to begin execution. Upon reset, the processor loads the Program Counter (PC) with the reset vector value from a predefined memory location. On CPU08 architecture, this is at location $FFFE:$FFFF.</p>
<p>When the reset vector is not necessary, developers normally take it for granted and don’t program into the final image. As a result, the processor doesn't start up on the final product. It is a common mistake that takes place during the debug phase.</p>
<h2>Stack Pointer</h2>
<p>Stack is implemented in RAM and a CPU register is used to access it called SP (Stack Pointer) register. SP register is an 8-bit register and can address memory addresses of range 00h to FFh. Initially, the SP register contains value 07 to point to location 08 as the first location being used for the stack by the 8051.</p>
<p>When the content of a CPU register is stored in a stack, it is called a PUSH operation. When the content of a stack is stored in a CPU register, it is called a POP operation. In other words, a register is pushed onto the stack to save it and popped off the stack to retrieve it.</p>
<h2>Infinite Loop</h2>
<p>An infinite loop or an endless loop can be identified as a sequence of instructions in a computer program that executes endlessly in a loop, because of the following reasons −</p>
<p>Such infinite loops normally caused older operating systems to become unresponsive, as an infinite loop consumes all the available processor time. I/O operations waiting for user inputs are also called "infinite loops". One possible cause of a computer "freezing" is an infinite loop; other causes include <b>deadlock</b> and <b>access violations</b>.</p>
<p>Embedded systems, unlike a PC, never "exit" an application. They idle through an Infinite Loop waiting for an event to take place in the form of an interrupt, or a <b>pre-scheduled task</b>. In order to save power, some processors enter special <b>sleep</b> or <b>wait modes</b> instead of idling through an Infinite Loop, but they will come out of this mode upon either a timer or an External Interrupt.</p>
<h2>Interrupts</h2>
<p>Interrupts are mostly hardware mechanisms that instruct the program that an event has occurred. They may occur at any time, and are therefore asynchronous to the program flow. They require special handling by the processor, and are ultimately handled by a corresponding Interrupt Service Routine (ISR). Interrupts need to be handled quickly. If you take too much time servicing an interrupt, then you may miss another interrupt.</p>
<h2>Little Endian Vs Big Endian</h2>
<p>Although numbers are always displayed in the same way, they are not stored in the same way in memory. Big-Endian machines store the most significant byte of data in the lowest memory address. A Big-Endian machine stores 0x12345678 as −</p>
<p>Little-Endian machines, on the other hand, store the least significant byte of data in the lowest memory address. A Little-Endian machine stores 0x12345678 as −</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="result notranslate">
ADD+0: 0x12
ADD+1: 0x34
ADD+2: 0x56
ADD+3: 0x78
</pre>
<p>Little-Endian machines, on the other hand, store the least significant byte of data in the lowest memory address. A Little-Endian machine stores 0x12345678 as −</p>
<pre class="result notranslate">
ADD+0: 0x78
ADD+1: 0x56
ADD+2: 0x34
ADD+3: 0x12
</pre>
<title>Embedded Systems Assembly Language</title>
<h1>Embedded Systems - Assembly Language</h1>
<p>Assembly languages were developed to provide <b>mnemonics</b> or symbols for the machine level code instructions. Assembly language programs consist of mnemonics, thus they should be translated into machine code. A program that is responsible for this conversion is known as <b>assembler</b>. Assembly language is often termed as a low-level language because it directly works with the internal structure of the CPU. To program in assembly language, a programmer must know all the registers of the CPU.</p>
<p>Different programming languages such as C, C++, Java and various other languages are called high-level languages because they do not deal with the internal details of a CPU. In contrast, an assembler is used to translate an assembly language program into machine code (sometimes also called <b>object code</b> or <b>opcode</b>). Similarly, a compiler translates a high-level language into machine code. For example, to write a program in C language, one must use a C compiler to translate the program into machine language.</p>
<h2>Structure of Assembly Language</h2>
<p>An assembly language program is a series of statements, which are either assembly language instructions such as ADD and MOV, or statements called <b>directives</b>.</p>
<p>An <b>instruction</b> tells the CPU what to do, while a <b>directive</b> (also called <b>pseudo-instructions</b>) gives instruction to the assembler. For example, ADD and MOV instructions are commands which the CPU runs, while ORG and END are assembler directives. The assembler places the opcode to the memory location 0 when the ORG directive is used, while END indicates to the end of the source code. A program language instruction consists of the following four fields −</p>
<p>A square bracket ( [ ] ) indicates that the field is optional.</p>
<p>The <b>label field</b> allows the program to refer to a line of code by name. The label fields cannot exceed a certain number of characters.</p>
<p>The <b>mnemonics</b> and <b>operands fields</b> together perform the real work of the program and accomplish the tasks. Statements like ADD A , C & MOV C, #68 where ADD and MOV are the mnemonics, which produce opcodes ; "A, C" and "C, #68" are operands. These two fields could contain directives. Directives do not generate machine code and are used only by the assembler, whereas instructions are translated into machine code for the CPU to execute.</p>
<p>The <b>comment field</b> begins with a semicolon which is a comment indicator.</p>
<p>Notice the Label "HERE" in the program. Any label which refers to an instruction should be followed by a colon.</p>
<h2>Assembling and Running an 8051 Program</h2>
<p>Here we will discuss about the basic form of an assembly language. The steps to create, assemble, and run an assembly language program are as follows −</p>
<p>First, we use an editor to type in a program similar to the above program. Editors like MS-DOS EDIT program that comes with all Microsoft operating systems can be used to create or edit a program. The Editor must be able to produce an ASCII file. The "asm" extension for the source file is used by an assembler in the next step.</p>
<p>The "asm" source file contains the program code created in Step 1. It is fed to an 8051 assembler. The assembler then converts the assembly language instructions into machine code instructions and produces an <b>.obj file</b> (object file) and a <b>.lst file</b> (list file). It is also called as a <b>source file</b>, that's why some assemblers require that this file have the "src" extensions. The "lst" file is optional. It is very useful to the program because it lists all the opcodes and addresses as well as errors that the assemblers detected.</p>
<p>Assemblers require a third step called <b>linking</b>. The link program takes one or more object files and produces an absolute object file with the extension "abs".</p>
<p>Next, the "abs" file is fed to a program called "OH" (object to hex converter), which creates a file with the extension "hex" that is ready to burn in to the ROM.</p>
<h2>Data Type</h2>
<p>The 8051 microcontroller contains a single data type of 8-bits, and each register is also of 8-bits size. The programmer has to break down data larger than 8-bits (00 to FFH, or to 255 in decimal) so that it can be processed by the CPU.</p>
<h3>DB (Define Byte)</h3>
<p>The DB directive is the most widely used data directive in the assembler. It is used to define the 8-bit data. It can also be used to define decimal, binary, hex, or ASCII formats data. For decimal, the "D" after the decimal number is optional, but it is required for "B" (binary) and "Hl" (hexadecimal).</p>
<p>To indicate ASCII, simply place the characters in quotation marks ('like this'). The assembler generates ASCII code for the numbers/characters automatically. The DB directive is the only directive that can be used to define ASCII strings larger than two characters; therefore, it should be used for all the ASCII data definitions. Some examples of DB are given below −</p>
<p>Either single or double quotes can be used around ASCII strings. DB is also used to allocate memory in byte-sized chunks.</p>
<h2>Assembler Directives</h2>
<p>Some of the directives of 8051 are as follows −</p>
<p><b>ORG (origin)</b> − The origin directive is used to indicate the beginning of the address. It takes the numbers in hexa or decimal format. If H is provided after the number, the number is treated as hexa, otherwise decimal. The assembler converts the decimal number to hexa.</p>
<p><b>EQU (equate)</b> − It is used to define a constant without occupying a memory location. EQU associates a constant value with a data label so that the label appears in the program, its constant value will be substituted for the label. While executing the instruction "MOV R3, #COUNT", the register R3 will be loaded with the value 25 (notice the # sign). The advantage of using EQU is that the programmer can change it once and the assembler will change all of its occurrences; the programmer does not have to search the entire program.</p>
<p><b>END directive</b> − It indicates the end of the source (asm) file. The END directive is the last line of the program; anything after the END directive is ignored by the assembler.</p>
<h2>Labels in Assembly Language</h2>
<p>All the labels in assembly language must follow the rules given below −</p>
<p>Each label name must be unique. The names used for labels in assembly language programming consist of alphabetic letters in both uppercase and lowercase, number 0 through 9, and special characters such as question mark (?), period (.), at the rate @, underscore (_), and dollar($).</p>
<p>The first character should be in alphabetical character; it cannot be a number.</p>
<p>Reserved words cannot be used as a label in the program. For example, ADD and MOV words are the reserved words, since they are instruction mnemonics.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="result notranslate">
[ label: ] mnemonics [ operands ] [;comment ]
</pre>
<p>A square bracket ( [ ] ) indicates that the field is optional.</p>
<ul class="list">
<li><p>The <b>label field</b> allows the program to refer to a line of code by name. The label fields cannot exceed a certain number of characters.</p></li>
<li><p>The <b>mnemonics</b> and <b>operands fields</b> together perform the real work of the program and accomplish the tasks. Statements like ADD A , C & MOV C, #68 where ADD and MOV are the mnemonics, which produce opcodes ; "A, C" and "C, #68" are operands. These two fields could contain directives. Directives do not generate machine code and are used only by the assembler, whereas instructions are translated into machine code for the CPU to execute.</p></li>
</ul>
<pre class="result notranslate">
1.0000 ORG 0H ;start (origin) at location 0
2 0000 7D25 MOV R5,#25H ;load 25H into R5
3.0002 7F34 MOV R7,#34H ;load 34H into R7
4.0004 7400 MOV A,#0 ;load 0 into A
5.0006 2D ADD A,R5 ;add contents of R5 to A
6.0007 2F ADD A,R7 ;add contents of R7 to A
7.0008 2412 ADD A,#12H ;add to A value 12 H
8.000A 80FE HERE: SJMP HERE ;stay in this loop
9.000C END ;end of asm source file
</pre>
<ul class="list">
<li><p>The <b>comment field</b> begins with a semicolon which is a comment indicator.</p></li>
<li><p>Notice the Label "HERE" in the program. Any label which refers to an instruction should be followed by a colon.</p></li>
</ul>
<h2>Assembling and Running an 8051 Program</h2>
<p>Here we will discuss about the basic form of an assembly language. The steps to create, assemble, and run an assembly language program are as follows −</p>
<ul class="list">
<li><p>First, we use an editor to type in a program similar to the above program. Editors like MS-DOS EDIT program that comes with all Microsoft operating systems can be used to create or edit a program. The Editor must be able to produce an ASCII file. The "asm" extension for the source file is used by an assembler in the next step.</p></li>
<li><p>The "asm" source file contains the program code created in Step 1. It is fed to an 8051 assembler. The assembler then converts the assembly language instructions into machine code instructions and produces an <b>.obj file</b> (object file) and a <b>.lst file</b> (list file). It is also called as a <b>source file</b>, that's why some assemblers require that this file have the "src" extensions. The "lst" file is optional. It is very useful to the program because it lists all the opcodes and addresses as well as errors that the assemblers detected.</p></li>
<li><p>Assemblers require a third step called <b>linking</b>. The link program takes one or more object files and produces an absolute object file with the extension "abs".</p></li>
<li><p>Next, the "abs" file is fed to a program called "OH" (object to hex converter), which creates a file with the extension "hex" that is ready to burn in to the ROM.</p></li>
</ul>
<img src="/embedded_systems/images/steps_program.jpg" alt="Steps to create program" />
<h2>Data Type</h2>
<p>The 8051 microcontroller contains a single data type of 8-bits, and each register is also of 8-bits size. The programmer has to break down data larger than 8-bits (00 to FFH, or to 255 in decimal) so that it can be processed by the CPU.</p>
<h3>DB (Define Byte)</h3>
<p>The DB directive is the most widely used data directive in the assembler. It is used to define the 8-bit data. It can also be used to define decimal, binary, hex, or ASCII formats data. For decimal, the "D" after the decimal number is optional, but it is required for "B" (binary) and "Hl" (hexadecimal).</p>
<p>To indicate ASCII, simply place the characters in quotation marks ('like this'). The assembler generates ASCII code for the numbers/characters automatically. The DB directive is the only directive that can be used to define ASCII strings larger than two characters; therefore, it should be used for all the ASCII data definitions. Some examples of DB are given below −</p>
<pre class="result notranslate">
ORG 500H
DATA1: DB 28 ;DECIMAL (1C in hex)
DATA2: DB 00110101B ;BINARY (35 in hex)
DATA3: DB 39H ;HEX
ORG 510H
DATA4: DB "2591" ;ASCII NUMBERS
ORG 520H
DATA6: DA "MY NAME IS Michael" ;ASCII CHARACTERS
</pre>
<title>Embedded Systems Registers</title>
<h1>Embedded Systems - Registers</h1>
<p>Registers are used in the CPU to store information on temporarily basis which could be data to be processed, or an address pointing to the data which is to be fetched. In 8051, there is one data type is of 8-bits, from the MSB (most significant bit) D7 to the LSB (least significant bit) D0. With 8-bit data type, any data type larger than 8-bits must be broken into 8-bit chunks before it is processed.</p>
<p>The most widely used registers of the 8051 are A (accumulator), B, R0-R7, DPTR (data pointer), and PC (program counter). All these registers are of 8-bits, except DPTR and PC.</p>
<h2>Storage Registers in 8051</h2>
<p>We will discuss the following types of storage registers here −</p>
<h3>Accumulator</h3>
<p>The accumulator, register A, is used for all arithmetic and logic operations. If the accumulator is not present, then every result of each calculation (addition, multiplication, shift, etc.) is to be stored into the main memory. Access to main memory is slower than access to a register like the accumulator because the technology used for the large main memory is slower (but cheaper) than that used for a register.</p>
<h3>The "R" Registers</h3>
<p>The "R" registers are a set of eight registers, namely, R0, R1 to R7. These registers function as auxiliary or temporary storage registers in many operations. Consider an example of the sum of 10 and 20. Store a variable 10 in an accumulator and another variable 20 in, say, register R4. To process the addition operation, execute the following command −</p>
<p>After executing this instruction, the accumulator will contain the value 30. Thus "R" registers are very important auxiliary or <b>helper registers</b>. The Accumulator alone would not be very useful if it were not for these "R" registers. The "R" registers are meant for temporarily storage of values.</p>
<p>Let us take another example. We will add the values in R1 and R2 together and then subtract the values of R3 and R4 from the result.</p>
<p>As you can see, we used R5 to temporarily hold the sum of R3 and R4. Of course, this is not the most efficient way to calculate (R1 + R2) – (R3 + R4), but it does illustrate the use of the "R" registers as a way to store values temporarily.</p>
<h3>The "B" Register</h3>
<p>The "B" register is very similar to the Accumulator in the sense that it may hold an 8-bit (1-byte) value. The "B" register is used only by two 8051 instructions: <b>MUL AB</b> and <b>DIV AB</b>. To quickly and easily multiply or divide A by another number, you may store the other number in "B" and make use of these two instructions. Apart from using MUL and DIV instructions, the "B" register is often used as yet another temporary storage register, much like a ninth R register.</p>
<h3>The Data Pointer</h3>
<p>The Data Pointer (DPTR) is the 8051’s only user-accessible 16-bit (2-byte) register. The Accumulator, R0–R7 registers and B register are 1-byte value registers. DPTR is meant for pointing to data. It is used by the 8051 to access external memory using the address indicated by DPTR. DPTR is the only 16-bit register available and is often used to store 2-byte values.</p>
<h3>The Program Counter</h3>
<p>The Program Counter (PC) is a 2-byte address which tells the 8051 where the next instruction to execute can be found in the memory. PC starts at 0000h when the 8051 initializes and is incremented every time after an instruction is executed. PC is not always incremented by 1. Some instructions may require 2 or 3 bytes; in such cases, the PC will be incremented by 2 or 3.</p>
<p><b>Branch, jump</b>, and <b>interrupt</b> operations load the Program Counter with an address other than the next sequential location. Activating a power-on reset will cause all values in the register to be lost. It means the value of the PC is 0 upon reset, forcing the CPU to fetch the first opcode from the ROM location 0000. It means we must place the first byte of upcode in ROM location 0000 because that is where the CPU expects to find the first instruction.</p>
<h3>The Stack Pointer (SP)</h3>
<p>The Stack Pointer, like all registers except DPTR and PC, may hold an 8-bit (1-byte) value. The Stack Pointer tells the location from where the next value is to be removed from the stack. When a value is pushed onto the stack, the value of SP is incremented and then the value is stored at the resulting memory location. When a value is popped off the stack, the value is returned from the memory location indicated by SP, and then the value of SP is decremented.</p>
<p>This order of operation is important. SP will be initialized to 07h when the 8051 is initialized. If a value is pushed onto the stack at the same time, the value will be stored in the internal RAM address 08h because the 8051 will first increment the value of SP (from 07h to 08h) and then will store the pushed value at that memory address (08h). SP is modified directly by the 8051 by six instructions: PUSH, POP, ACALL, LCALL, RET, and RETI.</p>
<h2>ROM Space in 8051</h2>
<p>Some family members of 8051 have only 4K bytes of on-chip ROM (e.g. 8751, AT8951); some have 8K ROM like AT89C52, and there are some family members with 32K bytes and 64K bytes of on-chip ROM such as Dallas Semiconductor. The point to remember is that no member of the 8051 family can access more than 64K bytes of opcode since the program counter in 8051 is a 16-bit register (0000 to FFFF address).</p>
<p>The first location of the program ROM inside the 8051 has the address of 0000H, whereas the last location can be different depending on the size of the ROM on the chip. Among the 8051 family members, AT8951 has $k bytes of on-chip ROM having a memory address of 0000 (first location) to 0FFFH (last location).</p>
<h3>8051 Flag Bits and PSW Register</h3>
<p>The program status word (PSW) register is an 8-bit register, also known as <b>flag register</b>. It is of 8-bit wide but only 6-bit of it is used. The two unused bits are <b>user-defined flags</b>. Four of the flags are called <b>conditional flags</b>, which means that they indicate a condition which results after an instruction is executed. These four are <b>CY </b>(Carry), <b>AC</b> (auxiliary carry), <b>P</b> (parity), and <b>OV</b> (overflow). The bits RS0 and RS1 are used to change the bank registers. The following figure shows the program status word register.</p>
<p>The PSW Register contains that status bits that reflect the current status of the CPU.</p>
<p></p>
<p>We can select the corresponding Register Bank bit using RS0 and RS1 bits.</p>
<p><b>CY, the carry flag</b> − This carry flag is set (1) whenever there is a carry out from the D7 bit. It is affected after an 8-bit addition or subtraction operation. It can also be reset to 1 or 0 directly by an instruction such as "SETB C" and "CLR C" where "SETB" stands for set bit carry and "CLR" stands for clear carry.</p>
<p><b>AC, auxiliary carry flag</b> − If there is a carry from D3 and D4 during an ADD or SUB operation, the AC bit is set; otherwise, it is cleared. It is used for the instruction to perform binary coded decimal arithmetic.</p>
<p><b>P, the parity flag</b> − The parity flag represents the number of 1's in the accumulator register only. If the A register contains odd number of 1's, then P = 1; and for even number of 1's, P = 0.</p>
<p><b>OV, the overflow flag</b> − This flag is set whenever the result of a signed number operation is too large causing the high-order bit to overflow into the sign bit. It is used only to detect errors in signed arithmetic operations.</p>
<h3>Example</h3>
<p>Show the status of CY, AC, and P flags after the addition of 9CH and 64H in the following instruction.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="result notranslate">
ADD A,R4
</pre>
<p>After executing this instruction, the accumulator will contain the value 30. Thus "R" registers are very important auxiliary or <b>helper registers</b>. The Accumulator alone would not be very useful if it were not for these "R" registers. The "R" registers are meant for temporarily storage of values.</p>
<p>Let us take another example. We will add the values in R1 and R2 together and then subtract the values of R3 and R4 from the result.</p>
<pre class="result notranslate">
MOV A,R3 ;Move the value of R3 into the accumulator
ADD A,R4 ;Add the value of R4
MOV R5,A ;Store the resulting value temporarily in R5
MOV A,R1 ;Move the value of R1 into the accumulator
ADD A,R2 ;Add the value of R2
SUBB A,R5 ;Subtract the value of R5 (which now contains R3 + R4)
</pre>
<p>As you can see, we used R5 to temporarily hold the sum of R3 and R4. Of course, this is not the most efficient way to calculate (R1 + R2) – (R3 + R4), but it does illustrate the use of the "R" registers as a way to store values temporarily.</p>
<img src="/embedded_systems/images/8bit_registers.jpg" alt="8 Bit registers" />
<h3>The "B" Register</h3>
<p>The "B" register is very similar to the Accumulator in the sense that it may hold an 8-bit (1-byte) value. The "B" register is used only by two 8051 instructions: <b>MUL AB</b> and <b>DIV AB</b>. To quickly and easily multiply or divide A by another number, you may store the other number in "B" and make use of these two instructions. Apart from using MUL and DIV instructions, the "B" register is often used as yet another temporary storage register, much like a ninth R register.</p>
<h3>The Data Pointer</h3>
<p>The Data Pointer (DPTR) is the 8051’s only user-accessible 16-bit (2-byte) register. The Accumulator, R0–R7 registers and B register are 1-byte value registers. DPTR is meant for pointing to data. It is used by the 8051 to access external memory using the address indicated by DPTR. DPTR is the only 16-bit register available and is often used to store 2-byte values.</p>
<h3>The Program Counter</h3>
<p>The Program Counter (PC) is a 2-byte address which tells the 8051 where the next instruction to execute can be found in the memory. PC starts at 0000h when the 8051 initializes and is incremented every time after an instruction is executed. PC is not always incremented by 1. Some instructions may require 2 or 3 bytes; in such cases, the PC will be incremented by 2 or 3.</p>
<p><b>Branch, jump</b>, and <b>interrupt</b> operations load the Program Counter with an address other than the next sequential location. Activating a power-on reset will cause all values in the register to be lost. It means the value of the PC is 0 upon reset, forcing the CPU to fetch the first opcode from the ROM location 0000. It means we must place the first byte of upcode in ROM location 0000 because that is where the CPU expects to find the first instruction.</p>
<h3>The Stack Pointer (SP)</h3>
<p>The Stack Pointer, like all registers except DPTR and PC, may hold an 8-bit (1-byte) value. The Stack Pointer tells the location from where the next value is to be removed from the stack. When a value is pushed onto the stack, the value of SP is incremented and then the value is stored at the resulting memory location. When a value is popped off the stack, the value is returned from the memory location indicated by SP, and then the value of SP is decremented.</p>
<p>This order of operation is important. SP will be initialized to 07h when the 8051 is initialized. If a value is pushed onto the stack at the same time, the value will be stored in the internal RAM address 08h because the 8051 will first increment the value of SP (from 07h to 08h) and then will store the pushed value at that memory address (08h). SP is modified directly by the 8051 by six instructions: PUSH, POP, ACALL, LCALL, RET, and RETI.</p>
<h2>ROM Space in 8051</h2>
<p>Some family members of 8051 have only 4K bytes of on-chip ROM (e.g. 8751, AT8951); some have 8K ROM like AT89C52, and there are some family members with 32K bytes and 64K bytes of on-chip ROM such as Dallas Semiconductor. The point to remember is that no member of the 8051 family can access more than 64K bytes of opcode since the program counter in 8051 is a 16-bit register (0000 to FFFF address).</p>
<p>The first location of the program ROM inside the 8051 has the address of 0000H, whereas the last location can be different depending on the size of the ROM on the chip. Among the 8051 family members, AT8951 has $k bytes of on-chip ROM having a memory address of 0000 (first location) to 0FFFH (last location).</p>
<img src="/embedded_systems/images/rom_space.jpg" alt="ROM Space" />
<h3>8051 Flag Bits and PSW Register</h3>
<p>The program status word (PSW) register is an 8-bit register, also known as <b>flag register</b>. It is of 8-bit wide but only 6-bit of it is used. The two unused bits are <b>user-defined flags</b>. Four of the flags are called <b>conditional flags</b>, which means that they indicate a condition which results after an instruction is executed. These four are <b>CY </b>(Carry), <b>AC</b> (auxiliary carry), <b>P</b> (parity), and <b>OV</b> (overflow). The bits RS0 and RS1 are used to change the bank registers. The following figure shows the program status word register.</p>
<p>The PSW Register contains that status bits that reflect the current status of the CPU.</p>
<table class="table table-bordered">
<tr>
<th>CY</th>
<th>CA</th>
<th>F0</th>
<th>RS1</th>
<th>RS0</th>
<th>OV</th>
<th>-</th>
<th>P</th>
</tr>
</table>
<p></p>
<table class="table table-bordered">
<tr>
<td>CY</td>
<td>PSW.7</td>
<td>Carry Flag</td>
</tr>
<tr>
<td>AC</td>
<td>PSW.6</td>
<td>Auxiliary Carry Flag</td>
</tr>
<tr>
<td>F0</td>
<td>PSW.5</td>
<td>Flag 0 available to user for general purpose.</td>
</tr>
<tr>
<td>RS1</td>
<td>PSW.4</td>
<td>Register Bank selector bit 1</td>
</tr>
<tr>
<td>RS0</td>
<td>PSW.3</td>
<td>Register Bank selector bit 0</td>
</tr>
<tr>
<td>OV</td>
<td>PSW.2</td>
<td>Overflow Flag</td>
</tr>
<tr>
<td>-</td>
<td>PSW.1</td>
<td>User definable FLAG</td>
</tr>
<tr>
<td>P</td>
<td>PSW.0</td>
<td>Parity FLAG. Set/ cleared by hardware during instruction cycle to indicate even/odd number of 1 bit in accumulator.</td>
</tr>
</table>
<p>We can select the corresponding Register Bank bit using RS0 and RS1 bits.</p>
<table class="table table-bordered">
<tr>
<th style="text-align:center;">RS1</th>
<th style="text-align:center;">RS2</th>
<th style="text-align:center;">Register Bank</th>
<th style="text-align:center;">Address</th>
</tr>
<tr style="text-align:center;">
<td>0</td>
<td>0</td>
<td>0</td>
<td>00H-07H</td>
</tr>
<tr style="text-align:center;">
<td>0</td>
<td>1</td>
<td>1</td>
<td>08H-0FH</td>
</tr>
<tr style="text-align:center;">
<td>1</td>
<td>0</td>
<td>2</td>
<td>10H-17H</td>
</tr>
<tr style="text-align:center;">
<td>1</td>
<td>1</td>
<td>3</td>
<td>18H-1FH</td>
</tr>
</table>
<ul class="list">
<li><p><b>CY, the carry flag</b> − This carry flag is set (1) whenever there is a carry out from the D7 bit. It is affected after an 8-bit addition or subtraction operation. It can also be reset to 1 or 0 directly by an instruction such as "SETB C" and "CLR C" where "SETB" stands for set bit carry and "CLR" stands for clear carry.</p></li>
<li><p><b>AC, auxiliary carry flag</b> − If there is a carry from D3 and D4 during an ADD or SUB operation, the AC bit is set; otherwise, it is cleared. It is used for the instruction to perform binary coded decimal arithmetic.</p></li>
<li><p><b>P, the parity flag</b> − The parity flag represents the number of 1's in the accumulator register only. If the A register contains odd number of 1's, then P = 1; and for even number of 1's, P = 0.</p></li>
<li><p><b>OV, the overflow flag</b> − This flag is set whenever the result of a signed number operation is too large causing the high-order bit to overflow into the sign bit. It is used only to detect errors in signed arithmetic operations.</p></li>
</ul>
<h3>Example</h3>
<p>Show the status of CY, AC, and P flags after the addition of 9CH and 64H in the following instruction.</p>
<p style="padding-left:10%;">MOV A, #9CH</p>
<p style="padding-left:10%;">ADD A, # 64H</p>
<pre class="result notranslate">
Solution: 9C 10011100
+64 01100100
100 00000000
CY = 1 since there is a carry beyond D7 bit
AC = 0 since there is a carry from D3 to D4
P = 0 because the accumulator has even number of 1's
</pre>
<title>Embedded Systems Registers Bank/Stack</title>
<h1>Embedded Systems - Registers Bank/Stack</h1>
<p>The 8051 microcontroller has a total of 128 bytes of RAM. We will discuss about the allocation of these 128 bytes of RAM and examine their usage as stack and register.</p>
<h2>RAM Memory Space Allocation in 8051</h2>
<p>The 128 bytes of RAM inside the 8051 are assigned the address 00 to 7FH. They can be accessed directly as memory locations and are divided into three different groups as follows −</p>
<p>32 bytes from 00H to 1FH locations are set aside for register banks and the stack.</p>
<p>16 bytes from 20H to 2FH locations are set aside for bit-addressable read/write memory.</p>
<p>80 bytes from 30H to 7FH locations are used for read and write storage; it is called as <b>scratch pad</b>. These 80 locations RAM are widely used for the purpose of storing data and parameters by 8051 programmers.</p>
<h2>Register Banks in 8051</h2>
<p>A total of 32 bytes of RAM are set aside for the register banks and the stack. These 32 bytes are divided into four register banks in which each bank has 8 registers, R0–R7. RAM locations from 0 to 7 are set aside for bank 0 of R0–R7 where R0 is RAM location 0, R1 is RAM location 1, R2 is location 2, and so on, until the memory location 7, which belongs to R7 of bank 0.</p>
<p>The second bank of registers R0–R7 starts at RAM location 08 and goes to locations OFH. The third bank of R0–R7 starts at memory location 10H and goes to location to 17H. Finally, RAM locations 18H to 1FH are set aside for the fourth bank of R0–R7.</p>
<h2>Default Register Bank</h2>
<p>If RAM locations 00–1F are set aside for the four registers banks, which register bank of R0–R7 do we have access to when the 8051 is powered up? The answer is register bank 0; that is, RAM locations from 0 to 7 are accessed with the names R0 to R7 when programming the 8051. Because it is much easier to refer these RAM locations by names such as R0 to R7, rather than by their memory locations.</p>
<h2>How to Switch Register Banks</h2>
<p>Register bank 0 is the default when the 8051 is powered up. We can switch to the other banks using PSW register. D4 and D3 bits of the PSW are used to select the desired register bank, since they can be accessed by the bit addressable instructions SETB and CLR. For example, "SETB PSW.3" will set PSW.3 = 1 and select the bank register 1.</p>
<h2>Stack and its Operations</h2>
<h3>Stack in the 8051</h3>
<p>The stack is a section of a RAM used by the CPU to store information such as data or memory address on temporary basis. The CPU needs this storage area considering limited number of registers.</p>
<h3>How Stacks are Accessed</h3>
<p>As the stack is a section of a RAM, there are registers inside the CPU to point to it. The register used to access the stack is known as the stack pointer register. The stack pointer in the 8051 is 8-bits wide, and it can take a value of 00 to FFH. When the 8051 is initialized, the SP register contains the value 07H. This means that the RAM location 08 is the first location used for the stack. The storing operation of a CPU register in the stack is known as a <b>PUSH</b>, and getting the contents from the stack back into a CPU register is called a <b>POP</b>.</p>
<h3>Pushing into the Stack</h3>
<p>In the 8051, the stack pointer (SP) points to the last used location of the stack. When data is pushed onto the stack, the stack pointer (SP) is incremented by 1. When PUSH is executed, the contents of the register are saved on the stack and SP is incremented by 1. To push the registers onto the stack, we must use their RAM addresses. For example, the instruction "PUSH 1" pushes register R1 onto the stack.</p>
<h3>Popping from the Stack</h3>
<p>Popping the contents of the stack back into a given register is the opposite to the process of pushing. With every pop operation, the top byte of the stack is copied to the register specified by the instruction and the stack pointer is decremented once.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>Embedded Systems Instructions</title>
<h1>Embedded Systems - Instructions</h1>
<p>The flow of program proceeds in a sequential manner, from one instruction to the next instruction, unless a control transfer instruction is executed. The various types of control transfer instruction in assembly language include conditional or unconditional jumps and call instructions.</p>
<h2>Loop and Jump Instructions</h2>
<h3>Looping in the 8051</h3>
<p>Repeating a sequence of instructions a certain number of times is called a <b>loop</b>. An instruction <b>DJNZ reg, label</b> is used to perform a Loop operation. In this instruction, a register is decremented by 1; if it is not zero, then 8051 jumps to the target address referred to by the label.</p>
<p>The register is loaded with the counter for the number of repetitions prior to the start of the loop. In this instruction, both the registers decrement and the decision to jump are combined into a single instruction. The registers can be any of R0–R7. The counter can also be a RAM location.</p>
<h3>Example</h3>
<p><b>Multiply 25 by 10 using the technique of repeated addition.</b></p>
<p><b>Solution</b> − Multiplication can be achieved by adding the multiplicand repeatedly, as many times as the multiplier. For example,</p>
<p>25 * 10 = 250(FAH)</p>
<p>25 + 25 + 25 + 25 + 25 + 25 + 25 + 25 + 25 + 25 = 250</p>
<p><b>Drawback in 8051</b> − Looping action with the instruction <b>DJNZ Reg label</b> is limited to 256 iterations only. If a conditional jump is not taken, then the instruction following the jump is executed.</p>
<h3>Looping inside a Loop</h3>
<p>When we use a loop inside another loop, it is called a <b>nested loop</b>. Two registers are used to hold the count when the maximum count is limited to 256. So we use this method to repeat the action more times than 256.</p>
<p><b>Example</b></p>
<p>Write a program to −</p>
<p><b>Solution</b> − Since 700 is greater than 255 (the maximum capacity of any register), two registers are used to hold the count. The following code shows how to use two registers, R2 and R3, for the count.</p>
<h2>Other Conditional Jumps</h2>
<p>The following table lists the conditional jumps used in 8051 −</p>
<p><b>JZ (jump if A = 0)</b> − In this instruction, the content of the accumulator is checked. If it is zero, then the 8051 jumps to the target address. JZ instruction can be used only for the accumulator, it does not apply to any other register.</p>
<p><b>JNZ (jump if A is not equal to 0)</b> − In this instruction, the content of the accumulator is checked to be non-zero. If it is not zero, then the 8051 jumps to the target address.</p>
<p><b>JNC (Jump if no carry, jumps if CY = 0)</b> − The Carry flag bit in the flag (or PSW) register is used to make the decision whether to jump or not "JNC label". The CPU looks at the carry flag to see if it is raised (CY = 1). If it is not raised, then the CPU starts to fetch and execute instructions from the address of the label. If CY = 1, it will not jump but will execute the next instruction below JNC.</p>
<p><b>JC (Jump if carry, jumps if CY = 1)</b> − If CY = 1, it jumps to the target address.</p>
<p><b>JB (jump if bit is high)</b></p>
<p><b>JNB (jump if bit is low)</b></p>
<p><b>Note</b> − It must be noted that all conditional jumps are short jumps, i.e., the address of the target must be within –128 to +127 bytes of the contents of the program counter.</p>
<h2>Unconditional Jump Instructions</h2>
<p>There are two unconditional jumps in 8051 −</p>
<p><b>LJMP (long jump)</b> − LJMP is 3-byte instruction in which the first byte represents opcode, and the second and third bytes represent the 16-bit address of the target location. The 2-byte target address is to allow a jump to any memory location from 0000 to FFFFH.</p>
<p><b>SJMP (short jump)</b> − It is a 2-byte instruction where the first byte is the opcode and the second byte is the relative address of the target location. The relative address ranges from 00H to FFH which is divided into forward and backward jumps; that is, within –128 to +127 bytes of memory relative to the address of the current PC (program counter). In case of forward jump, the target address can be within a space of 127 bytes from the current PC. In case of backward jump, the target address can be within –128 bytes from the current PC.</p>
<h2>Calculating the Short Jump Address</h2>
<p>All conditional jumps (JNC, JZ, and DJNZ) are short jumps because they are 2-byte instructions. In these instructions, the first byte represents opcode and the second byte represents the relative address. The target address is always relative to the value of the program counter. To calculate the target address, the second byte is added to the PC of the instruction immediately below the jump. Take a look at the program given below −</p>
<h3>Backward Jump Target Address Calculation</h3>
<p>In case of a forward jump, the displacement value is a positive number between 0 to 127 (00 to 7F in hex). However, for a backward jump, the displacement is a negative value of 0 to –128.</p>
<h2>CALL Instructions</h2>