Learning to program the PIC16LF1823

Thread Starter

cmartinez

Joined Jan 17, 2007
8,768
Ok, I think I figured it out.

Say I'm working on pin RA0. To make it work bidirectionally, I'm first going to clear LATA0 and then enable its internal pull up by setting WPUA0, and then set it as an input by setting TRISA0.

If the pin is supposed to be receiving information, all I have to do is read the state of RA0. If I want to output a logic high, all I have to do is leave things as they are (leave TRISA0 as set, that is), but if I want to output a logic low, then I will clear TRISA0. This latter situation will happen because LATA0 had already previously been cleared. At the end of the transmission TRISA0 will be back to being set to make sure that the pin functions as an input again.
 

Thread Starter

cmartinez

Joined Jan 17, 2007
8,768
Internal pull-ups are very weak, about >100k, i2c bus has usually lower pull-up resistor values.
Thanks for that info ... in fact, I've prepared my circuit so I can install my own external pull ups in case I need to tweak their values to adjust for reliability vs power draw.
 

Thread Starter

cmartinez

Joined Jan 17, 2007
8,768
Question, how do I access SRAM (General Purpose Registers) available in memory banks other than Bank 0?

i.e. , if I wanted to access the register located at 0A5h in Bank 1, would directly referring to that address do the trick?

Code:
TEMPVAR1   EQU A5H
movlw d'30'
movwf TEMPVAR1
or would my code have to look like this?;
Code:
TEMPVAR1   EQU A5H
movlb 1                ;select Bank 1
movlw d'30'
movwf TEMPVAR1
Is there a better way to handle this sort of thing?
 

MrSalts

Joined Apr 2, 2020
2,767
Question, how do I access SRAM (General Purpose Registers) available in memory banks other than Bank 0?

i.e. , if I wanted to access the register located at 0A5h in Bank 1, would directly referring to that address do the trick?

Code:
TEMPVAR1   EQU A5H
movlw d'30'
movwf TEMPVAR1
or would my code have to look like this?;
Code:
TEMPVAR1   EQU A5H
movlb 1                ;select Bank 1
movlw d'30'
movwf TEMPVAR1
Is there a better way to handle this sort of thing?
Older chips had a SELECT register snd one bit was used to indicate the active Bank.
otherwise, use the SFR (special function register) that has a BSR bit (bits?). But I haven't used ASM in a long time.

... but I don't know that my suggestions above are "better".
 

Papabravo

Joined Feb 24, 2006
22,083
I'm not 100% sure of this answer, but you might need to set the "Bank Select" Register bits. Let me take a quick look at the datasheet.

EDIT: This datasheet DS40001413F describes on page 317 the MOVLB -- Move literal to BSR (Bank Select Register) -- instruction.
Additional material is on pages 19 and 43 describing direct addressing and indirect addressing.
 
Last edited:

JohnInTX

Joined Jun 26, 2012
4,787
Use the ‘banksel’ directive if it’s supported in pic-as. If not, use the movlb (move literal to BSR) instruction is the most common.
 

Thread Starter

cmartinez

Joined Jan 17, 2007
8,768
Use the ‘banksel’ directive if it’s supported in pic-as. If not, use the movlb (move literal to BSR) instruction.
Yes, 'banksel' is indeed supported in the pic16lf1823, and I use it frequently. But I didn't know it could be also be used on user-declared registers.
 

Thread Starter

cmartinez

Joined Jan 17, 2007
8,768
The datasheet states:

"Use of the linear data memory region allows buffers to be larger than 80 bytes because incrementing the FSR beyond one bank will go directly to the GPR memory of the next bank."

I guess then that in some cases (i.e. arrays) indirect addressing would be a better strategy to handle SRAM beyond 80 bytes? ... then again, incrementing or decrementing the FSR is not a straightforward thing to do, since it's actually a 16 bit (2 byte) register.
 

JohnInTX

Joined Jun 26, 2012
4,787
The nice thing about ‘banksel’ is that it will determine the bank setting from the variable name if fully declared. For example:
Var1 equ 403h
Var2 equ 753h

banksel Var1 generates movlb 4 and banksel Var2 generates movlb 7 on an 18F part.
 

MrSalts

Joined Apr 2, 2020
2,767
The nice thing about ‘banksel’ is that it will determine the bank setting from the variable name if fully declared. For example:
Var1 equ 403h
Var2 equ 753h

banksel Var1 generates movlb 4 and banksel Var2 generates movlb 7 on an 18F part.
When I first started with ASM on PICs, I tried to be a purist and I wanted to know what was happening for each clock cycle so I didn't like the banksel macro. Then I realized how many errors it prevented exactly for the reason you listed.
 

Thread Starter

cmartinez

Joined Jan 17, 2007
8,768
incrementing or decrementing the FSR is not a straightforward thing to do, since it's actually a 16 bit (2 byte) register.
I take that last statement back. I forgot that there are three very interesting instructions available that can increment, decrement or offset the FSRn registers in a straightforward way:

1654207169520.png
 

Thread Starter

cmartinez

Joined Jan 17, 2007
8,768
When I first started with ASM on PICs, I tried to be a purist and I wanted to know what was happening for each clock cycle so I didn't like the banksel macro. Then I realized how many errors it prevented exactly for the reason you listed.
I first learnt how to program MCU's using the 8081 architecture. And when I was forced to use and learn the PIC16 chips I was pulling my hair off trying to understand what the hell the designer was thinking by, first, limiting the instructions' length to 14 bits, and second, dividing memory locations into groups of "banks" ... but I have to admit that with time I've come to appreciate all the work and effort that was put into it.
 

Papabravo

Joined Feb 24, 2006
22,083
I first learnt how to program MCU's using the 8081 architecture. And when I was forced to use and learn the PIC16 chips I was pulling my hair off trying to understand what the hell the designer was thinking by, first, limiting the instructions' length to 14 bits, and second, dividing memory locations into groups of "banks" ... but I have to admit that with time I've come to appreciate all the work and effort that was put into it.
Everything is about tradeoffs when it comes to instruction sets and CPU architecture.
 

Thread Starter

cmartinez

Joined Jan 17, 2007
8,768
A little bit aside of this topic: I'm trying to buy about 200 units of the PIC16LF1825, and about 4 days ago I found that Digikey had 457 in stock at $1.58 each. So I put them into my cart along with other parts that I needed, but I didn't immediately click "buy" because I wanted to make sure that I was acquiring everything I needed from them in a single purchase. This because shipping costs down to where I am are quite significant.

Anyway, by next morning (about 10 hours later) I finally made up my mind and when I clicked "buy" .... lo and behold! ... 455 units had been sold overnight and only 2 pieces remained in stock. Needless to say that felt like a bucket of cold water on my head...

So I've been looking everywhere for said parts and I found quite a few suppliers in alibaba that are listing them with prices ranging from between $2.34 to an exorbitant $10.55 each.

This morning I received a quotation from a Chinese supplier offering them at exactly $1.00 each, which is even cheaper than Digikey itself ... what gives? ... would this be a risky buy? ... is there such a thing as counterfeit Microchip MCU's out there?
 

MrSalts

Joined Apr 2, 2020
2,767
A little bit aside of this topic: I'm trying to buy about 200 units of the PIC16LF1825, and about 4 days ago I found that Digikey had 457 in stock at $1.58 each. So I put them into my cart along with other parts that I needed, but I didn't immediately click "buy" because I wanted to make sure that I was acquiring everything I needed from them in a single purchase. This because shipping costs down to where I am are quite significant.

Anyway, by next morning (about 10 hours later) I finally made up my mind and when I clicked "buy" .... lo and behold! ... 455 units had been sold overnight and only 2 pieces remained in stock. Needless to say that felt like a bucket of cold water on my head...

So I've been looking everywhere for said parts and I found quite a few suppliers in alibaba that are listing them with prices ranging from between $2.34 to an exorbitant $10.55 each.

This morning I received a quotation from a Chinese supplier offering them at exactly $1.00 each, which is even cheaper than Digikey itself ... what gives? ... would this be a risky buy? ... is there such a thing as counterfeit Microchip MCU's out there?
Many of the alibaba suppliers are selling parts from PCB houses that are left over from other projects. Often it is cheaper to just buy 12 full reels in a case directly from Microchip instead of buying 7 reels from a distributor. The assembly company sells them for pennys (or gives them to employees) who resell them on alibaba.
I don't think such an obscure part would be the part to lure customers into buying a counterfeit part. I would assume it is real. It is worth the $200 risk in my book. You could also contact the seller and offer $100 or $150 for 200 pieces.
 
Top