Is there a way of changing the oscillator frequency 'on the fly'?
Thanks to Chris Roper, the white noise source is running well. The 'noise' generated sounds rather different at different ocsillator frequencies and I thought it might be useful to be able to change this using a button to select the different oscillator frequencies.
Is this possible with GCB, or is the #Chip compiler directive the only way of setting the frequency?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Yes, you can. With ease but... you will impact the delay timing and other timing calcs.
So, set the chip frequency as normal. Then, when you change simply proportionally change the delays. Remember, the USART would change also - so, just recalc and be aware of the proportional change.
How?
I would create a program with the different frequencie(s). Compile. Open the ASM. Lift the top piece of INITSYS and paste into your main program as a new Sub.
;Source: system.h (99)INITSYS;asm showdebug OSCCON type is 105 'Bit(SPLLEN) Or Bit(IRCF3) And NoBit(INTSRC) and ifdef Bit(IRCF3);osccon type is 105;OSCCON = OSCCON AND b'10000111';OSCCON = OSCCON OR b'01100000';Set IRCF3 OnbankselOSCCONbsfOSCCON,IRCF3;Set IRCF2 OnbsfOSCCON,IRCF2;Set IRCF1 OffbcfOSCCON,IRCF1;Set IRCF0 OffbcfOSCCON,IRCF0;Set SPLLEN OffbcfOSCCON,SPLLEN;Ensure all ports are set for digital I/O and, turn off A/D
So, the code you needs for this is the Great Cow BASIC code.
Here is my code for adjusting the frequency.
A button on PortA.3 is tested for being pressed for a period of time which then increments the frequency. The button must be released for a period of time before the next change can be made.
#Chip12F1840,32'Inspired by the Practical Electronics White Noise project, September 2019'White Noise source'Using a 31 bit shift register random generator XOR Q31 and Q28'Most of this work was done by Chris Roper'of the GCB forum for which I am most grateful.#DefineButtonPortA.3DirButtonIn#DefinePressed0DimNoiseAsLong' Create a 32bit shift registerLetNoise=Random' Seed the LSFRDimPressTimeAsWordDimReleaseTimeAsWordDimTimeOutAsWordDimLastFreqAsByteDimFreqAsByteDimLSAsBit'This allows us to the port on, or off. Without it the port goes'momentarily low in each cycle, even if it is intended to stay high'You end up with this:'____|‾|‾|‾|____|‾|‾|____'Instead of this:'____|‾‾‾‾‾|____|‾‾‾|____LetPressTime=0LetReleaseTime=0LetLastFrq=0'0 + 1 = 1 = 2MHzLetFreq=Frq2#DefineFrq015#DefineFrq057#DefineFrq111#DefineFrq212#DefineFrq413#DefineFrq814#DefineFrq1615#DefineFrq3230Wait100mSChangeFrequencyDoIfButton=PressedThenIfPressTime>TimeOutThenIfReleaseTime=0ThenChangeFrequencyLetReleaseTime=PressTimeEndIfElseLetPressTime=PressTime+1EndIfElseIfReleaseTime>0ThenLetReleaseTime=ReleaseTime-1IfReleaseTime>TimeOutThenLetReleaseTime=TimeOutEndIfEndIfIfReleaseTime=0ThenLetPressTime=0EndIfEndIfLSFRLoopSubChangeFrequencyLetLastFreq=LastFreq+1IfLastFreq>8ThenLetLastFreq=1EndIfSelectCaseLastFreqCase1LetFreq=Frq2LetTimeOut=400Case2LetFreq=Frq4LetTimeOut=800Case3LetFreq=Frq8LetTimeOut=1600Case4LetFreq=Frq16LetTimeOut=3200Case5LetFreq=Frq32LetTimeOut=6400Case6LetFreq=Frq01LetTimeOut=25Case7LetFreq=Frq05LetTimeOut=50Case8LetFreq=Frq1LetTimeOut=200EndSelectIfFreq.0=1ThenSetIRCF0OnElseSetIRCF0OffEndIfIfFreq.1=1ThenSetIRCF1OnElseSetIRCF1OffEndIfIfFreq.2=1ThenSetIRCF2OnElseSetIRCF2OffEndIfIfFreq.3=1ThenSetIRCF3OnElseSetIRCF3OffEndIfIfFreq.4=1ThenSetSPLLENOnElseSetSPLLENOffEndIfEndSubSubLSFR'This line causes an error:'Error: GCASM: Symbol 3.0 has not been defined'Let C = Noise.28 XOR Noise.31 ' Test and set Carry flag'Replaced with this If...ThenIfNoise.27<>Noise.30ThenLetC=1ElseLetC=0EndIfRotateNoiseLeft' Rotate all 32 bitsIfNoise.31<>LSThenLetLS=Noise.31LetPortA.2=LS' Output bit 31EndIfEndSub
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You shouldn't be running the Processor at 32MHz.
You are hearing a strange pitch because you are running 4 times faster than the original code was designed for.
The ASM in the article and the GCBASIC version both expect a Clock frequency of 8MHz.
Change your Chip definition to:
#Chip12F1840,8
and I think you will find that your pitch issues are reduced if not eliminated with no need to have a Clock adjust button.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This is intended to go into a unit that will connect to headphones (with appropriate attenuation) to create "noise" to mask external sounds (snoring!) that prevent me from sleeping. The actual frequency of the "noise" isn't important to me, but the effectiveness of it helping me to sleep is. I wanted the option of changing the pitch (if noise has any pitch at all) to give me a few things to try.
If anything, the very low frequencies seemed to have most potential. I was using the lowest (125kHz) when debugging as I tried to decode why the XOR wasn't working, eventually shifting all zeros into the register.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The orignal device from P.E. was a 12F617 (I originally mistakenly said it was a 12F675). The source code comments suggest it was running at 2MHz?
; 2 to the power of 31 -1 results = 2,147,483,647
; clock is 2MHz so 13 cycles is 153.8451kHz generating max frequency of 76.923kHz
; minimum frequency is 76.923kHz/ 2,147,483,647 = 35.83uHz
; sequence repeats after 13us x 2,147,483,647 = 27,917,280s or 7.75hours
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The PIC has a 4 phase Clock so that the Fetch, Execute, Store and Branch portions of the instruction execution can overlap. If you read any of the Datasheets you will see mention of FSC/4 which is Front Side Clock or Clock Speed divided by 4. For that reason the Clock frequency of the PIC is divided by 4 to get that actual instruction Cycle time.In this case an 8Mhz Clock speed would result in a 2MHz Instruction cycle time hence 2Mhz being used to calculate the bandwidth in the article.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Is there a way of changing the oscillator frequency 'on the fly'?
Thanks to Chris Roper, the white noise source is running well. The 'noise' generated sounds rather different at different ocsillator frequencies and I thought it might be useful to be able to change this using a button to select the different oscillator frequencies.
Is this possible with GCB, or is the #Chip compiler directive the only way of setting the frequency?
Yes, you can. With ease but... you will impact the delay timing and other timing calcs.
So, set the chip frequency as normal. Then, when you change simply proportionally change the delays. Remember, the USART would change also - so, just recalc and be aware of the proportional change.
How?
I would create a program with the different frequencie(s). Compile. Open the ASM. Lift the top piece of INITSYS and paste into your main program as a new Sub.
So, the code you needs for this is the Great Cow BASIC code.
This code is for ,2
Then, I create new code with ,8. If you look carefully
Set IRCF1 On
Now create all that you need. Take the code from INITSYS and stop at the line
;Ensure all ports are set for digital I/O and, turn off A/D
Thanks.
As I'm not using any timing or other bits and pieces, I shouldn't need to do much else.
Here is my code for adjusting the frequency.
A button on PortA.3 is tested for being pressed for a period of time which then increments the frequency. The button must be released for a period of time before the next change can be made.
You shouldn't be running the Processor at 32MHz.
You are hearing a strange pitch because you are running 4 times faster than the original code was designed for.
The ASM in the article and the GCBASIC version both expect a Clock frequency of 8MHz.
Change your Chip definition to:
and I think you will find that your pitch issues are reduced if not eliminated with no need to have a Clock adjust button.
This is intended to go into a unit that will connect to headphones (with appropriate attenuation) to create "noise" to mask external sounds (snoring!) that prevent me from sleeping. The actual frequency of the "noise" isn't important to me, but the effectiveness of it helping me to sleep is. I wanted the option of changing the pitch (if noise has any pitch at all) to give me a few things to try.
If anything, the very low frequencies seemed to have most potential. I was using the lowest (125kHz) when debugging as I tried to decode why the XOR wasn't working, eventually shifting all zeros into the register.
The orignal device from P.E. was a 12F617 (I originally mistakenly said it was a 12F675). The source code comments suggest it was running at 2MHz?
; 2 to the power of 31 -1 results = 2,147,483,647
; clock is 2MHz so 13 cycles is 153.8451kHz generating max frequency of 76.923kHz
; minimum frequency is 76.923kHz/ 2,147,483,647 = 35.83uHz
; sequence repeats after 13us x 2,147,483,647 = 27,917,280s or 7.75hours
The PIC has a 4 phase Clock so that the Fetch, Execute, Store and Branch portions of the instruction execution can overlap. If you read any of the Datasheets you will see mention of FSC/4 which is Front Side Clock or Clock Speed divided by 4. For that reason the Clock frequency of the PIC is divided by 4 to get that actual instruction Cycle time.In this case an 8Mhz Clock speed would result in a 2MHz Instruction cycle time hence 2Mhz being used to calculate the bandwidth in the article.