[go: up one dir, main page]

0% found this document useful (0 votes)
23 views82 pages

Over View About Microcontroller New

Uploaded by

PHONG LÊ BÁ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views82 pages

Over View About Microcontroller New

Uploaded by

PHONG LÊ BÁ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 82

Micro-controller

Researches
• Visual servo
• Image Processing
• Impedance control
• Intelligent control
Le Duc Hanh, Phd • Robotics
Lecturer at Ho Chi Minh city University
of Technology

Email: ldhanh@hcmut.edu.vn
Mobile: 84909930983
Web: https://ldhanh.wixsite.com/rovis/researches

ME3007 Robotic & Vision Lab RoVis


Web: https://ldhanh.wixsite.com/rovis/researches

Robotic & Vision Lab RoVis


REFERENCE

Robotic & Vision Lab RoVis


Syllabus

• Module 1: I/O application


• Module 2: A/D analog to digital conversion
• Module 3: Timer
• Module 4: PWM and motor control
• Module 5: Communication ( RS232, RS485, I2C, SPI, CAN…)

Robotic & Vision Lab RoVis


Over View About Microcontroller

Robotic & Vision Lab RoVis


Block control flowchart

Disturbance

Controlled Manipulated
Set-point Error Signal Variable
or
Reference
Actual
input + Output
+ +
+ Controller Actuator + Process
-

Sensor
Feedback Signal

Robotic & Vision Lab RoVis


What is a Microcontroller?

• A microcontroller is a kind of miniature computer that


found in all kinds of gizmos

• Generally speaking, if a device has buttons and a


digital display, chances are it also has a
programmable microcontroller brain.

Robotic & Vision Lab RoVis


Microcontrollers cont.

Microcontrollers are 'single chip'


computers specifically designed to:
• Read input devices, such as buttons and sensors
• Process data or information
• Control output devices, such as lights, displays, motors and
speakers

Robotic & Vision Lab RoVis


Embedded Control

• Microcontrollers are placed in devices, or


embedded, for operation and control.

• Can you name other devices in your life that


have embedded control?

Robotic & Vision Lab RoVis


Microprocessor vs. Microcontroller

• A microprocessor is the “brain” of a computer system


• Generally referred to as the central processing unit
(CPU), the microprocessor by itself is practically useless
• To be useful, one must have means of communicating
with it using input and output devices
• One must also add memory (ROM and RAM) so that
the system can be programmed.

Robotic & Vision Lab RoVis


Microprocessor vs. Microcontroller Cont.

• A microcontroller is a computer chip designed for control-


oriented applications
• Unlike ordinary microprocessors, microcontrollers have
built-in features that make them operate almost
independent of additional circuitry
• This is possible because microcontrollers contain things like
• memory (ROM, EPROM, RAM, etc)
• input and output ports
• timers
• serial and parallel communication capability
• analog-to-digital converters

Robotic & Vision Lab RoVis


Robotic & Vision Lab RoVis
Microcontroller Packaging and Appearance

From left to right: PIC 12F508, PIC 16F84A, PIC


16C72, Motorola 68HC05B16, PIC 16F877,
Motorola 68000
Dr.

Robotic & Vision Lab RoVis


Gheith 14
Abanda
ALU is responsible for adding, subtracting,
shifting and performing logical operations

Robotic & Vision Lab RoVis


Robotic & Vision Lab RoVis
http://www.mikroe.com/ Robotic & Vision Lab RoVis
von-Neumann Architecture

- Microcontrollers using this architecture have only one memory block and one 8-bit data
bus.
- As all data are exchanged by using these 8 lines, this bus is overloaded and
communication itself is very slow and inefficient. The CPU can either read an instruction or
read/write data from/to the memory. Both cannot occur at the same time since the
instructions and data use the same bus system.

Robotic & Vision Lab RoVis


Harvard Architecture

Microcontrollers using this architecture have two different data buses. One is 8 bits
wide and connects CPU to RAM. Another consists of several lines (12, 14 or 16) and
connects CPU to ROM. Accordingly, the CPU can read an instruction and perform a
data memory access at the same time

Robotic & Vision Lab RoVis


Robotic & Vision Lab RoVis
Bus
• The data bus is a collection of wires or tracks that are used to transport numbers into and
out of the CPUPhysically, the bus consists of 8, 16 or more wires.
• There are two types of buses: the address bus and the data bus.
• The address bus consists of as many lines as necessary for memory addressing. It is
used to transmit the address from the CPU to the memory.
• The data bus is as wide as the data, in our case it is 8 bits or wires wide. It is used to
connect all circuits inside the microcontroller.

Find 16 bit processor had a 20 bit address bus?

Robotic & Vision Lab RoVis


• The CPU can recognizes only 35 simple instructions
• The excution time lasts 4 clock cycles. EX: the crystal: 20Mhz->0.2us for one
intruction

Robotic & Vision Lab RoVis


Memory
• ROM: permanently save the program: EX: PIC16F877 has
8kb of Rom
• EEPROM: The contents of EEPROM is permanently saved,
even the power goes off. However, unlike ROM, the contents
of the EEPROM can be changed during operation of the
microcontroller. It is used to save the results created and
used during programing
• RAM Memory: RAM (random access memory) is used for
storing values that are liable to change during the course of
the execution of a program. RAM contents are (usually) lost
each time systems are turned off/on
• it consists of two parts: general-purpose registers and special-
function registers (SFR).

Robotic & Vision Lab RoVis


Robotic & Vision Lab RoVis
General-Purpose Registers
General-Purpose registers are used for storing temporary data and results created during
operation. For example, if the program performs a counting (for example, counting
products on the assembly line

SFR Registers
Special-Function registers are also RAM memory locations, but unlike general-purpose
registers, their purpose is predetermined during manufacturing process and cannot be
changed. Since their bits are physically connected to particular circuits on the chip (A/D
converter, serial communication module, etc.), any change of their contents directly affects
the operation of the microcontroller or some of its circuits

Robotic & Vision Lab RoVis


SFR register
The special function registers can be classified into two categories:
- Core (CPU) registers - control and monitor operation and processes in the central
processor. Even though there are only a few of them, the operation of the whole
microcontroller depends on their contents.
- Peripheral SFRs- control the operation of peripheral units (serial communication module,
A/D converter etc.). Each of these registers is mainly specialized for one circuit and for that
reason they will be described along with the circuit they are in control of.

Robotic & Vision Lab RoVis


Interrupt System

- when an interrupt request arrives is to execute


- the current instruction is execute and then stop regular program execution.
- Immediately after that, the current program memory address is automatically
pushed onto the stack and the default address (predefined by the manufacturer) is
written to the program counter. That location from where the program continues
execution is called the interrupt vector. For the PIC16F887 microcontroller, this
address is 0004h.

Robotic & Vision Lab RoVis


Timers

Timers are typically constructed using a clock source and a counter. Counters count clock
periods that are input to them. Some counters count up, some down. Counters have a limited
“width” or number of bits and as a result can only count up or down so far without “rolling
over”.
• An 8 bit up counter for example can only count up to 255 decimal; one more period input
will cause it to overflow and revert to zero.
• A 16 bit down-counter will decrement down to zero, and given one more input it will roll
around to 65535 (0xffff).
• When this roll over takes place, most counters emit some kind of signal.

Robotic & Vision Lab RoVis


Robotic & Vision Lab RoVis
Interrupt system register
everything is based on a simple and efficient idea. When an interrupt request arrives, the
flag bit is to be set first.

If the appropriate IE bit is not set (0), this event will be completely ignored. Otherwise,
an interrupt occurs! In case several interrupt sources are enabled, it is necessary to
detect the active one before the interrupt routine starts execution. Source detection is
performed by checking flag bits.

It is important to understand that the flag bits are not automatically cleared, but by
software during interrupt routine execution

Robotic & Vision Lab RoVis


PIEx (x=1,2..)
PIRx (x=1,2..)

Robotic & Vision Lab RoVis


Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
PIC 16F887 layout
Analog I/O
PORTA

I2C

UART

http://www.mikroe.com/eng/chapters/view/74/pic-basic-book-chapter-1-
world-of-microcontrollers/

Robotic & Vision Lab RoVis


34
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Demo movie
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Microcontroller Ports and Pins

 The communication channels


through which information flows
into or out of the C
microcontroller
 Ex. PORTB
 Pins PB0 – PB7
 May not be contiguous
 Often bi-directional

Robotic & Vision Lab RoVis


Port Pin Data Directionality

• Input
• When you want to take information from the external
world (sensors) into the MCU
• Output
• When you want to change the state of something outside
the MCU (turn a motor on or off, etc.)
• Pins default to input direction on power-up or reset
• Your program can set or change the directionality of a
pin at any time

Robotic & Vision Lab RoVis


Pin Voltages

• Microcontrollers are fundamentally digital devices.


For digital IO pins:
• Information is ‘coded’ in two discrete states:
• HIGH or LOW (logic: 1 or 0)
• Voltages
• TTL
• 5 V (for HIGH)
• 0 V (for LOW)
• 3.3 V CMOS
• 3.3 V (for HIGH)
• 0 V (for LOW)

Robotic & Vision Lab RoVis


Electrical device

Robotic & Vision Lab RoVis


Robotic & Vision Lab RoVis
A pull-up resistor is a resistor
- connected between a signal conductor and a positive power
supply voltage to ensure that the signal will be a valid logic
level if external devices are disconnected or high-impedance is
introduced

Robotic & Vision Lab RoVis


Pull-down resistors work in the same manner as pull-up resistors,
except that they pull the pin to a logical low value. They are
connected between ground and the appropriate pin on a device

Robotic & Vision Lab RoVis


Pull up and Pull down Resistor

Robotic & Vision Lab RoVis


Example

Robotic & Vision Lab RoVis


Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Port B is an 8-bit wide, bidirectional port. Bits of the TRISB register determine the
function of its pins

Robotic & Vision Lab RoVis


The ANSEL and ANSELH registers are used to configure the input
mode of an I/O pin to analog or digital.

Robotic & Vision Lab RoVis


JDM/PG2 Board

USB board

Robotic & Vision Lab RoVis


Robotic & Vision Lab RoVis
Basic elements that make a PIC works

Robotic & Vision Lab RoVis


Robotic & Vision Lab RoVis
CCS Programing

Robotic & Vision Lab RoVis


Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Output
Lệnh áp đặt chức năng của port hay chân
set_tris_X(value)
X: các port của PIC16F877
Value: 0: output (chân tương ứng sẽ là 0V)
1: input (chân tương ứng sẽ là 5V)
Vd: SET_TRIS_B( 0x0F );
// B7,B6,B5,B4 are outputs
// B3,B2,B1,B0 are inputs

Các lệnh xuất của I/O pin :


Truy xuất từng chân và thanh ghi
+Output_high(PIN_XXX): đặt giá trị tại chân tương ứng là 5V
+Output_low(PIN_XXX): đặt giá trị tại chân tương ứng là 0V
+Output_bit (PIN_XXX, value): value=0 hay là 1
PIN(XXX) : được định nghĩa sẵn trong file device.h

Robotic & Vision Lab RoVis


Input

 Các lệnh đọc giá trị của cổng và thanh ghi


+ Đọc giá trị từ một chân của port
value=input(PIN_XXX);
value: trị trả về là true hay false
PIN_XXX: tên của chân

value=input_xx();
value: trị trả về là giá trị 8 bit trạng thái của port
xx: tên của port

Robotic & Vision Lab RoVis


PIC16F877 I/O Example

Robotic & Vision Lab RoVis


Robotic & Vision Lab RoVis
Phương pháp truy xuất trực tiếp địa chỉ thanh ghi
Khi kết hợp 2 tiền xử lý sau đây sẽ giúp người lập trình truy xuất đến từng thanh ghi
giống như lập trình trong ASM
#byte id=x
id: tên gợi nhớ
x: biến C hay là một hằng số, có thể coi x như địa chỉ thanh ghi
# bit id=x.y
id: tên gợi nhớ thường là tên của thanh ghi
x: là giá trị hằng số hay biến C, y: 0->7

Robotic & Vision Lab RoVis


#include<16F877.h>
#use delay(clock = 20000000)
#include <stdlib.h>
#include <string.h>
#byte porb=0x06
#bit bo=porb.0// hay là #bit bo=0x06.0
#bit trisbo=0x86.0
void main()
{
trisbo=0;
bo=0
delay_ms(1000);
bo=1
delay_ms(1000);
}

Robotic & Vision Lab RoVis

You might also like