43
Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity and get more from a mid-range PIC. Download source code and this PowerPoint file from canzonatech.com. Click on “Free Stuff”

Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

Embed Size (px)

Citation preview

Page 1: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

Tricks with PICs

Don Rowe P.E.

Canzona Technologies

Registered Microchip Consultant

Tips, Techniques and Old Indian Tricks to help increase productivity and get more from a mid-range PIC.

Download source code and this PowerPoint file from canzonatech.com. Click on “Free Stuff”

Page 2: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

Vedit Text Processor

http://vedit.com/from $79, free 30-day trial

Almost everything is configurable

Integrated compiler support

Built-in programming language

File compare macro

Configurable syntax highlighting

Hex display/edit

Powerful search/replace

Page 3: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

Vedit’s Search/Replace

Works on multiple files and subdirectories

Unix-type regular expressionsUse a portion of the matched string in the

replace string

Special search characters and pattern matching

end/beginning of line, whitespace

any letter, any digit, multiple characters

any character in a list, or not in a list

If that isn’t enough, write your own macrosConvert compiler-specific directives

Rename variables in multiple files

Page 4: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

Take Command and 4NT

http://jpsoft.com/From $69.95, free 21-day trial

Command line interpreter for windows

Much more capable than windows “Command Prompt”

Most commands also work on subdirectories

Apply commands to each file in a listArchive files from multiple directories to a backup

directory

Specify files by date, attributes, etc.

Page 5: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

Beyond Compare

http://www.scootersoftware.com$30, free 30-day trial

Compare individual files, directory trees or an entire drive

Compare by any combination of date/time, size, attributes, contents

Selectively display matching or mismatched files

Displays differences in text and binary files

Page 6: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

Excel

create RETLW opcodes for lookup tables, etc.=CONCATENATE("retlw high D'",INT(D13),"'")

Page 7: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

PIC Resources

microchip.com

http://www.pic-c.com/links/

http://www.tinaja.com/picup01.html

http://o.webring.com/hub?ring=picmicro

Page 8: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

Mid-range PIC Overview

Up to 8K of 14-bit program memory

Up to 368 bytes of data memory

Up to 256 bytes of EEPROM

22 or 33 I/O pins

All opcodes are 14 bits and execute in 1 cycle

Page 9: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

Mid-range PIC Internals

Upward compatible with earlier PICs

W registerOpcode parameters in w and either literal or register

result returned in w or register

FSR for indirect addressing

BTFSC, BTFSS opcodesconditionally skip the following opcode

Bank switching for data and program memory

8-level stack

Program counter and stack are not addressable

Page 10: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

SPI

Asynchronous serial port

2 PWM

7 external interrupts

capture/compare

3 timers

several multiplexed A/D inputs

PSP port

EEPROM

Flash and OTP

In-Circuit Debugging

Mid-range PIC Peripherals

Page 11: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

PCM C Compiler

http://www.ccsinfo.com/

From $125

PCM won’t directly let you have the address of a function or data in code space (stored with RETLW opcode), but you can reference program addresses within functions.

You can also use this general principle with functions.

Page 12: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

PCM - addresses in code space

Insert data into a function which returns the address of the data.

long TxtAdrHelp()

{dBegTxStr0

retlw ’?’

retlw ’\r’

retlw ’\n’

retlw 0

dEndTxStr0

}

Page 13: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

PCM - dBegTxStr0 macro

#define dBegTxStr0 \

#byte RetHi = _RETURN_+1 \

#asm \goto SkipText \

TextAdr:

Page 14: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

PCM - dEndTxStr0 macro

#define dEndTxStr0 \

SkipText: \movplw TextAdr \

movwf _RETURN_ \

movphw TextAdr \

movwf RetHi \

#endasm

Page 15: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

// Fetch byte from *l in code space

// 2 stack levels

byte ReadArray(u16 l)

{char ch;

char OldPc;

#byte LLo = l

#byte LHi = l +1

PCM - Read data in code space

Page 16: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

#asmgoto SkipSubrt

subrt:

movf LHi,w

movwf PCLATH // Hi-byte of data address

movf LLo,w // Lo-byte of data address

movwf PCL // GOTO l

SkipSubrt:

PCM - ReadArray()

Page 17: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

SkipSubrt:

movf PCLATH,w

movwf OldPc // Save PCLATH

call subrt // w = *l

movwf ch

movf OldPc,w

movwf PCLATH // Restore original PCLATH

#endasm

return ch;

PCM - ReadArray()

Page 18: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

Extended-precision Math

Extended-precision fixed-point and integer calculations

16-bit math in complicated calculationsEfficiently allocates and reuses storage for

temporary values and intermediate results.

You compiler may not support the optimum precision for your application.

Passing parameters to functions may require additional code, and additional storage for the results.

Page 19: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

Math Parameter Stack

Similar to the Forth programming language and HP calculators using RPN

You always know where your data is.

Operands are popped from the stack

Results are pushed back onto the stack

Intermediate values may be kept on the stack

Page 20: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

Push first number

Push second number

Call AddStk() function

The sum is returned on the stack and may be left until needed or popped to memory.

Adding 2 numbers

Page 21: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

DivStk() function may optionally also leave the remainder on the stack.

MulStk() function may optionally leave a double-precision product on the stack

Separate functions for signed and unsigned values

Functions to rotate, complement, negate, compare

Math functions may use memory at StackPointer+1 for intermediate storage

Stack functions

Page 22: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

Pushing and Popping

Push and Pop functions for 8-bit signed and unsigned 16-bit signed and unsigned Pointers to data

Page 23: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

Additional Stack Functions

DupStk() copies 1st value on stack to TOS

OverStk() copies 2nd value on stack to TOS

DropStk() deletes TOS

Del2ndStk() deletes 2nd value on stack

SwapStk() swaps 2 top values

OverStk() function

Page 24: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

// Global variables

byte *MathDstP; // destination/result pointer

byte *MathSrcP; // source pointer

AddPtr(): *MathDstP += *MathSrcP

SubPtr(): *MathDstP -= *MathSrcP

AddStk() {

MathDstP = MathPtr - 2*StackDataSize;

MathSrcP = MathPtr - StackDataSize;

addPtr();

DropStk(); }

Stackless Math

Page 25: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

Download from canzonatech.com (“Free Stuff”), then go forth and multiply.

#define StackDataSize as size of data in bytes

MathPtr must be externally initialized to lowest address of stack

MathCarry bit set from PIC’s carry flag after calculations

MathDouble configuration bit enables double precision multiplys and divides

PicMath.c

Page 26: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

Another Async Serial Port

Bit-bangsoftware intensive

External interrupt detects start bit

Timer interrupt reads data bitsRequires precision timing

SPI port

Page 27: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

Async Serial Port

Asynchronous Serial PortStart bit, 8 data, stop bit

Least-significant bit first

Page 28: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

SPI8 data bits

Separate clock

Synchronous

Most-significant bit first

SPI Port

Page 29: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

SPI Port Setup

Clock idles highOutput on falling edge

Input on rising edge

Clocked by TMR2Timer runs at twice bit rate as determined by PR2

Resets TMR2 and toggles SPI clock when TMR2 == PR2

Page 30: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

Wait for falling edge of start bit

Load TMR2 with -PR2

SPI Port in Async Mode

Reverse data bits

Page 31: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

Improved Timing of SPI

Interrupt latency can affect timing

Capture falling edge of start bit with CCP/PWM pinStores TMR1 value in CCPRx

Load TMR2 with TMR1-CCPRx-PR2

Page 32: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

Dual Serial Port TX

Use any convenient leftover components to switch the UART TX pin between your serial devices.PLD

Digital Logic

Transistors

Page 33: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

Async Serial Port Bits

8MHz or 16MHz osc. Is a good choice for accurate baud rates

FERR framing error and all data bits == 0 indicates a break

TX9 enables 9-bit TXTX9D is 9th bit

Parity

Extra stop bit for half-duplex RS485

Page 34: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

Half-duplex RS485

If polling TRMT, a 9th data bit always set to ‘1’ functions as a stop bit since TRMT is set after shifting the last data bit.

If your hardware allows receiving each character sent, you can use the RX interrupt to know when a character is completely sent.

Page 35: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

PWM

CCPRx = 0: 0%

CCPRx = PR2+1: 100% duty cycle

Page 36: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

Initial Configuration

Sometimes you want the system to start in a special command or configuration mode that should be off limits to users.jumpers

hold a button during power-up

Serial port command

RS232 break detect

Force the RS232 RX pin low with a jumper plug.

RX pin is still readable when configured as async serial port

Page 37: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

Parallel Slave Port

Tri-state output buffer enabled by /RD, input latch clocked by /WR.

Page 38: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

Although it’s possible to devise communication protocols using only the existing capabilities, in some cases, you need extra handshaking lines to recreate the equivalent of the internal IBF and OBF status bits. This usually requires at least one extra output pin, and one extra input pin to monitor the signal.

Parallel Protocols

Page 39: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

A quick pulse can indicate a sender or receiver is ready. You can directly connect a handshaking output to an edge-triggered interrupt pin.RB0

CCP1, CCP2

RB[7..4] can generate an interrupt, but if it’s a quick pulse, reading the port pins will just show the current logic level.

PSP Handshaking

Page 40: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

PSP Handshaking

Level activated handshaking risks the two sides getting out of sync.A sender might see the receiver’s “READY”

handshake line, send a byte, and recheck the “READY” signal before the receiver responds.

A receiver might see the sender’s “READY” handshake line, read a byte, recheck the “READY” signal before the sender responds and read the same data again.

A PLD or other external logic can create external handshake signals that mimic the internal IBF and OBF status bits.

Page 41: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

XIBF can be set by the sender’s /WR signal, and cleared by the receiver’s output handshaking pin. The sender monitors XIBF to determine when the receiver is ready for more.

XIBF

Page 42: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

XOBF can be set by the sender’s handshaking pin to signal that data is ready to read, and cleared by the receiver’s /RD signal. The sender doesn’t need to monitor XOBF as it’s internal OBF duplicates the signal

XOBF

Page 43: Tricks with PICs Don Rowe P.E. Canzona Technologies Registered Microchip Consultant Tips, Techniques and Old Indian Tricks to help increase productivity

Tricks with PICs

Download source code and this PowerPoint file from canzonatech.com. Click on “Free Stuff”