What is a microprocessor?
The microprocessor is the integration of a number of useful functions. These functions are:
The ability to execute a stored set of instructions to carry out user defined tasks. General-purpose microprocessor.
Data Bus
Serial COM Port
RAM
ROM
I/O Port
Timer
Address Bus
What is Microcontroller?
Microcontroller is a device which integrates a number of the components of a microprocessor system onto a single microchip. So a microcontroller combines onto the same microchip : The CPU core Memory (both ROM and RAM) Some parallel digital I/O
Example Motorolas 6811, Intels 8051, Zilogs Z8 and PIC 16X,ATMEL 89S51,52 etc
Microcontrollers
Targeted for low end of market where performance doesnt matter Low power dissipation low cost Memory plus I/O devices, all integrated into one chip Mostly used in embedded systems
Embedded Systems
Embedded system means the processor is embedded into that application An embedded product uses a microprocessor or microcontroller to do one task only
In an embedded system, there is only one application software that is typically burned into ROM
Example printer, keyboard, video game player
Introduction to Microcontroller
Microcontroller is a very common component in modern electronic systems. Its use is so widespread that it is almost impossible to work in electronics without coming across it. Used in a wide number of electronic systems such as: Engine management systems in automobiles. Keyboard of a PC. Electronic measurement instruments (such as digital multimeters, frequency synthesizers, and oscilloscopes). Printers, Mobile phones. Televisions, radios, CD players, tape recording equipment.
Security alarm systems, fire alarm systems, and building services systems. Special-purpose Applications.
Features of 8051
ROM RAM Timer I/O pins Serial port Interrupt sources 4K bytes 128 bytes 2 32 1 6
8051 is based on CISC architecture. It is based on Harvard architecture. So, it has separate program and data memory.
8051
4K 128 2 32 1 6
8052
8K 256 3 32 1 8
8031
10K 128 2 32 1 6
Architecture
Based on the instruction set used: CISC RISC
CISC
Complex Instruction Set Controller. It invariably employ a microcode for decoding the instructions and generate the necessary control signals for performing the intended operation. When an instruction is fetched from memory, it is compared with the existing bit pattern, when match is found it generates the associated control signals to ALU and registers.
It takes good amount of time to decode the meaning of complex instructions which perform more than one operation. Also microcode requires more number of transistors that are used in fabricating the CISC architecture based processors.
RISC
Reduced Instruction Set Controller. These are fast at numerical computations required in science, graphics and engineering applications. The total number of transistors used for the design of RISC is half the number used in CISC. This reduced number of transistors reduces the power consumption.
Computer Architecture
Depending on how external memory is connected to the processors: Von-Neumann machine. Harvard machine.
Von-Neumann machine
It has 3 hardware subsystems; a CPU, a main memory system and an I/O system. It uses stored program concept i.e. the program and data are stored in the same memory unit. It has a single path between the main memory system and the control unit of the CPU. Because the common path is used to access both the program as well as the data, there is possibility of congestion on the bus. This situation is called Von Neumann Bottleneck.
Harvard machine
It uses separate memories for storing the program and data.
To connect these separate memories, it uses a separate set of address, data and control lines. As separate memories and buses are used, simultaneous access to the memory is possible without any congestion over the bus, which in turn increases the performances. It increases the cost of the system as separate memories & separate buses are used.
Timer/Counter
On-chip RAM
Timer 1
Timer 0
Counter Inputs
OSC
Bus Control
4 I/O Ports
P0 P1 P2 P3
TxD RxD
Address/Data
Registers in 8051
In CPU, registers are used to store information temporarily. That information could be a byte of data to be processed, or an address pointing to the data to be fetched. 8051 have 8-bit registers
D7 D6 D5 D4 D3 D2 D1 D0
MSB
LSB
The most widely used registers are A (accumulator), B, R0 R7, DPTR (Data Pointer), PC (Program Counter). All the registers except DPTR and the program counter are 8 bits. DPTR and PC are 16 bits.
DPTR
PC
Flag Bit
8051 has a flag register to indicate arithmetic conditions such as carry bit. The flag register of 8051 is called Program Status Word (PSW) register.
RS1 0 0 1 1
RS0 0 1 0 1
Register bank 0 1 2 3
Four bits are called conditional flags, they indicate some conditions that result after an instruction is executed.
These 4 are CY (carry), AC (Auxiliary Carry), P (Parity) and OV (Over Flow). The bits PSW.3 and PSW.4 are designated as RS0 and RS1, respectively are used to change the bank registers.
CY flag:- This flag is set when there is a carry out from the D7 bit. This flag bit is affected after an 8-bit addition or subtraction. AC flag :- If there is a carry from D3 to D4 during ADD or SUB operation, this bit is set; otherwise cleared.
P flag :- It reflects the number of 1s in the A (accumulator) register only. If A register contains an odd number of 1s, then P=1.If its even, then P=0. OV Flag :- This is set whenever the result of a signed number operation is too large, causing the high order bit to overflow into sign bit.
Example:
38h + 2Fh = 67 00011100 00101111 01100111 +
CY = 0 no carry beyond D7. AC = 1 carry from D3 to D4. P = 1 since accumulator has an odd number of 1s.
RAM location
00-07 08-0F 10-17 18-1F
Register bank
0 1 2 3
Stack in 8051
It is a section of RAM. Stack is used to store information temporarily. The information could be data or address. CPU needs this storage area since there are only a limited number of registers.
8051 (8031)
The 8051 has an on-chip oscillator but requires an external clock to run it. A quartz crystal oscillator is connected to inputs XTAL1 and XTAL2. It also needs 2 capacitors of 30pF value. One side of each capacitor is connected to the ground as shown below:
C2 XTAL2 30pF C1 XTAL1 30pF GND
Addressing Modes
The CPU can access data in various ways. The data could be in a register or in memory or be provided as an immediate value. These various ways of accessing data are called addressing modes. Total 5 addressing modes: 1) Immediate 2) Register 3) Direct 4) Register indirect 5) Indexed
MOV 56h,A
The # symbol distinguishes between the direct addressing and immediate addressing mode.
Checksum byte
To ensure the data integrity of ROM contents every system must perform the checksum byte calculation. The process of checksum will detect any corruption of the contents of ROM. One of the causes of ROM corruption is current surge, when the system is turned on or during operation.
To ensure data integrity in ROM, the checksum process uses what is called checksum byte.
Checksum byte is the extra byte that is tagged to the end of a series of bytes of data. To calculate checksum byte of a series of bytes of data, the following steps can be taken:
1. Add the bytes together and drop the carries. 2. The the 2s compliment of the total sum; this is the
checksum byte, which becomes the last byte of the series.
Solution
25H + 62H + 3FH + 52H 118H
Dropping the carry, we have 18H. Take 2s Compliment of it is E8H. Therefore the checksum byte is E8H.
Signed char
8-bit data type. D7 of D7-D0 represent sign(-ve or +ve). We have 7 bits for value of signed number. Range from -128 to 127. Default is signed value.
Unsigned int
16bit data type. value in a range of 0-65535. used to define 16-bit memory address. 8051 is 8-bit microcontroller, so int data type take 2bytes of RAM.
Signed int
16 bit data type. Uses MSB (D15 of D15-D0) to represent sign(-ve or +ve). 15 bit for magnitude. Value range from -32768 to 32767.
Time Delay
There are two ways to create a time delay 8051:
1. Using a simple loop 2. Using the 8051 timers
8051 Timers
Both Timer 0 and Timer 1 are 16 bits wide. Since the 8051 has an 8-bit architecture, each 16-bit timer is accessed as 2 separate registers of low byte and high byte.
Timer 0 Register
The 16-bit registers of Timer 0 are accessed as low byte and high byte. The low byte is called TL0 (Timer 0 low byte) and the high byte register is called TH0 (Timer 0 high byte). These registers can be accessed like any other register, such as A,B,R0,R1, etc.
TH0
TL0
Timer 1 register
Timer 1 is also 16 bit register, and its 16-bit register is split into two bytes, referred to as TL1(Timer 1 low byte) and TH1 (Timer 1 high byte). These registers are accessible in the same way as Timer 0.
TH0 D4 D3 D2 D1 D0
MSB
GATE C/T M1 M0 GATE C/T M1
LSB
M0
Timer 1
Timer 0
GATE
Every timer has a means of starting and stopping. Some do this by software, some by hardware and some by both. If GATE=0, if we use software for this purpose. If GATE=1, if we use external hardware for this purpose.
C/T
Used to select Timer/Counter operation.
Set (1) Counter. Cleared(0) Timer.
M1 & M2
These are modes bits used in order to select the different operating modes of the timer.
M1 M0
0 0 1 1 0 1 0 1
TCON register
8-bit addressable register.
TR0 and TR1 flags used to turn on or turn off the timer are part of TCON (Timer Control) register.
MSB TF1 7 TR1 6 TF1 5 TR0 4 IE1 3 IT1 2 IE0 1 LSB IT0 0
BIT
TCON.7 TCON.6 TCON.5 TCON.4
SYMBOL
TF1 TR1 TF0 TR0
FUNCTION
Timer 1 overflow flag Timer 1 run control bit Timer 0 overflow flag Timer 0 run control bit
Mode 1
It is a 16-bit timer; it allows values of 0000 to FFFFH to be loaded into timers registers TL and TH. After TL and TH are loaded with 16-bit initial value, the timer must be started by setting TR0 for timer 0 and TR1 for timer 1. After the timer is started, it starts to count up. It counts up until it reaches its FFFFH. When it rolls over from FFFFH to 0000, it sets high a flag bit called TF (timer flag).
Now stop the timer bit by clearing TR0 and TR1 respectively. Each timer is having its own flag TF0 for T0 and TF1 for T1. After the timer reaches its limit and rolls over, in order to repeat the process reload TH and TL with original value and TF must be reset to 0.
Example:
Assume that XTAL=11.0592 MHz. We want 5ms delay
Solution:
(5ms / 1.085 micro sec) = 4608 clocks (65536 4608) = 60928 = EE00H Therefore, we have TH=EE TL=00
XTAL Oscillator
/12 TR
TH
TL
TF
C/T=0
Overflow flag
Mode 0
Its exactly similar to mode1 except that it is a 13 bit timer instead of 16bit.
Hold values between 0000-1FFFH in TH and TL. Therefore, when the timer reaches its maximum of 1FFFH it rolls over to 0000 and TF is raised.
Mode 2
8-bit timer allows values from 00-FFH to be loaded to TH. After TH is loaded with 8-bit value, the 8051 will give a copy of it to TL. Then the timer is started. After timer is started, it starts count by incrementing the TL register. It counts up until it reaches FFH. When it rolls from FFH to 00H, TF is set; TF0 for timer 0 and TF1 for Timer 1. TL is reloaded automatically with the original value kept by TH register. To repeat the process, simply clear TF and let it go without reloading the original value by programmer. Here TH is unchanged. It holds the copy of the original value.
XTAL Oscillator
/12 TR C/T=0
TL
TF Reload
TH
Mode 3
Timer mode 3 is a split-timer mode.
When Timer 0 is placed in mode 3, it essentially becomes two separate 8-bit timers. That is to say, Timer 0 is TL0 and Timer 1 is TH0. Both timers count from 0 to 255 and overflow back to 0.
Counter
If C/T of TMOD is 1, it is counter, it takes its pulses from outside the 8051. The counter counts up as pulses are fed from T0 and T1. In T0, P3.4 provides clock pulse and counter counts up for each clock pulse coming from that pin. For T1, each pulse coming in P3.5 makes the counter count up.
To transfer to a device located many meters away, the serial method can be used. In serial communication, the data is send one bit at a time.
Half Duplex
Receiver Transmitter
Transmitter
Receiver Transmitter
Asynchronous serial communication is widely used for character-oriented transmission, while block-oriented data transfer uses synchronous method. In asynchronous method, each character is placed between start and stop bits. This is called framing. Start bit is always one bit and it is a low bit Stop bit can be one or two bits and it is a high bit
space
stop bit
0
D7
1
D0
start bit
RS232 Standards
To allow compatibility among data communication equipment made by various manufacturers, an interfacing standard called RS232 was set by the Electronics Industries Association (EIA) in 1960. Many types like RS232A,RS232B,RS232C
This standard is used in PCs and numerous types of equipment. This standard was set long before the advent of TTL logic family, its input & output voltage levels are not TTL compatible.
In RS232 While 1=(-3 to -25V) 0=(+3 to +25V)
For this reason to connect any RS232 to a MC system we must use voltage converters such as MAX232 to convert the TTL logic levels to the RS232 voltage levels, and vice versa. MAX232 IC chips are commonly referred to as line drivers.
4800
-6
FA
2400
-12
F4
1200
-24
E8
SBUF register
SBUF is an 8-bit register used for serial communication in the 8051. To transfer a byte of data via the TxD line, it must be placed in the SBUF register. Similarly, SBUF hold a byte of data when it is received by the 8051s RxD line. SBUF can be assed by any other register in 8051. The moment a byte is written into SBUF, it is framed with a start & stop bits & transferred serially via the TxD pin. Similarly, When the bits are received serially via RxD, the 8051 de frames it by eliminating the start and stop bits, making a byte of data received, and placing it in the SBUF.
SM0
SMI
SM2
REN
TB8
RB8
TI
RI
SM0 SCON.7 Serial port mode specifier SM1 SCON.6 Serial port mode specifier SM2 SCON.5 Used for multiprocessor communication. (make it as 0) REN SCON.4 Set / Cleared by SW to enable / disable reception. TB8 SCON.3 Not widely used RB8 SCON.2 Not widely used TI SCON.1 Transmit interrupt flag. RI SCON.0 Receive interrupt flag.
SM0 SM1 0 0 0 1
1 1 0 1
Serial Mode 0 Serial Mode 1, 8-bit data, 1 stop bit, 1 Start bit Serial Mode 2 Serial Mode 3
We are not using 8051 in a multiprocessor environment so make SM2 = 0. REN (Receive Enable) = 1, it allows the 8051 to receive data on the RxD pin of the 8051 also for transferring the data through TxD. REN = 0, receiver is disabled.
TB8(transfer bit 8) is used only for mode 2 & 3. Make TB8 as 0. RB8(receiver bit 8) in serial mode 1, this bits gets a copy of stop bit when the 8-bit data is received. TI (transmit interrupt) bit is set after transfer of 8-bit of character. RI (receive interrupt) bit is set after receiving of 8-bit of character.
D7
D0
SMOD
---
---
---
GF1
GF0
PD
IDL
And
921.6 KHz / 32 = 28,800 Hz since SMOD = 0
And
921.6 KHz / 16 = 57,600 Hz since SMOD = 1
19,200
9,600
-3
FD
9,600
4,800
-6
FA
4,800
2,400
-12
F4
2,400
1,200
-24
E8
Interrupts Programming
A single microcontroller can serve several devices. There are 2 ways to do that. i.e Interrupt and Polling.
Interrupt Method
In this method, whenever any devices needs its service, the device notifies the MC by sending it an interrupt signal.
Upon receiving an interrupt signal, the MC interrupts whatever it is doing and serves the device. The program associated with the interrupt is called interrupt service routine (ISR) or interrupt handler.
Polling Method
In this method MC continuously monitors the status of a given devices; when the status condition is met it perform the service. It is not a efficient use of MC In interrupt method according to Priority the MC will service. In polling method cannot assign priority since it checks all devices in a round-robin fashion.
For every interrupt , there Is a fixed location in memory that holds the address of its ISR. The group of memory location set aside to hold the address of ISRs is called the interrupt vector table.
Level-Triggered Interrupt
In this mode, INT0 & INT1 pins are normally high (just like all I /O port pins) & if a low level signal is applied to them, it triggers the interrupt. This is called as level triggered interrupt.
If the low level interrupt signal is not removed before the ISR is finished it is interpreted as another interrupt & the 8051 jumps to the vector table to execute the ISR again. Therefore to ensure the activation of the hardware interrupt at the INTn pin, make sure that the duration of the low level signals is around 4 machine cycles, but not more.(INT0 have highest priority than INT1)
Edge-Triggered Interrupts
If we want to make INT0 & INT1 as edge triggered interrupt we must program the bits of TCON register.
MSB TF1 TR1 TF1 TR0 IE1 IT1 IE0 LSB IT0
BIT
TCON.7 TCON.6 TCON.5 TCON.4 TCON.3 TCON.2 TCON.1 TCON.0
SYMBOL
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
FUNCTION
Timer 1 overflow flag Timer 1 run control bit Timer 0 overflow flag Timer 0 run control bit External interrupt 1 edge flag Interrupt 1 type control bit External interrupt 0 edge flag Interrupt 0 type control bit
IT0 & IT1 or bits D0 & D2 of the TCON register determine whether the interrupt is level triggered or edge triggered.
By default IT0 & IT1 is 0 means that it is a level triggered interrupt. When this pins become high (1) then it is a edge triggered interrupt. In edge triggered interrupt a high to low signal is given to the interrupt pins to do the interrupt function (Ex: LED).
If we want the interrupt function to be executed again give a high to low signal to interrupt pin.
In level triggered interrupt as long as INTn is kept at a low level the interrupt function (LED) is kept in the on state. But for edge triggered interrupt to turn on the LED again the INTn pulse must be brought back high & then forced low to create a falling edge to activate the interrupt.
In edge triggered interrupt the external source must be held high for at least one machine cycle to ensure that the transition is seen by the microcontroller.
Priority bit =1 assigns high priority. Priority bit=0 assigns low priority
Reserved Reserved Timer 2 priority bit Serial port interrupt priority bit Timer 1 interrupt priority bit External interrupt 1 priority bit Timer 0 interrupt priority bit External interrupt 0 priority bit
Interrupt Programming in C
8051/8052 Interrupt Numbers in C
Interrupt Name Numbers used by 8051 C
External Interrupt 0
Timer Interrupt 0
INT0
TF0
0
1
External Interrupt 1
Timer Interrupt 1
INT1
TF1
2
3
Serial Communication
Timer 2 (8052 only)
RI + TI
TF2
4
5
LCD Interfacing
In recent years the LCD is finding widespread use replacing LEDs (seven-segment LEDs or other multisegment LEDs). This is due to the following reasons. The declining prices of LCDs. The ability to display numbers, characters, and graphics. This is in contrast to LEDs, Which are limited to numbers and a few characters. Incorporation of a refreshing controller into the LCD, thereby relieving the CPU of the task of refreshing the LCD.
Ease of programming for characters and graphics.
There are two very important register inside the LCD. The RS pin (Input pin) is used for their selections as follows.
If RS=0,the instruction command code register is selected, allowing the user to send a command such as clear display, cursor at home, etc If RS=1 the data register is selected, allowing the user to send data to be displayed on the LCD. VSS = Ground
VEE = Power supply to control contrast R / W (Input pin) = 0 for write & 1 for read E (I /O pin) for enable DB0 DB7 (I / O Pin)
Display capacity of 16 characters X 2 line,20 X 1, 20X2, 20X4, 40X2 etc.. The 8-bit data pin, D0 D7 are used to send information to LCD or read the contents of the LCDs internal registers.
To display letters & numbers, we send ASCII codes for the letter A Z, a z & 0 9 to these pins while making RS = 1.
Keyboard Interfacing
VCC 4.7K
3 D0
D1 7 B
4.7K
6 A
5 9
4 8
D2
D3 F E D C
D3
D2
D1
D0
Keypad are the mostly used input devices of the 8051/52.Keypad is organized in a matrix of rows and columns. The MCU accesses both rows and columns through ports; therefore, with two 8-ports, an 8X8 matrix of keys can be connected to a microcontroller. When a key is pressed, a row and a column make a contact; otherwise, there is no connection between rows and columns. It is the function of program stored in the EPROM of the microcontroller to scan the keys continuously, identify, which one has been activated.
If one of the columns bit has a zero (i.e. D0 to D3 = 1101) this means that a key press has occurred this means a key in the D1 column has been pressed. Then the MC start to identifying the key. starting up with the top row, the MC ground it by providing a low to row D0 only.
The MC read the columns if it is all 1s then no key pressed in that row. so it check the 2nd row by grounding it.
Sample I2C Implementation. Regardless of how many slave units are attached to the I2C bus, there are only two signals connected to all of them. Consequently, there is additional overhead because an addressing mechanism is required for the master device to communicate with a specific slave device.
Because all communication takes place on only two wires, all devices must have a unique address to identify it on the bus. Slave devices have a predefined address.
Theory of Operation
I2C has a master/slave protocol. The master initiates the communication. The sequences of events are: The Master device issues a start condition. This condition informs all the slave devices to listen on the serial data line for instructions. The Master device sends the address of the target slave device and a read/write flag. The Slave device with the matching address responds with an acknowledgement signal. Communication proceeds between the Master and the Slave on the data bus. Both the master and slave can receive or transmit data depending on whether the communication is a read or write.
The transmitter sends 8-bits of data to the receiver which replies with a 1-bit acknowledgement. When the communication is complete, the master issues a stop condition indicating that everything is done.
I2C Communication Protocol Since there are only two wires, this protocol includes the extra overhead of an addressing mechanism and an acknowledgement mechanism
Features I2C has many features other important features worth mentioning. It supports multiple data speeds: standard (100 kbps), fast (400 kbps) and high speed (3.4 Mbps) communications. Built in collision detection, 10-bit Addressing, Mutli-master support, Data broadcast (general call).
(EEPROM) AT24C02A
ADC
ADC are among the most widely used devices for data acquisition. Digital computers use binary (discrete) values, but in the physical world everything is analog (continuous). Temperature, Pressure (wind or Liquid), humidity, & velocity are few examples.
A physical quantity is converted into electrical (voltage, current) signals using a device called a transducer (Sensors). Sensor converts the temperature, pressure etc.. Into its corresponding voltage (or current). like this way ADC works.
An ADC has n-bit where n can be 8, 10, 12, 16 or even 24 bits. The highest resolution ADC provides a smaller step size, where step size is the smallest change that can be discerned by an ADC. Conversion time should be less.
8
10 12 16
256
1024 4096 65563
5 / 256 = 19.53
5 / 1024 = 4.88 5 / 4096 = 1.2 5 / 65536 = 0.076
ADC0804 Chip
+5V
20 10K POT 6 7 8 9 19 10K 4 150 pF
1 2 10
D0 D1 D2 D3 D4 D5 D6 D7
18 17 16 15 14 13 12 11
LEDs
WR 3 5 INTR
WR D0 D7 INTR RD
DAC
EX: binary weighted & R /2R ladder.