I was trying to use a cheaper device for a design I'm making 50 of. I thought I would replace a 16F18xx with a 16F152xx. It's only a small program so I don't need much program storage.
I get an error that there is no EeProm capability on the 152xx devices, which is correct, there isn't. I changed all references to SAFRead/SAFWrite and the program compiled. No problem.
I thought I'd get clever and use some 'IfDef' statements to conditionally compile for devices with either SAF or EeProm but I can't get any of them to work other than specifying the exact chip I'm using.
I don't doubt I'm missing something rather obvious, but can't see what I could do to select programmatically to compile either EpRead/EpWrite or SAFRead/SAFWrite.
Last edit: c-conversion 2025-06-17
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Look at PICINFO for the available constants for the chip(s). The ChipData tab in PICInfo show the exposed chip specific constants.
Here is an analysis and recommendation to resolve.
/*GCBASICsourcegeneratedbyBLOCKLY.AnOpensourcecodegenerator.AGCBASICsourceprogram*///Date:06/14/2025//Version:0.9b//Author:Evan#chip 16F15244#option explicitDimEePromWVal,HiAddrasWord//#IfDef CHIP_16F15244 // GOOD//#IfDef Bit(SAFEN) // SAFEN is CONFIG BIT and not exposed.//#IfDef OPTION(SAFEN) // OPTION.. maybe this is something we could implement, but not exposed///#IfDef VAR(SAFEN) (fails to compile the SAFRead/SAFWrite section) // VAR is register inspection. So, not valid. May be there is a REGISTER than does have the SAF state//#IfDef Var(SAF_ROWSIZE_BYTES) // SAF_ROWSIZE_BYTES is CONSTANT, see next line#IF SAF_ROWSIZE_BYTES <> 0SAFRead(HiAddr+1,EePromWVal)SAFRead(HiAddr,EePromWVal_H)#EndIf//OR,usingPICINFOinformationwhichisalotsmarterasthisexposedasaPUBLICconstant#IF ChipSAFMemWords <> 0SAFRead(HiAddr+1,EePromWVal)SAFRead(HiAddr,EePromWVal_H)#EndIf
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Many thanks.
I'd looked in PicInfo, and I'd seen SAFEN (and tried it) I then found SAF_ROWSIZE_BYTES (and tried it too) but hadn't spotted ChipSAFMemWords and so hadn't tried it. Mind you, by then I'd thought I was going in the wrong direction.
To clarify what you are saying.
Does #If work as a conditional compile option with additional parameters, as an alternative form of IfDef which has no parameters?
To remind me when I'm searching for answers in the future, I'm currently using this code to select automagically use of either SAF or EeProm in the appropriate devices:
SubGetWEeprom(HiAddrAsByte,OutEePromWValAsWord)'Takes the address of the High Byte and returns'a Word made from the HiAddr and the adjacent'byte in Eeprom#IfChipSAFMemWords<>0SAFRead(HiAddr+1,EePromWVal)SAFRead(HiAddr,EePromWVal_H)#EndIf#IfChipSAFMemWords=0EpRead(HiAddr+1,EePromWVal)EpRead(HiAddr,EePromWVal_H)#EndIfEndSub
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I was trying to use a cheaper device for a design I'm making 50 of. I thought I would replace a 16F18xx with a 16F152xx. It's only a small program so I don't need much program storage.
I get an error that there is no EeProm capability on the 152xx devices, which is correct, there isn't. I changed all references to SAFRead/SAFWrite and the program compiled. No problem.
I thought I'd get clever and use some 'IfDef' statements to conditionally compile for devices with either SAF or EeProm but I can't get any of them to work other than specifying the exact chip I'm using.
I tried:
I don't doubt I'm missing something rather obvious, but can't see what I could do to select programmatically to compile either EpRead/EpWrite or SAFRead/SAFWrite.
Last edit: c-conversion 2025-06-17
Hi,
Look at PICINFO for the available constants for the chip(s). The ChipData tab in PICInfo show the exposed chip specific constants.
Here is an analysis and recommendation to resolve.
Many thanks.
I'd looked in PicInfo, and I'd seen SAFEN (and tried it) I then found SAF_ROWSIZE_BYTES (and tried it too) but hadn't spotted ChipSAFMemWords and so hadn't tried it. Mind you, by then I'd thought I was going in the wrong direction.
This seems to be working:
As is the case with "#IfDef" if the "<> 0" is present at the end of the line, the comparison will always fail.
I have just looked at this again.
Have a look in the CDF file. This is created by the compiler if you have turned this on in the Preferences.
This does show the conditional compilation outcome.
As you correctly say the the CDF shows that
#ifdef ChipSAFMemWords <> 0
does fail, where#if ChipSAFMemWords <> 0
does work.I think the CDF file has the debug we require.
To clarify what you are saying.
Does
#If
work as a conditional compile option with additional parameters, as an alternative form ofIfDef
which has no parameters?I had thought your:
was a typo, and you'd intended to write:
Last edit: c-conversion 2025-06-18
They are different conditional tests. #IFDEF() ( or #IF DEF()) .. does something exist. #IF CONSTANT} =|>|<|<> {value} tests a value.
These are high level summaries, and, the Help has more tests like OneOf() etc.
Thanks for the clarification. I'll read up on
#If
.Well done. I really should add a lot more debug to conditional compilation. I will if I find time later this year.
To remind me when I'm searching for answers in the future, I'm currently using this code to select automagically use of either SAF or EeProm in the appropriate devices:
Very nice. :-)