Tricks with PICs - PowerPoint PPT Presentation

About This Presentation
Title:

Tricks with PICs

Description:

Clocked by TMR2. Timer runs at twice bit rate as determined by PR2 ... Tri-state output buffer enabled by /RD, input latch clocked by /WR. ... – PowerPoint PPT presentation

Number of Views:113
Avg rating:3.0/5.0
Slides: 44
Provided by: donr84
Category:
Tags: clocked | jumpers | pics | tricks

less

Transcript and Presenter's Notes

Title: Tricks with PICs


1
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

2
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

3
Vedits 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 isnt enough, write your own macros
  • Convert compiler-specific directives
  • Rename variables in multiple files

4
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.

5
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

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

7
PIC Resources
  • microchip.com
  • http//www.pic-c.com/links/
  • http//www.tinaja.com/picup01.html
  • http//o.webring.com/hub?ringpicmicro

8
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

9
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

10
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

11
PCM C Compiler
  • http//www.ccsinfo.com/
  • From 125
  • PCM wont 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.

12
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

13
PCM - dBegTxStr0 macro
  • define dBegTxStr0 \
  • byte RetHi _RETURN_1 \
  • asm \
  • goto SkipText \
  • TextAdr

14
PCM - dEndTxStr0 macro
  • define dEndTxStr0 \
  • SkipText \
  • movplw TextAdr \
  • movwf _RETURN_ \
  • movphw TextAdr \
  • movwf RetHi \
  • endasm

15
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

16
PCM - ReadArray()
  • asm
  • goto 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

17
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

18
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.

19
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

20
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.

21
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 StackPointer1
    for intermediate storage

22
Pushing and Popping
  • Push and Pop functions for
  • 8-bit signed and unsigned
  • 16-bit signed and unsigned
  • Pointers to data

23
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
24
Stackless Math
  • // Global variables
  • byte MathDstP // destination/result pointer
  • byte MathSrcP // source pointer
  • AddPtr() MathDstP MathSrcP
  • SubPtr() MathDstP - MathSrcP
  • AddStk()
  • MathDstP MathPtr - 2StackDataSize
  • MathSrcP MathPtr - StackDataSize
  • addPtr()
  • DropStk()

25
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 PICs carry flag after
    calculations
  • MathDouble configuration bit enables double
    precision multiplys and divides

26
Another Async Serial Port
  • Bit-bang
  • software intensive
  • External interrupt detects start bit
  • Timer interrupt reads data bits
  • Requires precision timing
  • SPI port

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

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

29
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

30
SPI Port in Async Mode
  • Wait for falling edge of start bit
  • Load TMR2 with -PR2

Reverse data bits
31
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

32
Dual Serial Port TX
  • Use any convenient leftover components to switch
    the UART TX pin between your serial devices.
  • PLD
  • Digital Logic
  • Transistors

33
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

34
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.

35
PWM
  • CCPRx 0 0
  • CCPRx PR21 100 duty cycle

36
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

37
Parallel Slave Port
  • Tri-state output buffer enabled by /RD, input
    latch clocked by /WR.

38
Parallel Protocols
  • Although its 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.

39
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
  • RB7..4 can generate an interrupt, but if its a
    quick pulse, reading the port pins will just show
    the current logic level.

40
PSP Handshaking
  • Level activated handshaking risks the two sides
    getting out of sync.
  • A sender might see the receivers READY
    handshake line, send a byte, and recheck the
    READY signal before the receiver responds.
  • A receiver might see the senders 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.

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

42
XOBF
  • XOBF can be set by the senders handshaking pin
    to signal that data is ready to read, and cleared
    by the receivers /RD signal. The sender doesnt
    need to monitor XOBF as its internal OBF
    duplicates the signal

43
Tricks with PICs
  • Download source code and this PowerPoint file
    from canzonatech.com. Click on Free Stuff
Write a Comment
User Comments (0)
About PowerShow.com