Sunteți pe pagina 1din 246

Mechatronics Workshop

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 1

Agenda
Microchip Technology Inc. Mechatronics PIC Microcontroller Basics
- Cycle

Labs
- Simple I/O - Timer 0 - A/D LCD - DC

Resources
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 2

Agenda
PIC Microcontroller PICMechatronics PIC Microcontroller Basics
- Cycle

Labs
- Simple I/O - Timer 0 - A/D LCD - DC

Resources
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 3

The PIC Microcontroller Family


256 128 Memory (kBytes) 64 32 16 8 4 2 1 6 8 14 18 28 Pins

dsPIC
Digital Signal Controller (DSC) 30 MIPs, Up to 48k Instructions, 84 pins

High Performance
10 MIPs, Up to 64k Instructions, 18-84 pins

Mid-Range
5 MIPs, Up to 8k Instructions, 8-64 pins

Baseline
5 MIPs, up to 2k Instructions, 6-40 pins

40

64

84
Slide 4

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Migration
14 - Pin 20 - Pin 8 - Pin

Pin / Code Peripheral

VDD RA5 RA4 RA3 RC5 RC4

VSS RA0 RA1 RA2 RC0 RC1 RC2 RB4 RB5 RB6

Seamless Migration across more than 200 Products


2005 Microchip Technology Incorporated. All Rights Reserved.

RC3 RC6 RC7 RB7

Mechatronics Workshop-in-a-Box

Slide 5

Complete Signal Chain Solutions from Microchip


Digital Pot Op Amp

Sensor

Mux Ref

Filter

A/D

PIC
microcontroller

Performance: Low Power, High Precision Low Cost: System Cost, Low Component count Size: Small Packaging SC70, SOT23, DFN

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 6

Analog & Interface Products


Thermal Management
Temperature Sensors Fan Speed Controllers Fan Fault Detectors

Power Management
Linear Regulators Switching Regulators/Controllers Charge Pump DC/DC Converters

Mixed Signal
SAR/Delta-Sigma A/D Converters Dual Slope A/D Converters Display A/D Converters System D/A Converters V/F and F/V Converters Digital Potentiometers

Interface Products
CAN Peripherals Infrared Peripherals LIN Transceivers Serial Peripherals

Linear Products
Single Supply CMOS Op Amps Comparators Linear Integrated Devices Programmable Gain Amplifiers

Voltage References CPU/System Supervisors Voltage Detectors Power MOSFET Drivers Battery Management PWM Controller

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 7

Overview of Memory Products


Wide Operating Voltage 1.8V 5.5V High Density 128 1 Mbits

Supports Major Busses Microwire, I2C, SPI

Microchip Serial EEPROM Products


High Endurance 1M Erase/Write Cycles

Small Packaging DFN, SOT-23 TSSOP, MSOP

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 8

Proven Quality Record across all Product Families ISO9001 Certification QS9000 Certification Quality Awards from numerous Fortune 100 customers PPM levels consistently below 10 Field Failures for PIC microcontrollers virtually non-existent
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 9

Agenda
PIC Microcontroller PICMechatronics PIC Microcontroller Basics
- Cycle

Labs
- Simple I/O - Timer 0 - A/D LCD - DC

Resources
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 10

Mechatronics is:
Implementing electronic controls in a mechanical system Enhancing existing mechanical designs with intelligent controls Replacing mechanical components with an electronic solution A perfect system for a PIC microcontroller!!
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 11

1: Flashlight Switch Traditional Flashlight: (On / Off ) Hybrid Flashlights: Mode selector

White LEDs

Xenon Bulb

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 12

Intelligent Switch
Xenon Bulb

Push Button/ Tactile Switch

PIC Microcontroller

LED

Modes: Xenon bulb on LEDs on (dim) LEDs on (bright)


2005 Microchip Technology Incorporated. All Rights Reserved.

Strobe LEDs Flashlight off

Mechatronics Workshop-in-a-Box

Slide 13

Switch
Mechanical Hybrid Control Current Control Programmable Low Battery Detect Strobe Capable Adaptable
2005 Microchip Technology Incorporated. All Rights Reserved.

Intelligent Yes Yes Yes Yes Yes YES!


Slide 14

No No No No No NO!
Mechatronics Workshop-in-a-Box

Possible Solution: PIC10F20x


SOT-23 package 6 pins:
- 3 Input/Output pins - 1 Input only - 1 Power, 1 Ground

4 MHz internal oscillator


- 2% accuracy over voltage and temperature

1 Analog Comparator
- internal voltage reference

Actual Size

Ultra-low sleep current 8-bit timer 2V to 5V operating range


2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 15

2 : Household Thermostat

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 16

Example Application: Thermostat


Desired Temperature

Dial
60 50 40 70 80 80 70 60 50 40

Current Temperature

To Heating Unit

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 17

Example Application: Thermostat


Desired Temperature
60 50 40 70 80 80 70 60 50 40

Current Temperature

Sensor/Switch

To Heating Unit

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 18

Thermostat
60 50 40 70 80

User Feedback

Temperature Sensor

80 70 60 50 40

User Feedback

Switch to turn on Heater

User Input

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 19

Mechatronic
LCD
User Input
Tactile Switches

User Feedback

Temperature Sensor
Small IC

Control
PIC Microcontroller

Switch to turn on Heater


MOSFET Regulator

Power

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 20


Current Temperature Desired Temperature

74oF

70oF

Higher resolution and accuracy Reduces household heating costs Self calibrating Flexible Design Environmentally Friendly
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 21

Possible Solution: PIC16F917


PIC16F917
10-bit ADC LCD Module Internal Oscillator FLASH EEPROM

CCPs

I2C/SSP Comparators

AUSART

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 22

Temperature Sensing Options from Microchip


Temperature Sensors

Logic Output TC6501/2/3/4 TC620/1/2/3/4

Voltage Output TC1046 TC1047/47A MCP9700/1

Serial Output

SMBus/I2C TC74 TCN75A MCP9800/1/2/3

SPI TC72 TC77

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 23

Temperature Sensing Options from Microchip


Temperature Sensors

Logic Output TC6501/2/3/4 TC620/1/2/3/4

Voltage Output TC1046 TC1047/47A MCP9700/1

Serial Output

SMBus/I2C TC74 TCN75A MCP9800/1/2/3

SPI TC72 TC77

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 24

Linear Output TC1047A Temperature Sensor


Linear Slope
- 10 mV/C
2 1.8 1.6

-40 to 125C Temperature Range 2C Accurate Low Current Consumption Small Package
- SOT-23

Voltage Out (V)

1.4 1.2 1 0.8 0.6 0.4 0.2 0 -40 -20 0 20 40 60 80 100 120

Temperature (deg C)

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 25

3: Washing Machine

User interface

Sensors (door closed, water level, balance sensor) AC Induction Motor Control
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 26

User Interface Improvements

Use standard dials and knobs Same knobs used on different models Reprogramming microcontroller gives knobs different functionality Switches less prone to wear compared to mechanical washing machine
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 27

Benefits of Modern Sensors


*: Door **: Door Balance **: *: Benefits:

** (for people and the machine)


2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 28

ACIM / BLDC Motor Control


Microcontroller : (more speeds possible)

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 29

Possible Solution: PIC18F4431


PWM Output: 8
- 14-bit resolution - Edge and center-aligned - Programmable dead-time

3-ch QEI(Quadrature Encoder Interface) 10-bit A/D Converter Created from the ground up for precision motor control!
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 30

Microchip MOSFET Driver Solutions


Power MOSFET Drivers 0.5A Peak Output
TC1410/N single

1.5A Peak Output Single


TC4403 floating load TC4626/7 voltage boost TC4431/2 30V high/low

2A Peak Output
TC1412/N single

6A Peak Output
TC429 TC4420/9 single

1A Peak Output
TC1411/N single

3A Peak Output
TC1413/N single TC4423/4/5 dual

9A Peak Output
TC4421/2 single

1.2A Peak Output


TC1426/7/8 dual TC4467/8/9 quad

Dual
TC4x6/7/8 Enhanced TC442xA matched delay TC4404/5 split out, open drain

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 31

Mechatronics Review
Mechatronics is _________________. What are the benefits to mechatronics?
- How does the consumer benefit? - How does the manufacturer benefit?

What is the worlds smallest microcontroller? What microcontroller can directly drive a LCD?
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 32

Agenda
PIC Microcontroller PICMechatronics PIC Microcontroller Basics
- Cycle

Labs
- Simple I/O - Timer 0 - A/D LCD - DC

Resources
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 33

PIC Microcontroller Basics


We will learn how to: Create code Compile Test and debug Use MPLAB IDE (Integrated Development Environment) . . . all with a focus on learning about PIC microcontroller architecture and terms
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 34

What is MPLAB IDE?


MPLAB IDE : Microchips PC Software Embedded System Software Class Tool

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 35

Embedded Software Design Cycle

Build Code

Run Code

Write Code

Debug Code

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 36

Agenda
PIC Microcontroller PICMechatronics PIC Microcontroller Basics
- Cycle Cycle #1

Labs
- Simple I/O - Timer 0 - A/D LCD - DC

Resources
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 37

Architecture: Overview

Program Memory

Data Memory

Special Features

Input / Output Ports

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 38

Architecture: Overview

Program Memory

Data Memory

Special Features

I/O Ports

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 39

Architecture: PIC16F917

Program Memory

Hard Drive

14 bits wide 8192 words

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 40

Architecture: PIC16F917

Floppy Disk

Data Memory

8 bits wide 512 bytes

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 41

Hands-on with MPLAB IDE

MPLAB IDE

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 42

Configure Select Device

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 43

Selecting PIC16F917

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 44

Program Memory View

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 45

Program Memory View

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 46

Hexadecimal to Binary

3FFF

11111111111111

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 47

Data Memory (File) View

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 48

Data Memory (File) View

8 bits
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 49

Special Function Registers

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 50

Separate Program & Data

Program Memory

Data Memory

Program Data Why separate?

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 51

Cycle #1: What Did I Learn?


I learned how to select my target device Program memory is ___ bits by ___ words Data memory is ___ bits by ___ words A file register is just another name for ___ Program and data memory are separate Certain file registers are special function
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 52

Agenda
PIC Microcontroller PICMechatronics PIC Microcontroller Basics
- Cycle Cycle #2

Labs
- Simple I/O - Timer 0 - A/D LCD - DC

Resources
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 53

Architecture: Overview

Program Memory

Data Memory

Special Features

I/O Ports

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 54

Architecture: W Register

W register
- 8-bit Special Features -

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 55

Hands-on with MPLAB IDE

MPLAB IDE

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 56

MPLAB Simulator

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 57

Creating My First Program File New


4 :

addlw 1 movwf 0x20 goto 0 end


2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 58

Creating Your First Program File


-

Save As
c:\???\h1.asm

Project

Quickbuild h1.asm

Output Window
- BUILD SUCCEEDED
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 59

Running Your First Program

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 60

Adding a Watch Window

View

Watch

0x20

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 61

Cycle #2 : Hitech C Compiler


#include <pic.h> counter @ 0x20;

volatile char

main() { while(1) { counter++; } }


2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 62

Cycle #2 : CCS-C Compiler


Click on Project Project Wizard Enter the source C code below:
#include <16F917.h> int1 bit_1; int8 byte_1; int16 word_1; short bit_2; short bit_3; int byte_2; long word_2; #zero_ram // device header file // 1bit variable // 8bit variable // 16bit variable // 1bit variable // 1bit variable // 8bit variable // 16bit variable // ram clear

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 63

Cycle #2 Project : CCS-C


// continue

void main(void) { while(1) { byte_1++; word_1++; bit_1++; bit_2++; bit_3++; } }

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 64

Cycle #2: What Did I Learn?


W Register Create a .asm file and Project for Hi-Tech C Quickbuild MPLAB SIM step, animate Watch window Status bar Three instructions One assembler directive
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 65

Agenda
PIC Microcontroller PICMechatronics PIC Microcontroller Basics
- Cycle Cycle #3

Labs
- Simple I/O - Timer 0 - A/D LCD - DC

Resources
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 66

Architecture: Overview

Program Memory

Data Memory

Special Features

I/O Ports

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 67

I/O Ports: PORTA Register

PORTA

PORTA 0x87

RA7
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box

RA0
Slide 68

I/O Ports: PORTA Register

PORTA

1 0 0 0 0 1 1 1

5V 0V
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 69

RA7

RA0

I/O Ports: TRISA Register

TRISA

TRISA 0x16

RA7
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box

RA0
Slide 70

I/O Ports: TRISA Register

TRISA

0 0 0 1 0 1 1 0

RA7
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box

RA0
Slide 71

I/O Ports: Port D


PORTD
- 8-bit I/O port - data memory 0x08

TRISD
- 8-bit direction control register - data memory 0x88
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 72

Hands-on with MPLAB IDE

MPLAB IDE

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 73

Code Round 3
Editor Windows Code
movlw b00000000 movwf 0x08 movlw b11111111 movwf 0x08 goto 0 end

Quickbuild Watch Windows


2005 Microchip Technology Incorporated. All Rights Reserved.

address 0x08
Mechatronics Workshop-in-a-Box Slide 74

Stepping Through the Code


Window Tile Horizontally

Single Step : F7

movlw movwf movlw movwf


goto 0
2005 Microchip Technology Incorporated. All Rights Reserved.

b00000000 0x08 b11111111 0x08


Slide 75

Mechatronics Workshop-in-a-Box

PORTD Direction Control


TRISD (0x88) Watch Windows PORTD
- (Input) !!!

TRISD
- All Outputs : 0x00

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 76

TRISD - Direction Control


line

clrf 0x88 ; clear TRISD


Build, single-step TRISD clear ?

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 77

Why doesnt TRISD clear?


Lets revisit data memory

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 78

Architecture: Overview

Program Memory

Data Memory

Special Features

I/O Ports

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 79

Accessing Data Memory

Data Memory

clrf 0x88

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 80

Architecture: Data Memory

Address

Data Memory

clrf 0x88

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 81

Architecture: Data Memory


2 Data Memory

3<6:5>
STATUS<RP1:RP0>

Address

clrf b10001000

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 82

Architecture: Data Memory


3,6 3,5

00

01

10

11

Bank 0 Bank 1 Bank 2 Bank 3 0-7F 80-FF 100-17F 180-1FF 00xxxxxxx 01xxxxxxx 10xxxxxxx 11xxxxxxx

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 83

Architecture: Data Memory

00

01
88

10

11

Bank 0 Bank 1 Bank 2 Bank 3 0-7F 80-FF 100-17F 180-1FF 00xxxxxxx 01xxxxxxx 10xxxxxxx 11xxxxxxx

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 84

Architecture: Bank 1 Select

00

01 bsf 3,5

Bank 0 Bank 1 0-7F 80-FF 00xxxxxxx 01xxxxxxx

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 85

Architecture: TRISD

00

01
88

Bank 0 Bank 1 0-7F 80-FF 00xxxxxxx 01xxxxxxx

bsf 3,5 clrf 0x88

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 86

Architecture: Bank 0 Select

00
8

01 bsf 3,5 clrf 0x88 ????

Bank 0 Bank 1 0-7F 80-FF 00xxxxxxx 01xxxxxxx

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 87

TRISD - Direction Control


Our final code

bsf 3,5 clrf 0x88 bcf 3,5 movlw b00000000 movwf 0x08 movlw b11111111 movwf 0x08 goto 3
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 88

Symbol Name vs File Register


include <p16f917.inc> bsf STATUS,RP0 clrf TRISD bcf STATUS,RP0 Loop movlw 0 movwf PORTD movlw 0xff movwf PORTD goto Loop
Mechatronics Workshop-in-a-Box Slide 89

bsf 3,5 clrf 8 bcf 3,5 movlw 0 movwf 8 movlw 0xff movwf 8 goto 3
2005 Microchip Technology Incorporated. All Rights Reserved.

Cycle #3 : Hitech C Compiler


#include <pic.h> main() { while(1) { TRISD = 0x00; PORTD = 0x00; PORTD = 0xFF; } }
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 90

Cycle #3 : Hitech C Compiler

How to add delay( ) ???

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 91

Cycle #3 Project : CCS-C


#include <16F917.h> void main(void) { set_tris_d(0x00); while(1) { output_d(0x00); output_d(0xff); } } // device header file

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 92

Cycle #3 Project : CCS-C How to add delay( ) ???


#include <16F917.h> // device header file #fuses NOWDT, INTRC #use delay(clock = 8000000) void main(void) { setup_oscillator(OSC_8MHZ); set_tris_d(0x00); while(1) { output_d(0x00); delay_ms(500); output_d(0xff); delay_ms(500); } }
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 93

// delay_us(x), delay_cycles(x)

Cycle #3: What Did I Learn?


PORTx Registers TRISx Registers Banking STATUS register, bits RP0 & RP1 Symbol name and number correlation Four instructions One assembler directive
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 94

Agenda
PIC Microcontroller PICMechatronics PIC Microcontroller Basics
- Cycle Cycle #4

Labs
- Simple I/O - Timer 0 - A/D LCD - DC

Resources
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 95

Architecture: Overview

Program Memory

Data Memory

Special Features

I/O Ports

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 96

PICDEM Mechatronics Board

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 97

Hardware

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 98

ICD 2 Power

J9

J11
ICD2

USB

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 99

MPLAB ICD 2
MPLAB ICD 2
- In-circuit Debugger and Programmer

Debug mode:
- Program - Data Memory - Code Step - Break Point

Program mode:
- Target Device
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 100

Hands-on with MPLAB IDE

MPLAB IDE

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 101

MPLAB IDE Project

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 102

MPLAB IDE Project


PIC16F917 Next Microchip MPASM Toolsuite Next Project test C:\xxx Next h1.asm project Next Finish
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 103

Save Workspace

Save the Workspace as test in C:\

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 104

Opening Source Files


Project window
- source, include, linker

Open
-


- Source Files


- Remove
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 105

ICD 2 Setup
Debugger Select Tool MPLAB ICD2 Debugger Connect Configuration Configuration Bits

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 106

Cycle #4: Source Code


include bsf clrf bsf bcf Loop btfsc goto incf goto <p16f917.inc> STATUS,RP0 TRISD TRISA,4 STATUS,RP0 PORTA,4 Loop PORTD,f Loop
Mechatronics Workshop-in-a-Box Slide 107

2005 Microchip Technology Incorporated. All Rights Reserved.

Cycle #4: Hitech C Compiler


#include <pic.h> main() { TRISD = 0x00; TRISA = 0x10; while(1) { if(RA4 == 0) { PORTD ++; } } }

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 108

Cycle #4: CCS-C Compiler


#include <16F917.h> // device header file #fuses NOWDT, INTRC #use delay(clock = 8000000)

int8 counter; int1 SW2; #zero_ram void main(void) { setup_oscillator(OSC_8MHZ); set_tris_d(0x00); output_d(0x00); set_tris_a(PIN_A4); // RA4 : input

while(1) { SW2 = input(PIN_A4); if (SW2 == 0){ output_d(counter++); delay_ms(100); } } }

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 109

Building and Programming


Project Build All (Ctrl + F10)
(Do this now!)

PIC16F917 Device :

>> << build & program


2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 110


Watch window PORTD PORTA Animate SW2 ?

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 111

Cycle #4: What Did I Learn?


PICDEM Mechatronics Board MPLAB ICD 2 Project Workspace Build PIC16F917 MPLAB ICD 2

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 112

Agenda
PIC Microcontroller PICMechatronics PIC Microcontroller Basics
- Cycle

Labs
- Simple I/O - Timer 0 - A/D LCD - DC

Resources
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 113

Lab
Problem: Management wants you to design a cooling fan for an electronics bay in the new Mars Rover

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 114

Agenda
PIC Microcontroller PICMechatronics PIC Microcontroller Basics
- Cycle

Labs
- Simple I/O - Timer 0 - A/D LCD - DC LAB 1

Resources
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 115

Lab 1: Design Objectives


Thermal breaker Breaker Trip Turn on the Fan Breaker resets wait 2 seconds Turn off the Fan

Thermal Breaker
2005 Microchip Technology Incorporated. All Rights Reserved.

PIC16F917 Fan
Mechatronics Workshop-in-a-Box Slide 116

Lab 1: Hardware
Thermal Breaker Tact Switch Cooling Fan Brushed DC Motor PIC16F917
SW2

Tactile Switch

PIC16F917
Mechatronics Workshop-in-a-Box

Brushed DC motor
Slide 117

2005 Microchip Technology Incorporated. All Rights Reserved.

Lab 1: Schematic
+5V +5V R1 RA4 SW2 RD7 MOSFET N2 Motor

PIC16F917 D0

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 118

Lab 1: Connection Diagram

RD7,D0 RD7,N2 RA4,SW2 Vdd,P1

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 119

Lab 1: Connection Diagram

JP8: +5Vdc

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 120

Timer 0
8-bit : 0~255 counting...!!! Overflow Flag: INTCON<T0IF>
Example: Timer 0
11111110 11111111 00000000
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box

INTCON<T0IF>
0 0 1
Slide 121

Timer 0 Prescaler
(Tcy) Prescaler P , P x Tcy Prescaler: OPTION_REG<2:0>

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 122

Timer 0 Hands-On
include Counter Loop btfss goto bcf incf goto end <p16f917.inc> equ 0x20 INTCON,T0IF Loop INTCON,T0IF Counter,f Loop
Mechatronics Workshop-in-a-Box Slide 123

Create in h1.asm in the test workspace

2005 Microchip Technology Incorporated. All Rights Reserved.

Animate Step Interval


Debugger Debugger Select Tool Settings 10 ms MPLAB SIM

Animation/Realtime Updates Animate Step Time

10
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 124

Build and Watch


Build All the project : Ctrl + F10 Watch Window TMR0, OPTION_REG, Counter OPTION_REG 0
- prescaler 1:2 , - Timer 0 2 Tcy

Debugger

Animate
Mechatronics Workshop-in-a-Box Slide 125

2005 Microchip Technology Incorporated. All Rights Reserved.

Watch Window

OPTION_REG 0~7 ...


2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 126

Timer 0 Tcy( )
OPTION_REG Timer 0 <2:0> 000 (0) 2 001 (1) 4 010 (2) 8 011 (3) 16 100 (4) 32 101 (5) 64 110 (6) 128 111 (7) 256
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box

Rollover (4MHz) 512 s 1.02 ms 2.05 ms 4.10 ms 8.20 ms 16.4 ms 32.8 ms 65.5 ms
Slide 127

Relationship between Timer 0 and Instruction Cycle time


OPTION_REG bits 0 - 2 Instructions per one Timer 0 count 0 2 1 4 2 8 2 Timer 0 3 16 Overflow ? 4 32 5 64 6 128 7 256
Mechatronics Workshop-in-a-Box

Rollover (4MHz) 512 s 1.02 ms 2.05 ms 4.10 ms 8.20 ms 16.4 ms 32.8 ms 65.5 ms
Slide 128

2005 Microchip Technology Incorporated. All Rights Reserved.

decfsz
clrf TMR0 movlw .30 movwf Counter Loop btfss INTCON,T0IF goto Loop bcf INTCON,T0IF decfsz Counter,f goto Loop end
2005 Microchip Technology Incorporated. All Rights Reserved.

Enter this code after: Counter equ 0x20

Mechatronics Workshop-in-a-Box

Slide 129

Simulator (Processor Freq.) Debugger Settings Osc/Trace Tab Processor Frequency 4 MHz

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 130

The MPLAB Stopwatch


Debugger Stopwatch

Build the Project Watch Windows


- OPTION_REG 7

Simulator (Run)

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 131

The MPLAB Stopwatch

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 132

Timer 0 Review
Timer 0 is an ____ - bit Timer. The prescaler for Timer 0 is in the _______ register. Increasing the prescaler speeds up or slows down Timer 0? The ______ instruction is great for counting down the number of Timer 0 overflows.

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 133

Lab 1: Design Objectives Review


SW2 (Thermal Breaker) SW2 , turn on the DC motor (Fan) SW2 , 2 turn off the motor (Fan)

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 134

Program States

Initialize State 1

State 2
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box

State 3
Slide 135

Lab 1: Your Task


Initialize I/O and TMR0

Initialize State 1
Motor Off, Check for Button Pressed

State 2
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box

Motor On, Check for Button Not Pressed

State 3
Slide 136

Wait 2 Seconds

State 1: Wait for Button Press


Turn off Motor

PORTD,7 = 0

Is SW2 Pressed?

No

PORTA,4
Yes

State 2

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 137

State 2: Wait for Button Release


Turn on Motor

PORTD,7 = 1

Is SW2 Released?

No

PORTA,4
Yes

State 3

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 138

State 3: Two Second Delay


You just wrote this code!

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 139

Code for LAB1 (Hitech C)


#include #define sw2 #define motor #define pressed #define released volatile char main() { //initial TRISA = 0x10; TRISD = 0x00; motor = 0; OPTION = 0x07; while(1) { //State 1 motor = 0; while(sw2 == released); //State 2 while(sw2 == pressed) { motor = 1; } //State 3 TMR0 = 0; counter = 30; while(counter!=0) { if(T0IF == 1) { T0IF = 0; counter--; if(counter==0) } } } } 2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 140 // RA4 : input // PORTD : output <pic.h> RA4 RD7 0 1 counter @ 0x20;

// TMR0 Prescaler : 1:256 , 65.53ms T0IF

break;

Code for LAB1 (CCS-C)


#include <16F917.h> #fuses NOWDT, INTRC #use delay(clock = 8000000) #zero_ram #BIT T0IF = 0x0B.2 int8 counter_32ms; int1 SW2; void main(void) { setup_oscillator(OSC_8MHZ); set_tris_d(0x00); output_d(0x00); set_tris_a(0xFF); setup_timer_0(RTCC_DIV_256); while(1) { // State 1 : Motor Off output_low(PIN_D7); while ( input(PIN_A4) == 1 ); // State 2 : SW2 Check output_high(PIN_D7); while ( input(PIN_A4) == 0 ); counter_32ms = 0; // State 3 : 2sec Check while (counter_32ms < 60) { while ( !T0IF ); counter_32ms++; T0IF = 0; } } } // T0IF // device header file

// RA4 : input

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 141

Lab 1: Review
PIC16F917
- Timer 0 (TMR0) - OPTION_REG

Tools
- Stopwatch - Simulator: animate interval, processor frequency

- decfsz

Concepts
- Programming with states
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 142

Agenda
PIC Microcontroller PICMechatronics PIC Microcontroller Basics
- Cycle

Labs
- Simple I/O - Timer 0 - A/D LCD - DC

LAB 2

Resources
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 143

Lab 2: Design Objectives


Analog If Temperature > 40oC Turn on the fan If Temperature =< 40oC Turn off the fan Display

Temperature Sensor
2005 Microchip Technology Incorporated. All Rights Reserved.

PIC16F917 Fan
Mechatronics Workshop-in-a-Box Slide 144

Lab 2: Hardware
--> Brushed DC motor Fan LCD A/D

POT1

Potentiometer

LCD
Mechatronics Workshop-in-a-Box

Brushed DC motor
Slide 145

2005 Microchip Technology Incorporated. All Rights Reserved.

Lab 2: Schematic
+5V +5V Motor POT1 RA0 RD7 MOSFET N2

PIC16F917 D0

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 146

Lab 2: Connection Diagram

RA0,POT1

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 147

Analog-To-Digital Converter
analog binary number (Resolution)
- How many different readings are possible - 8-bit : 28 256 possible readings - 10-bit : 210 1024 possible readings

: 5V 8 ?
Voltage resolution = 5V/256 = 19.5 mV per count

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 148

ADC Registers
ANSEL
- analog pins

ADCON1
- conversion clock

ADCON0
- analog channel, turn the module on, do the conversion

ADRESL
- Low-byte of conversion value

ADRESH
- High-byte of conversion value

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 149

A/D Conversion Steps


1. Turn on the analog module 2. ADCON0<GODONE> = 1 3. ADCON0<GODONE> = 0 4. Read A/D
ADRESH
Mechatronics Workshop-in-a-Box Slide 150

2005 Microchip Technology Incorporated. All Rights Reserved.

ADC Hands-On(Assembler)
include <p16f917.inc> bsf ADCON0,ADON Create in h1.asm in LoopOuter the test workspace bsf ADCON0,GO_DONE LoopInner btfsc ADCON0,GO_DONE goto LoopInner movf ADRESH,w goto LoopOuter end
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 151

#include static char static char

<pic.h> ad_value_hi; ad_value_lo;

main() { TRISD = 0x00; TRISA = 0x01; // initial A/D channel 0 ANSEL = 0x01; ADFM = 0; VCFG1 = 0; VCFG0 = 0; CHS2 = 0; CHS1 = 0; CHS0 = 0; ADGO = 0; ADON = 1; //ADCON0 ADCS2 = 1; ADCS1 = 1; ADCS0 = 1; //ADCON1

//RA0 Port A/D Channel 0 : input = high //A/D Conevrting Port Select : bit 0 = high //ADRESH : 8bit + ADRESL : 2 bit = xxxxxxxx xx000000 //If ADFM = 0, ADRESH : 2bit + ADRESL : 8 bit = 000000xx xxxxxxxx //Low Voltage refences = VSS //High Voltage refences = VDD //CHS[2:0] = "000", AD0 select

//A/D Conversion Start and Check this bit, if done,ADGO => 0 //A/D Conversion Operating = 0b10000001; //Setting ADCON1 Register //A/D Converting Clock Source Select : 000 = Internal RC

= 0b01110000;

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 152

while(1) { //AD Converter Start ADGO = 1; //AD End Check while(ADGO)continue; //Read AD Converting Value ad_value_hi = ADRESH; ad_value_lo = ADRESL; if(ad_value_hi > 0x30) RD7 = 1; else RD7 = 0; } }
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 153

Code for LAB2 (CCS-C)


#include <16F917.h> #DEVICE *=16 ADC=10 #FUSES NOWDT, INTRC #USE delay(clock = 8000000) #ZERO_RAM #BIT T0IF = 0x0B.2 #BIT ADFM = 0x1F.7 #BIT GODONE = 0x1F.1 #BYTE #BYTE #BYTE #BYTE ADCON0 ADCON1 ADRESH ADRESL = = = = 0x1F 0x9F 0x1E 0x9E // T0IF // device header file

int16 ad_value[3]; unsigned char i;

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 154

Code for LAB2 (CCS-C)


void main(void) { setup_oscillator(OSC_8MHZ); set_tris_d(0x00); output_d(0x00); set_tris_a(0xFF); // Setup Timer0 setup_timer_0(RTCC_DIV_256); // Setup ADC setup_adc(ADC_CLOCK_DIV_32 ); setup_adc_ports( sAN0 | VSS_VDD ); while(1) { for (i=0; i<3; i++) { set_adc_channel(i); delay_us(10); ad_value[i] = read_adc(); } if ( ad_value[1] > 300 ) output_high(PIN_D7); else output_low(PIN_D7); delay_ms(10); } }

// RA4 : input

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 155

Looking at the ADC module


Debugger MPLAB ICD 2 watch window ADRESH Build the project Program Click animate What happens to ADRESH when you turn POT1?
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 156

ADC Module Review


The analog to digital module converts an ________ into a ________ . The ________ register is used to turn the module on and start an A-to-D conversion. The ________ register contains the high order bits of the ADC value. You must wait for the ________ bit to clear before reading the ADC value.

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 157

Displaying Values on the LCD


PIC16F917 LCD Module LCD 3 digit display LCD LCD linkable files

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 158

Linking Files in a Project Linking :


- - Code - Code

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 159

call Instruction
Loop call movlw movwf goto TurnOnRA4 bsf return
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box

TurnOnRA4 0x78 PORTD Loop

PORTA,4

Slide 160

call Instruction
Loop call movlw movwf goto TurnOnRA4 bsf return
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box

TurnOnRA4 0x78 PORTD Loop

PORTA,4

Slide 161

DisplayHex and DisplayDecimal Functions


Example 1: movlw 0xFF call DisplayDecimal

Example 2: movlw 0xFF call DisplayHex

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 162

LCD Hands-on
File Open Workspace
- c:\Mechatronics WIB\Lab2\Lab2.mcw

The code from the last Hands-on is duplicated in the Lab2.asm file After the line: movf ADRESH, W Call one of the following: DisplayDecimal or DisplayHex Build and animate the code using the ICD 2
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 163

Experimentation
Light Sensor
- Move the jumper wire from POT1 to LIGHT - Vary the light to the light sensor

Temperature Sensor
- Try moving the same wire to TEMP - Breath heavily on the temperature sensor
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 164

Comparing Two Numbers


Recall these design objectives:
- Turn the motor (fan) on when the result of the ADC (temperature) is greater than 40 - Turn off the motor when the result of the ADC is equal to or less than 40

This requires comparing two numbers Solution: Use the sublw instruction

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 165

sublw Instruction
Subtracts W from a literal and places the result in W Carry and Zero STATUS flags affected Example: (Setpoint Temp) movlw Temp sublw Setpoint Relation Setpoint > Temp Setpoint = Temp Setpoint < Temp
2005 Microchip Technology Incorporated. All Rights Reserved.

Result STATUS,Z STATUS,C + 0 Mechatronics Workshop-in-a-Box

0 1 0

1 1 0
Slide 166

sublw Instruction
What is the result of the following operation? movlw sublw 4 5

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 167

Using sublw for Greater Than


Problem: You want to find out if the value in ADRESH is greater than 40. Solution:
1. Move the value in ADRESH to W 2. Subtract W from .40 3. Check the Carry flag
If clear, then the value is greater than 40 If set, then the value is less than or equal to 40
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 168

Lab 2 State Diagram


Read ADC

Display ADC Value

Turn on Motor

Compare ADC Value

Turn off Motor

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 169

Lab 2 State Diagram


Read ADC

Display ADC Value

Turn on Motor

Compare ADC Value

Turn off Motor

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 170

Lab 2: Implement a Solution


Add to your existing code for Lab2 Use sublw to test whether the ADC value is 40 or greater Check the STATUS,C flag If clear, turn on the motor If set, turn off the motor

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 171

Lab 2 Review
New PIC16F917 Peripherals
- Analog to Digital Conversion Module - LCD Module

New Tools
- MPLAB Linker

New Instructions
- call and return - sublw

New Concepts
- Comparing two numbers
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 172

Agenda
PIC Microcontroller PICMechatronics PIC Microcontroller Basics
- Cycle

Labs
- Simple I/O - Timer 0 - A/D LCD - DC

LAB 3

Resources
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 173

Lab 3: Design Objectives


Analog Fan

Temperature Sensor
2005 Microchip Technology Incorporated. All Rights Reserved.

PIC16F917 Fan
Mechatronics Workshop-in-a-Box Slide 174

Lab 3: Schematic
+5V +5V Motor POT1 RA0 RD2 MOSFET N2

PIC16F917 D0

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 175

Lab 3: Connection Diagram

RD2,D0 RD2,N2

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 176

Brushed DC Motor Speed Control


1000 Motor Speed (RPM) 180 0V
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box

5V

Average Voltage
Slide 177

Pulse Width Modulation


Pulse Width

Period Pulse Width Duty Cycle = Period


2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 178

PWM and Average Voltage


VP A

A is the area

VAVG = Duty Cycle x VP VAVG A

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 179

Frequency

Lower

20 Hz Audible Range

Higher

20 kHz

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 180

PWM Hands-On

Experimenting with Duty Cycle

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 181

PWM Hands-On
include bsf bcf bcf PWMLoop bsf nop nop . . bcf goto end <p16f917.inc> STATUS,RP0 Create in h1.asm in TRISD,2 the test workspace STATUS,RP0 PORTD,2 Copy these three lines and paste here 6 times

PORTD,2 PWMLoop
Mechatronics Workshop-in-a-Box Slide 182

2005 Microchip Technology Incorporated. All Rights Reserved.

Simulating the PWM Loop


1. Choose MPLAB SIM 2. Build the project

3. Set a Breakpoint at goto PWMLOOP

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 183

Set Up the Logic Analyzer


1. Choose View - Simulator Logic Analyzer 2. Select Channel and Add Signals 3. Choose RD2 in the available Signals Box and click Add => 4. Run the simulator (press F9) 5. Run again (press F9) 6. And again (press F9)
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 184

Simulate using the Logic Analyzer

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 185

Change the Duty Cycle


Starting from the bottom change bsf instructions to bcf one at a time Build the project and simulate between changes
Do you see the change in duty cycle?

Remove the breakpoint Switch to the ICD 2 as the debugger Do the same experiment as above
How does the motor behave to bsf changes?
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 186

PWM Review
PWM stands for _______ _______ ______. PWM signals have a fixed _______. The duty cycle is the ratio between _______ and ________. The human ear can hear frequencies below ____ kHz. Which frequency is higher (right or left)?

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 187

Source Code Generated PWM Drawbacks Processor intensive PWM Loop must be continually executed Little time to accomplish other tasks Possible Solutions: 1. Slow down the PWM 2. Decrease PWM resolution 3. Use MCU with PWM peripheral
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 188

Capture, Compare, PWM (CCP) Module CCP H/W PWM PWM Timer 2 10-bits
- 1024

Internal OSC 8Mhz


- 31.2 kHz

CCP2 RD2 Multiplex


2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 189

CCP2 Registers
CCP2CON
- Select PWM mode, 2 LSB of Duty Cycle

CCPR2L
- 8 MSB of Duty Cycle

PR2
- Frequency select register

T2CON
- Turn on Timer 2

TRISD
- Make CCP2 pin an output
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 190

PWM BLOCK
CCPR2L : 8bit + CCP2CON[5:4] = 10bit Resolution MCU Clock : 8MHz => 8MHz / 1024 = 7.8KHz

PR2 8bit 10bit 8bit PR2 = 0xFF MCU Clock : 8MHz => (8MHz/4)/ (255+1) = 7.8KHz PR2 = 0x3F => 8MHz/4 / (63+1) = 31.2KHz

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 191

Lab 3: Using the CCP


V

ADC
t

ADRESH

CCPR2L

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 192

Lab 3 State Diagram

Read ADC

Display ADC Value

Set CCP2 Duty Cycle

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 193

Lab 3: Implement a Solution


Open the Lab3 workspace in: c:\Mechatronics WIB\Lab3 All I/O ports and modules (including the CCP) are initialized for you Copy the first two states in Lab 2
- Read ADC - Display ADC Value

Move result of the analog-to-digital conversion into CCPR2L


2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 194

Motor Whine

Question: Why does the motor whine?

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 195

QuietPWM Function
Generates a 31.2 kHz signal using the 8 MHz internal oscillator Has 8-bits of resolution How to use:
- Move duty cycle value in W - Call QuietPWM

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 196

Code for LAB3 (Hitech C)


#include #include #define #define #define <pic.h> "lcd.h" Motor_PWM on off RD2 1 0

static char pwm_value,temp; main() { // initial general GIE=0; TRISA = 0x01; TRISD = 0x00; OSCCON = 0x70; PR2

//grobal interrupt disable //RA0 Port A/D Channel 0 : input = high //RD2 Port Motor PWM Output : Low //8MHz Internal Clock Select : IRCF[2:0] = 111 : 8MHZ = 0x3F; // Timer2 Setting Value : 31.2KHz PWM Select // 8MHz / 256 = 31.2KHz Why? PWM 10bit = 8bit(PR2) + 2bit

// initial PWM CCP2CON= 0b00001100; CCPR2L = 0; // Start Timer2 TMR2ON = 1; // initial A/D channel 0 ANSEL = 0x01; ADCON0 = 0b00000001; ADCON1 = 0b01110000; InitLCD();

//pwm mode : CCPxM[3:0] = 11xx //Clear Duty Cycle

//turn on PWM

//A/D Conevrting Port Select : bit 0 = high

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 197

Code for LAB3 (Hitech C)


while(1) { //AD Converter Start ADGO = 1;

//AD End Check while(ADGO)continue; //Read AD Converting Value pwm_value = ADRESH; DisplayDigit1(pwm_value%10); DisplayDigit2(pwm_value/10 % 10); DisplayDigit3(pwm_value/100); // Control PWM PR2 temp = pwm_value; temp <<= 4; CCP2CON |= (temp & 0x30); temp = pwm_value; temp >>= 2; temp &= 0x3F; CCPR2L = temp; } } = 0x3F; // PWM Duty Cycle Setting Value : 31.2KHz PWM Select

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 198

Code for LAB3 (CCS-C)


#include <16F917.h> #DEVICE *=16 ADC=10 #FUSES NOWDT, INTRC #USE delay(clock = 8000000) #ZERO_RAM #define #define Fosc Fcy 8000000 Fosc/4 // T0IF // device header file

#BIT T0IF = 0x0B.2 #BIT ADFM = 0x1F.7 #BIT GODONE = 0x1F.1 #BYTE #BYTE #BYTE #BYTE ADCON0 ADCON1 ADRESH ADRESL = = = = 0x1F 0x9F 0x1E 0x9E

int16 ad_value[3]; unsigned char i; unsigned int16 duty_pwm2; unsigned int8period_pwm2;

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 199

Code for LAB3 (CCS-C)


void main(void) { setup_oscillator(OSC_8MHZ); set_tris_d(0x00); output_d(0x00); set_tris_a(0xFF); // Setup Timer0 setup_timer_0(RTCC_DIV_256); // Setup ADC setup_adc(ADC_CLOCK_DIV_32 ); setup_adc_ports( sAN0 | VSS_VDD ); // Setup Timer2 period_pwm2 = 63; setup_timer_2 ( T2_DIV_BY_1, period_pwm2, 1); // Setup CCP2 as PWM setup_ccp2(CCP_PWM); duty_pwm2 = 0; set_pwm2_duty(duty_pwm2); // Prescaler, PR2, Postscaler

// RA4 : input

while(1) { for (i=0; i<3; i++) { set_adc_channel(i); delay_us(10); ad_value[i] = read_adc(); } duty_pwm2 = ad_value[0]>> 2; set_pwm2_duty(duty_pwm2); } }

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 200

Lab 3 Review
Introduction to Pulse Width Modulation
- Duty Cycle is linear to motor speed - When driving motors, modulating the PWM signal above 18 kHz is a good idea

Development Tools
- MPLAB Signal Analyzer

CCP Module
- Can generate an 8-bit 31.2 kHz PWM signal using the 8 MHz internal oscillator

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 201

Which book is the best for CCS-C ?

PIC C CCS-C PIC . 1. CCS-C PCW MR.PIC-IDE 2. PIC 3. C 4. CCS-C 5. CCS-C CCS-C 6. 4X4 LCD LCD LED DS1302 32.768KHz IR 1 (Edge ) IR 2 (RS232C ) 20

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 202

Agenda
PIC Microcontroller PICMechatronics PIC Microcontroller Basics
- Cycle

Labs
- Simple I/O - Timer 0 - A/D LCD - DC

Resources
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 203

Project 1: Hello World (Light a LED)

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 204

Project 1: Hello World (Light a LED)

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 205

Project 1: Hello World (Light a LED) Instructions


Pressing SW2 toggles LED (D0) on and off.

Discussion
Switch debouncing is done to ensure that mechanical contact chatter in the switch is not mistaken for more than one button push. Debouncing also ensures that for every one press of the button, only one function is executed. In this project that function is toggling a LED.

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 206

Project 2: Dusk Indicator Using the Voltage Comparator

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 207

Project 2: Dusk Indicator Using the Voltage Comparator

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 208

Using the Voltage Comparator

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 209

Using the Voltage Comparator

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 210

Project 2: Dusk Indicator Using the Voltage Comparator


Instructions Provide a light source for the light sensor and set Potentiometer 1 to the desired trip point. Block the light source and LED1 should turn on. Experiment with the set point and the amount to which you block the light from the sensor. Move your hand slowly in front of the sensor and note that the LED does not flicker. This demonstrates how software hysteresis prevents the output of the comparator from chattering near the trip point. Discussion Comparators are found in many PIC microcontrollers due to the versatility and low cost they offer the user. As you can see, the comparator is one of the more easily used peripherals. Refer to the Comparator Tips n Tricks pamphlet included on the CD in this kit for more information on comparators.

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 211

Project 3: Thermometer Using the Analog-to-Digital Converter

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 212

Project 3: Thermometer Using the Analog-to-Digital Converter

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 213

TC1047A Temperature Sensor

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 214

Project 3: Thermometer Using the Analog-to-Digital Converter


Instructions The LCD displays a temperature reading in degrees Celsius. Breathe on the temperature sensor or introduce another heat source. The displayed temperature should rise. Move the jumper wire from the temperature sensor to the potentiometer (POT1 on J4). Move the potentiometer to view the full range of temperature conversion. Discussion The project introduces you to use of the Analog-to-Digital Converter module. One of the biggest advantages to using a PICmicro microcontroller in applications that require temperature sensing is that the microcontroller can be used to calibrate the temperature sensor reading automatically (over varying supply voltages and process variations in the parts themselves). This saves the manufacturer costly calibration time at the factory that is typically required by traditional temperature sensing technologies. LCD functions were introduced in this project for displaying data on the provided LCD. Take a look at the LCD worksheet in Appendix B. LCD Segment Mapping Worksheet. This is the same worksheet located in the PIC16F91X Data Sheet, only it has been filled in using the information from the PICDEM Mechatronics Schematic (see Appendix A. Hardware Schematics for the schematic). In the LCD source code provided in this project, you can see that the information in this worksheet

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 215

Project 4: Digital Clock Using Timer1

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 216

Project 4: Digital Clock Using Timer1

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 217

Project 4: Timer1 Block

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 218

Project 4: T1CON Register

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 219

Project 4: Digital Clock Using Timer1


Instructions Use Switch 2 to set the hours and Switch 3 to set the minutes. Pressing either of these switches will make the hours or minutes increment between displaying the time (hours and minutes) and displaying pressing Switch 4. Discussion The 32.768 kHz crystal is used to take the guess work out of creating You may recognize this frequency as being 215 kHz. This makes clock pulses to seconds in the binary world of a microcontroller. Timer1 changes, one second has elapsed. A crystal is used because will have an unacceptable degree of error after several days.

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 220

Project 5: Brushed DC Motor Speed Control with Optical Encoder Feedback

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 221

Project 5: Brushed DC Motor Speed Control with Optical Encoder Feedback

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 222

Project 5: Brushed DC Motor Speed Control with Optical Encoder Feedback


Instructions Use POT1 to adjust the speed of the motor. Turn the potentiometer counter-clockwise to reduce speed; turn it clockwise to increase motor speed. Multiply the LCD display by 1000 to obtain speed in RPM. Discussion With the CCP module operating with 8 bits of resolution, the module can output a PWM signal at a frequency of 31.2 kHz. This is sufficiently high frequency that motor whine is outside of the audible frequency for humans. This is especially important in applications where the motor may turn at slow speeds. Using an optical sensor to gather feedback from a motor is an integral part of many mechatronic systems. Some systems require a constant speed over varying loads. An optical encoder is similar to the optical interrupter circuit used in this project. The difference is that an optical encoder uses more than one optical sensor (as many as four) to deduce speed and shaft position.

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 223

Project 6: Brushed DC Speed Control with Back EMF Feedback

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 224

Project 6: Brushed DC Speed Control with Back EMF Feedback

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 225

Project 6: Brushed DC Speed Control with Back EMF Feedback


Instructions POT1 adjusts the speed of the motor. Set POT1 at 100% to run the motor at full speed. The speed indicated on the LCD should be similar to the speed shown in Project 5. Next, run the motor at half speed by setting POT1 to 50% and do the comparison again. Discussion Back EMF is typically not as accurate as speed feedback from an optical encoder. However, in many applications the accuracy does not have to be very precise and measuring Back EMF is more cost effective than any other speed feedback mechanism.
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 226

Project 7: Stepper Motor Control: Single Stepping, Half Stepping and Microstepping

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 227

Project 7: Stepper Motor Control: Single Stepping, Half Stepping and Microstepping

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 228

Project 7: Stepper Motor Control: Single Stepping, Half Stepping and Microstepping
Instructions Adjusting POT1 varies the speed of the motor. Toggle between single stepping, half stepping, and microstepping modes by pressing SW2. At low speeds, the motor should noticeably step in single stepping and half stepping modes. The movement will be quite jerky in both modes, though to a lesser extent in the half stepping mode. In microstepping mode, the jerky motion should be virtually eliminated. Discussion Stepper motors are used in many positioning applications. For example, ink jet printers and smaller CNC machines employ stepper motors. Stepper motors are ideal for these applications because, as long as they are not overloaded, the distance a stepper motor moves is always known. Stepper motors are also brushless, which makes them more reliable than brushed motors. Finally, stepper motors are very responsive to starting and stopping and will produce the highest torque at low speeds. Half stepping is used to give a stepping motor two times its rated step resolution. However, there is a word of caution. A stepper motor is typically not rated to have more than one winding energized at a time. As a result, the motor will heat up if both windings are energized at the rated voltage of the motor. To offset this, when both windings are energized simultaneously, the average current to each winding should be 0.707 times the rated current. Microstepping offers several advantages over single stepping and half stepping. First, torque is fairly constant between steps. This results in smoother rotation and decreased shaft oscillation. Secondly, a higher step resolution is achieved. This means a low-cost motor can be used in an application that would normally require a more expensive, higher resolution motor. Finally, the current in the motor windings is being controlled in a way that prevents the motor from running outside its rated current, thereby eliminating the excess heat associated with half stepping. The drawback to microstepping is that the drive circuitry is more complex.
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 229

Project 8: PC Interface Using the USART

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 230

Project 8: PC Interface Using the USART

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 231

Project 8: PC Interface Using the USART

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 232

Project 8: PC Interface Using the USART


Instructions Locate the PIC MCU Communicator GUI in the PIC MCU Communicator folder in Project 8 on the CD-ROM. Choose the Comm Port and baud rate. Then click Open Comm. Enter TRISD in the top Label text field. Enter 088 into the top Addr text field. Check the top Hex box. Enter 7F into the Data field. In the box labeled Bit Control enter PORTD in the label text field. Enter 008 in the Addr field. Finally, toggle bit 7 using the T button under that bit. The LED on your board should toggle on and off. See Figure 2-18. Discussion The firmware for this project implements RS-232 communication between the PC and PIC16F917. The USART is used to perform this communication. If a crystal oscillator were used as the clock source for the PIC16F917, auto-baud would not be necessary. However, because the PIC16F917 is using the internal RC oscillator, it is necessary to perform auto-baud. Auto-baud refers to sending out a known character from the PC, the MCU measures the time it takes to receive the character, interprets this time into bit width, and then sets up the baud rate registers accordingly for the USART. This method allows the RS-232 communication to be free from errors on different microcontrollers and over temperature variations. The PIC MCU Communicator GUI is included in this project as an example GUI. However, it is also a great tool for debugging. Experiment with using the GUI to setup other peripherals on the microcontroller.

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 233

Project 9: Brushed DC Motor Control Using the ECCP

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 234

Project 9: Brushed DC Motor Control Using the ECCP

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 235

Project 9: Brushed DC Motor Control Using the ECCP


Instructions Use POT1 to adjust the speed of the motor. SW2 toggles between the following four modes of operation: Motor Off Forward Motor Off Reverse Discussion The ECCP module is ideal for driving a full-bridge circuit. Dead-band Delay Control and Automatic Shutdown are some of the other features of the ECCP module when configured in PWM mode. Dead-band Delay Control allows you to control when the MOSFETs in the full-bridge circuit are turned on and off, in relation to one another. This is done in order to automatically account for the MOSFET turn-on and turn-off times. Automatic Shutdown allows you to define the shutdown state of the ECCP when a shutdown event occurs.

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 236

Agenda
Introduction to Microchip Mechatronics examples and benefits PIC Microcontroller Basics
- Hands-On Learning Cycles

Labs
- Simple I/O and Timer 0 - Reading an analog sensor, LCD module - Controlling the speed of a motor

Projects Resources
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 237

Continue to learn with the Mechatronics Board . . .

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 238

PICDEM Mechatronics Projects For Self-Study Learn to read an analog temperature sensor and light sensor How to create a real-time clock Brushed DC motor control Stepper motor control Using the LCD module on the PIC16F917 Using serial communication Other concepts: Back EMF, optical speed sensing, and current sensing
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 239

Microchip Web Resources Electronic Product Selector Guide Design Centers www.microchip.com/mechatronics Step-by-step Tutorials Webinars Online presentations Application Notes, Technical Briefs, Articles Upcoming Training and Events Technical Support
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 240

PICkit 2 Microcontroller Programmer


Programs all Flash PIC microcontrollers and dsPIC Digital Signal Controllers $34.95 USD PICkit 2 Flash Starter Kit Programmer bundled with small development board for $49.95 USD Compatible with the PICDEM Mechatronics Board

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 241

MPLAB ICD 2
MPLAB ICD 2 Part # DV164007
- Includes 9V power supply, serial cable, USB cable, ICD interface cable, and MPLAB ICD 2

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 242

Third Party Tools


Third Party Tool Developers are our Partners
- Give them access to early samples/information - License Hardware Design to accommodate certain Markets

Preferred Programmer Vendor Top Compilers Choices


- Hi-Tech - IAR - CCS

Complete listing at: www.microchip.com/thirdparty


2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 243

Summary
Designing a PIC microcontroller into a mechanical system is beneficial to you Youve see how easy it is to get started with Microchip and PIC microcontrollers You can now use Microchips low-cost tools to change, adapt and add features to a design You can now perform basic mechatronic tasks using a PIC microcontroller
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 244

Thank You for Coming!

Microchip appreciates your feedback on the content of this presentation. Please tell us what you liked and didnt like about the presentation. Please send your comments to: mechatronicsWIB@microchip.com
2005 Microchip Technology Incorporated. All Rights Reserved. Mechatronics Workshop-in-a-Box Slide 245

Trademarks & Disclaimers


The Microchip name and logo, the Microchip logo, Accuron, dsPIC, KEELOQ, microID, MPLAB, PIC, PICmicro, PICSTART, PRO MATE, PowerSmart, rfPIC and SmartShunt are registered trademarks of Microchip Technology Incorporated in the U.S.A. and other countries. AmpLab, FilterLab, Migratable Memory, MXDEV, MXLAB, PICMASTER, SEEVAL, SmartSensor and The Embedded Control Solutions Company are registered trademarks of Microchip Technology Incorporated in the U.S.A. Analog-for-the-Digital Age, Application Maestro, dsPICDEM, dsPICDEM.net, dsPICworks, ECAN, ECONOMONITOR, FanSense, FlexROM, fuzzyLAB, In-Circuit Serial Programming, ICSP, ICEPIC, MPASM, MPLIB, MPLINK, MPSIM, PICkit, PICDEM, PICDEM.net, PICtail, PowerCal, PowerInfo, PowerMate, PowerTool, rfLAB, rfPICDEM, Select Mode, Smart Serial, SmartTel and Total Endurance are trademarks of Microchip Technology Incorporated in the U.S.A. and other countries. SQTP is a service mark of Microchip Technology Incorporated in the U.S.A. All other trademarks mentioned herein are property of their respective companies. Information contained in this publication regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchips products as critical components in life support systems is not authorized except with express written approval by Microchip. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.

2005 Microchip Technology Incorporated. All Rights Reserved.

Mechatronics Workshop-in-a-Box

Slide 246

S-ar putea să vă placă și