µC tidbits
joshua_, Sat Feb 18 2006, 05:41AM
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.
Re:
µC tidbits
Carbon_Rod, Sat Feb 18 2006, 06:01AM
The microchip forum is the place to start as they have seen it all:
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.)
What chip(s) do you use?
Cheers,
Re:
µC tidbits
joshua_, Sat Feb 18 2006, 06:18AM
Carbon_Rod wrote ...
The microchip forum is the place to start as they have seen it all:
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.)
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.
Re: µC tidbits
Steve Conner, Sat Feb 18 2006, 11:44PM
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.
Re:
µC tidbits
Bjørn, Mon Feb 20 2006, 10:35AM
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
Re: µC tidbits
Steve Conner, Mon Feb 20 2006, 12:11PM
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?
Re: µC tidbits
Bjørn, Mon Feb 20 2006, 01:29PM
"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.