| View previous topic :: View next topic | 
	
	
		| Author | Message | 
	
		| MCUprogrammer 
 
 
 Joined: 08 Sep 2020
 Posts: 233
 
 
 
			    
 
 | 
			
				| Adjustable Frequency |  
				|  Posted: Mon Jan 10, 2022 4:32 am |   |  
				| 
 |  
				| Hello Everbody 
 I want to produce PWM with which I can adjust the frequency with the potentiometer read from the ADC in the range of 160 hertz to 2 kHz with the PIC. How can I do this. Which processor and crystal should I use?
 _________________
 Best Regards...
 MCUprogrammer
 _______________________________
 Work Hard
 |  | 
	
		|  | 
	
		| Ttelmah 
 
 
 Joined: 11 Mar 2010
 Posts: 19966
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Mon Jan 10, 2022 8:27 am |   |  
				| 
 |  
				| Depends massively on the resolution you actually want. Chips like the 18F46J50, have a 16bit ECCP PWM, and with this at say
 40MHz, you could use /62500 to give 160Hz, and /5000 to give 2KHz.
 You'd probably need to use a look up table for the divisors, if you want a
 linear response.
 Remember there are PIC's with explicit frequency generator modules
 rather than the PWM. Might be better.
 |  | 
	
		|  | 
	
		| temtronic 
 
 
 Joined: 01 Jul 2010
 Posts: 9588
 Location: Greensville,Ontario
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Mon Jan 10, 2022 12:28 pm |   |  
				| 
 |  
				| There's two main parts to this project, and Mr. T has replied about the 'generator' part. 
 The 'front end' part...
 Using the ADC can be tricky !
 With a  10 bit ADC, it's about 180 Hz per bit (2KHz-160 Hz) but we all KNOW that 'pots' are usually 270* rotation and very, very 'noisy' ( carbon track dust) so setting a precise frequency will be, well, impossible. And that's if you use a very stable, highly accurate  Vref AND use some form of SW to reduce reading errors, in addition to very well designed PCB layout. 8-bit ADC is easy, 10 bits will challenge most designers.
 
 Break up the project into those 2 parts. First, get the 'frequency generator' section running, say with 5-10  preset  frequencies, over the entire range (160-2000). Once that is running properly, THEN tackle the pot and ADC section.
 |  | 
	
		|  | 
	
		| MCUprogrammer 
 
 
 Joined: 08 Sep 2020
 Posts: 233
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Wed Jan 12, 2022 2:01 am |   |  
				| 
 |  
				| The PIC you are typing Mr T does not have 16 bit ECCP. I didn't see it on the datasheet. _________________
 Best Regards...
 MCUprogrammer
 _______________________________
 Work Hard
 |  | 
	
		|  | 
	
		| Ttelmah 
 
 
 Joined: 11 Mar 2010
 Posts: 19966
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Wed Jan 12, 2022 3:44 am |   |  
				| 
 |  
				| I think only the Q products actually allow you to use the 16bit register for the PWM. Key is that though it supports 16bit for a lot of it's stuff
 for 'reverse compatibility' the ECCP behaves like the CCP when used as
 a PWM. I think it is only the chips with the separate PWM module that
 allow the 16bit register to be used.
 Look in the devices directory for chips that have the setup_pwm function
 as opposed to the ccp functions.
 However I have to repeat my comment that you'd honestly be
 much better off using one of the chips with the programmable frequency
 generator. Problem is that with PWM, you have to change both the period and
 the duty registers, which will result in glitches when one is changed before
 the other. Resolution is also not that good, and you will need a lookup table
 to get a linear output change...
 Look at the Q43, which is one I know does support 16bit PWM.
 However this also has the NCO, which is the better solution.
 |  | 
	
		|  | 
	
		| MCUprogrammer 
 
 
 Joined: 08 Sep 2020
 Posts: 233
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Wed Jan 12, 2022 5:09 am |   |  
				| 
 |  
				| Yes yes it works for me on PIC16F1779 :D Thanks. _________________
 Best Regards...
 MCUprogrammer
 _______________________________
 Work Hard
 |  | 
	
		|  | 
	
		|  |