Upload
madlyn-woods
View
214
Download
0
Embed Size (px)
Citation preview
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 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
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.
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 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
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
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 ’\r’
retlw ’\n’
retlw 0
dEndTxStr0
}
PCM - dBegTxStr0 macro
#define dBegTxStr0 \
#byte RetHi = _RETURN_+1 \
#asm \goto SkipText \
TextAdr:
PCM - dEndTxStr0 macro
#define dEndTxStr0 \
SkipText: \movplw TextAdr \
movwf _RETURN_ \
movphw TextAdr \
movwf RetHi \
#endasm
// 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
#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()
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()
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.
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
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
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
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
// 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
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
Another Async Serial Port
Bit-bangsoftware intensive
External interrupt detects start bit
Timer interrupt reads data bitsRequires precision timing
SPI port
Async Serial Port
Asynchronous Serial PortStart bit, 8 data, stop bit
Least-significant bit first
SPI8 data bits
Separate clock
Synchronous
Most-significant bit first
SPI Port
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
Wait for falling edge of start bit
Load TMR2 with -PR2
SPI Port in Async Mode
Reverse data bits
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
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 TXTX9D 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.
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
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
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 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
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
Tricks with PICs
Download source code and this PowerPoint file from canzonatech.com. Click on “Free Stuff”