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.

? about banking and CBLOCK w/16F1827

Status
Not open for further replies.

AGCB

Member
The data sheet says the total GPRs are spread over 15(I think) banks. Am I right in assuming that CBLOCK entries will use up all GPR in bank 0 before using any in bank 1? I can't find it specified in the data sheet. And if all in one bank then no banking problems? But if in several banks then banking required?
Thanks Aaron
 
The banks aren't contiguous, so I would presume you would need to specify each block in it's own CBLOCK?.

However, there is the option of referencing many of them as a contiguous block, and you could access those using that much higher address.

I use CBLOCK 0x2050 to allocate a 240 byte buffer, starting in bank1.
 
Nigel
Do you have to write and read it using FSR? If you would attach a sample code, it would help me understand. I've found that the data sheet for F1827 is vague and skimpy. I've just started to use this chip and there are few code examples to be found in assembly.
Thanks Aaron
 
Nigel
Do you have to write and read it using FSR? If you would attach a sample code, it would help me understand. I've found that the data sheet for F1827 is vague and skimpy. I've just started to use this chip and there are few code examples to be found in assembly.
Thanks Aaron

Yes, there are two FSR's now, and extra instructions as well - the datasheet explains them.

Here's a routine I use to clear my buffer.

Clr_Buffer
banksel 0x00 ; ensure bank 0
movlw Buff_Size
movwf count ; set number of bytes to clear
movlw high Buff
movwf FSR0H ; set buffer address
movlw low Buff
movwf FSR0L
BuffLoop clrw
movwi FSR0++ ; clear each GPR in turn
decfsz count, f ; check if all cleared
bra BuffLoop
return
 
Last edited:
Hi,

Just to follow on what Nigel has said.

You have a total of 15 memory banks on that chip, the first 9 of them are used by the System Registers but only the first 5 are used by the User Registers / GPR.

You can place any amout of GPRs in any of the 5 registers, though it is normal to fill up bank 0 first; as soon as you go into the other banks that means you have to remember to use the Banksel instruction before them as well as for all the system registers.

There is an exception to this, notice the last 16 bytes of each register as called Access, this area is common to whatever bank you are in, so if you are in Bank 0,1 or 4 then data is always the same in those 16 bytes, and you can read and write to them without changing the Banksel.

One of the nice things about coding the 18F range of chips is that their GPR banks are 256 bytes each and all the system registers are in the same single bank so changing banks is seldom done.
 
Thanks Nigel and WP100. As soon as I have time I'll sit down with the data sheet and try to put all this together in my head. Aaron
 
Hi,

The Banks can seem confusing.

Try to think of it as originally one continuous list, comprisng of the System Registers like PORTA, TRISA, STATUS and then space for your User registers for your data files / variables.

Thats nice and simple so you just specify a register and the system knows it must be on that one list or Bank

Then someone comes along and decides to complicate things, instead of one long list he chops it into 4 sections, all containing some system and users registers.
Many 16F chips have just 4 banks, but seems chopper went mad on the 1827, though basically it really only uses banks 0-8, not all 0-15

When you want to use any register in your program code you now have to tell the system which of the 4 sections or BANK the particular register is in.

So the original one section /bank code would be like this

movlw 0x00
movwf PORTA
movwf TRISA
movwf TRISB

With it now in 4 or more banks it has to become

movlw 0x00

banksel PORTA
movwf PORTA

banksel TRISA
movwf TRISA
movwf TRISB

banksel 0 ; return to bank 0

You will find it a real pain in the backside when you advance onto larger or more complex projects using more features of the chip , thats one reason why I moved to the 18F chips, all the system registers are in one Bank and you have 384 bytes of user registers before you need to change User register banks.

It 99% compatible with 16F assembler instruction - well worth changing imho.

hth
 
I've been sorta looking at the 18f chips but have not played with one yet. Would there be any reason to come back to 16f once having moved on to 18f? Are there any disadvantages for, say, smaller programs? I don't see many good tutorials for 18 series like I learned with for the f84a, 628. Theres a lot of new instructions, most being easily figured out but some looking and sounding strange.
And it seems most people program 18s with C which I do not know or understand. How much can a old dog learn??? I wanted to learn the 1827 for the pulse steering feature for controling multiple motors on a robot that I began building 20 years ago. Aaron
 
Hi,

Know what you mean, probably 90%+ of the diy Pic tutorials and projects are based on the 84A, 628A, 88 and 876/7 chips.

If you are familiar with the 16F instructions the few extra 18F ones will only help and the odd old 16F ones will be caught out for you when building the 18F project

Like using the 16F chip you just need a simple template type file for the 18Fs to just set things up initially as all the new features always seem a bit confusing.
- can post a copy if you go that way.


The 18Fs are good for C as they have plenty of program memory and many have inbuilt usb hardware, but they are 100% fine for assembler.

I've used the 28 pin 18f2520 and the 40pin 4520 and 4620 chips to replace the old 16f 876 and 877 chips and never looked back at using a 16F chip but still use 16F code snippets I may need without problems.

Too old, nah - think I'm older that the ET assembler guru Nigel, so you must be still a young lad by comparison.

I know he's a bit of a Luddite like me when it comes to C but perhaps he might think about adding a 18F Assembler update section to his tutorial pages.
 
Last edited:
you just need a simple template type file for the 18Fs to just set things up initially

I've made up templates for the 16f chips I've used. They work good and help me remember things I would otherwise foget.


perhaps he might think about adding a 18F Assembler update section to his tutorial pages.

I've been looking and hoping for the same thing!!!!
 
Hi,

If you do a search of this forum with '18F assembler' there is a surprising amount of content and code examples .
 
I've been sorta looking at the 18f chips but have not played with one yet. Would there be any reason to come back to 16f once having moved on to 18f? Are there any disadvantages for, say, smaller programs? I don't see many good tutorials for 18 series like I learned with for the f84a, 628. Theres a lot of new instructions, most being easily figured out but some looking and sounding strange.
And it seems most people program 18s with C which I do not know or understand. How much can a old dog learn??? I wanted to learn the 1827 for the pulse steering feature for controling multiple motors on a robot that I began building 20 years ago. Aaron

The Enhanced 16F series (like the 1827) are a good stepping stone, it introduces many very valuable extra features, yet remains a 16F at heart. The 16F1827 is also amazingly cheap, a nice chip.
 
I know he's a bit of a Luddite like me when it comes to C but perhaps he might think about adding a 18F Assembler update section to his tutorial pages.

I have actually been going through them, and converting to 18F assembler - but my current long term 16F1827 project has got in the way.
 
Hi,

If you do a search of this forum with '18F assembler' there is a surprising amount of content and code examples .

I can not make search work. See "searches" in the 'site feedback furum'.

Nor can I find any instructions!
 
Last edited:
Here is some 16f1827 assembly code, compiled from GCBasic. Just don't ask me to explain it:), as assembler is not something I dig into unless necessary! The code uses the hardware uart to echo an input buffer variable (i.e. terminal input) back to the terminal, and output a number from 0-9 on the leds. The uart is initialized for 9600 baud 8N1.

Code:
;********************************************************************************

;Set up the assembler options (Chip type, clock source, other bits and pieces)
 LIST p=16F1827, r=DEC
#include <P16F1827.inc>
 __CONFIG _CONFIG1, _BOREN_OFF & _MCLRE_ON & _PWRTE_ON & _WDTE_OFF & _FOSC_HS
 __CONFIG _CONFIG2, _LVP_OFF & _PLLEN_OFF

;********************************************************************************

;Set aside memory locations for variables
DELAYTEMP	EQU	112
DELAYTEMP2	EQU	113
SysWaitTempMS	EQU	114
SysWaitTempMS_H	EQU	115
COMMANDX	EQU	579
COMMAND	EQU	32
HSERRECEIVE	EQU	33
LOOPS	EQU	34
SERDATA	EQU	35
SysTemp1	EQU	36

;********************************************************************************

;Alias variables
AFSR0	EQU	FSR0L
AFSR0_H	EQU	FSR0H

;********************************************************************************

;Vectors
	ORG	0
	goto	BASPROGRAMSTART
	ORG	4
	retfie

;********************************************************************************

;Start of program memory page 0
	ORG	5
BASPROGRAMSTART
;Call initialisation routines
	call	INITSYS
	call	INITUSART

;Start of the main program
	banksel	TRISB
	bcf	TRISB,3
	bcf	TRISB,4
	bcf	TRISB,5
	bcf	TRISB,6
	banksel	LOOPS
	clrf	LOOPS
SysForLoop1
	incf	LOOPS,F
	bsf	PORTB,3
	movlw	250
	movwf	SysWaitTempMS
	clrf	SysWaitTempMS_H
	call	Delay_MS
	bcf	PORTB,3
	movlw	250
	movwf	SysWaitTempMS
	clrf	SysWaitTempMS_H
	call	Delay_MS
	movlw	4
	subwf	LOOPS,W
	btfss	STATUS, C
	goto	SysForLoop1
SysForLoopEnd1
START
;FNSTART,HSERRECEIVE
	call	FN_HSERRECEIVE138
	movlw	low COMMANDX
	addlw	1
	movwf	AFSR0
	clrf	SysTemp1
	movlw	high COMMANDX
	addwfc	SysTemp1,W
	movwf	AFSR0_H
	movf	HSERRECEIVE,W
	movwf	INDF0
	movlw	low COMMANDX
	addlw	1
	movwf	AFSR0
	clrf	SysTemp1
	movlw	high COMMANDX
	addwfc	SysTemp1,W
	movwf	AFSR0_H
	movf	INDF0,W
	movwf	SERDATA
	call	HSERSEND
	movlw	low COMMANDX
	addlw	1
	movwf	AFSR0
	clrf	SysTemp1
	movlw	high COMMANDX
	addwfc	SysTemp1,W
	movwf	AFSR0_H
	movlw	48
	subwf	INDF0,W
	movwf	COMMAND
	btfss	COMMAND,0
	goto	ELSE2_1
	bsf	PORTB,3
	goto	ENDIF2
ELSE2_1
	bcf	PORTB,3
ENDIF2
	btfss	COMMAND,1
	goto	ELSE3_1
	bsf	PORTB,4
	goto	ENDIF3
ELSE3_1
	bcf	PORTB,4
ENDIF3
	btfss	COMMAND,2
	goto	ELSE4_1
	bsf	PORTB,5
	goto	ENDIF4
ELSE4_1
	bcf	PORTB,5
ENDIF4
	btfss	COMMAND,3
	goto	ELSE5_1
	bsf	PORTB,6
	goto	ENDIF5
ELSE5_1
	bcf	PORTB,6
ENDIF5
	goto	START
BASPROGRAMEND
	sleep
	goto	BASPROGRAMEND

;********************************************************************************

Delay_MS
	incf	SysWaitTempMS_H, F
DMS_START
	movlw	227
	movwf	DELAYTEMP2
DMS_OUTER
	movlw	6
	movwf	DELAYTEMP
DMS_INNER
	decfsz	DELAYTEMP, F
	goto	DMS_INNER
	decfsz	DELAYTEMP2, F
	goto	DMS_OUTER
	decfsz	SysWaitTempMS, F
	goto	DMS_START
	decfsz	SysWaitTempMS_H, F
	goto	DMS_START
	return

;********************************************************************************

;Overloaded signature: 
FN_HSERRECEIVE138
	call	HSERRECEIVE139
	movf	SERDATA,W
	movwf	HSERRECEIVE
	return

;********************************************************************************

;Overloaded signature: BYTE:
HSERRECEIVE139
SysWaitLoop2
	btfss	PIR1,RCIF
	goto	SysWaitLoop2
	btfss	PIR1,RCIF
	goto	ENDIF6
	banksel	RCREG
	movf	RCREG,W
	banksel	SERDATA
	movwf	SERDATA
ENDIF6
	banksel	RCSTA
	btfss	RCSTA,OERR
	goto	ENDIF7
	bcf	RCSTA,CREN
	bsf	RCSTA,CREN
ENDIF7
	banksel	STATUS
	return

;********************************************************************************

HSERSEND
SysWaitLoop1
	btfss	PIR1,TXIF
	goto	SysWaitLoop1
	movf	SERDATA,W
	banksel	TXREG
	movwf	TXREG
	banksel	STATUS
	return

;********************************************************************************

INITSYS
	banksel	ADCON0
	bcf	ADCON0,ADON
	bcf	ADCON1,ADFM
	banksel	ANSELA
	clrf	ANSELA
	clrf	ANSELB
	banksel	CM2CON0
	bcf	CM2CON0,C2ON
	bcf	CM1CON0,C1ON
	banksel	PORTA
	clrf	PORTA
	clrf	PORTB
	return

;********************************************************************************

INITUSART
	movlw	7
	banksel	SPBRG
	movwf	SPBRG
	movlw	2
	movwf	SPBRGH
	bsf	BAUDCON,BRG16
	bsf	TXSTA,BRGH
	bcf	TXSTA,SYNC
	bsf	RCSTA,SPEN
	bsf	RCSTA,CREN
	bsf	TXSTA,TXEN
	banksel	STATUS
	return

;********************************************************************************

;Start of program memory page 1
	ORG	2048

 END
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top