1.1 What is an ATmega328P Microcontroller?
It is this 28-pin semiconductor chip of Fig-1.1. The full name is “Micro Controlling Unit”, the short name is “Microcontroller”, and the abbreviated name is “MCU”. The MCU can process only 8-bit data at a time.
(a) Pictorial view of ATmega328P MCU
(b) Pin/signal diagram of ATmega328P MCU
Figure-1.1: Pictorial view, pin diagram, and pin signals of ATmega328P microcontroller
1.2 Use of a Microcontroller
We can use a MCU to build the following instruments:
(1) Taxi Meter,
(2) Prepaid Electrical Energy Meter (PEM), ,and
(3) Digital Weighing Machine (DWM), etc.
(a) Taxi Meter
(b) Prepaid Electrical Energy Meter
(c) Digital Weighing Machine
Figure-1.2: Microcontroller based measuring instruments
1.3 Basic Tasks of a Microcontroller
(1) Takes command (+, _, *, and /) from the user via keyboard,
(2) Takes data from the user via keyboard,
(3) Modifies data in a way the user wants, and
(4) Delivers the result to the user via 7-segment/LCD display device.
1.4 Pin Diagram and Pin Signals of ATmega328P MCU
ATmega328P MCU has 28 pins (Fig-1.1) of which 20 are IO line (also known as port-line or port-pin). An IO (input and output) line is used to connect input device like switch and output device like LED.
A pin is associated with one or more signals. For example: Pin-5 is associated with four signals, and these are: PCINT19, OC2B, INT1, and PD3. The signal PD3 which is outside the parentheses is the default function of the pin. The other three signals (PCINT19, OC2B, and INT1) are the alternate signals, and they can be activated if needed through a process known as software initialization or software configuration. A pin has the following features:
(1) It has a serial number. For example: Pin-5
(2) It is associated with one or more signal. For example: Pin-5 is associated with signal PD3
(3) The signal has a meaning. For example: PD3 stands for ‘Bit-3 of PORTD as Output Line’.
(4) The pin/signal has a direction. For example: PD3 signal is an output line.
(5) The pin/signal has electrical characteristics. For example: PD3 can deliver 20 mA current at 4.20 V.
The words pin and signal will interchangeably refer to the same thing. Thus, saying that “Pin-6” works as IO line is equivalent to saying that “signal PD4” works as IO line. The pins are used to connect input devices (switches, temperature sensor, and etc.) and output devices (7-seg display unit, stepper motor, and etc.) with the MCU. A pin is also known as port-pin or port-line as it exchanges 1-bit data with IO (input and output) devices. In Ch-2, we will study the alternate functions of the IO lines.
1.5 Port Structured Diagram for the Pins of ATmega328P Microcontroller
(1) In Fig-1.1, we observe that the MCU has 20 IO lines spread over various pins with symbolic names: PB0 – PB5; PC0 – PC5; PD0 – PD7. In this Subsection, let us organize the pins so that pins with similar function stay together (Fig-1.3).
Figure-1.3: Port structured diagram of ATmega328P MCU
(2) All the IO lines of the MCU have similar functional characteristics. Therefore, any discussion that will be done on a particular IO line (say, Bit-2 of Port-D) will apply to all other IO lines.
(3) Port-D: It will refer to a port when the directions of its IO lines are not yet determined as to be working as input or output lines. Thus, pd0 – pd7 will refer to bit-0 to bit-7 of Port-D Register.
(4) PORTD: It will refer to a port when the directions of the IO line are configured to work as output lines. Thus, PD0–PD7 (or PORTD0–PORTD7) will refer to bit-0 to bit-7 of PORTD Register.
(5) PIND: It will refer to a port when the directions of the IO lines are configured to work as input lines. Thus, PIND0 – PIND7 will refer to bit-0 to bit-7 of PIND Register.
(6) The direction of an IO line is set as output by executing the following command:
pinMode(DPin, OUTPUT); //DPin = digital Pin Connector: 0–7 (PD0–PD7), 8–13 (PB0-P5), 14/A0–19/A5 (PC0–PC5)
(7) The direction of an IO line is set as input by executing the following command:
pinMode(DPin, INPUT);
** **(8)** **
The direction of an IO line is set as input with internal pull-up resistor by executing this command:
pinMode(DPin, INPUT_PULLUP); //every input line has an internal pull-up resistor (Rip = 20k – 50k) Fig-1.3
(9) In Fig-1.3, LED1 is an output device. It is to be placed on the breadboard along with a series current limiting resistor R1. One side of LED1 would be connected with GND pin of UNO (Fig-1.4) with the help of a jumper wire. The other side of LED1 (actually, the other side of R1) will be connected with DPin-8 of UNO (Fig-1.5).
In Fig-1.3, it is observed that DPin-8 is internally connected with pin-14 of MCU which is Bit-0 of Port-B. Bit-0 has been assigned a “signal name” called PB0. Here, the LED1 is connected with an output line named DPin-8 or PB0; however, the name DPin-8 (simply 8
) would be used in the sketch during programming.
(10) To turn ON LED1, we must send Logic High (LH: 3V – 5V) signal at DPin-8 by executing the following Arduino Code/command/instruction (Section-1.12, Table-1.1).
digitalWrite(8, HIGH);
(11) In Fig-1.3, K1 is an input device. One side of K1 is connected with 5V of UNO (Fig-1.5). The other side of K1 is connected with an input line of MCU which is named as DPin-A3 or DPin-17 or PINC3 or Bit-3 of Port-C.
When K1 is not pressed, the logic level of DPin-A3 is GND (0V) by virtue of pull-down resistor R2. DPin-A3 will assume LH signal when K1 is pressed; as a result, HIGH (1) will appear at Bit-3 of Port-C.
Careful observation reveals that there is an internal pull-up resistor Rip which can be connected with DPin-A3 by executing the following command. Let us note that both external pull-down and internal pull-up must not be connected with DPin-A3 at the same time. Also, not that every input line has its own Rip that could be connected or left unconnected.
pinMode(A3, INPUT_PULLUP); //DPin-A3 is an input line with internal pull-up connected.
pinMode(A3, INPUT); //DPin-A3 is an input line without internal pull-up.
(12) To check the closing condition of K1 of Fig-1.3, we may execute the following code lines:
pinMode(A3, INPUT); //DPin-3 works as input line with external pull-down connected
bool n = digitalRead(A3); //if K1 is closed, then LH (5V) will be stored in variable n
if(n == HIGH)
{
Serial.print("K1 is found at closed condition."); //message will appear on Serial Monitor
}
elae
{
Serial.print("K1 is found at opened condition."); //message will appear on Serial Monitor
}
(13) The operating frequency of the MCU of UNO Board can be set to either 8 MHz from an internal oscillator or to 16 MHz using an external crystal Y1. Currently, the MCU runs at 16 MHz clock frequency.
... to be continued in the next post.
Ch1FundOnline.pdf (851 KB)