You are on page 1of 37

915 TAT

Tips & Tricks on Designing with Microchips 6/8/14-pin Devices

2005 Microchip Technology Incorporated. All Rights Reserved.

Slide

What this class is...

Ideas Hints Tips Tricks


2005 Microchip Technology Incorporated. All Rights Reserved. Class

Slide

Agenda
Presentation: 1 hr
Hardware Tips and Tricks Software Tips and Tricks

Break: 10 min Hands-on session: 3 hours

2005 Microchip Technology Incorporated. All Rights Reserved.

Class

Slide

8- and 14-pin Flash Devices


High Performance MCU
DC to 20 MHz operating speed 8-level deep stack

FLASH Program Memory


Re-programmable In-Circuit Serial Programming (ICSP) capability

Low Power Consumption


Low Voltage operation with EEPROM write capability over whole voltage range (2V-5.5V)

2005 Microchip Technology Incorporated. All Rights Reserved.

Class

Slide

Tips and Tricks with Hardware

2005 Microchip Technology Incorporated. All Rights Reserved.

Class

Slide

Dual Speed RC Oscillator


1. After reset I/O pin is High Impedence (running at low frequency) 2. Output a high on I/O pin 3. R1, R2 and C determine Osc Frequency 4. Works also with additional capacitors
+5V R1 R2 GP0 OSC1 C PIC12F6XX

2005 Microchip Technology Incorporated. All Rights Reserved.

Class

Slide

Dual Speed RC Oscillator


Freq = 2.1 MHz Duty Cycle = 91%

Freq = 4.3 MHz Duty Cycle = 90%

Screen Captures using Hewlett-Packard Mixed Signal Oscilloscope 54645

2005 Microchip Technology Incorporated. All Rights Reserved.

Class

Slide

Input/Output: Multiplexing
Example - Six LEDs on 3 I/O pins
# LEDs = # I/Os * (# I/Os - 1) (1)
3V GPIO 0 1 2 0 0 1 Z Z 0 1 0 0 0 1 1 1 1 0 1 0 0 1 Z Z 0 1 1 0 0 1 1 0 Z Z 1 0 1 0 1 0 1 0 1 0 1 LEDs 1 2 3 4 5 6 0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 1 0

GP0
5 1 2 6

GP1 GP2 PIC12F6XX


3 4

Note 1: #LEDs also limited by pin IOL, IOH and max device current.
2005 Microchip Technology Incorporated. All Rights Reserved. Class

Slide

Read 3 States from 1 Pin


To check state Z
Drive output pin high Set to input Read 1 Drive output pin low Set to input Read 0
PIC MCU I/O Pin 0 0v State Pin 0 Pin 1 5v Pin 1

To check state 0
Read 0 on pin

To check state 1
Read 1 on pin
2005 Microchip Technology Incorporated. All Rights Reserved. Class

0 1 Z

closed open open closed open open


Slide 9

4x4 Keyboard with 1 Input


Resistors chosen for unique voltages
Use A/D to measure button voltage
VDD

PIC12F6XX

GP0/AN0

2005 Microchip Technology Incorporated. All Rights Reserved.

Class

Slide

10

Decode Keys and ID Settings


GP0

When GP4=1 and no keys pressed, read ID setting When GP4=0, read switch buttons

GP1 GP2 GP3 GP4

VDD

2005 Microchip Technology Incorporated. All Rights Reserved.

Class

Slide

11

Reading four switches with one Comparator and two pins


Set CVref to check for each valid level Toggle input select to check second pair Switches pull up to optimize range of CVref
VDD

PIC16F630

COUT

CVref

2005 Microchip Technology Incorporated. All Rights Reserved.

Class

Slide

12

1 Pin Power/Data
Single direction data Transistor pulls data line low via Sender GPIO Capacitor stabilizes VDD while data line low
VDD - 0.7V VDD VDD

Receiver GP0

Sender

GP0

2005 Microchip Technology Incorporated. All Rights Reserved.

Class

Slide

13

Generating High Voltages


CLKOUT generates frequency for charge pump Low cost Minimal part count
VDD

Vout max = (2 * VDD) - (2 * Vdiode)


CLKOUT Cpump Cfilter PIC12F6XX w/ RC CLKOUT

2005 Microchip Technology Incorporated. All Rights Reserved.

Class

Slide

14

VDD Self Controlling Circuit


Key Features:
Pressing button powers up PIC MCU Executing SLEEP instruction powers off PIC MCU Low cost (uses n-channel FET) Reliable No additional I/O pins required
VDD VDD VDD CLKOUT PIC12F6XX VBAT

VDD

2005 Microchip Technology Incorporated. All Rights Reserved.

Class

Slide

15

Smart Current Limiter Using A/D


Motor control and power supplies often need current limiter Intelligent control can be done for:
In-rush current Current limiting Over-current recovery Smart circuit breaker

2005 Microchip Technology Incorporated. All Rights Reserved.

Class

Slide

16

Using PICmicro MCU A/D for Smart Current Limiter


Detect current through low side sense resistor Optional peak filter capacitor Varying levels of overcurrent response can be realized in software

PIC12F6XX 10K AN0 Rsense Load or Motor

2005 Microchip Technology Incorporated. All Rights Reserved.

Class

Slide

17

Reading an Analog Sensor Without an ADC Peripheral

1. RC timing method with reference resistor 2. Creating a Delta-Sigma ADC

2005 Microchip Technology Incorporated. All Rights Reserved.

Class

Slide

18

Reading an Analog Sensor Without an ADC Peripheral


1. RC Timing Method:
Vc(t)

Simple RC step response Vc(t) = VDD * (1 - e -t/(RC)) t = -RC ln(1 - Vth/VDD) Vth/VDD is constant R2 = (t2/t1) * R1
2005 Microchip Technology Incorporated. All Rights Reserved. Class

R1

R2

Vth

t=0

t = t1

t = t2

Time

Slide

19

Reading an Analog Sensor Without an ADC Peripheral


1. RC Timing Method
PIC12F6XX
Rref GP2 Rsen GP1 GP0

1. Set GP1 and GP2 to inputs, and GP0 to a low output to discharge C. 2. Set GP0 to an input and GP1 to a high output. 3. Measure tRsen (GP0 changes to 1). 4. Repeat step 1. 5. Set GP0 to an input and GP2 to a high output. 6. Measure tRref (GP0 changes to 1). 7. Use film polypropylene capacitor. 8. Rsen = tRsen x Rref
tRref

Other alternatives: Voltage comparator in the PIC12F6XX


2005 Microchip Technology Incorporated. All Rights Reserved. Class

Slide

20

Reading a Sensor w/ Higher Resolution


Used to dynamically adjust the range as needed.
PIC12F6XX
100K GP2 10K GP1 AN0 (A/D input) Rsen

1. 10K and 100K resistors are used to set the range. 2. Set port pins for desired resistors to output 1, others to inputs. 3. Vref for A/D = VDD. 4. Rsen calculation is independent of VDD. 5. Count = Rsen/(Rsen+Rref) x 255 6. Dont forget to allow acquisition time for the A/D

2005 Microchip Technology Incorporated. All Rights Reserved.

Class

Slide

21

Delta-Sigma Converter
1. GP1 average voltage = CVref 2. Time base as sampling rate. 3. At the end of each time base period: - If GP1 > CVref, then GP2 Output Low. - If GP1 < CVref, then GP2 Output High.
GP1 Vin

PIC12F6XX
CVref COUT Software closes loop

GP2

4. Accumulate the GP2 lows over many samples. 5. Number of samples determines resolution.

2005 Microchip Technology Incorporated. All Rights Reserved.

Class

Slide

22

Delta-Sigma Converter
Pros:
Cons: Requires some F/W High resolution overhead Good noise immunity Slow conversion Easy to scale and offset More bits = longer the signal, even far conversion time above VDD and below VSS Absolute accuracy requires stable power Inherently linear and supply monotonic Requires minimal external components (2 Rs & 1 C)

2005 Microchip Technology Incorporated. All Rights Reserved.

Class

Slide

23

Tips and Tricks with Software

2005 Microchip Technology Incorporated. All Rights Reserved.

Class

Slide

24

Delay Techniques
Use GOTO next instruction instead of two NOPs. Use CALL Rtrn as quad, 1 instruction NOP (where Rtrn is the exit label from an existing subroutine). NOP NOP GOTO$+1 CALL Rtrn ... RETURN

;2 instructions, 2 cycles ;1 instruction, 2 cycles ;1 instruction, 4 cycles

Rtrn

2005 Microchip Technology Incorporated. All Rights Reserved.

Class

Slide

25

Optimizing Destinations
Destination bit determines W or F for result Look at data movement and re-structure
Example: A + B => A MOVF ADDWF MOVWF 3 instructions A,W B,W A MOVF ADDWF B,W A,F

2 instructions

2005 Microchip Technology Incorporated. All Rights Reserved.

Class

Slide

26

Optimizing Function Calls


Replace a CALL instruction that is immediately followed by a RETURN instruction by a GOTO instruction

... CALL Subroutine RETURN ... GOTO Subroutine Subroutine . . . RETURN


2005 Microchip Technology Incorporated. All Rights Reserved. Class

; 2 instructions, 4 cycles

; 1 instruction, 2 cycles ; Do something


Slide 27

Conditional Bit Set/Clear


To move single bit of data from REGA to REGB: Precondition REGB bit Test REGA bit and fix REGB if necessary
BTFSS BCF BTFSC BSF REGA,2 REGB,5 REGA,2 REGB,5 BCF BTFSC BSF REGB,5 REGA,2 REGB,5

4 instructions

3 instructions

Note : When controlling ports, a two cycle zero pulse can occur
2005 Microchip Technology Incorporated. All Rights Reserved. Class

Slide

28

Swap File Register with W


The following macro swaps the contents of W and REG without using a second register
SWAP MACRO XORWF XORWF XORWF ENDM Needs: REG REG,F REG,W REG,F W = 0xaa, REG = 0x55 1010 1010 0101 0101 1111 1111 (REG) 1010 1010 0101 0101 (W) 1111 1111 1010 1010 (REG)
Slide 29

0 TEMP Registers 3 Instructions 3 Tcy


Class

2005 Microchip Technology Incorporated. All Rights Reserved.

Bit Shifting using Carry Bit


Rotate a byte through carry without using RAM variable for loop count: Easily adapted to serial protocol routines. Carry bit is cleared (except last cycle) and the cycle repeats until the zero bit sets indicating the end.

2005 Microchip Technology Incorporated. All Rights Reserved.

Class

Slide

30

Bit Shifting using Carry Bit


C 1 7 0 1 0 1 0 1 0 1 0

1st Rotate

0 1 0 1 0 1 0 1

2nd Rotate Final Rotate Z=1

1 0 1 0 1 0 1 0

0 0 0 0 0 0 0 0

2005 Microchip Technology Incorporated. All Rights Reserved.

Class

Slide

31

Conclusion
Use of existing features wisely
TRIS states on I/Os CLKOUT output Destination locations

Simple external circuits


RC circuits Resistor ladders / dividers

2005 Microchip Technology Incorporated. All Rights Reserved.

Class

Slide

32

References
AN512 - Implementing Ohmmeter/Temperature Sensor AN611 - Resistance and Capacitance Meter Using a PIC16C622 AN683 - A Comparator Based Slope ADC AN700 Make a Delta-Sigma Converter Using a Microcontroller's Analog Comparator Module Tips and Tricks Booklet LCD PICmicro MCU Tips 'n Tricks
2005 Microchip Technology Incorporated. All Rights Reserved. Class

Slide

33

BREAK

2005 Microchip Technology Incorporated. All Rights Reserved.

Slide

34

Hands-on Material
Hardware
MPLAB ICD2 Programmer / Emulator Demonstration board

Software
MPLAB IDE Project template

Documentation
Hand-out with schematic Tips n Tricks booklet

2005 Microchip Technology Incorporated. All Rights Reserved.

Class

Slide

35

Lets Write Some Code!

1. Multiplexing LEDs 2. Multiplexing buttons 3. Reading a sensor with higher accuracy 4. BONUS: Implement a Delta-Sigma A/D converter

2005 Microchip Technology Incorporated. All Rights Reserved.

Class

Slide

36

2005 Microchip Technology Incorporated. All Rights Reserved.

Class

Slide

37

You might also like