I'm trying to move the PWM (CCP2) facility to PortA.5 on a 16F1825. I understand that I need to set Bit 0 of the AFPCON1 register to 1 to acheive this.
How do I access the AFPCON1 register?
I've tried:
DimRegistersAsByte'Set the PWM Port to PortA.5LetRegisters=AFPCON1LetRegisters.0=1LetAFPCON1=Registers
But this fails due to AFPCON1 not having been declared.
I guess there is another (GCB freindly) way of referencing AFPCON1?
Last edit: mkstevo 2019-04-25
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I can confirm that it has indeed set the PWM Channel 2 to PortA.5.
#DefinePWMPortA.5DirPWMOutDimRegistersAsByte'Set the PWM Port to PortA.5LetRegisters=APFCON1LetRegisters.0=1LetAPFCON1=RegistersHPWM2,15,122'15kHz @ 50% (122 = half of 255)
Once I spelled it correctly, it made all the difference...
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks for that. No, I didn't try that, I wasn't aware that it was possible to address the bits of a register in that way.
That indeed looks much simpler.
I've finished my PWM experimentation * so I'm not sure when I will next get to check that feature, but I will bookmark your suggestion for the next time I need to change something in the APFCON register.
*I was trying to generate an IR carrier using PWM to reduce the vulnerability of some IR gate sensors to daylight, the use of the carrier worked, but didn't make any difference if exposed to daylight - I didn't think it would but was told to try. As the output pin for the IR drive transistor On/Off was adjacent to PortA.5 (and A.5 was unused) it was convenient to use this pin for the PWM out, feed that through a 100R resistor to PortA.4, then active and deactivate the PWM by turning PortA.4 into an output and turning PortA.4 off to 'kill' the PWM, then returning PortA.4 into an input to re-enable the PWM carrier. I was unable to do the same thing using PWMon PWMoff as these only work on PWM Channel 1.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Registers are effectively Named RAM locations which is exactly how GCBASIC defines variables.
So any register, as long as it is defined in the .dat file, can treated as if it were a variable and may be assigned to, addressed as a bit array or passed to a function, just like any other variable.
Some of the register bits may be read only however.
That is one of my favoare design concepts of GCBASIC and makes hardware level access and direct register manipulation a breeze but should only be done if you have a good understanding of the device and its data sheet (Which it would appear that you have as you knew what bit to set).
Cheers
Chris
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The manual for the 16F1825 did show some of the registers as potentially being 'read only', but I thought all of the bits in the APFCON and APFCON1 registers were r/w.
I had scanned the .dat file for the APFCON details, thinking it might give me a clue as to how it might be alternatively referenced, but managed to miss it . Twice. I didn't search for it, though if I'd typed it incorrectly I wouldn't have found it anyway.
Having looked again... I now see the 16F1825.dat file has this entry:
CCP2SEL,APFCON1,0
Which makes me imagine I could do this:
#DefinePWMPortA.5DirPWMOutLetCCP2SEL=1'Set the PWM Port to PortA.5HPWM2,15,122'15kHz @ 50% (122 = half of 255)
Which looks easier still?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Whilst I have not been using the PWM this last week, I did have further need for using the APFCON register. This time for a 12F1840, wanting to move the Serial out function from the default to PortA.4.
Trying the above code for setting the TXCKSEL register (APFCON.2) did indeed work, as did setting APFCON.2.
#Chip12F1840,32#OptionExplicit#ConfigOsc=Int,PLLEN_ON'Turn on RS232.'Define the USART port#DefineUSART_BAUD_RATE38400#DefineUSART_BLOCKING#DefineSerOutPortPortA.4'Set pin directionsLetTXCKSEL=1'Set pin function of PortA.4 to serial out'Serial TX on the 12F1840 defaults to PortA.0'To set the port for serial TX, set APFCON.2 to 1 (move from default to PortA.4)'To set the port for serial TX, set TXCKSEL to 1 (move from default to PortA.4)'Serial RX on the 12F1840 defaults to PortA.1'To set the port for serial RX, set APFCON.7 to 1 (move from default to PortA.5)'To set the port for serial RX, set RXCKSEL to 1 (move from default to PortA.5)DirSerOutPortOut
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I certainly see no harm in doing it that way but I would be inclined to be a bit more specific with the #Define as PWM is a common term and may conflict with other libraries etc.
I would suggest:
#Chip 16F1825
#Define PWM_Pin PortA.5
Dir PWM_Pin Out
Let CCP2SEL = 1 'Set the PWM Port to PortA.5
HPWM 2,15,122 '15kHz @ 50% (122 = half of 255)
And you calculator batteries may need changing as mine says that 127 is half of 255 ; >)
Cheers
Chris
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
And you calculator batteries may need changing as mine says that 127 is half of 255 ; >)
I spotted that yesterday! Then forgot again today. I was thinking (as much thinking as I do) of half of 250 was 125, then thought that half of the remaining 5 was 2.5, then for some reason took the 2.5 OFF 125, leaving me with 122.5 which I rounded to 122. Of course I should have ADDED my 2.5 but... I'm in a busy workshop... Things were not working... I couldn't see why... Then messed up my maths...
I should have been paying more attention!
You are also right that PWM_Pin is a much better definition. I do usually try make my definitions a little more specific, but was attempting to quickly give this a go.
So that's two lame excuses, in just one reply.
#Chip16FInTroubleAgainDimLinesCountAsWordForLinesCount=1to1000Print"I must pay more attention to my coding..."NextEnd'Embarrassed face here.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
No Need to be embarrassed, I actually did have to use a calculator to confirm my suspicion.
I personally use Hex almost exclusively when working with arbitrary values such as PWM and so consider 100% to be 0xFF and 50% to be 0x7F.
But having spent the last 40 years thinking in HEX it is my Decimal that needs a calculator and the reason my wife has to balance the checkbook and calculate the tip's.
Waiters don't appreciate it if you accidentally write F0FF in the tip field :>)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm trying to move the PWM (CCP2) facility to PortA.5 on a 16F1825. I understand that I need to set Bit 0 of the AFPCON1 register to 1 to acheive this.
How do I access the AFPCON1 register?
I've tried:
But this fails due to AFPCON1 not having been declared.
I guess there is another (GCB freindly) way of referencing AFPCON1?
Last edit: mkstevo 2019-04-25
Ah ha!
Worked (or at least compiled) once I corrected the name of the register to the correct APFCON1.
Let's see if it generates PWM on the correct pin.
I can confirm that it has indeed set the PWM Channel 2 to PortA.5.
Once I spelled it correctly, it made all the difference...
Did you try addressing the register directly?
Should work fine, no need to use all the other assignments.
Cheers
Chris
Last edit: Chris Roper 2019-04-25
Thanks for that. No, I didn't try that, I wasn't aware that it was possible to address the bits of a register in that way.
That indeed looks much simpler.
I've finished my PWM experimentation * so I'm not sure when I will next get to check that feature, but I will bookmark your suggestion for the next time I need to change something in the APFCON register.
*I was trying to generate an IR carrier using PWM to reduce the vulnerability of some IR gate sensors to daylight, the use of the carrier worked, but didn't make any difference if exposed to daylight - I didn't think it would but was told to try. As the output pin for the IR drive transistor On/Off was adjacent to PortA.5 (and A.5 was unused) it was convenient to use this pin for the PWM out, feed that through a 100R resistor to PortA.4, then active and deactivate the PWM by turning PortA.4 into an output and turning PortA.4 off to 'kill' the PWM, then returning PortA.4 into an input to re-enable the PWM carrier. I was unable to do the same thing using PWMon PWMoff as these only work on PWM Channel 1.
Registers are effectively Named RAM locations which is exactly how GCBASIC defines variables.
So any register, as long as it is defined in the .dat file, can treated as if it were a variable and may be assigned to, addressed as a bit array or passed to a function, just like any other variable.
Some of the register bits may be read only however.
That is one of my favoare design concepts of GCBASIC and makes hardware level access and direct register manipulation a breeze but should only be done if you have a good understanding of the device and its data sheet (Which it would appear that you have as you knew what bit to set).
Cheers
Chris
The manual for the 16F1825 did show some of the registers as potentially being 'read only', but I thought all of the bits in the APFCON and APFCON1 registers were r/w.
I had scanned the .dat file for the APFCON details, thinking it might give me a clue as to how it might be alternatively referenced, but managed to miss it . Twice. I didn't search for it, though if I'd typed it incorrectly I wouldn't have found it anyway.
Having looked again... I now see the 16F1825.dat file has this entry:
Which makes me imagine I could do this:
Which looks easier still?
Whilst I have not been using the PWM this last week, I did have further need for using the APFCON register. This time for a 12F1840, wanting to move the Serial out function from the default to PortA.4.
Trying the above code for setting the TXCKSEL register (APFCON.2) did indeed work, as did setting APFCON.2.
I certainly see no harm in doing it that way but I would be inclined to be a bit more specific with the #Define as PWM is a common term and may conflict with other libraries etc.
I would suggest:
And you calculator batteries may need changing as mine says that 127 is half of 255 ; >)
Cheers
Chris
I spotted that yesterday! Then forgot again today. I was thinking (as much thinking as I do) of half of 250 was 125, then thought that half of the remaining 5 was 2.5, then for some reason took the 2.5 OFF 125, leaving me with 122.5 which I rounded to 122. Of course I should have ADDED my 2.5 but... I'm in a busy workshop... Things were not working... I couldn't see why... Then messed up my maths...
I should have been paying more attention!
You are also right that PWM_Pin is a much better definition. I do usually try make my definitions a little more specific, but was attempting to quickly give this a go.
So that's two lame excuses, in just one reply.
No Need to be embarrassed, I actually did have to use a calculator to confirm my suspicion.
I personally use Hex almost exclusively when working with arbitrary values such as PWM and so consider 100% to be 0xFF and 50% to be 0x7F.
But having spent the last 40 years thinking in HEX it is my Decimal that needs a calculator and the reason my wife has to balance the checkbook and calculate the tip's.
Waiters don't appreciate it if you accidentally write F0FF in the tip field :>)