Download presentation
Presentation is loading. Please wait.
Published byTrevor Bates Modified over 7 years ago
1
Introduction to Smart Systems Analogue to Digital Conversion
Sensor Types and Uses Analogue to Digital Conversion Analogue Comparator 1
2
Digital Sensors (Input) – Directly readable via parallel Input port bits
Device Use Example Applications Push switch Momentary input User option selection, user control, mechanical operation such as machine end-stop. Floor switch in intruder detection systems. Slide switch Constant value (stays in position) Configuration input Magnetic switch Remote sensing (very short distances). Mechanical systems to detect moving parts, end-stop etc. Intruder alarm systems. Pressure switch Detect a pre-set pressure, force or weight Safety cut-out in mechanical, hydraulic, pneumatic equipment. Temperature switch (Thermostat) Detect a pre-set temperature Safety cut-out in mechanical, electrical equipment. Automatically switch heating on/off, fans on/off (e.g. processor temperature in PC). Light-activated switch (uses a light beam) Detect position of equipment (light beam broken?) Safety cut-outs in mechanical equipment. Intruder detection with light beams. Detection of position of moving parts. Shaft Encoder Detect position / rotation angle and/or rotation speed of a shaft Motor speed control. Position control in motorised mechanical systems. Position detection in systems such as a mechanical computer mouse. Passive Infra-Red (PIR) Detect movement of IR emitters (e.g. people). Intruder detection systems. Courtesy / safety light activation. 2 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich
3
Digital Output Devices
Use Example Applications Light Emitting Diode (LED) Signal some condition / event Indication, run-time debugging Seven Segment Display Alpha-Numerical output Indication, run-time debugging (requires software encoder for character set) Liquid Crystal Display Indication, run-time debugging (typically has local dedicated processor and requires interfacing via software registers and hardware handshaking signals) Motor Mechanical output General movement (rotation especially) Solenoid Movement (linear especially) e.g. electronic door locks Servo Movement (typically linear) e.g. remote controlled toys (car steering, airplane rudder). Sounder / Loudspeaker Sound output Digitised sound files, acknowledgement beeps (such as when keys are pressed on a phone), warning tones. 3 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich
4
Analogue Sensors (Input) – Directly readable via Analogue Input port
Device Use Example Applications Variable resistor Detect a control value input User inputs such as volume, light, temperature preferences. Any user-controlled analogue value can be represented. Light-sensitive diode (Photodiode) / transistor (Phototransistor) / resistor Measure light intensity Activate devices based on light levels. Adjust exposure time on a camera. Temperature-sensitive resistor (Thermistor) Measure temperature Control temperature of environments, liquids etc. in production systems. Could form part of a sophisticated fire detection system. Frost warning (for greenhouses, and in diesel lorries to ensure the fuel does not freeze). Ultrasonic transducer Detect distance (by measuring propagation time for a sound signal) Intruder detection. Electronic ‘tape measure’ Accelerometer Measure acceleration, tilt vibration etc Game control handsets. Robotics. Microphone Record sound, measure sound intensity Speech control input (requires sophisticated software). Sound control input (such as detect a series of hand-claps). 4 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich
5
Analogue Outputs Device or Signal Use Example Applications
Motor - Speed control via PWM Control speed or torque to a motor. Control acceleration and braking effects All systems with motors where speed control is needed – kitchen appliances, industrial machines, electric vehicles, remote controlled models, toys … Light / LED - Brightness control via PWM Control brightness of an indicator Light dimmer. Power saving for battery-powered devices Sound – Direct control of volume via amplifier gain Adjust volume dynamically PA / Music volume control Sound - Volume control via PWM (only for a simple monotone) Warning alarm or condition indicator volume control (only works with simple audible tones, not speech / music). Power saving for battery-powered devices 5 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich
6
Analogue sensors for the ATmega1281
Twin Variable Resistor Sensor Connects to Analogue inputs ADC2 and ADC3 (bits 2 and 3 of port F) Rotating the dials changes the analogue resistance values 6 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich
7
Analogue Signals – Analogue to Digital Conversion (ADC) 1
Analogue signals are continuously variable (most real-world systems). Digital signals have ‘step-values’ (the way we represent numbers in a digital computer). → Analogue signals (e.g. light intensity) must be converted into digital form. ‘Sample Rate’ is the number of samples taken per second Sample Analogue signal Digital value 4 8 12 16 20 24 28 31 7 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich
8
Analogue Signals – ADC 2 Conversion of an Analogue signal into digital form looses information Using a 5-bit ADC (32 discrete steps), the sample values for the signal shown are: 7, 12, 16, 8, 11, 16, 27, 25 Information loss #1: The digital values do not represent the precise value of the signal. Information loss #2: Whatever happens between sample times is lost. Sample Digital value 4 8 12 16 20 24 28 31 Analogue signal 7 8 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich
9
Analogue Signals – ADC 3 – Comparison of 4-bit and 5-bit ADCs
0 Analogue value (e.g. Voltage at ADC input pin) FSD Digital Value 5-bit ADC 4-bit ADC 4 8 12 16 20 24 28 31 2 6 10 14 15 Each digital value represents a range of analogue values Analogue signal 9 5 11 Precision – Typical A to D converters (ADC) have 8, 10 or 12 bits. This means that the signal value range is represented as either 256, 1024 or 4096 discrete values. The analogue signal is presented as a voltage on the ADC input. The maximum value is termed ‘Full Scale Deflection’ (FSD). Information loss is reduced by using a higher precision ADC. For some applications, a lower precision is acceptable, and sometimes not all of the bits of an ADC value are needed. Sampling with 4-bit precision Sampling with 5-bit precision 9 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich
10
Analogue Signals – ADC - Sampling rate (frequency)
Must sample an analogue signal at a suitably high rate to capture the information of interest within the signal. The Nyquist–Shannon sampling theorem states: “If a function x(t) contains no frequencies higher than B Hz, it is completely determined by giving its ordinates at a series of points spaced 1/(2B) seconds apart ”. In reality the sampling rate must actually be greater than 2B, other wise ‘phase ambiguity’ can make several signals of frequency B indistinguishable. Sample Digital value Several sine waves (all with frequency B but different phases) cannot be distinguished at sample rate 2B. Need Rate > 2B An important example is speech sampling. Speech has a frequency range of approximately 300Hz to 3.5KHz. The typical sample rate used is 8KHz. 10 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich
11
Analogue To Digital Conversion – ATmega1281 functionality (1)
EOC Generated Interrupt signal A single 10-bit ADC is available. 8 Input channels are available, so up to 8 different analogue sensors / signals can be connected to the microcontroller – a flag in a register configures which channel will be converted. The input channels are implemented as alternate functions for Port F (ADC0 is mapped to PortF0 … ADC7 is mapped to PortF7) A-D converter Key: Analogue signal Digital Value 8 Inputs 11 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich
12
Analogue To Digital Conversion – ATmega1281 functionality (2)
The ADC operation is configured via a number of registers: ADC Multiplexer Selection Register (ADMUX) (select voltage reference, format output, and select input channel) ADC Control and Status Register A (ADCSRA) (Enable ADC, Start Conversion, Enable Interrupts, set Prescaler) ADC Control and Status Register B (ADCSRB) (Select auto trigger options) ADC data registers ADCH (high) and ADCL (Low) (The digital result value is read from these) Digital Input Disable Register 0 and 2 (DIDR0 and DIDR2) (Disable digital input circuit when using as analogue input to reduce power consumption) Status Register (SREG) (Enable Interrupts for whole system) ADC End Of Conversion (EOC) is usually detected via the ‘Conversion Complete’ interrupt (avoids polling). The ADC Conversion Complete interrupt vector is located at: 0x003A (use .org) Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich 12
13
Caution, program behaviour will be sensitive to resistor setting
Analogue To Digital Conversion – ATmega1281 functionality (3) The ADC Multiplexer Selection Register (ADMUX) Bits 7 and 6 select the voltage reference For AREF pin (blue variable resistor on STK300) For AVCC (5V, fixed value) The AREF pin is connected internally to the chip's circuitry. Even when AREF set to 'AVCC' the blue variable resistor on STK300 board still affects the values of readings. Solution is to electrically isolate the variable resistor, by removing the adjacent jumper. Bit 5 adjusts the 10-bit result position in the two 8-bit data registers (low 8-bits - ADCL and high 8 bits ADCH) – see next slide. Bits 4,3,2,1,0 select the analogue channel to be converted. If using single-ended input – all ‘our’ devices work this way: Set bits 4 and 3 to ‘0’. Set the binary channel identifier on bits 2,1,0 e.g. for channel 0, set the value for channel 1, set the value for channel 7, set the value Caution, program behaviour will be sensitive to resistor setting 13 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich
14
Analogue To Digital Conversion – ATmega1281 functionality (4)
The ADC data register bit, accessed as 8-bit registers ADCH and ADCL (and the effect of ADMUX bit 5, ADC Left Adjust Result (ADLAR)) ADLAR = 0 ADLAR = 1 Use if need full 10-bit precision, or if analogue signal range is less than FSD / 4 Use if need only 8-bit precision, and want to read upper 8 bits from a single register 14 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich
15
Analogue To Digital Conversion – ATmega1281 functionality (5)
The ADC Control and Status Register (ADCSRA) Bit 7 ADC Enable – Set to ‘1’ to enable the ADC, ‘0’ to turn off the ADC. Bit 6 ADC Start Conversion – Set to ‘1’ to start a conversion. Bit 5 ADC Auto Trigger Enable – Set to ‘1’ to automatically trigger the ADC (for continuous operation, or for trigger from a timer to control sample rate). Bit 4 ADC Interrupt Flag – Can be read to see if an ADC conversion has completed (if polling). Bit 3 ADC Interrupt Enable – Set to ‘1’ to enable the ADC to generate an Interrupt when conversion is complete (‘I’ bit must be set in SREG see later). Bits 2,1,0 ADC Prescaler Select Bits – these control the internal operating speed of the ADC logic (and hence affect the conversion time). (see page 292 of manual for full details) 15 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich
16
Analogue To Digital Conversion – ATmega1281 functionality (6)
The ADC Control and Status Register (ADCSRB) Bit 3 - MUX5: Analogue Channel and Gain Selection Bit This bit is used together with MUX4:0 in ADMUX to select which combination in of analogue inputs are connected to the ADC. Bit 2:0 – ADTS2:0: ADC Auto Trigger Source If ADATE in ADCSRA is written to one, the value of these bits selects which source will trigger an ADC conversion. (see page 290 of manual for full details) 16 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich
17
Analogue To Digital Conversion – ATmega1281 functionality (7)
ADCSRB Bits 2, 1, ADC Auto Trigger Source. General purpose continuous operation (see page 295 for full details) A Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich 17
18
Analogue To Digital Conversion – ATmega1281 functionality (8)
DIDR0 – Digital Input Disable Register 0 DIDR2 – Digital Input Disable Register 2 When these bits are written logic one, the corresponding digital input buffer on the ADC pin is disabled. This reduces power consumption.
19
Analogue To Digital Conversion – System register configuration 1
The Status Register (SREG) Bit 7 Global Interrupt Enable (I). This bit must be set to ‘1’ to enable interrupts globally. 19 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich
20
Analogue Signals – Analogue Comparator - Overview
The Analogue comparator allows two analogue values AIN0 and AIN1 to be compared by hardware (you could of course read two ADC channels and compare in software). Hardware is simpler to use and much faster (don’t have to wait for ADC conversion). Uses Port E pins alternate function: Bit 2 AIN0 (‘Positive’ Input) Bit 3 AIN1 (‘Negative’ Input) This is the reason why the twin variable resistor input device we created uses bits 2 and 3 on the cable (can plug to ADC channels 2 and 3 Port F, or onto Port E to use the Analogue Comparator). Port E bits 2 and 3 should be configured as inputs, with the pullup resistors turned off for correct Analogue Comparator operation. Instead of using AIN1 as the negative input, it is possible to select any one of the 8 ADC inputs as the negative value for comparison with AIN0 – this means that it is possible to software select from up to 9 sensors to compare with a reference signal. 20 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich
21
Analogue Signals – Analogue Comparator - Operation
When the voltage on the ‘Positive’ pin AIN0 is higher than the voltage on the ‘Negative’ pin AIN1 the Analogue Comparator Output ACO is set to ‘1’. This can trigger the Timer/Counter1 input capture (to record the time at which the event occurred), or can trigger the Analogue Comparator Interrupt. The Analogue Comparator interrupt vector is located at: 0x0038 (use .org) Positive input Negative input Alternative Negative inputs From ADC multiplexer Comparator Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich 21
22
(to set detection threshold)
Analogue Signals – Analogue Comparator – Usage example A temperature alarm system Applications include: checking a Freezer is cold enough, or a Greenhouse is warm enough, warning if your Diesel is going to freeze, or if a Chemical production process is overheating, etc. In such applications it is not important to know the exact temperature at each given moment (we would use the ADC for this), but it is very important to detect if the temperature is above or below a certain threshold value (the comparator is ideal). Positive input Negative input Analogue Comparator Generate interrupt if temperature is above / below the threshold value (can be configured for either case by swapping the +ve / -ve inputs) Thermistor (measures temperature) Resistors (to bias the voltage at the comparator’s input pins) Variable Resistor (to set detection threshold) + Vcc Gnd 22 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich
23
Analogue Comparator – Configuration registers 1
The Analogue Comparator Control and Status Register (ACSR) Bit 7 ACD – Analogue Comparator Disable - set to disable, saves power. Bit 6 ACBG – Analogue Comparator Bandgap Select - if set, uses a fixed reference voltage instead of the AIN0 positive input voltage. Bit 5 ACO – Analogue Comparator Output (readable). Bit 4 ACI - Analogue Comparator Interrupt Flag (readable). Bit 3 ACIE – Analogue Comparator Interrupt Enable – Set to ‘1’ to enable the Analogue Comparator to generate an Interrupt (‘I’ bit must be set in SREG see earlier). (see page 273 of ATmega1281 PDF manual) 23 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich
24
Analogue Comparator – Configuration registers 2
The Analogue Comparator Control and Status Register (ACSR) (Continued) Bit 2 ACIC - Analogue Comparator Input Capture Enable – Enables Timer1 Input Capture to be triggered by the Analogue Comparator. Bit 1, 0 ACIS1, ACIS0 – Analogue Comparator Interrupt Mode Select: ACIS1 ACIS0 Interrupt Mode Generate Interrupt on output toggle 1 Generate Interrupt on falling output edge Generate Interrupt on rising output edge 24 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich
25
Analogue Comparator – Configuration registers 3
DIDR1 – Digital Input Disable Register 1 Bit 1, 0 – AIN1D, AIN0D: AIN1, AIN0 Digital Input Disable When these bits are written logic one, the digital input buffer on the AIN1/0 pin is disabled. This reduces power consumption.
26
Analogue Comparator – System register configuration 1
ADCSRB – ADC Control and Status Register B Bit 6 ACME – Analogue Comparator Multiplexer Enable If the ADC is not in use, it is possible to use any of the 8 ADC inputs as the negative analogue value for the Analogue Comparator, instead of AIN1 (the positive value is always AIN0). The ADC multiplexer is configured as described in the ADC section of this lecture, however, the ADEN bit in ADCSRA must be cleared ‘0’). Bit 6 set ‘1’ – Use output of ADC multiplexer as negative input Bit 6 cleared ‘0’ – use AIN1 as negative input 26 Embedded Systems Programming II Richard Anthony, Computer Science, The University of Greenwich
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.