Micro Controllers - Embedded programming with real hardware

88
Team Emertxe Microcontrollers

Transcript of Micro Controllers - Embedded programming with real hardware

Page 1: Micro Controllers - Embedded programming with real hardware

Team Emertxe

Microcontrollers

Page 2: Micro Controllers - Embedded programming with real hardware

Microcontrollers

● Introduction

● System Development Concept (Low Level)

● Understanding Target

● Work Environment and Development Guidelines

● Lets Roll Out on Interfaces

Page 3: Micro Controllers - Embedded programming with real hardware

Introduction

Page 4: Micro Controllers - Embedded programming with real hardware

Introduction

● What is a Microcontroller

● General Architectures

● µP vs µC

● Choosing a Microcontroller

Page 5: Micro Controllers - Embedded programming with real hardware

IntroductionWhat is a Microcontroller?

● An Integrated Circuit which is capable of being programmed to preform a specific task.

● The design normally has restrictions on its

– Memory Size

– I/O Capabilities

– Peripheral Functions etc.,

Page 6: Micro Controllers - Embedded programming with real hardware

IntroductionGeneral Architectures

● Shared signals and memory for code and data

● Physically separate signals and storage for code and data

Page 7: Micro Controllers - Embedded programming with real hardware

IntroductionµP vs µC - Microprocessors

● All separate components

● More flexible

● More design complexity

Page 8: Micro Controllers - Embedded programming with real hardware

IntroductionµP vs µC - Microcontroller

● All components in single chip

● Less flexible

● Less design complexity

Page 9: Micro Controllers - Embedded programming with real hardware

IntroductionChoosing a Microcontroller

● Applications

● Performance

● Price

● Availability

● Availability of Tools

● Special Capabilities

Page 10: Micro Controllers - Embedded programming with real hardware

System Development Concepts(Low Level)

Page 11: Micro Controllers - Embedded programming with real hardware

System Development Concept

● Host and Target● Host Tools

Page 12: Micro Controllers - Embedded programming with real hardware

System Development ConceptsHost and Target

● Host:

A system which is used to develop the target.

● Target:

A system which is being developed for specific application.

Page 13: Micro Controllers - Embedded programming with real hardware

System Development ConceptsHost Tools

● Software

– Cross Compiler : XC8 (Free Version)

– Code Down Loader : Tiny Bootloader● Hardware

– None :). Just a PC with DB9 port or an USB to Serial Converter

Page 14: Micro Controllers - Embedded programming with real hardware

Understanding Target – RL-eCee-PIC184580

Page 15: Micro Controllers - Embedded programming with real hardware

Understanding Target

● RL-eCee-18F4580 Architecture

● RL-eCee-18F4580 Development Board

● Understanding the Target Controller

Page 16: Micro Controllers - Embedded programming with real hardware

Understanding TargetRL-eCee-PIC184580 Architecture

PIC18F4580

I2CRTC / EEPROM

LM35Temp Sensor

RS232

GPIO

Digital / MatrixKeypad

LEDsSSDs

CLCD

ADC(Single Port)

CAN

TSOPIR Sensor

Buzzer

ON ChipBootloader

Page 17: Micro Controllers - Embedded programming with real hardware

Understanding TargetRL-eCee-PIC184580 Development Board

● PIC18F4580● CLCD● SSD● LEDs● Matrix Keypad● Digital Keypad● RS232● CAN● I2C - EEPROM● I2C - DS1338● Temperature

Sensor● IR Sensor● I/O Extensions

Page 18: Micro Controllers - Embedded programming with real hardware

Understanding Target ControllerPIC18F4580

Page 19: Micro Controllers - Embedded programming with real hardware

Understanding Target ControllerPIC18F4580

● Architecture

● Program Memory

● Data Memory

● Pin Diagram

● Clocking

● I/O Config

Page 20: Micro Controllers - Embedded programming with real hardware

Understanding Target ControllerPIC18F4580 Clocking

Page 21: Micro Controllers - Embedded programming with real hardware

Understanding Target ControllerPIC18F4580 I/O Config

Page 22: Micro Controllers - Embedded programming with real hardware

Work Environment and DevelopmentGuidelines

Page 23: Micro Controllers - Embedded programming with real hardware

Work Environment and Development Guidelines

● Workspace Creation

● Code Structure

● Code Template

● Code Dump Creation

Page 24: Micro Controllers - Embedded programming with real hardware

Workspace Creation

Page 25: Micro Controllers - Embedded programming with real hardware

Code Structure

Page 26: Micro Controllers - Embedded programming with real hardware

Code Template

#ifndef MAIN_H#define MAIN_H

#include <htc.h>

#endif

#include “main.h”

void init_config(void){

/* Initilization Code */}

void main(void){

init_config();

while (1){

/* Application Code */}

}

__CONFIG(DEBUGDIS & LVPDIS & FCMDIS & IESODIS & BORDIS & DUNPROTECT & UNPROTECT & MCLREN & PWRTDIS & WDTDIS & HS );

__CONFIG(WP0 & BORV21);

Page 27: Micro Controllers - Embedded programming with real hardware

Code Dump Creation

● Compilation using picc would be$ <path>/xc8 --chip=18f4580 main.c

● Hex code dumping

$ <path>/tinybldlin.py –-port /dev/<device_file> --file pic16f887.hex

Page 28: Micro Controllers - Embedded programming with real hardware

Lets Roll Out on Interfaces

Page 29: Micro Controllers - Embedded programming with real hardware

Lets Roll Out on Interfaces

● LEDs

● Digital Keypad

● Interrupts

● Timers

● Clock I/O

● Analog Inputs

● SSDs

● CLCD

● Matrix Keypad

● Data Storage

Page 30: Micro Controllers - Embedded programming with real hardware

Light Emitting Diodes

Page 31: Micro Controllers - Embedded programming with real hardware

LEDsIntroduction

● Simplest device used in most on the embedded applications as feedback

● Works just like diodes

● Low energy consumptions, longer life, smaller size, faster switching make it usable in wide application fields like

– Home lighting,

– Remote Controls, Surveillance,

– Displays and many more!!

Page 32: Micro Controllers - Embedded programming with real hardware

LEDsExample

● Which side will work?

Page 33: Micro Controllers - Embedded programming with real hardware

LEDsExample

ON

● Oops :( wrong choice. Can you explain why?

Page 34: Micro Controllers - Embedded programming with real hardware

LEDsExample – Correct Choice :)

● Ooh :) looks like you know the funda.

Page 35: Micro Controllers - Embedded programming with real hardware

LEDsCircuit on Board

Page 36: Micro Controllers - Embedded programming with real hardware

Digital Keypad

Page 37: Micro Controllers - Embedded programming with real hardware

Digital Keypad

● Introduction

● Interfacing

● Input Detection

● Bouncing Effect

● Circuit on Board

Page 38: Micro Controllers - Embedded programming with real hardware

Digital KeypadIntroduction

● Provides simple and cheap interface

● Comes in different shapes and sizes

● Preferable if the no of user inputs are less

● Mostly based on tactile switches

● Some common application of tactile keys are

– HMI

– Mobile Phones

– Computer Mouse etc,.

Page 39: Micro Controllers - Embedded programming with real hardware

Digital KeypadTactile Switches Interfacing

● Considering the below design what will be input to the controller if the switch is pressed?

Page 40: Micro Controllers - Embedded programming with real hardware

Digital KeypadTactile Switches Interfacing

● Will this solve the problem which may arise in the design mentioned in previous slide?

Page 41: Micro Controllers - Embedded programming with real hardware

Digital KeypadTactile Switches Interfacing

● Now will this solve the problem which may arise in the design mentioned in previous slides?

Page 42: Micro Controllers - Embedded programming with real hardware

Digital KeypadTactile Switches Interfacing

● What would you call the this design. Is there any potential problem?

Page 43: Micro Controllers - Embedded programming with real hardware

Digital KeypadTactile Switches Interfacing

Page 44: Micro Controllers - Embedded programming with real hardware

Digital KeypadTriggering Methods

Page 45: Micro Controllers - Embedded programming with real hardware

Digital KeypadBouncing Effects

Page 46: Micro Controllers - Embedded programming with real hardware

Digital KeypadCircuit on Board

Page 47: Micro Controllers - Embedded programming with real hardware

Interrupts

Page 48: Micro Controllers - Embedded programming with real hardware

Interrupts

● Basic Concepts

● Interrupt Source

● Interrupt Classification

● Interrupt Handling

Page 49: Micro Controllers - Embedded programming with real hardware

● An interrupt is a communication process set up in a microprocessor or microcontroller in which:

– An internal or external device requests the MPU to stop the processing

– The MPU acknowledges the request

– Attends to the request

– Goes back to processing where it was interrupted● Polling

InterruptsBasic Concepts

Page 50: Micro Controllers - Embedded programming with real hardware

● Loss of Events

● Response

● Power Management

InterruptsInterrupt vs Polling

Page 51: Micro Controllers - Embedded programming with real hardware

● External

● Timers

● Peripherals

InterruptSources

Page 52: Micro Controllers - Embedded programming with real hardware

InterruptClassification

Page 53: Micro Controllers - Embedded programming with real hardware

InterruptHandling

Page 54: Micro Controllers - Embedded programming with real hardware

InterruptService Routine (ISR)

● Similar to a subroutine

● Attends to the request of an interrupting source

– Clears the interrupt flag

– Should save register contents that may be affected by the code in the ISR

– Must be terminated with the instruction RETFIE

● When an interrupt occurs, the MPU:

– Completes the instruction being executed

– Disables global interrupt enable

– Places the address from the program counter on the stack

● Return from interrupt

Page 55: Micro Controllers - Embedded programming with real hardware

InterruptService Routine (ISR)

● What / What Not

Page 56: Micro Controllers - Embedded programming with real hardware

InterruptLatency

● Latency is determined by:

– Instruction time (how long is the longest)

– How much of the context must be saved

– How much of the context must be restored

– The effort to implement priority scheme

– Time spend executing protected code

Page 57: Micro Controllers - Embedded programming with real hardware

bit glow_led;

void interrupt external_pin(void){

if (INTFLAG){

glow_led = 1;INTFLAG = 0;

}}

static void init_config(void){

init_external_interrupt();}

InterruptsExternal Interrupt - Example

void main(void){

init_config();

while (1){

while (!glow_led){

if (SWITCH == 1)

{glow_led =

1;}_asm;

NOP;NOP;NOP;

_endasm}if (glow_led){

LED = 0;}

}}

Page 58: Micro Controllers - Embedded programming with real hardware

Timers

Page 59: Micro Controllers - Embedded programming with real hardware

TimersIntroduction

● Resolution Register Width

● Tick Up Count or Down Count

● Quantum System Clock settings

● Scaling Pre or Post

● Modes

● Counter● PWM or Pulse Generator● PW or PP Measurement etc.,

● Examples

Page 60: Micro Controllers - Embedded programming with real hardware

TimersExample

● Requirement – 5 pulses of 8 µsecs

● Resolution – 8 Bit

● Quantum – 1 µsecs

● General

Page 61: Micro Controllers - Embedded programming with real hardware

TimersExample

Page 62: Micro Controllers - Embedded programming with real hardware

Clock I/O

Page 63: Micro Controllers - Embedded programming with real hardware

Clock I/OIntroduction

● Most peripheral devices depends on Clocking

● Controllers have to generate clock to communicate with the peripherals

● Some of the Controllers internal peripherals work on external clocking provided at it pins

Page 64: Micro Controllers - Embedded programming with real hardware

Clock I/OIntroduction

● The activity on the devices could be on

– Edges

– Levels

Page 65: Micro Controllers - Embedded programming with real hardware

Clock I/OPWM

● Sometimes called as PDM (Pulse Duration Modulation)

● A technique used to vary the active time vs inactive time in a fixed period.

● Mostly used to control the average voltage of the Load connected.

● Used in wide applications like Motor Controls, Lamp Dimmers, Audio, Power Controls and many more..

Page 66: Micro Controllers - Embedded programming with real hardware

Clock I/OPWM

Page 67: Micro Controllers - Embedded programming with real hardware

Analog Inputs

Page 68: Micro Controllers - Embedded programming with real hardware

Analog InputsIntroduction

● Very important peripheral in embedded systems for real time activities

● Multiplexed with normal GPIO

● Comes with different resolutions

Page 69: Micro Controllers - Embedded programming with real hardware

Analog InputsSAR

Page 70: Micro Controllers - Embedded programming with real hardware

Seven Segment Displays

Page 71: Micro Controllers - Embedded programming with real hardware

SSDIntroduction

● Array of LEDs connected internally

● Possible configurations are common cathode and common anode

● Very good line of sight

● Used in many applications

Page 72: Micro Controllers - Embedded programming with real hardware

SSDIntroduction

Segment Data – 10100100 & Control Line - C2Segment Data – 10010010 & Control Line - C3

Let's assume that we are using common anode display

Data map - hgfedcba

Circuit

Page 73: Micro Controllers - Embedded programming with real hardware

SSDExample

Segment Data – 10100100 & Control Line - C2

Page 74: Micro Controllers - Embedded programming with real hardware

SSDExample

Segment Data – 10010010 & Control Line - C3

Page 75: Micro Controllers - Embedded programming with real hardware

SSDExample

Page 76: Micro Controllers - Embedded programming with real hardware

SSDExample

Page 77: Micro Controllers - Embedded programming with real hardware

SSDExample

Page 78: Micro Controllers - Embedded programming with real hardware

SSDExample

Page 79: Micro Controllers - Embedded programming with real hardware

SSDCircuit on Board

Page 80: Micro Controllers - Embedded programming with real hardware

Character Liquid Crystal Display

Page 81: Micro Controllers - Embedded programming with real hardware

CLCDIntroduction

● Most commonly used display ASCII characters

● Some customization in symbols possible

● Communication Modes

– 8 Bit Mode

– 4 Bit Mode

Page 82: Micro Controllers - Embedded programming with real hardware

CLCDCircuit on Board

Page 83: Micro Controllers - Embedded programming with real hardware

Matrix Keypad

Page 84: Micro Controllers - Embedded programming with real hardware

Matrix KeypadIntroduction

● Used when the more number of user inputs is required and still want to save the some controller I/O lines

● Uses rows and columns concept

● Most commonly used in Telephonic, Calculators, Digital lockers and many more applications

Page 85: Micro Controllers - Embedded programming with real hardware

Matrix KeypadExample

Page 86: Micro Controllers - Embedded programming with real hardware

Data Storage

Page 87: Micro Controllers - Embedded programming with real hardware

● Mostly used in every Embedded System

● The size and component (memory) of storage depends upon the requirements

● Modern controllers has built in data storage

– EEPROM

– Data Flash etc.

Data StorageIntroduction

Page 88: Micro Controllers - Embedded programming with real hardware

Thank You