UNIT-3
UNIT-3
• To write your first C program for the ATmega2560 microcontroller open a new design project in AVR Studio 5 by
selecting File>New>Project to bring up the following dialogue window.
• Under installed templates choose AVR GCC>C and select the C executable project, enter a name for your
project e.g. HelloWorldC and click OK. The device selector window will then appear. Select the ATmega2560 as
before and click OK.
• This will now create your project and open a blank HelloWorldC.c file in the editor.
• Paste the following Hello World C code into the editor. This code causes the LED attached to pin 13 of the
Arduino board to blink at about 1Hz.
How to program a ATmega2560?
• The analog signals are usually connected to the Analog-to-Digital converters of the processor.
• Change of Bias and Level of Signals In most applications the sensor signals need some form of change of bias and/or
shift of voltage level.
• For example, the output voltage available from the sensor may vary from 0.2 V to 1.3 V as a change of the input
variable over the entire range of measurement.
• When the output of the sensor is connected to a microcontroller, it may require a voltage level from 0 V to 3.3 V for
the same variation of the input variable.
• The required signal conditioning is done by changing the output of the sensor from 0.2 V to 0 V with the input
variable is 0 V.
• This may be done simply by subtracting 0.2 V from the output of the sensor.
• This may be defined as a bias adjustment or zero shift of the output.
• Once the bias adjustment is done, the output of the sensor is now varying from 0 V to 1.1 V.
• In order to utilise the full range of 0 V to 3.3 V of the processor, the signal is multiplied by a gain of 3
• This process is known as amplification and the amplifying factor (3 in this case) is known as gain. In many situations,
the output to the processor need to be reduced, this is known as attenuation.
• Both the amplification and attenuation is achieved by the same circuit, known as amplifier.
• The gain of the amplifier is more than unity for an amplifier and is less than unity for an attenuator.
• It is important to take note of the frequency response, input impedance and output impedance while the bias and
amplifier circuits are designed.
• Loading Effect on Sensor’s Output It is useful to understand the loading effect of sensor’s output while
designing the signal conditioning circuit.
• The voltage measured across the output of the sensor is the open-circuit voltage.
• When the sensor is connected to a circuit, the voltage across the terminals of the sensor drops down to a
value and is less than the opencircuit voltage.
• The output of the sensor is represented by the source voltage Vs.
• The impedance of the sensor (the output resistance) is Rs.
• The sensor is connected to a load of resistance Rload which can be the input resistance of any amplifier or
any port pin of the processor.
• +62061Under open-circuit condition the output of the sensor is Vs.
• RELAYS AND OPTOISOLATORS
• This section begins with an overview of the basic operations of electromechanical relays, solid-state relays, reed
switches, and optoisolators.
• Then we describe how to interface them to the 8051. We use both Assembly and C language programs to
demonstrate their control.
• The maximum DC/AC voltage and current that can be handled by the contacts.This is in the range of a few volts
to hundreds of volts, while the current can be from a few amps to 40A or more, depending on the relay.
• Notice the difference between this voltage/current specification and the voltage/current needed for energizing
the coil.
• The fact that one can use such a small amount of voltage/current on one side to handle a large amount of
voltage/current on the other side is what makes relays so widely used in industrial controls.
Relay diagrams
Driving a relay
Digital systems and microcontroller pins lack sufficient current to drive the relay. While the relay’s coil
needs around 10 mA to be energized, the microcontroller’s pin can provide a maximum of 1-2 mA
current. For this reason, we place a driver, such as the ULN2803, or a power transistor between the
microcontroller and the relay
• Interfacing an optoisolator
• The optoisolator comes in a small 1C package with four or more pins. There are also packages that
contain more than one optoisolator. When placing an optoisolator between two circuits, we must
use two separate voltage sources, one for each side
• Unlike relays, no drivers need to be placed between the microcontroller/digital output and the
optoisolators.
•
Data acquisition and control using microcontrollers
• Data acquisition is the process of sampling signals that measure real-world physical conditions
and converting the resulting samples into digital numeric values that can be manipulated by a
computer. Data acquisition systems, abbreviated by the acronyms DAS, DAQ, or DAU, typically
convert analog waveforms into digital values for processing. The components of data acquisition
systems include:
• Sensors, to convert physical parameters to electrical signals.
• Signal conditioning circuitry, to convert sensor signals into a form that can be converted to digital values.
• Analog-to-digital converters, to convert conditioned sensor signals to digital values.
Hardware
• Computer Automated Measurement and Control (CAMAC)
• Industrial Ethernet
• Industrial USB
• LAN eXtensions for Instrumentation
• Network interface controller
• PCI eXtensions for Instrumentation
• VMEbus
• VXI
Interrupt systems of ATMEGA 328
• The ATmega 328P supports two external interrupts which are individually enabled by setting bits
INT1 and INT0 in the External Interrupt Mask Register (Section 12.2. 2 EIMSK). becomes set (one).
• If the I-bit in SREG and the INT0 bit in EIMSK are set (one), the MCU will jump to the
corresponding Interrupt Vector.
ATmega328P Interrupt Processing
• (1) When an interrupt occurs, (2) the microcontroller completes the current instruction and (3) stores the address
of the next instruction on the stack
• It also turns off the interrupt system to prevent further interrupts while one is in progress. This is done by (4)
clearing the SREG Global Interrupt Enable I-bit.
• The (5) Interrupt flag bit is cleared for Type 1 Interrupts only (see the next page for Type definitions).
• The execution of the ISR is performed by (6) loading the beginning address of the ISR specific for that
interrupt into the program counter. The AVR processor starts running the ISR.
• (7) Execution of the ISR continues until the return from interrupt instruction (reti) is encountered. The
(8) SREG I-bit is automatically set when the reti instruction is executed (i.e., Interrupts enabled).
• When the AVR exits from an interrupt, it will always (9) return to the interrupted program and (10) execute
one more instruction before any pending interrupt is served.
• The Status Register is not automatically stored when entering an interrupt routine, nor restored when
returning from an interrupt routine. This must be handled by software.
• push reg_F
in reg_F,SREG
:
out SREG,reg_F
pop reg_F
By the numbers
Type 1
• The user software can write logic one to the I-bit to enable nested interrupts. All enabled interrupts can then
interrupt the current interrupt routine.
• The SREG I-bit is automatically set to logic one when a Return from Interrupt instruction – RETI – is
executed.
• There are basically two types of interrupts…The first type (Type 1) is triggered by an event that sets the
Interrupt Flag. For these interrupts, the Program Counter is vectored to the actual Interrupt Vector in order to
execute the interrupt handling routine, and hardware clears the corresponding Interrupt Flag.
• If the same interrupt condition occurs while the corresponding interrupt enable bit is cleared, the Interrupt
Flag will be set and remembered until the interrupt is enabled, or the flag is cleared by software (interrupt
cancelled).
• Interrupt Flag can be cleared by writing a logic one to the flag bit position(s) to be cleared.
• If one or more interrupt conditions occur while the Global Interrupt Enable (SREG I) bit is cleared, the
corresponding Interrupt Flag(s) will be set and remembered until the Global Interrupt Enable bit is set on
return (reti), and will then be executed by order of priority.
Type 2
• The second type (Type 2) of interrupts will trigger as long as the interrupt condition is present. These interrupts
do not necessarily have Interrupt Flags. If the interrupt condition disappears before the interrupt is enabled, the
interrupt will not be triggered.