Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

need help modifying some pic ASM code

Status
Not open for further replies.

justDIY

Active Member
:eek: I have to admit I don't know much about pic assembly. I know most of the simpler instructions, enough that I can poke around an asm file if my basic program has some sort of weird glitch, but for the most part, I'm spoiled by basic.

I'm trying to modify the source for TINY BOOTLOADER to operate with the internal OSC + PLL on my 18F2620. I've changed the necessary fuses, and added what I think are the correct instructions, but it has a few weird problems.

problem 1) the bootloader operates at 28800 bps, not 57600 bps as instructed, which indicates the cpu is running slower than calculated... is it running at 16mhz instead of 32mhz, since 57600/2 = 28800?

problem 2) the bootloader self-destructs when it programs the pic, as if it is overwriting part of itself. after uploading my main program (which skips several bytes for the bootloader's block), I'm not able to connect to the bootloader again, after resetting the pic.

Here's what I've managed to cobble together from the original code example, the parts I've changed are in red:

Code:
	radix DEC
	LIST      P=18F2620	; change also: Configure->SelectDevice from Mplab 
	xtal EQU 32000000		; you may want to change: _XT_OSC_1H  _HS_OSC_1H  _HSPLL_OSC_1H
	[COLOR="Red"]baud EQU 57600[/COLOR]			; the desired baud rate, originally 115200
	; The above 3 lines can be changed and buid a bootloader for the desired frequency (and PIC type)
	
	;********************************************************************
	;	Tiny Bootloader		18F series		Size=100words
	;	claudiu.chiculita@ugal.ro
	;	http://www.etc.ugal.ro/cchiculita/software/picbootloader.htm
	;********************************************************************
	

	#include "icdpictypes.inc"	;takes care of: #include "p18fxxx.inc",  max_flash, IdTypePIC
	#include "spbrgselect.inc"	; RoundResult and baud_rate

	[COLOR="Red"]#define first_address max_flash-204[/COLOR]		;102 words originally 100
    	[COLOR="Red"]CONFIG OSC 		= INTIO67
	CONFIG FCMEN 	= OFF
	CONFIG IESO 	= OFF
	CONFIG PWRT 	= ON
	CONFIG BOREN 	= ON
	CONFIG BORV		= 2		; 4.2v
	CONFIG WDT		= OFF
	CONFIG WDTPS	= 1
	CONFIG MCLRE	= ON
	CONFIG PBADEN	= OFF
	CONFIG CCP2MX	= PORTC
	CONFIG STVREN	= ON
	CONFIG LVP		= OFF
	CONFIG XINST	= OFF
	CONFIG DEBUG	= OFF
	CONFIG CP0		= OFF
	CONFIG CP1		= OFF
	CONFIG CP2		= OFF
	CONFIG CP3		= OFF
	CONFIG CPB		= OFF
	CONFIG CPD		= OFF
	CONFIG WRT0		= OFF
	CONFIG WRT1		= OFF
	CONFIG WRT2		= OFF
	CONFIG WRT3		= OFF
	CONFIG WRTB		= OFF
	CONFIG WRTC		= OFF
	CONFIG WRTD		= OFF
	CONFIG EBTR0 	= OFF
	CONFIG EBTR1 	= OFF
	CONFIG EBTR2 	= OFF
	CONFIG EBTR3 	= OFF
	CONFIG EBTRB 	= OFF[/COLOR]

;----------------------------- PROGRAM ---------------------------------
	cblock 0
	crc
	i
	cnt1
	cnt2
	cnt3
	counter_hi
	counter_lo
	flag
	endc
	cblock 10
	buffer:64
	endc
	
SendL macro car
	movlw car
	movwf TXREG
	endm
	
;0000000000000000000000000 RESET 00000000000000000000000000

		ORG     0x0000
		GOTO    IntrareBootloader

;view with TabSize=4
;&&&&&&&&&&&&&&&&&&&&&&&   START     &&&&&&&&&&&&&&&&&&&&&&
;----------------------  Bootloader  ----------------------
;PC_flash:		C1h				U		H		L		x  ...  <64 bytes>   ...  crc	
;PC_eeprom:		C1h			   	40h   EEADR   EEDATA	0		crc					
;PC_cfg			C1h			U OR 80h	H		L		1		byte	crc
;PIC_response:	   type `K`
	
	ORG first_address		;space to deposit first 4 instr. of user prog.
	nop
	nop
	nop
	nop
	org first_address+8
IntrareBootloader
						
	[COLOR="Red"]movlw b'01110000'[/COLOR]	   ;set intrc to 8mhz
    	[COLOR="Red"]movwf OSCCON[/COLOR]
 						   ;init serial port
	movlw b'00100100'
	movwf TXSTA
	movlw spbrg_value
	movwf SPBRG
	movlw b'10010000'
	movwf RCSTA

	[COLOR="Red"]bsf   OSCTUNE,6,0 [/COLOR]		;enable 4x PLL
							;wait for computer
	rcall Receive			
	sublw 0xC1				;Expect C1h
	bnz way_to_exit
	SendL IdTypePIC			;send PIC type
MainLoop
	SendL 'K'				; "-Everything OK, ready and waiting."
mainl
	clrf crc
	rcall Receive			;Upper
	movwf TBLPTRU
		movwf flag			;(for EEPROM and CFG cases)
	rcall Receive			;Hi
	movwf TBLPTRH
		movwf EEADR			;(for EEPROM case)
	rcall Receive			;Lo
	movwf TBLPTRL
		movwf EEDATA		;(for EEPROM case)

	rcall Receive			;count
	movwf i
	incf i
	lfsr FSR0, (buffer-1)
rcvoct						;read 64+1 bytes
		movwf TABLAT		;prepare for cfg; => store byte before crc
	rcall Receive
	movwf PREINC0
	decfsz i
	bra rcvoct
	
	tstfsz crc				;check crc
	bra ziieroare
		btfss flag,6		;is EEPROM data?
		bra noeeprom
		movlw b'00000100'	;Setup eeprom
		rcall Write
		bra waitwre
noeeprom
		btfss flag,7		;is CFG data?
		bra noconfig
		tblwt*				;write TABLAT(byte before crc) to TBLPTR***
		movlw b'11000100'	;Setup cfg
		rcall Write
		bra waitwre
noconfig
							;write
eraseloop
	movlw	b'10010100'		; Setup erase
	rcall Write
	TBLRD*-					; point to adr-1
	
writebigloop	
	movlw 8					; 8groups
	movwf counter_hi
	lfsr FSR0,buffer
writesloop
	movlw 8					; 8bytes = 4instr
	movwf counter_lo
writebyte
	movf POSTINC0,w			; put 1 byte
	movwf TABLAT
	tblwt+*
	decfsz counter_lo
	bra writebyte
	
	movlw	b'10000100'		; Setup writes
	rcall Write
	decfsz counter_hi
	bra writesloop
waitwre	
	;btfsc EECON1,WR		;for eeprom writes (wait to finish write)
	;bra waitwre			;no need: round trip time with PC bigger than 4ms
	
	bcf EECON1,WREN			;disable writes
	bra MainLoop
	
ziieroare					;CRC failed
	SendL 'N'
	bra mainl
	  
;******** procedures ******************

Write
	movwf EECON1
	movlw 0x55
	movwf EECON2
	movlw 0xAA
	movwf EECON2
	bsf EECON1,WR			;WRITE
	nop
	;nop
	return


Receive
	movlw xtal/2000000+1	; for 20MHz => 11 => 1second delay
	movwf cnt1
rpt2						
	clrf cnt2
rpt3
	clrf cnt3
rptc
		btfss PIR1,RCIF			;test RX
		bra notrcv
	    movf RCREG,w			;return read data in W
	    addwf crc,f				;compute crc
		return
notrcv
	decfsz cnt3
	bra rptc
	decfsz cnt2
	bra rpt3
	decfsz cnt1
	bra rpt2
	;timeout:
way_to_exit
	bcf	RCSTA,	SPEN			; deactivate UART
	bra first_address
;*************************************************************
; After reset
; Do not expect the memory to be zero,
; Do not expect registers to be initialised like in catalog.

            END
 
Could it be that you need to select the internal oscillator block?
Code:
	movlw b'011100[COLOR="Red"]1[/COLOR]0'	   ;set intrc to 8mhz
    	movwf OSCCON

Edit, Looking at the data sheet, probably not.

Mike.
 
Last edited:
what if you change
Code:
	movlw spbrg_value
	movwf SPBRG

to
Code:
	movlw .34
	movwf SPBRG

Maybe not a bad idea to wait until the Internal OSC is stable before continue the program.

Not sure if i would trust the internal OSC for a Bootloader so far... sure you may need to tweak/calibrate it a little bit first.

and how about...
Code:
#define first_address max_flash-206
as you also added BSF OSCCON ???
 
Last edited:
mmm, try that one
Code:
	movlw   b'01110000'  	;set intrc to 8mhz
	movwf   OSCCON
	bsf     OSCTUNE,6,0 	;enable 4x PLL
				
	btfss   OSCCON,2	;wait 'til internal osc is stable
	GOTO    $-2

And change
Code:
	#define first_address max_flash-[B]210[/B]
 
I tried all of your suggestions, in various combinations, as well as putting PLLEN after the code that checks for the osc to be stable.

was not able to achieve any usable baud rates beyond 28800.

so I decided to fall back on the regular 8mhz intrc and 38400 baud, which gives a baud error of 0.16%

I still have the problem of the bootloader self destructing. once I flash my main code to the chip, via the bootloader, I am unable to contact the loader again.

i suspect the cause is from modifying the starting address?
Code:
#define first_address max_flash-208

first few instructions
Code:
movlw b'01110010'	    ;set intrc to 8mhz
movwf OSCCON

btfss   OSCCON,2		;wait 'til internal osc is stable
GOTO    $-2

any other ideas?
 
not at this moment, but i tested with the mod i suggested and i can connect to the PIC,it recognize the device... but it always tell me...
could not write
if i try to dump an .HEX fil into.

Weird... And this, with those few PIC model i tried, with or without crystal.

So, for now, i will bet your device programmer don't set/program correctly the configuration fuses... but yeah... it's just a bet. As always, i doubt the reliability of the internal OSC for a bootloader. Did you tried with a crystal?

If i can find time and have some success with, i'll forward the info here.
 
Last edited:
ok, I learned the pc-side flash application will only 'protect' the first 100 words of the bootblock, so the extra words added to the code were resulting in part (first few words?) of the program getting overwritten. that is why the 'loader would self destruct.

I haven't had opportunity to do a side by side comparison, but I found this modified source for a 18F4680, which when I changed the config fuses and processor type, compiled for my 18f2620. best thing is, it's the original 100 word size, so it does not self destruct.

thanks Steve and everyone else for your help!

Code:
	radix DEC
	LIST      P=18F2620	; change also: Configure->SelectDevice from Mplab 
xtal EQU 8000000		; you may want to change: _XT_OSC_1H  _HS_OSC_1H  _HSPLL_OSC_1H
baud EQU 38400			; standard TinyBld baud rates: 115200 or 19200
	; The above 3 lines can be changed and build a bootloader for the desired frequency (and PIC type)
	
	;********************************************************************
	;	Tiny Bootloader		18F series		Size=100words
	;	claudiu.chiculita@ugal.ro
	;	http://www.etc.ugal.ro/cchiculita/software/picbootloader.htm
	;	Modified by Nam Nguyen-Quang for testing different PIC18Fs with tinybldWin.exe v1.8
	;	namqn@yahoo.com
	;********************************************************************
	
;	Copy these include files to your project directory (i.e. they are in the same
;	directory with your .asm source file), if necessary

	#include "icdpictypes.inc"	; Takes care of: #include "p18fxxx.inc",  max_flash, IdTypePIC
	#include "spbrgselect.inc"	; RoundResult and baud_rate

		#define first_address max_flash-200		;100 words


    CONFIG OSC 		= INTIO67
	CONFIG FCMEN 	= OFF
	CONFIG IESO 	= OFF
	CONFIG PWRT 	= ON
	CONFIG BOREN 	= ON
	CONFIG BORV		= 1		; 4.2v
	CONFIG WDT		= OFF
	CONFIG WDTPS	= 1
	CONFIG MCLRE	= ON
	CONFIG PBADEN	= OFF
	CONFIG CCP2MX	= PORTC
	CONFIG STVREN	= ON
	CONFIG LVP		= OFF
	CONFIG XINST	= OFF
	CONFIG DEBUG	= OFF
	CONFIG CP0		= OFF
	CONFIG CP1		= OFF
	CONFIG CP2		= OFF
	CONFIG CP3		= OFF
	CONFIG CPB		= OFF
	CONFIG CPD		= OFF
	CONFIG WRT0		= OFF
	CONFIG WRT1		= OFF
	CONFIG WRT2		= OFF
	CONFIG WRT3		= OFF
	CONFIG WRTB		= OFF
	CONFIG WRTC		= OFF
	CONFIG WRTD		= OFF
	CONFIG EBTR0 	= OFF
	CONFIG EBTR1 	= OFF
	CONFIG EBTR2 	= OFF
	CONFIG EBTR3 	= OFF
	CONFIG EBTRB 	= OFF


;----------------------------- PROGRAM ---------------------------------
	cblock 0
	crc
	i
	cnt1
	cnt2
	cnt3
	counter_hi
	counter_lo
	flag
	endc
	cblock 10
	buffer:64
	dummy4crc
	endc
	
SendL macro car
	movlw car
	movwf TXREG
	endm
	
;0000000000000000000000000 RESET 00000000000000000000000000

		ORG     0x0000
		GOTO    IntrareBootloader

;view with TabSize=4
;&&&&&&&&&&&&&&&&&&&&&&&   START     &&&&&&&&&&&&&&&&&&&&&&
;----------------------  Bootloader  ----------------------
;PC_flash:		C1h				U		H		L		x  ...  <64 bytes>   ...  crc	
;PC_eeprom:		C1h			   	40h   EEADR   EEDATA	0		crc					
;PC_cfg			C1h			U OR 80h	H		L		1		byte	crc
;PIC_response:	   type `K`
	
	ORG first_address		;space to deposit first 4 instr. of user prog.
	nop
	nop
	nop
	nop
	org first_address+8
IntrareBootloader
							;init IntOSC, added by Nam Nguyen-Quang
	movlw 0x70
	movwf OSCCON
	; the above 2 lines should be commented out for designs not using the internal oscilator
	; or for the chips without the internal oscilator
							;init serial port
	movlw b'00100100'
	movwf TXSTA
	movlw spbrg_value
	movwf SPBRG
	movlw b'10010000'
	movwf RCSTA
							;wait for computer
	rcall Receive			
	sublw 0xC1				;Expect C1h
	bnz way_to_exit
	SendL IdTypePIC			;send PIC type
MainLoop
	SendL 'K'				; "-Everything OK, ready and waiting."
mainl
	clrf crc
	rcall Receive			;Upper
	movwf TBLPTRU
		movwf flag			;(for EEPROM and CFG cases)
	rcall Receive			;Hi
	movwf TBLPTRH
		movwf EEADR			;(for EEPROM case)
	rcall Receive			;Lo
	movwf TBLPTRL
		movwf EEDATA		;(for EEPROM case)

	rcall Receive			;count
	movwf i
	incf i
	lfsr FSR0, (buffer-1)
rcvoct						;read 64+1 bytes
		movwf TABLAT		;prepare for cfg; => store byte before crc
	rcall Receive
	movwf PREINC0
	decfsz i
	bra rcvoct
	
	tstfsz crc				;check crc
	bra ziieroare
		btfss flag,6		;is EEPROM data?
		bra noeeprom
		movlw b'00000100'	;Setup eeprom
		rcall Write
		bra waitwre
noeeprom
		btfss flag,7		;is CFG data?
		bra noconfig
		tblwt*				;write TABLAT(byte before crc) to TBLPTR***
		movlw b'11000100'	;Setup cfg
		rcall Write
		bra waitwre
noconfig
							;write
eraseloop
	movlw	b'10010100'		; Setup erase
	rcall Write
	TBLRD*-					; point to adr-1
	
writebigloop	
	movlw 8					; 8groups
	movwf counter_hi
	lfsr FSR0,buffer
writesloop
	movlw 8					; 8bytes = 4instr
	movwf counter_lo
writebyte
	movf POSTINC0,w			; put 1 byte
	movwf TABLAT
	tblwt+*
	decfsz counter_lo
	bra writebyte
	
	movlw	b'10000100'		; Setup writes
	rcall Write
	decfsz counter_hi
	bra writesloop
waitwre	
	;btfsc EECON1,WR		;for eeprom writes (wait to finish write)
	;bra waitwre			;no need: round trip time with PC bigger than 4ms
	
	bcf EECON1,WREN			;disable writes
	bra MainLoop
	
ziieroare					;CRC failed
	SendL 'N'
	bra mainl
	  
;******** procedures ******************

Write
	movwf EECON1
	movlw 0x55
	movwf EECON2
	movlw 0xAA
	movwf EECON2
	bsf EECON1,WR			;WRITE
	nop
	;nop
	return


Receive
	movlw xtal/2000000+1	; for 20MHz => 11 => 1second delay
							; for 18F2xxx chips, this should be xtal/1000000+1
	movwf cnt1
rpt2						
	clrf cnt2
rpt3
	clrf cnt3
rptc
		btfss PIR1,RCIF			;test RX
		bra notrcv
	    movf RCREG,w			;return read data in W
	    addwf crc,f				;compute crc
		return
notrcv
	decfsz cnt3
	bra rptc
	decfsz cnt2
	bra rpt3
	decfsz cnt1
	bra rpt2
	;timeout:
way_to_exit
	bcf	RCSTA,	SPEN			; deactivate UART
	bra first_address
;*************************************************************
; After reset
; Do not expect the memory to be zero,
; Do not expect registers to be initialised like in catalog.

            END
 
as a follow up, I think I was using the wrong value for spbrg ... despite a higher cpu clock given by the PLL, Fosc remains at 8mhz, and the baud rate generator pulls its clock directly from Fosc, not from the cpu clock. So SPBRG = 7 would have been the value for 57600 baud, but with a baud error of nearly 9%, it would generate a lot of errors. 38400 on the other hand has a baud error of 0.16%.
 
Kind of why i created the PicMultiCalc. It find the right value for you in few clicks and show you the error percentage AND find the best match for you... yeah lazy boy tool ;)

In our situation, if it was possible to bypass the utility bootloader code protection, the internal EUSART BRG16 would have been really handy.

115200 baud @32MHz and 0.64 err%
57600 baud @32MHz and 0.08err%

... really not bad ;)
 
Last edited:
your values are correct, assuming we had Fosc of 32mHz... but the pic supports a max of 20mHz for Fosc, via external crystal/clock. running in INTRC + PLL Fosc is only 8mhz, regardless of the cpu clock (4x PLL).

I use an excel spreadsheet I nocked up to calculate baud rates, based on the three formula offered in the datasheet.

how does the 16bit baud generator help in this situation? I understand it can be used to generate very slow baud rates with a high Fosc?
 
A 16 bit SPBRG (divider chain) value can also provide finer divider chain resolution and improve the bit error rate on most of the other baud rates.

**broken link removed**
 
You can download both the Spreadsheet and the stand-alone application pictured above from the following Forum.Microchip thread and try it yourself Sir;

**broken link removed**

Remember to press <enter> after changing the Fosc value (a limitation of the BASIC I used).
 
Sorry, but it really work @32MHz when using the internal 8MHz + PLL :eek:
Code:
        ;       -----------------------------------------------------------------        
        ;
        ;                   [ ----  8 MHZ  ---- ]       [ ----  32 MHz  ---- ]
        ;       Baudrate    SPBRG   TXSTA   ERR %       SPBRG   TXSTA   ERR %
        ;       --------    -----   -----   -----       -----   -----   -----
        ;       2400        .207    h'24'    0.17       .207    h'20'   0.17
        ;       -------------------------------------------------------------
        ;       4800        .103    h'24'    0.17       .103    h'20'   0.17
        ;       -------------------------------------------------------------
        ;       9600        .51     h'24'    0.16       .207    h'24'   0.16
        ;       -------------------------------------------------------------
        ;       19200       .25     h'24'    0.16       .103    h'24'   0.16
        ;       -------------------------------------------------------------
        ;       38400       .12     h'24'    0.16        .51    h'24'   0.16
        ;       -------------------------------------------------------------
        ;       57600       .8      h'24'    3.55        .34    h'24'   0.79
        ;       -------------------------------------------------------------   
        ;       115200      .3      h'24     8.51        .16    h'24'   2.12
        ;
SPBRG_VAL   =   .34
TXSTA_VAL   =   h'24'
        ;       57600 BAUDS @ 32MHz
        ;
        ;       ----------------------------------------------------------------- 

       
        LIST      P=18F4620
        include   "P18F4620.inc"

        CONFIG OSC 	= INTIO67
        CONFIG FCMEN 	= OFF
        CONFIG IESO 	= OFF
        CONFIG PWRT 	= ON
        CONFIG BOREN 	= ON
        CONFIG BORV	= 2		
        CONFIG WDT	= OFF
        CONFIG WDTPS	= 1
        CONFIG MCLRE	= ON
        CONFIG PBADEN	= OFF
        CONFIG CCP2MX	= PORTC
        CONFIG STVREN	= ON
        CONFIG LVP	= OFF
        CONFIG XINST	= OFF
        CONFIG DEBUG	= OFF

        ;
        ;       MACRO
        ;       =====
PAUSE   MACRO   DELAY
	    movlw   low(DELAY)
	    movwf   TMR0L
	    MOVLW   HIGH(DELAY)
	    MOVWF   TMR0H
	    CALL    DODELAY
	    ENDM

HSEROUT MACRO   StringToSend
        LOCAL JumpOver,String
        BRA JumpOver

String  
        DATA StringToSend

JumpOver        
        movlw   UPPER String
        movwf   TBLPTRU
        movlw   HIGH String
        movwf   TBLPTRH
        movlw   LOW String
        movwf   TBLPTRL
        CALL    SENDIT
        ENDM          

        ;
        ;       Software constants
        ;       ==================
                ;
                ;       Delays: to use with PAUSE macro
                ;       -------------------------------
_50MS  = .63973         ;  50 mSec
_500MS = .34286	        ; 500 mSec

		ORG     0
        ;
        ;       Hardware configuration
        ;       ======================
                ;
                ;       Internal OSC 
                ;       ------------
	    movlw   b'01110000'     ; 8mhz
	    movwf   OSCCON          ;
	    bsf     OSCTUNE,6 	    ; enable 4x PLL, now RUN @32MHz
                ;
                ;       USART
                ;       -----
	    movlw   h'90'           ; Enable USART & continuous receive
	    movwf   RCSTA   
	   
	    movlw   TXSTA_VAL       ; Enable USART transmit & set BRGH
	    movwf   TXSTA
	    
	    movlw   SPBRG_VAL       ; Set Baudrate
	    movwf   SPBRG
                ;
                ;       Timer0
                ;       ------
        MOVLW   B'00000110'     ; STOP TIMER0
                                ; CLK source = INTERNAL
                                ; PRESCALER 1:128
        MOVWF   T0CON
        
        ;
        ;       Program start
        ;       =============                
        PAUSE _50MS             ; OSC Settle time

START        
                ;
                ;       Send some data
                ;       --------------     
        HSEROUT "String #1\r\n\0"
        HSEROUT "------- Yet another\r\n\0"
        HSEROUT "----------- One more for the luck\r\n\0"
        HSEROUT "***************************** done ****************\r\n\0"
        PAUSE       _500MS
        BRA         START        

DODELAY	    
	BCF     INTCON,TMR0IF   ; clear overflow flag    
	BSF     T0CON,TMR0ON    ; START TIMER0
	BTFSS   INTCON,TMR0IF   ; overflow?
	GOTO    $-2             ;   ---- NO, spin in round
	BCF     T0CON,TMR0ON    ;   YES, STOP TIMER0        
	RETURN

SENDIT
        tblrd   *+
        MOVF    TABLAT,W        ; get character, save it to W
        BTFSC   STATUS,Z        ; EOM (character=0) ?
        RETURN                  ;   --- YES, Bye bye
        
        movwf   TXREG	        ;   NOPE, send it
        btfss   TXSTA,TRMT      ; wait for data TX
        goto    $-2
        bra     SENDIT          ; fetch next message character from table
	
        END
 
haha, I was talking out of my arse earlier, first I misread some example code, and then misread the datasheet. sorry :)

now, I was looking at the bootloader code, trying to see how to cram the code for brgh16, and pll into it, yet still stay under 100 words. I was wondering if the instructions for re-writing config fuses and data eeprom were removed, it would make room for the extra instructions for setting up the pll and the 16 bit brg. my first attempt at nearly 2am didn't go so well, but I'll take another crack at it now that i'm more awake.
 
unfortunately, the config fuse are not in the code space.

So unless you rewrite the PC software or have access to the source code, you shouldn't have any luck. But yeah, maybe there's some possible PIC code optimisations/modification to do... food for thought :)
 
right, the fuses don't take up code space, but the bootloader firmware has some instructions to test what type of data is being sent by the pc... program memory, data memory or config fuses. so if the sections of code for flashing data memory and config fuses were removed, leaving only code for flashing program memory, perhaps there would be enough room in the 100 word limit to make the changes.
 
High Baudrates in TinyBLD

Hello all,

albeit a bit late (hey, what's a year or so ;) ), i came up with a solution. Havent seen this thread earlier. Now you can use the internal 8MHz oscillator and have high baudrates on the 18F's. EEProm and config-word writing is still intact. See the code below. Hope it helps....

Greetings,

Chris

Code:
	radix DEC
	LIST      P=18F2620	; change also: Configure->SelectDevice from Mplab 
xtal EQU 8000000		; you may want to change: _XT_OSC_1H  _HS_OSC_1H  _HSPLL_OSC_1H
baud EQU .16			; for 115200
;baud EQU .34			; for 57600
;baud EQU .52			; for 38400
;baud EQU .103			; for 19200
	; The above 3 lines can be changed and buid a bootloader for the desired frequency (and PIC type)
	
	#include "icdpictypes.inc"	;takes care of: #include "p18fxxx.inc",  max_flash, IdTypePIC

		#define first_address max_flash-200		;100 words
	CONFIG OSC		= INTIO67
	CONFIG FCMEN	= OFF
	CONFIG IESO		= OFF
	CONFIG PWRT		= ON
	CONFIG WDT		= OFF
	CONFIG WDTPS	= 32
	CONFIG MCLRE	= ON
	CONFIG PBADEN	= OFF
	CONFIG CCP2MX	= PORTBE
	CONFIG STVREN	= OFF
	CONFIG LVP		= OFF
	CONFIG XINST	= OFF
	CONFIG DEBUG	= OFF
	CONFIG CP0		= OFF
	CONFIG CP1		= OFF
	CONFIG CP2		= OFF
	CONFIG CP3		= OFF
	CONFIG CPB		= OFF
	CONFIG CPD		= OFF
	CONFIG WRT0		= OFF
	CONFIG WRT1		= OFF
	CONFIG WRT2		= OFF
	CONFIG WRT3		= OFF
	CONFIG WRTB		= OFF
	CONFIG WRTC		= OFF
	CONFIG WRTD		= OFF
	CONFIG EBTR0	= OFF
	CONFIG EBTR1	= OFF
	CONFIG EBTR2	= OFF
	CONFIG EBTR3	= OFF
	CONFIG EBTRB	= OFF


;----------------------------- PROGRAM ---------------------------------
	cblock 0
	crc
	i
	cnt1
	cnt2
	cnt3
	counter_hi
	counter_lo
	flag
	endc
	cblock 10
	buffer:64
	endc
	
SendL macro car
	movlw car
	movwf TXREG
	endm
	
;0000000000000000000000000 RESET 00000000000000000000000000

	ORG     0x0000
	GOTO    IntrareBootloader

;view with TabSize=4
;&&&&&&&&&&&&&&&&&&&&&&&   START     &&&&&&&&&&&&&&&&&&&&&&
;----------------------  Bootloader  ----------------------
;PC_flash:		C1h				U		H		L		x  ...  <64 bytes>   ...  crc	
;PC_eeprom:		C1h			   	40h   EEADR   EEDATA	0		crc					
;PC_cfg			C1h			U OR 80h	H		L		1		byte	crc
;PIC_response:	   type `K`
	
	ORG first_address		;space to deposit first 4 instr. of user prog.
	nop
	nop
	nop
	nop

	ORG first_address+8

IntrareBootloader
							;switch internal oscillator to 8MHz
	setf OSCCON
							;init serial port
	movlw b'00100100'
	movwf TXSTA
	movlw b'10010000'
	movwf RCSTA
	bsf BAUDCON, BRG16
	movlw baud
	movwf SPBRG
							;wait for computer
	rcall Receive			
	sublw 0xC1				;Expect C1h
	bnz way_to_exit
	SendL IdTypePIC			;send PIC type

MainLoop
	SendL 'K'				; "-Everything OK, ready and waiting."

mainl
	clrf crc
	rcall Receive			;Upper
	movwf TBLPTRU
	movwf flag			;(for EEPROM and CFG cases)
	rcall Receive			;Hi
	movwf TBLPTRH
	movwf EEADR			;(for EEPROM case)
	rcall Receive			;Lo
	movwf TBLPTRL
	movwf EEDATA		;(for EEPROM case)

	rcall Receive			;count
	movwf i
	incf i
	lfsr FSR0, (buffer-1)

rcvoct						;read 64+1 bytes
	movwf TABLAT		;prepare for cfg; => store byte before crc
	rcall Receive
	movwf PREINC0
	decfsz i
	bra rcvoct
	
	tstfsz crc				;check crc
	bra ziieroare
	btfss flag,6		;is EEPROM data?
	bra noeeprom
	movlw b'00000100'	;Setup eeprom
	rcall Write
	bra waitwre

noeeprom
	btfss flag,7		;is CFG data?
	bra noconfig
	tblwt*				;write TABLAT(byte before crc) to TBLPTR***
	movlw b'11000100'	;Setup cfg
	rcall Write
	bra waitwre

noconfig
							;write
eraseloop
	movlw	b'10010100'		; Setup erase
	rcall Write
	TBLRD*-					; point to adr-1
	
writebigloop	
	movlw 8					; 8groups
	movwf counter_hi
	lfsr FSR0,buffer

writesloop
	movlw 8					; 8bytes = 4instr
	movwf counter_lo

writebyte
	movf POSTINC0,w			; put 1 byte
	movwf TABLAT
	tblwt+*
	decfsz counter_lo
	bra writebyte
	
	movlw	b'10000100'		; Setup writes
	rcall Write
	decfsz counter_hi
	bra writesloop

waitwre	
	;btfsc EECON1,WR		;for eeprom writes (wait to finish write)
	;bra waitwre			;no need: round trip time with PC bigger than 4ms
	
	bcf EECON1,WREN			;disable writes
	bra MainLoop
	
ziieroare					;CRC failed
	SendL 'N'
	bra mainl
	  
;******** procedures ******************

Write
	movwf EECON1
	movlw 0x55
	movwf EECON2
	movlw 0xAA
	movwf EECON2
	bsf EECON1,WR			;WRITE
	nop
	;nop
	return

Receive
	movlw xtal/2000000+1	; for 20MHz => 11 => 1second delay
	movwf cnt1

rpt2						
	clrf cnt2

rpt3
	clrf cnt3

rptc
	btfss PIR1,RCIF			;test RX
	bra notrcv
    movf RCREG,w			;return read data in W
    addwf crc,f				;compute crc
	return

notrcv
	decfsz cnt3
	bra rptc
	decfsz cnt2
	bra rpt3
	decfsz cnt1
	bra rpt2
	;timeout:

way_to_exit
	bcf	RCSTA,	SPEN			; deactivate UART
	bra first_address
;*************************************************************
; After reset
; Do not expect the memory to be zero,
; Do not expect registers to be initialised like in catalog.

            END
 
Last edited:
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top