3. SINA PROG. INTERFACING. 1. LCD,KEYPAD. 2. ADC,I2C,SERIAL,PWM. ATMEGA DEVELOPMENT BOARD. Programming With AVR. Microcontroller. programming book or an electronics text (or just look it up on the Net). But when .. If your PDF reader supports indexing, enable the in-. I highly recommend that you get the book, 'The C Programming Language – based on the AVR microcontroller, and the examples are as microcontroller.
|Language:||English, Spanish, German|
|Genre:||Science & Research|
|Distribution:||Free* [*Registration needed]|
The AVR1 family of MCUs from Atmel Corporation has become one of the most Since this is not primarily a book about programming, we will only be going on Unix or the AVRDude user manual (supplied with AVRDude as a PDF file on. This book is not a C language Programming Book or a Book to learn AVR Studio provides a project management tool, source file editor and chip simulator. Atmel AVR Microcontroller Primer: Programming and Interfacing. Pages · · MB · 1, The AVR Microcontroller and Embedded System by Muhammad Ali Mazidi Pdfdrive:hope Give books away. Get books you want.
In the SPI system, the transmitter and receiver share a common clock source. This requires an additional clock line between the transmitter and receiver but allows for higher data transmission rates as compared with the USART. The SPI may be viewed as a synchronous bit shift register with an 8-bit half residing in the transmitter and the other 8-bit half residing in the receiver.
The transmitter is designated the master because it provides the synchronizing clock source between the transmitter and the receiver. The receiver is designated as the slave.
We discuss the operation, programming, and application of the SPI in Chapter 2 of the text. The TWI subsystem allows the system designer to network a number of related devices microcontrollers, transducers, displays, memory storage, etc.
The TWI allows a maximum of devices to be connected together. Each device has its own unique address and may both transmit and receive over the two-wire bus at frequencies up to kHz. This allows the device to freely exchange information with other devices in the network within a small area.
The ADC converts an analog signal from the outside world into a binary representation suitable for use by the microcontroller. This means that an analog voltage between 0 and 5 V will be encoded into one of binary representations between 16 and 3F F This provides the ATmega16 with a voltage resolution of approximately 4.
We discuss the operation, programming, and application of the ADC in Chapter 3 of the text. However, sometimes, this normal sequence of events must be interrupted to respond to high-priority faults and status both inside and outside the microcontroller. When these higher-priority events occur, the microcontroller must temporarily suspend normal operation and execute event specific actions called an interrupt service routine.
Once the higher priority event has been serviced, the microcontroller returns and continues processing the normal program. The ATmega16 is equipped with a complement of 21 interrupt sources. Three of the interrupts are provided for external interrupt sources, whereas the remaining 19 interrupts support the efficient operation of peripheral subsystems aboard the microcontroller. We discuss the operation, programming, and application of the interrupt system in Chapter 4 of the text.
As a system designer, it is important to know the various physical and operating parameter options available to select the best option for a given application. The Pinout Diagram for the different packaging options are provided in Figure 1. The ATmega16L operates at supply voltages from 2. In the application examples that follow, we will use a standard laboratory 5-VDC power supply and also discuss a method of providing a 5-VDC supply using an off-the-shelf 9-VDC battery.
The current draw for the microcontroller is quite low. When placed in the idle mode, the microcontroller current draw reduces to less than 0.
To minimize power consumption, the microcontroller can be placed into various low- current sleep modes.
There are six different sleep modes available to the system designer. Additionally, power consumption can be further reduced by operating the microcontroller at the lowest practical clock frequency for a given application. As previously discussed, the operating speed of the microcontroller is set by the time base chosen for the processor.
One might believe that faster microcontroller operation is always better. This is not the case.
The system designer must determine the minimum practical speed of microcontroller operation for a given application. That is, the faster the operating speed of the microcontroller, the higher its power consumption.
This becomes especially critical in portable, battery-operated embedded systems applications. That completes our brief introduction to the features of the ATmega In the next section, we apply what we have learned in developing a testbench for the ATmega Most importantly, the Testbench will serve as a template to develop your own applications.
In this chapter, we provide the hardware configuration of a barebones testbench and a basic software framework to get the system up and operating. PORTA is configured with eight tact momentary switches with accompanying debouncing hardware.
We discuss the debounce circuit in detail in Chapter 6. We discuss this circuit in detail in Chapter 6. R SW0 K 0. ATmega16 R. ATmega16 Testbench hardware. Vcc Vcc. A standard 5-VDC power supply may be used for the power connections.
Pins 9 through 11 have a resistor 1 M , two capacitors 1. The program contains the following sections:. This file provides the software link between the names of the ATmega16 hardware registers and the actual hardware locations.
When a register is used by name in the program, reference is made to the contents of that register. We begin the main program by calling the function to initialize the ports and then enter a continuous loop. We briefly mention required components here. Software tools: This is an excellent, user-friendly compiler.
There are other excellent compilers available. The compiler is used to translate the source file testbench. Please refer to Figure 1. It should be noted that the STK will be used to program the microcontroller, which will then be placed in the Testbench circuit.
Load AVR Studio free download from www. Ensure that the STK is powered down.
Connect the STK as shown in Figure 1. Insert the ATmega16 into the red pin socket. Note the location of pin 1 in Figure 1. Power up the STK It is on the second line of the toolbar about halfway across the screen. At the bottom of the Program tab window, verify that the STK was autodetected. Set all tab settings: Input HEX file, Browse and find machine code file: Programming step: Power down the STK Remove the programmed chip from the STK board and place it in the Testbench circuit.
To ease the transition to another microcontroller, it is suggested using a direct bit assignment technique. Instead of setting an entire register content at once, selected bits may be set. The individual bit definitions for the ATmega16 are provided in Appendix B. Information presented in this chapter can be readily applied to other microcontrollers in the AVR line.
We then provided the Testbench hardware and software that we use throughout the text to illustrate peripheral subsystem operation aboard the ATmega In upcoming chapters, we provide additional details on selected ATmega16 subsystems.
What is a RISC processor? How does the ATmega16 interact with the external world? What are the different methods of applying a clock source to the ATmega16?
List the inherent advantages of each type. Describe the three different types of memory components aboard the ATmega What is each used for?
Describe the three registers associated with each port. With a specific port, can some port pins be declared as output pins while others as input pins? Describe the serial communication features aboard the ATmega Provide a suggested application for each. What is the purpose of the ADC system? What is the purpose of the interrupt system?
What is the purpose of the PWM system? What is the best clock speed to operate the ATmega16 at for a specific application?
Sketch a flow chart or UML activity diagram for the testbench. Data may be exchanged by using parallel or serial techniques. With parallel techniques, an entire byte of data is typically sent simultaneously from the transmitting device to the receiver device. Although this is efficient from a time point of view, it requires eight separate lines for the data transfer .
In serial transmission, a byte of data is sent a single bit at a time. Once 8 bits have been received at the receiver, the data byte is reconstructed. Although this is inefficient from a time point of view, it only requires a line or two to transmit the data. Before discussing the different serial communication features aboard the ATmega16, we review serial communication terminology.
In an asynchronous serial communication system, such as the USART aboard the ATmega16, framing bits are used at the beginning and end of a data byte. These framing bits alert the receiver that an incoming data byte has arrived and also signals the completion of the data byte reception.
The data rate for an asynchronous serial system is typically much slower than the synchronous system, but it only requires a single wire between the transmitter and receiver. Data bits are sent and received on the edge of the clock. This allows data transfer rates higher than with asynchronous techniques but requires two lines, data and clock, to connect the receiver and transmitter.
For example, baud indicates data are being transferred at bits per second. To do both transmission and reception simultaneously requires separate hardware for transmission and reception.
A single duplex system has a single complement of hardware that must be switched from transmission to reception configuration. A full duplex serial communication system has separate hardware for transmission and reception.
The important point is the transmitter and receiver must use a common coding standard so data may be interpreted correctly at the receiving end. The Atmel ATmega16  uses a nonreturn to zero coding standard. In nonreturn to zero, coding a logic 1 is signaled by a logic high during the entire time slot allocated for a single bit, whereas a logic 0 is signaled by a logic low during the entire time slot allocated for a single bit.
Over long distances, logic levels degrade and may be corrupted by noise. At the receiving end, it is difficult to discern a logic high from a logic low. The RS standard has been around for some time. Chips are commonly available e. The RS standard also specifies other features for this communication protocol. Parity is an additional bit or bits that may be transmitted with the data byte. The ATmega16 uses a single parity bit.
With a single parity bit, a single-bit error may be detected. Parity may be even or odd. At the receiver, the number of bits within a data byte including the parity bit are counted to ensure that parity has not changed, indicating an error, during transmission.
It has been in use for many decades, so some of the characters and actions listed in the ASCII table are not in common use today. It provides standardized bit encoding format for the written languages of the world. The interested reader is referred to the Unicode home page website at www. This is accomplished by equipping the ATmega16 with independent hardware for the transmitter and receiver.
It has the capability to be set to a variety of data transmission or baud bits per second rates. Because of space limitations, we cover only the most basic capability of this flexible and powerful serial communication system.
The block diagram may appear a bit overwhelming, but realize there are four basic pieces to the diagram: We discuss each in turn. L] and several additional dividers to set the baud rate. Solving for UBRR yields. The start and stop framing bits are automatically appended to the data within the Transmit Shift Register. The parity is automatically calculated and appended to the Transmit Shift Register. Data are then shifted out of the Transmit Shift Register via the TxD pin a single bit at a time at the established baud rate.
The UDRE flag sets when the transmit buffer is empty, indicating it is ready to receive new data. The TXC flag bit is set to logic 1 when the entire frame in the Transmit Shift Register has been shifted out and there are no new data currently present in the transmit buffer.
The TXC bit may be reset by writing a logic 1 to it. Data are received a single bit at a time via the RxD pin at the established baud rate. The RXC flag is logic 1 when unread data exist in the receive buffer. The function of these bits has already been discussed.
This allows the user to customize the data features to the application at hand. It should be emphasized that both the transmitter and receiver be configured with the same data features for proper data transmission. These activities are summarized in Figure 2. Both the transmitter and receiver must be initialized with the same communication parameters for proper data transmission.
The transmission and reception activities are similar except for the direction of data flow. In transmission, we monitor for the UDRE flag to set, indicating the data register is empty.
We then load the data for transmission into the UDR register. We then retrieve the data from the UDR register. USART activities. In the sample code provided, we assume the ATmega16 is operating at 10 MHz and we desire a baud rate of , asynchronous operation, no parity, one stop bit, and eight data bits. To achieve baud with an operating frequency of 10 MHz requires that we set the UBRR registers to 64, which is 0x The SPI system allows for fast and efficient data exchange between microcontrollers or peripheral devices.
There are many SPI-compatible external systems available to extend the features of the microcontroller.
The SPI may be viewed as a synchronous bit shift register with an 8-bit half residing in the transmitter and the other 8-bit half residing in the receiver as shown in Figure 2. A slave is chosen for reception by taking its slave select SS line low. Master Device Slave Device. SCK pulse. The data are received at the MOSI pin of the slave designated device. After eight master SCK clock pulses, a byte of data has been exchanged between the master and slave designated SPI devices.
At that time, another data byte may be transmitted. The registers for the SPI system are provided in Figure 2. We will discuss each one in turn. SPI Control Register. It also provides the flexibility for the SPI to be connected to a wide variety of devices with different data formats.
It is important that both the SPI master and slave devices be configured for compatible data formats for proper data transmission. The SPCR contains the following bits:. A logic 1 turns the system on and logic 0 turns it off. SPI Status Register. This contains the SPIF. The flag sets when eight data bits have been transferred from the master to the slave. SPI Data Register. To program the SPI system, the system must first be initialized with the desired data format.
Data transmission may then commence. Functions for initialization, transmission, and reception are provided below. In this specific example, we divide the clock oscillator frequency by to set the SCK clock frequency. Space does not permit a detailed discussion of this advanced serial communication system. Summarize the differences between parallel and serial conversion.
Draw the schematic of a system consisting of two ATmega16 that will exchange data via the SPI system. The system should include RS level shifting. Write the code to implement the system described in the question above. A microcontroller is used to process information from the natural world, decide on a course of action based on the information collected, and then issue control signals to implement the decision.
Because much of the information from the natural world is analog or continuous in nature and the microcontroller is a digital or discrete-based processor, a method to convert an analog signal to digital is required . We will discuss both types of converters in this chapter. In the first section, we present the fundamental concepts associated with the ADC process.
In the following section, we discuss the conversion process itself, followed by a presentation of different hardware implementations of the process.
Much of these early sections contain the same material you will find in our text, Microcontroller Fundamentals for Engineers and Scientists. We conclude our discussion of the ADC with several illustrative code examples.
We conclude the chapter with a discussion of the DAC process. We start with some definitions on analog and digital signals. The collection can be as small as only one value or can have as many values as you wish. In engineering, we usually arrange the values in order, for example, over time or over a spatial axis, to display the information.
The time and spatial variables are called independent variables, because they are not affected by the physical variables of interests. For example, we measure the temperature change over time. The temperature measured is dependent on the time, not the other way around.
Figure 3. The signal shows how the altitude of the bird changes continuously. The image captured the light intensities of the scene using a charge-coupled device camera. If we move from the left to the right on the image and observe the intensity changes, we can find vertical edges by detecting signal. Altitude trajectory generated by a flying bird. The same analysis can be performed as we move from the top to the bottom of the image.
Analog signals are those whose physical variable values change continuously over their independent variable. Most physical variables, your speech, movement of stars, and the music you hear at a concert are analog signals, signals that we find all around us. Digital signals, on the other hand, have their physical variables defined only for discrete instances over their independent variables. Although it may look continuous to human eyes, the photo example shown in Figure 3.
The image is only a finite composition of intensity values seen by a discrete number of pixels. Digital signals are important because all signals represented in digital systems, computers, and microcontrollers are in digital forms.
For example, human voices must be converted to corresponding digital signals before they can be routed by digital switching circuits in telephone communication systems. Similarly, voice commands to robots must be converted to a digital form before robots can understand the command.
As shown in the examples above, we live in an analog world; that is, physical variables are analog signals. It is precisely this reason why the ADC is so very important in any digital systems that interact with an analog environment. We first start with the subject of sampling. Imagine yourself as a photographer in an Olympic diving stadium. Your job is taking a sequence of pictures of divers jumping off from a diving board 10 meters above the surface of the diving pool. Your goal is to put the sequence of pictures together to reconstruct the motion of each diver.
If a diver tries a complex motion and you want to faithfully reconstruct his motion, you must take enough pictures from the start to the end of the dive. If a diver makes a simple routine dive, you only need to take a few pictures over the period of the dive. Two very different cases of motions generated by a diver is shown in Figure 3. The same time sequence is used to capture samples for both motions.
As can be seen from figure, frame a motion cannot be regenerated from the samples, whereas the motion shown in frame b can clearly be reconstructed from the same number of samples used to capture both motions. Naturally, when we sample a signal, we want to sample it in an optimal fashion such that we can capture the essence of the signal while minimizing the use of resources.
In essence, we want to minimize the number of samples while faithfully reconstructing the original signal from the samples. As can be deduced from our discussion above, the rate of change in a signal determines the number of samples required to faithfully reconstruct the signal, provided that all adjacent samples are captured with the same sample timing intervals. Harry Nyquist from Bell Laboratory studied the sampling process and derived a criterion that determines the minimum sampling rate for any continuous analog signals.
His, now famous, minimum sampling rate is known as the Nyquist sampling rate, which states that one must sample a signal at least twice as fast as the highest frequency content of the signal of interest. Engineers who work for telephone companies must deal with such issues. Sampling is important because when we want to represent an analog signal in a digital system, such as a computer, we must use the appropriate sampling rate to capture the analog signal for a faithful representation in digital systems.
Now that we understand the sampling process, let us move on to the second process of the ADC, quantization. Each digital system has a number of bits, which it uses as the basic units to represent data. A bit is the most basic unit where single binary information, 1 or 0, is represented. A nibble is made up of 4 bits put together. A byte is 8 bits. In the previous section, we tacitly avoided the discussion of the form of captured signal samples.
The quantization of a sampled signal is how the signal is represented as one of quantization level. Suppose you have a single bit to represent an incoming signal. You only have two different numbers, 0 and 1. You may say that you can distinguish only low from high.
Suppose you have 2 bits. You can represent four different levels, 00, 01, 10, and What if you have 3 bits? You now can represent eight different levels: Think of it as follows. When you had 2 bits, you were able to represent four different levels.
If we add one more bit, that bit can be 1 or 0, making the total possibilities 8. Similar discussion can lead us to conclude that given n bits, we have 2n different numbers or levels one can represent. In many digital systems, the incoming signals are voltage signals. The voltage signals are first obtained from physical signals with the help of transducers, such as microphones, angle sensors, and infrared sensors. The voltage signals are then conditioned to map their range with the input range of a digital system, typically 0 to 5 V.
In Figure 3. As can be seen from the figure, higher quantization levels means better mapping of an incoming signal to its true value. If we only had a single bit, we can only represent levels 0 and 1. Any analog signal value in between the range had to be mapped either as level 0 or level 1, not many choices. Now imagine what happens as we increase the number of bits available for the quantization levels.
What happens when the available number of bits is 8? How many different quantization levels are available now? Yes, How about 10, 12, or 14? Finally, the encoding process involves converting a quantized signal into a digital binary number. Suppose again we are using 8 bits to quantize a sampled analog signal.
The quantization levels are determined by the 8 bits, and each sampled signal is quantized as one of quantization levels. Consider the two sampled signals shown in Figure 3. The first sample is mapped to quantization level 2 and the second one is mapped to quantization level Note the amount.
Quantization with fewer bits. Now consider Figure 3. The same signal is sampled at the same time but quantized using a less number of bits. Note that the quantization error is inversely proportional to the number of bits used to quantize the signal.
Once a sampled signal is quantized, the encoding process involves representing the quanti- zation level with the available bits.
Thus, for the first sample, the encoded sampled value is , whereas the encoded sampled value for the second sample is As a result of the encoding process, sampled analog signals are now represented as a set of binary numbers. Thus, the encoding is the last necessary step to represent a sampled analog signal into its corresponding digital form, shown in Figure 3. Suppose again we have. The resolution in this case is 2. You can imagine how your TV screen will look if you only had only two levels to represent each pixel, black and white.
The maximum error, called the resolution error, is 2. Suppose you now have 4 bits to represent quantization levels. The resolution now becomes 1. Suppose you have 20 bits for quantization levels. The resolution now becomes 4. The discussion we presented simply illustrates that as we increase the available number of quantization levels within a range, the distance between adjacent levels decreases, reducing the quantization error of a sampled signal.
As the number grows, the error decreases, making the representation of a sampled analog signal more accurate in the corresponding digital form. The number of bits used for the quantization is directly proportional to the resolution of a system.
You now should understand the technical background when you watch high-definition television broadcasting. Now let us move onto the discussion of the data rate. The definition of the data rate is the amount of data generated by a system per some time unit. Typically, the number of bits or the number of bytes per second is used as the data rate of a system.
We just saw that the more bits we use for the quantization levels, the more accurate we can represent a sampled analog signal. Why not use the maximum number of bits current technologies can offer for all digital systems, when we convert analog signals to digital counterparts? It has to do with the cost involved.
In particular, suppose you are working for a telephone company and your switching system must accommodate , customers. For each individual phone conversation, suppose the company uses an 8-kHz sampling rate and you are using 10 bits for the quantization levels for each sampled signal. You will need some major computing power to meet the requirement. For such reasons, when designers make decisions on the number of bits used for the quantization levels, they must consider the computational burden the selection will produce on the computational capabilities of a digital system versus the required system resolution.
You will also encounter the term dynamic range when you consider finding appropriate ADCs. The dynamic range is a measure used to describe the signal to noise ratio. The unit used for the measurement is decibel, which is the strength of a signal with respect to a reference signal.
The greater the decibel number, the stronger the signal is compared with a noise signal. The definition of the dynamic range is 20 log 2b , where b is the number of bits used to convert analog signals.
Typically, you will find 8 to 12 bits used in commercial ADCs, translating the dynamic range from 20 log 28 dB to 20 log dB Oppenheim and Schafer . Toward this end, three signal processing procedures, sampling, quantization, and encoding, described in the previous section must be combined together.
Before the ADC process takes place, we first need to convert a physical signal into an electrical signal with the help of a transducer.
Depending on the purpose, we categorize a transducer as an input transducer or an output transducer. If the conversion is from physical to electrical, we call it an input transducer. The mouse, the keyboard, and the microphone for your PC all fall under this category. A camera, an infrared sensor, and a temperature sensor are also input transducers. The output transducer converts electrical signals to physical signals. The computer screen and the printer for your computer are output transducers.
Speakers and electrical motors are also output transducers. Therefore, transducers play the central part for digital systems to operate in our physical world by transforming physical signals to and from electrical signals. In addition to transducers, we also need signal conditioning circuitry before we apply the ADC process or its opposite process of DAC process.
The signal conditioning circuitry is called the transducer interface. The objective of the transducer interface circuit is to scale and shift the electrical signal range to map the output of the input transducer to the input of the ADC.
The output of the input transducer is first scaled by constant K. The box with constant K maps the output range of the input transducer to the input range of the converter. Once the range has been mapped, the signal now needs to be shifted. The second portion of the circuit shifts the range by 2. Actual implementation of the circuit components is accomplished using amplifiers with some feedback loops.
In general, the scaling and bias process may be described by two equations:. A block diagram of the signal conditioning for an ADC. The range of the sensor voltage output is mapped to the ADC input voltage range. The scalar multiplier maps the magnitudes of the two ranges, and the bias voltage is used to align two limits. The variable V1 max represents the maximum output voltage from the input transducer.
This voltage occurs when the maximum physical variable Xmax is presented to the input transducer. This voltage must be scaled by the scalar multiplier K and then have a DC offset bias voltage B added to provide the voltage V2 max to the input of the ADC converter. Similarly, The variable V1 min represents the minimum output voltage from the input transducer.
This voltage occurs when the minimum physical variable Xmin is presented to the input transducer. This voltage must be scaled by the scalar multiplier K and then have a DC offset bias voltage B added to produce voltage V2 min to the input of the ADC converter. Usually, the values of V1 max and V1 min are provided with the documentation for the transducer. Also, the values of V2 max and V2 min are known. We thus have two equations and two unknowns to solve for K and B. The circuits to scale by K and add the offset B are usually implemented with operational amplifiers.
We refer interested readers to Thomas and Rosa  listed in the References section. Once a physical signal has been converted to its corresponding electrical signal with the help of an input transducer and the output of the transducer mapped correctly to the input of the ADC, the ADC process can start.
Library Functions Reference. Answers to Selected Exercises. Leave a Reply Cancel Reply Your email address will not be published. Recent Posts. Project Categories. Richard H. Barnett , Larry D.
Cox , Sarah Alison Cox. Internet - Ethernet - LAN. Metering - Instrument. Sensor - Transducer - Detector.
Motor Project. Phone Project. Robotics - Automation Projects. Video - Camera - Imaging. Jurij Mikeln the author provides training sessions for programming in Bascom AVR, and the book reads very much as if he is sitting next to you and giving the tutorial.
Atmel's product data sheets and application notes are excellent but Jurij makes them readable and understandable for the beginner. What is especially good is that the author jumps straight in and tells the beginner about switch bounce, lack of this bit of information has given many beginners problems with switches that do not do the expected thing. The book then goes onto cover interfacing the micro-controller to analogue and digital sensors that measure the real physical world.
There is an excellent section on how to make your own programming tools for these micro-processors; this is worth the book price alone. Micro-processors are here, affordable and easy to use. download the book and enjoy the journey. Reviewed by M. Mannering, Hampshire, England. Even more experienced programmers can learn coding techniques from many of the example programs. The authors have demonstrated, how easy it is to learn to effectively make some small but useful circuits.
The book layout and flow is conducive for easy learning with some very useful examples that can be built as is, modified or incorporated into other applications. It is well edited and easy to read.. The tiny13 and tiny are designed to address the requirements of battery operated and portable applications by offering low power consumption and a high level of system integration in a small package. Also available in 1.
The schematics were all readable and text has a large font size. All very well thought out. Reviewed by Rick Savas The Bascom-AVR programming is one of it's kind down to earth book that has at it's core the motive of simplifying concepts to its atom state and taking the beginner from there towards mastering several concepts needed in this domain of physical computing.