Tricks with PICs - Canzona Technologies

Download Report

Transcript Tricks with PICs - Canzona Technologies

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”

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

Vedit’s Search/Replace

Works on multiple files and subdirectories Unix-type regular expressions

Use 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 macros

Convert compiler-specific directives Rename variables in multiple files

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 list Archive files from multiple directories to a backup directory Specify files by date, attributes, etc.

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

Excel

create RETLW opcodes for lookup tables, etc.

=CONCATENATE("retlw high D'",INT(D13),"'")

PIC Resources

microchip.com

http://www.pic-c.com/links/ http://www.tinaja.com/picup01.html

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

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

Mid-range PIC Internals

Upward compatible with earlier PICs W register Opcode parameters in w and either literal or register result returned in w or register FSR for indirect addressing BTFSC, BTFSS opcodes conditionally skip the following opcode Bank switching for data and program memory 8-level stack Program counter and stack are not addressable

Mid-range PIC Peripherals

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

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.

PCM - addresses in code space

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

long TxtAdrHelp() { dBegTxStr0 retlw ’?’ retlw retlw ’\r’ ’\n’ retlw 0 dEndTxStr0 }

PCM - dBegTxStr0 macro

#define dBegTxStr0 \ #byte RetHi = _RETURN_+1 \ #asm \ goto TextAdr: SkipText \

PCM - dEndTxStr0 macro

#define dEndTxStr0 \ SkipText: \ movplw TextAdr \ movwf _RETURN_ \ movphw TextAdr \ movwf RetHi \ #endasm

PCM - Read data in code space

// 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 - ReadArray()

#asm goto SkipSubrt subrt: movf movwf movf movwf LHi,w PCLATH LLo,w PCL SkipSubrt: // Hi-byte of data address // Lo-byte of data address // GOTO l

PCM - ReadArray()

SkipSubrt: movf movwf PCLATH,w OldPc call movwf movf movwf subrt ch OldPc,w PCLATH #endasm return ch; // Save PCLATH // w = *l // Restore original PCLATH

Extended-precision Math

Extended-precision fixed-point and integer calculations 16-bit math in complicated calculations Efficiently 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.

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

Adding 2 numbers

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.

Stack functions

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

Pushing and Popping

Push and Pop functions for

8-bit signed and unsigned

16-bit signed and unsigned

Pointers to data

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

Stackless Math

// 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(); }

PicMath.c

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

Another Async Serial Port

Bit-bang software intensive External interrupt detects start bit Timer interrupt reads data bits Requires precision timing SPI port

Async Serial Port

Asynchronous Serial Port Start bit, 8 data, stop bit Least-significant bit first

SPI Port

SPI 8 data bits Separate clock Synchronous Most-significant bit first

SPI Port Setup

Clock idles high Output on falling edge Input on rising edge Clocked by TMR2 Timer runs at twice bit rate as determined by PR2 Resets TMR2 and toggles SPI clock when TMR2 == PR2

SPI Port in Async Mode

Wait for falling edge of start bit Load TMR2 with -PR2 Reverse data bits

Improved Timing of SPI

Interrupt latency can affect timing Capture falling edge of start bit with CCP/PWM pin Stores TMR1 value in CCPRx Load TMR2 with TMR1-CCPRx-PR2

Dual Serial Port TX

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

PLD Digital Logic Transistors

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 TX TX9D is 9th bit

Parity Extra stop bit for half-duplex RS485

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.

PWM

CCPRx = 0: 0% CCPRx = PR2+1: 100% duty cycle

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

Parallel Slave Port

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

Parallel Protocols

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.

PSP Handshaking

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

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.

XIBF

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.

XOBF

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

Tricks with PICs

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