Welcome
Username or Email:

Password:


Missing Code




[ ]
[ ]
Online
  • Guests: 60
  • Members: 0
  • Newest Member: omjtest
  • Most ever online: 396
    Guests: 396, Members: 0 on 12 Jan : 12:51
Members Birthdays:
No birthdays today

Next birthdays
11/25 Chris (39)
11/25 JamesH (17)
11/25 Oakley (21)
Contact
If you need assistance, please send an email to forum at 4hv dot org. To ensure your email is not marked as spam, please include the phrase "4hv help" in the subject line. You can also find assistance via IRC, at irc.shadowworld.net, room #hvcomm.
Support 4hv.org!
Donate:
4hv.org is hosted on a dedicated server. Unfortunately, this server costs and we rely on the help of site members to keep 4hv.org running. Please consider donating. We will place your name on the thanks list and you'll be helping to keep 4hv.org alive and free for everyone. Members whose names appear in red bold have donated recently. Green bold denotes those who have recently donated to keep the server carbon neutral.


Special Thanks To:
  • Aaron Holmes
  • Aaron Wheeler
  • Adam Horden
  • Alan Scrimgeour
  • Andre
  • Andrew Haynes
  • Anonymous000
  • asabase
  • Austin Weil
  • barney
  • Barry
  • Bert Hickman
  • Bill Kukowski
  • Blitzorn
  • Brandon Paradelas
  • Bruce Bowling
  • BubeeMike
  • Byong Park
  • Cesiumsponge
  • Chris F.
  • Chris Hooper
  • Corey Worthington
  • Derek Woodroffe
  • Dalus
  • Dan Strother
  • Daniel Davis
  • Daniel Uhrenholt
  • datasheetarchive
  • Dave Billington
  • Dave Marshall
  • David F.
  • Dennis Rogers
  • drelectrix
  • Dr. John Gudenas
  • Dr. Spark
  • E.TexasTesla
  • eastvoltresearch
  • Eirik Taylor
  • Erik Dyakov
  • Erlend^SE
  • Finn Hammer
  • Firebug24k
  • GalliumMan
  • Gary Peterson
  • George Slade
  • GhostNull
  • Gordon Mcknight
  • Graham Armitage
  • Grant
  • GreySoul
  • Henry H
  • IamSmooth
  • In memory of Leo Powning
  • Jacob Cash
  • James Howells
  • James Pawson
  • Jeff Greenfield
  • Jeff Thomas
  • Jesse Frost
  • Jim Mitchell
  • jlr134
  • Joe Mastroianni
  • John Forcina
  • John Oberg
  • John Willcutt
  • Jon Newcomb
  • klugesmith
  • Leslie Wright
  • Lutz Hoffman
  • Mads Barnkob
  • Martin King
  • Mats Karlsson
  • Matt Gibson
  • Matthew Guidry
  • mbd
  • Michael D'Angelo
  • Mikkel
  • mileswaldron
  • mister_rf
  • Neil Foster
  • Nick de Smith
  • Nick Soroka
  • nicklenorp
  • Nik
  • Norman Stanley
  • Patrick Coleman
  • Paul Brodie
  • Paul Jordan
  • Paul Montgomery
  • Ped
  • Peter Krogen
  • Peter Terren
  • PhilGood
  • Richard Feldman
  • Robert Bush
  • Royce Bailey
  • Scott Fusare
  • Scott Newman
  • smiffy
  • Stella
  • Steven Busic
  • Steve Conner
  • Steve Jones
  • Steve Ward
  • Sulaiman
  • Thomas Coyle
  • Thomas A. Wallace
  • Thomas W
  • Timo
  • Torch
  • Ulf Jonsson
  • vasil
  • Vaxian
  • vladi mazzilli
  • wastehl
  • Weston
  • William Kim
  • William N.
  • William Stehl
  • Wesley Venis
The aforementioned have contributed financially to the continuing triumph of 4hv.org. They are deserving of my most heartfelt thanks.
Forums
4hv.org :: Forums :: Computer Science
« Previous topic | Next topic »   

µC tidbits

Move Thread LAN_403
joshua_
Sat Feb 18 2006, 05:41AM Print
joshua_ Registered Member #61 Joined: Thu Feb 09 2006, 05:50AM
Location: Mountain View, CA
Posts: 43
From time to time, you come across useful routines that do all that you need to set up some weird part of the microcontroller (or common part!) that you reference in all of your code. I'm sure they'd be useful to the rest of us; post them in this thread.

This is a fairly standard skeleton, but always helpful to have a pasteable source:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
isr_save_ram    UDATA
ISR_STAT        RES     01
ISR_BSR         RES     01
ISR_W           RES     01

reset_vec       CODE 0x00
        GOTO    start

inth_vec        CODE 0x08
        GOTO    isr

intl_vec        CODE 0x18
        GOTO    isr

main            CODE
start:
        ...

isr:
        MOVWF   ISR_W
        MOVFF   STATUS, ISR_STAT
        MOVFF   BSR, ISR_BSR

        MOVFF   ISR_BSR, BSR
        MOVF    ISR_W, W
        MOVFF   ISR_STAT, STATUS
        RETFIE
;;;;;;;;;;;;;;;;;;;;;;;;;;;;


Similarly useful is this UART bringup code:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
UART    CODE

        GLOBAL  uart_init
uart_init:
        BSF             TRISC, 7                ; set port directions
        BCF             TRISC, 6

        BCF             PIE1, RCIE
        BCF             PIE1, TXIE

        BCF             BAUDCON, ABDEN  ; we'll set the baud rate
        BCF             TXSTA, BRGH             ; low speed
        BSF             BAUDCON, BRG16  ; 16bit baud rate
        MOVLW   (((32000000/19200)/16)-1) >> 8
        MOVF    SPBRGH
        MOVLW   (((32000000/19200)/16)-1) & 0xFF
        MOVF    SPBRG

        BCF             RCSTA, CREN             ; receive disable
        BCF             TXSTA, TXEN             ; transmit disable
        BCF             TXSTA, SYNC             ; async mode

        BSF             RCSTA, SPEN             ; enable the serial port

        BCF             TXSTA, RX9              ; 8bit tx
        BSF             TXSTA, TXEN             ; transmit enable

        BCF             RCSTA, RX9              ; 8bit rx
        BSF             RCSTA, CREN             ; receive enable

        puts    uart_init_initializing

        RETURN
uart_init_initializing:
        DB              "Alive!\r\n",0

        GLOBAL  uart_putc
uart_putc:
        BTFSS   TXSTA, TRMT
         GOTO   uart_putc
        MOVWF   TXREG
        RETURN

        GLOBAL  uart_puts
uart_puts:
        TBLRD*+
        MOVF    TABLAT,W
        BTFSC   STATUS, Z
         RETURN
        CALL    uart_putc
        GOTO    uart_puts

        END
;;;;;;;;;;;;;;;;;;;;;;;;

What have you guys got? I'm looking for a little bit of ADC sample code for my segway project.
Back to top
Carbon_Rod
Sat Feb 18 2006, 06:01AM
Carbon_Rod Registered Member #65 Joined: Thu Feb 09 2006, 06:43AM
Location:
Posts: 1155

The microchip forum is the place to start as they have seen it all:
Link2

I have been there more than a few times (like when some errata fails to be sent with the app-notes)

The problem with ASM is it does not like to port, the code will differ for the P16c, DSPIC, and PIC18LFxxxx.

Like some PIC18 chips use the 55h preamble code to set the baud rate automatically in the EUART,

Note microchip's "Application Maestro" is free and has good lib docs (even for the ASM auto-code.)
Link2


What chip(s) do you use?
Cheers,
Back to top
joshua_
Sat Feb 18 2006, 06:18AM
joshua_ Registered Member #61 Joined: Thu Feb 09 2006, 05:50AM
Location: Mountain View, CA
Posts: 43
Carbon_Rod wrote ...

The microchip forum is the place to start as they have seen it all:
Link2

I have been there more than a few times (like when some errata fails to be sent with the app-notes)
I took a look there once, but mostly dismissed it. I'll have to look again.

wrote ...

The problem with ASM is it does not like to port, the code will differ for the P16c, DSPIC, and PIC18LFxxxx.
Many PICs have similar startup sequences if they have the same type of peripheral -- they pretty much just glue the same logic blocks onto the bus, just in different permutations.

wrote ...

Note microchip's "Application Maestro" is free and has good lib docs (even for the ASM auto-code.)
Link2
I took a look into that. I have it on one of the machines that I've installed mplab on, and it seems to be horribly out of date and lacking in samples for lots of the parts. Or should I be downloading modules for it separately?

wrote ...

What chip(s) do you use?
When I sample, I usually go after the 18F2550s (FS-USB PICs) since they tend to have just about the right combination of stuff for me.
Back to top
Steve Conner
Sat Feb 18 2006, 11:44PM
Steve Conner Registered Member #30 Joined: Fri Feb 03 2006, 10:52AM
Location: Glasgow, Scotland
Posts: 6706
I'm a 16F877 man, myself. I think the code joshua posted is for an 18 series since it uses the TBLRD instruction. It also mentions a "puts" macro which would need to be defined elsewhere.

I do a bunch of PIC programming as part of my job, and I do indeed have a bunch of standard skeleton routines, for things like initialisation, keypad scanning, LCD drivers, and quadrature encoders, that I copy and paste around as needed. I can't post any code here though since it's commercially sensitive.
Back to top
Bjørn
Mon Feb 20 2006, 10:35AM
Bjørn Registered Member #27 Joined: Fri Feb 03 2006, 02:20AM
Location: Hyperborea
Posts: 2058
Here is my 8x8 bit multiplication for PIC16xxx, it is only 11 instructions, uses no temporary variables and takes a constant 67 cycles. It is pretty optimal for low resource use, one cycle and one memory location can be saved if mulcnd is contained in W on entry. The basic method can be found in the Microchip application notes, but their implementation is not very optimal.

mul8x8s clrf    prodH
        movlw   .128
        movwf   prodL
        movfw   mulcnd
mulsl   rrf     mulplr
        skpnc
        addwf   prodH
        rrf     prodH
        rrf     prodL
        skpc
        goto    mulsl
Back to top
Steve Conner
Mon Feb 20 2006, 12:11PM
Steve Conner Registered Member #30 Joined: Fri Feb 03 2006, 10:52AM
Location: Glasgow, Scotland
Posts: 6706
Cool, I always used the Microchip multiply routine that takes 77 cycles or whatever it is. Just one question, what is "movfw", is it a macro you defined somewhere else, or did you mean to type movwf?
Back to top
Bjørn
Mon Feb 20 2006, 01:29PM
Bjørn Registered Member #27 Joined: Fri Feb 03 2006, 02:20AM
Location: Hyperborea
Posts: 2058
"movfw file" is what Microchip calls a "special instruction mnemonic" it is identical to "movf file,W" and is accepted by any MPASM compatible assembler.

I try to not specify the destination of an operation with ",W" or ",F" in cases that are obvious then I can easily spot the non obvious cases in the source code and I have less chance of mixing it up.

I find the Microchip assembly language a bit peculiar, but after I made my own system that I find logical I have no problems.
Back to top

Moderator(s): Chris Russell, Noelle, Alex, Tesladownunder, Dave Marshall, Dave Billington, Bjørn, Steve Conner, Wolfram, Kizmo, Mads Barnkob

Go to:

Powered by e107 Forum System
 
Legal Information
This site is powered by e107, which is released under the GNU GPL License. All work on this site, except where otherwise noted, is licensed under a Creative Commons Attribution-ShareAlike 2.5 License. By submitting any information to this site, you agree that anything submitted will be so licensed. Please read our Disclaimer and Policies page for information on your rights and responsibilities regarding this site.