You are on page 1of 14

CHAPTER 3

Microcontroller
Introduction Microcontroller Microprocessor vs. Microcontroller Programming Microcontroller Burning the microcontroller Interfacing microcontroller with PC Computer Interface

10

3.1 Introduction Microcontroller is one of the amazing inventions of modern world. It performs various control tasks. Moreover low cost, faster operation and reliability make it useful device. Most of the industries are controlled by embedded system. Microprocessor and microcontroller are widely used in embedded system products. Low power consumption and less space required made microcontroller superior than microprocessor. Moreover, todays microcontrollers offer multi functional task using a single controller. 3.2 Microcontroller A microcontroller is a structure that integrates in a single chip a microprocessor, a certain amount of memory, and a number of peripheral interfaces. What is the difference between a microprocessor and microcontroller? By micro processor is meant the generalpurpose microprocessors such as Intels x86 family (8086, 80286, 80386, 80486 and the Pentium) or Motorolas 680x0 family (68000, 68010, 68020, 68030, 68040 etc). These microprocessors contain no RAM, no ROM and no I/O ports no the chip itself. For this reason, they are commonly referred to as general-purpose microprocessors. Fig. 3.1 shows the difference between microprocessor systems with microcontroller system.

Figure 3.1: Difference between microprocessor and microcontroller

11

The Central Processing Unit (CPU) is connected to the other subsystems of the microcontroller by means of the address and data buses. Depending on how the CPU accesses the program memory, there are two possible architectures for microcontrollers, called Von Neumann, and Harvard [2]. Fig. 3.2 shows the two architectures.

Figure 3.2: Microcontroller architecture

The Von Neumann microcontrollers tend to have a large instruction set, including some really complex instructions. This is the reason why computers having the Von Neumann architecture are often called CISC, or Complex Instruction Set Computers. The main disadvantage of this architecture is that the more complex the instruction, the longer it takes to fetch, decode, execute it, and store the result. On the other hand, the Harvard architecture was created to increase the overall speed of computers in the early years, when very slow magnetic core memory was used to store the program. It includes an additional, separate bus to access the program memory (refer to Fig. 1.2 (b)). The presence of the second bus makes the following things possible: While an instruction is executed, the next instruction can be fetched from the program memory. This technique is called pipelining and brings a significant increase of computer speed. The program memory can be organized in words of different size from, and usually larger than, the data memory. Wider instructions mean a greater data flow to the CPU, and therefore the overall speed is higher. All AVR microcontroller use Harvard architecture. Thats why AVR micro controller is faster. A quick overview on Microcontroller architectures 12

Registers- All information in the microcontroller, from the program memory, the timer information, to the state on any of input or output pins, is stored in registers. Registers are like shelves in the bookshelf of processor memory. In an 8-bit processor, like the ATMega 32 we are using, the shelf can hold 8 books, where each book is a one bit binary number, a 0 or 1. Each shelf has an address in memory, so that the controller knows where to find it.

Bits and bytes- 8-bits are equal to one byte, and there are 256 unique possible values for each byte. All the information in the microcontroller is stored in bytesize chunks; since it would be tedious to write out all the 1's and 0's in binary format, we represent each byte of information as a two-digit hexadecimal number. For example, 11110011 in binary=243 in decimal=F3 in hexadecimal. Programmer usually writes 0xF3 to clue people in that the numbers are in base 16. Also, people sometimes write F3h, where h means hex.

Hex code- fix these links ... All the code written is linked, assembled and otherwise compiled into hex code using some complier (In this case CODE VISION AVR), which is a series of hexadecimal numbers strategically arranged to tell the controller what to do. The wonder of a nice high-level programming language like C is that these numbers are loaded in the appropriate places in memory without your having to know very much at all about how the logic in the controller is laid out. Thats making easy with working microcontroller with high level language.

I/O Registers- In order to read and write to the input and output pins on the microcontroller, however, we will need to know a little about the input and output architecture. The reason that the 32 IO pins of the ATMega 32 are divided into 4 ports of 8 pins is that this allows the state of the pins to be representedbytesnamed PORTA, PORTB, PORTC and PORTD. Each physical I/O pin corresponds to a logical bit on the port. The value of pin3 on Port D lives in slot 3 on the PORTD bookshelf.

Setting and Clearing I/O - The term for assigning a logical high value (1) to a pin is setting, and the term for assigning a logical low value (0) is clearing. We can write to the I/O registers one byte at a time, for example, by assigning the 13

value 0xBB to PORTB, or by assigning one bit at a time, clearing PB2 (port B bit 2) and PB6, and setting the rest.

DDR Registers- Not all of the I/O registers are physical pins. Since the I/O pins are configurable to be either input or output, the controller needs some place to store the directionality of each bit. These are stored in the Data Direction Registers. Like all the other registers, the DDRs have 1's and 0's, but its 1's and 0's indicate whether the corresponding port pin is an input (1) or output (0). This register acts as an I/O port librarian, controlling who is allowed to change the data on the shelves, bit by bit. So, if we set DDRA to 0xF0 (i.e. 1111 0000), this means that bits 7-4 on PORTA are set to input, and bits 3-0 are set to output. If PORTA is originally set to 0xFF (i.e. 1111 1111) and we subsequently write 0xAA (i.e. 1010 1010) to PORTA, we should read 0xFA on the PORTA.

Port Features- The pins on the different ports also have different features, much as each of the Super friends had different super powers. For instance, PORT A can be bi-directional I/O, but it can also be used for analog input. This functionality can be very useful for reading back sensor data. To enable the switching between analog and digital inputs, a special register called ADCSR (Analog to Digital Control & Status Register) is needed; each bit in ADCSR sets some aspect of the A/D operations. We do not need to set these bits explicitly, but we need to be aware that we should run the library commands in the appropriate library to tell the processor to set these registers. A more complete description of all the ports and their special magical powers can be found in the ATMega 32 summary.

Interrupts- A single microcontroller can serve several devices. There are two ways to do that: interrupts or polling. In the interrupt method, whatever and device needs its service, the device notifies the microcontroller by sending it an interrupt signal. Upon receiving an interrupt signal, the microcontroller interrupts whatever it is doing and serves the device. The program associated with the interrupts is called the interrupt service routine (ISR) or interrupt handler. In polling, the microcontroller continuously monitors the status of a given device; when the status condition is met, it performs the service. After that, it moves on to 14

monitor the next device until each one serviced. Although polling can monitor the status of several devices and serve each of them as certain conditions is met, it is not an efficient use of the microcontroller. The advantage of interrupts is that the microcontroller can serve many devices (not all at the same time, of course); each device can get attention of the microcontroller based on the priority assigned to it. The interrupts is used where a thing happen cant tell before i.e. uncertainty.

Interrupt Service Routine- For every interrupt, there must be an interrupt service routine (ISR), or interrupt handler. When an interrupt is invoked, the microcontroller runs the interrupt service routine. For every interrupt, there is a fixed location in memory that holds the address of its ISR. The group of memory locations set aside to hold the address of its ISRs is called interrupt vector table.ATmega 32 interrupt vector table is shown 3-1

Table 3-1

3.2.1 Microprocessor vs. Microcontroller Microprocessor:


CPU is stand-alone, RAM, ROM, I/O, timer are separate designer can decide on the amount of ROM, RAM and I/O ports. expansive 15

versatility general-purpose

Microcontroller:
CPU, RAM, ROM, I/O and timer are all on a single chip fix amount of on-chip ROM, RAM, I/O ports for applications in which cost, power and space are critical single-purpose

3.3 Programming Microcontroller Various programming tools are available to program the microcontroller. Three useful tools such as CODE VISION AVR were widely used. CODE VISION AVR use the C language . Since C incorporate with both low level and high level language thus it is used in this thesis. For programming AVR microcontroller, three software required such as CODE VISION AVR and AVR libray. All the softwares are available freely. All the software belongs to GNU (GNU Not Unix) copyright. Fig. 3.3 shows the CODE VISION AVR program.

Proteus simulation software:

16

figure:3.4 Proteus ISIS circuit simulation. But demo version of Proteus is available. In this thesis work both AVR Studio and Proteus ISIS was used for circuit simulation. 3.4 Burning the microcontroller When code is built in coad vision avr , a .hex file was created at the .c file location. This hex file is burnt inside the microcontroller to operate it. PonyProg [7] is widely used for burning code inside microcontroller. 3.4.1.PonyProg setting PonyPro [6] is used to download the hex file of the C code for microcontroller. Before starting download, perform following procedure: 1. Menu bar select Setup => Interface Setup Check parallel, select Avr ISP I/O and check LPT1, then click ok. 2. Menu bar select Command => Program options Check Reload flies, Erase and write program Flash memory, then click ok. 3. Menu bar select Device => micro and chose your microcontroller. Before loading hex file to microcontroller, calibrate from setup menu. Now open the program hex file from file menu and select launch program cycle. By default every 17

microcontroller operates on 1 MHz frequency. To change this, select security and configuration bits from command menu. Here check or uncheck the clock select bits by reading the data sheet of the microcontroller are used.

Figure 3.5: PonyProg programmer

In the fig. 3.5, PonyProg programmer software is shown. The hex code are contains in the file. These hex codes are load inside the microcontroller and it operates automatically. By writing codes, many functions are performed in the microcontroller. So it reduces the bulky of digital logic circuit. Thats why microcontroller gains so much popularity.

3.5 Interfacing microcontroller with PC 18

Various techniques are available for interfacing microcontroller with PC. But DB25 parallel port pc interface is widely used. The connection diagram is shown in fig. 3.6

It is called ISP programmer because any microcontroller can be programmed when it is working. ISP stands for In System Programming. To make the ISP, a DB25 parallel cable is required. One side of the cable is cut off. From the 25 cable only five cables are required as shown in the above fig3. 6. Other cables are unused.

Figure 3.6 :AVR ISP programmer

3.6 Computer Interface:


The computers parallel port was used as the means of communication between the Microcontrollers. A 25-pin male printer cable was used as the bus for information transfer. Figure 4.6 shows the pin out arrangement of the parallel port.

3.6.1 Some Basics of a Parallel Port

Parallel Port
19

A port contains a set of signal lines that the CPU sends or receives data with other components. We use ports to communicate via modem, printer, keyboard, mouse etc. In signaling, open signals are "1" and close signals are "0" so it is like binary system. A parallel port sends 8 bits and receives 5 bits at a time. The serial port RS-232 sends only 1 bit at a time but it is multidirectional so it can send 1 bit and receive 1 bit at a time...

Figure 3.6.1: Parallel Port Configuration Parallel Port- Data Port: In sending the sequences, you will need the data ports which can be seen in the picture from D0 to D7. Parallel Port- Status Port: These ports are made for reading signals. The range is like in data ports which are S0-S7. But S0, S1, S2 are invisible in the connector. And S0 is different; this bit is for timeout flag in EPP (Enhanced Parallel Port) compatible ports. The address of this status port is 20

0x379.This will always be refer to "DATA+1" and it can send 5 numeric data from the 10 - 11 - 12 - 13 15th pins. So how can we reach the data ports? It is simple: every parallel port has an address. In Windows 2000, you can see yours by Settings > Control Panel > System > Hardware > Device Manager > Ports (COM & LPT) > Printer Port (LPT1) > Properties = in Resources > Resource Setting and you can see your address for your parallel port. For Ex: Generally it is 0378-037F. This is hexadecimal like in math (mod 16). 0x378 belongs to 888 in decimal form. In this way you can look for your com port or game port addresses. Let's enlighten these bits with a printer example:

S0: This bit becomes higher (1) if a timeout operation occurs in EPP mode. S1: Not used (Maybe for decoration :)) S2: Mostly not used but sometime this bit shows the cut condition (PIRQ) of the port S3: If the printer determines an error it becomes lower (0). Which is called n Error or n Fault S4: It is high (1) when the data inputs are active. Which is called Select S5: It is high (1) when there is no paper in printer. Which is called Paper End, Paper Empty or P Error S6: It sends low impact signaling when the printer gets a one byte data. Which is called n Ack or n Acknowledge S7: This is the only reversed pin on the connector (see my table in the article) . If the printer is busy and it cannot get any additional data this pin becomes lower. Which is called Busy

Parallel Port- Control Port: This port usually used for outputting but these can be used for inputting. The range is like in data ports C0-C7 but C4, C5, C6, C7 are invisible in connector. And the address for this is 0x37A

C0: This pin is reversed. It sends a command to read D0-D7 on the port. When the computer starts it is high in the connector. Which is called n Strobe C1: This pin is reversed. It sends a command to the printer to feed the next line. It is high in the connector after the machine starts. Which is called Auto LF

21

C2: This pin is for reset the printer and clear the buffer. Which is called n Init, n initialize C3: This pin is reversed. Sends a high (1) for opening data inputs. It is low after the machine starts. Which is called n Select In C4: Opens the cut operation for the printer. Not visible in the connector C5: Sets the direction control in multidirectional ports. Not visible in the connector C6: Not used and also Not visible in the connector... C7: Mostly not used but it is used as a C5 in some ports. Not visible in the connector

Parallel Port- Ground Pins: These are (G0 - G7) the pins from 18 to 25. These are mostly used for completing the circuit. Different pins are required when using all the pins including the inputs. After these we will be using data ports in experiment because there are reversed pins in control and status ports. Here is an explanation for reversed pins: While you are not sending any signals to the data port it is in closed position like "00000000" so the 8 pins have no voltage on it (0 Volt) .If you send decimal "255" (binary "11111111") every pin (D0-D7) has a +5 Volt... On the other hand, if we use control ports, there are reversed pins which are C0, C1 and C3 so while we send nothing to the control port its behavior is "0100" in binary (decimal "11") Signal -Strobe +Data Bit 0 +Data Bit 1 +Data Bit 2 +Data Bit 3 +Data Bit 4 +Data Bit 5 +Data Bit 6 +Data Bit 7 -Acknowledge BIT C0 D0 D1 D2 D3 D4 D5 D6 D7 S6 22 PIN 1 2 3 4 5 6 7 8 9 10 Direction Output Output Output Output Output Output Output Output Output Input

+Busy +Paper End +Select In -Auto Feed -Error -Initialize -Select Ground

S7 S5 S4 C1 S3 C2 C3 -

11 12 13 14 15 16 17 18-25

Input Input Input Output Input Output Output Ground

Table 3.6.2: Pin configuration of parallel port

23

You might also like