2024microprocessor BE3EC Lecture No.6
2024microprocessor BE3EC Lecture No.6
Microcontroller
All the essential elements of a computer on a single chip
MPU, R/W memory, ROM, and I/O interfacing devices.
Examples: Intel MCS-51, Motorola MC68HC51, Microchip PIC series, Arduino series
40 pins
Clock frequency 12 MHz of MPU
128 bytes of data memory (RAM) plus 21 special-function registers
4096 bytes of program memory (EPROM/ROM) for OS
four programmable I/O ports (32 lines)
two 16-bit timer/event counters
a serial I/O port with a UART (Universal Asynchronous Receiver/Transmitter)
five interrupt lines: two for external signals and three for internal operations
Frequency
Reference Counters
8051 CPU
-1-
6.2 Motorola MC68HC11 Microcontroller Family
Made by Motorola in 1984
48 pins
Used in industrial applications
Clock frequency 2 MHz of MPU
8K bytes of ROM and 512 bytes of EEPROM
256 bytes of RAM (R/W memory)
40 I/O pins with multiple functions
Eight-channel, 8-bit AD converter
16-bit timer system
8-bit pulse accumulator system
Serial communication and serial peripheral interface
Computer operating properly (COP) watchdog system (Error → RESET)
48,64,80,100,144,176 pins
Used in automobile
Clock frequency 80 MHz of CPU G3
32K bytes of ROM and 256K − 2M bytes of EEPROM
Maximum sixty-channel, 8-bit AD converter
Maximum two channel, computer operating properly (COP) watchdog system
-2-
Software Development Environment
MPLAB X IDE (Integrated Development Environment)
Assembly language
C language (MPLAB XC?? Compiler, CCS C-Compiler)
PIC16F84A
8 bit series
35 instructions (instruction width 14 bits, data width 8 bits)
Maximum operating clock 20 MHz
Program memory of size 1024 words = 1796 bytes (OS)
Data ROM of 68 bytes
Data EEPROM of 64 bytes
13 I/O pins (PORTA 5 bits, PORTB 8 bits)
8 bit timer counter module
RA2 ·1 18 RA1
RA3 2 17 RA0
RA4/T0CKI 3 16 OSC1/CLKIN
PIC16F84A-20/P
തതതതതതതത
MCLR 4 15 OSC2/CLKOUT
VSS 5 14 VDD
RB0/INT 6 13 RB7
RB1 7 12 RB6
RB2 8 11 RB5
RB3 9 10 RB4
-3-
Arduino UNO R3
DC Power Supply through Power Jack
• must be a DC adapter.
• should be between 9V and 12V DC.
• must be rated for a minimum of 250mA current output.
• must have a 2.1mm power plug on the Arduino end.
• The plug must be "centre positive".
Reset Button
By pushing the reset button, a sketch (user program) restarts from the beginning.
Crystal Oscillator
• 16 MHz
• The crystal oscillator acts as the ridiculously fast metronome for ATmega328P.
Voltage Regulator
5V power supply constantly
Resettable Fuse
It protects the USB port from shorts and overcurrent.
-4-
Fig. 6-4 Components of Arduino UNO R3
-5-
6.6.1 Blinking Built-In LED
Simple programming on Arduino
The L LED mounted on the Arduino board is used to confirm action of a program. See Fig. 6-5.
//***************************************************************//
// Name : Blinking Built-In LED //
// Author : Kazuhiro Muramatsu //
// Date : 9 December 2020 //
// Version : 1.0 //
// Notes : Code on Blinking Built-In LED 13 //
//***************************************************************//
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
}
-6-
Be careful to distinguish between upper- and lowercase letters. pinMode should be typed as
pinMode exactly. PinMode or Pinmode is wrong. The meaning of setup, pinMode, digitalWrite,
and delay are explained after.
setup()
Is called once when the sketch starts.
Setup tasks like setting pin modes or initializing libraries.
The code between the { and } = The body of the setup function
Returns no value The type of the functions is void.
void setup()
{
setup code here, to run once
}
pinMode(pin, mode)
Configures the specified pin to behave either as an input or an output.
Parameters: pin; the number of the pin whose mode you wish to set
mode; INPUT, OUTPUT, or INPUT_PULLUP
pinMode(13, OUTPUT);
loop()
Is called over and over and is heart of most sketches.
Loops consecutively
The code between the { and } = The body of the loop function
Returns no value The type of the functions is void.
void loop()
{
main code here, to run repeatedly
}
digitalWrite(pin, value)
Writes a HIGH or a LOW value to a digital pin.
Parameters: pin; the pin number
value: HIGH or LOW
-7-
digitalWrite(13, HIGH);
delay(ms)
Pauses the program for the amount of time (in milliseconds).
Parameters: ms; the number of milliseconds to pause (unsigned long)
delay(500);
digitalWrite(13, LOW);
digitalWrite(13, LOW);
ON OFF ON OFF
500 millisec. 500 millisec. 500 millisec. 500 millisec.
Repeat
//***************************************************************//
// Name : Blinking Built-In LED //
// Author : Kazuhiro Muramatsu //
// Date : 9 December 2020 //
// Version : 1.0 //
-8-
// Notes : Code on Blinking Built-In LED 13 //
// : #define is used //
//***************************************************************//
#define LED_PIN 13
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
delay(500);
}
#define LED_PIN 13
The following sketch is control of the L LED on/off by serial monitor. (Use_Serial_Monitor)
-9-
//***************************************************************//
// Name : Using Serial Monitor //
// Author : Kazuhiro Muramatsu //
// Date : 9 December 2020 //
// Version : 1.0 //
// Notes : Code for using the serial monitor //
//***************************************************************//
#define LED_PIN 13
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
int inputchar;
inputchar = Serial.read();
if(inputchar != -1) {
switch(inputchar) {
case 'o':
Serial.print("LED ON¥n");
digitalWrite(LED_PIN, HIGH);
break;
case 'p':
Serial.print("LED OFF¥n");
digitalWrite(LED_PIN, LOW);
break;
}
}
}
Serial.begin(speed[, config])
Opens serial port with the specific data rate in bits per second (baud).
Parameters: speed; in bits per second (baud), one of these rates: 300, 600, 1200, 2400, 4800,
9600, 14400, 19200, 28800, 38400, 57600, or 115200 (long int)
config; the data, parity, and stop bits are configured. [optional]
- 10 -
Serial.begin(9600);
Serial.read()
Reads incoming serial data.
Return: the first byte of incoming serial data available (or -1 if no data is available)
inputchar = Serial.read();
Serial.print(val[, format])
Prints the val to the serial port as ASCII text.
Parameters: val; the value to print (any data type)
format; specifies the base (format) to use [optional]
BIN: binary or base 2
OCT: octal or base 8
DEC: decimal or base 10 (default)
HEX: hexadecimal or base 16
For floating point numbers, this parameter specifies the number of
decimal places to use.
Serial.print("LED ON¥n");
- 11 -
6.6 Architecture of ATmega328P Microcontroller
6.6.1 Pin Configuration of ATmega328P
The pin out diagram for ATmega328P
ADC[5:0]
Analog inputs to the 10-bit A/D converter
- 12 -
AVCC
The supply voltage for the A/D Converter
AREF
The analog reference for the A/D Converter
SCL
I2C / TWI (Two-Wire Interface) Serial Bus Clock Line
SDA
I2C / TWI (Two-Wire Interface) Serial Bus Data Input/Output Line
SCK
SPI (Serial Peripheral Interface) Bus Master clock Input
MISO
SPI (Serial Peripheral Interface) Bus Master Input/Slave Output
MOSI
SPI (Serial Peripheral Interface) Bus Master Output/Slave Input
തതത
SS
SPI (Serial Peripheral Interface) Bus Master Slave select
RXD
USART (Universal Synchronous Asynchronous Receiver/Transmitter) Input Pin
TXD
USART (Universal Synchronous Asynchronous Receiver/Transmitter) Output Pin
INT0
External Interrupt Request 0
INT1
External Interrupt Request 1
- 13 -
Fig. 6-8 Block Diagram of ATmega328P
- 14 -
6.6.3 Port System of ATmega328P
Three ports: PORTB (8-bit), PORTC (7-bit), and PORTD (8-bit)
All of these ports also have alternate functions.
Each port has three registers associated with it.
• 8-bit Data Register PORTx —- used to write output data to the port.
• 8-bit Data Direction Register DDRx —- used to set a specific port pin to either output (1)
or input (0).
• 8-bit Input Pin Address PINx —- used to read input data from the port.
- 15 -
2 ms
Serial USART
is used for full duplex (two way) communication between a receiver and transmitter.
Example: Arduino and PC
- 16 -
Example: Arduino and plural I/O devices
(f) Interrupts
Interrupts
allow certain important tasks to happen in the background and are enabled by default.
- 17 -
jump program sequence to an interrupt routine.
INT0 and INT1 can be set to trigger on RISING or FALLING signal edges, or on low level.
- 18 -