Suleyman Demirel University 20151 CSS340 Microprocessor Systems – Lecture 2 ATMEGA328P...

12
Suleyman Demirel University 2015 1 CSS340 Microprocessor Systems – Lecture 2 ATMEGA328P ARCHITECTURE ANALOG INPUTS

Transcript of Suleyman Demirel University 20151 CSS340 Microprocessor Systems – Lecture 2 ATMEGA328P...

Page 1: Suleyman Demirel University 20151 CSS340 Microprocessor Systems – Lecture 2 ATMEGA328P ARCHITECTURE ANALOG INPUTS.

Suleyman Demirel University 2015 1

CSS340 Microprocessor Systems – Lecture 2

ATMEGA328P ARCHITECTUREANALOG INPUTS

Page 2: Suleyman Demirel University 20151 CSS340 Microprocessor Systems – Lecture 2 ATMEGA328P ARCHITECTURE ANALOG INPUTS.

Suleyman Demirel University 2015 2

ATMEGA328P ARCHITECTUREIn-System Programmable(ISP) flash to store programs holds run-time variables

store any data

23 general purpose input/output lines

three Timer/Counterswith compare modes

2-wire Serial Interface

Serial Peripheral Interface 6-channel 10-bit Analog/Digital Converter

All peripheral devices are controlled via sets of specific registers, each of which is connected to the 8-bit data bus

Page 3: Suleyman Demirel University 20151 CSS340 Microprocessor Systems – Lecture 2 ATMEGA328P ARCHITECTURE ANALOG INPUTS.

Suleyman Demirel University 2015 3

Page 4: Suleyman Demirel University 20151 CSS340 Microprocessor Systems – Lecture 2 ATMEGA328P ARCHITECTURE ANALOG INPUTS.

Suleyman Demirel University 2015 4

General-Purpose Input/Output

Basic operation to output a digital signal on pin n of port x is to:1. write a ‘1’ into DDRx_Bitn, then2. write either a ‘1’ or ‘0’ into Portx_Bitn.

Basic operation to input a digital signal on pin n of port x is to:1. write a ‘0’ into DDRx_Bitn, then2. read the value out of Pinx_Bitn.

Page 5: Suleyman Demirel University 20151 CSS340 Microprocessor Systems – Lecture 2 ATMEGA328P ARCHITECTURE ANALOG INPUTS.

Suleyman Demirel University 2015 5

INTERNAL PULL-UP RESISTORGenerally, when a port pin is not being used, the microcontroller will default the pins to input, in order to save on power.

Floating pin data can lead to strange and unexpected behavior.

Very standard method for managing unused input pins is to connect a fairly large resistance between the pin and the power supply.

Page 6: Suleyman Demirel University 20151 CSS340 Microprocessor Systems – Lecture 2 ATMEGA328P ARCHITECTURE ANALOG INPUTS.

Suleyman Demirel University 2015 6

PORT B (8-13) I/O REGISTERS

pinMode(pin, mode);

//Input sample code: pinMode(8, INPUT);//Sets DDRB0 to 0digitalRead(8);//Reads PINB0

//Output sample code: pinMode(8, OUTPUT);//Sets DDRB0 to 1digitalWrite(8,HIGH);//Sets PORTB0 to 1

Page 7: Suleyman Demirel University 20151 CSS340 Microprocessor Systems – Lecture 2 ATMEGA328P ARCHITECTURE ANALOG INPUTS.

Suleyman Demirel University 2015 7

Analog Signals Potentiometer - a mechanical knob is used to adjust the resistance

between terminals;

Thermistor - a device that changes resistance based on temperature;

Accelerometer - a device that measures the acceleration of gravity in three dimensions and outputs an associated analog value for each dimension (e.g., this is the basis behind the Nintendo Wii video-game controllers);

Ambient light sensor - a device that measure the environmental ambient light intensity and outputs an associated analog value (e.g., many laptop computers will adjust the backlight level based on the surrounding environment light level);

Microphone - a device that converts vibrations (i.e., acoustic-waves) into analog levels.

Page 8: Suleyman Demirel University 20151 CSS340 Microprocessor Systems – Lecture 2 ATMEGA328P ARCHITECTURE ANALOG INPUTS.

Suleyman Demirel University 2015 8

Analog Input Port In order for a microcontroller to

operate on the sensor outputs, an Analog-to-Digital Conversion (ADC) circuit must be used

The parallel ADC uses typical values for Vmin and Vmax where the minimum voltage is simply set to ground and Vmax is controlled externally via the user-defined reference voltage VREF.

The resistor network on the left-hand side is designed such that

VREF−V2=V2−V1=V1−V0=V0 The analog voltage VA is first

converted into a three-bit codeword defined by (c2c1c0), where each bit is the output of a comparator.

4 possible codewords are then passed through an encoder that outputs the final two-bit value (b1b0).

Page 9: Suleyman Demirel University 20151 CSS340 Microprocessor Systems – Lecture 2 ATMEGA328P ARCHITECTURE ANALOG INPUTS.

Suleyman Demirel University 2015 9

Analog Input Port Arduino development board has six 10-bit ADCs, all of which

are found on Port C. (A0-A5) For generic usage of the ADC port functionality, the

following steps should be addressed. Register ADCSRA needs to be enabled, started, auto trigger

enabled, and an appropriate prescalar selected; Register ADCSRB needs to have the auto trigger source set to

free running, so the program can read the converted value of a low-frequency sensor (which is often our application);

Register ADMUX needs to have a VREF source selected, right or left justification selected, and the desired ADC channel selected;

Register DIDR0 should have the input pin associated with the ADC channel selected in ADMUX disabled.

Once the ADC is initialized, the program can poll the ADC output by reading the 16-bit ADC register which contains the 10-bit result

Page 10: Suleyman Demirel University 20151 CSS340 Microprocessor Systems – Lecture 2 ATMEGA328P ARCHITECTURE ANALOG INPUTS.

Suleyman Demirel University 2015 10

Analog Port Registers

Page 11: Suleyman Demirel University 20151 CSS340 Microprocessor Systems – Lecture 2 ATMEGA328P ARCHITECTURE ANALOG INPUTS.

Suleyman Demirel University 2015 11

Analog Input PortanalogReference(type);Configures the reference voltage used for analog input. The options are:DEFAULT: the default analog reference of 5 volts.INTERNAL: a built-in reference, equal to 1.1 volts on the ATmega168 or ATmega328 and 2.56 volts on the ATmega8 (not available on the Arduino Mega)EXTERNAL: the voltage applied to the AREF pin (0 to 5V only) is used as the reference.

analogRead(pin);Reads the value from the specified analog pin. The Arduino Uno board contains a 6 channel, 10-bit analog to digital converter. This means that it will map input voltages between 0 and 5 volts into integer values between 0 and 1023. This yields a resolution between readings of: 5 volts / 1024 units or, 4.9 mV per unit. The input range and resolution can be changed using analogReference(). It takes about 100 microseconds to read an analog input, so the maximum reading rate is about 10,000 times a second.

Page 12: Suleyman Demirel University 20151 CSS340 Microprocessor Systems – Lecture 2 ATMEGA328P ARCHITECTURE ANALOG INPUTS.

Suleyman Demirel University 2015 12

Reading Analog Value

int analogPin = A0; // potentiometer wiper (middle terminal) connected to // analog pin 3 outside leads to ground and +5V

int val = 0; // variable to store the value read

void setup(){

Serial.begin(9600); // setup serial }

void loop() {

val = analogRead(analogPin); // read the input pinSerial.println(val); // debug value

}