54 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Arduino
		
	
	
	
	
	
		
		
			
		
	
	
			54 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Arduino
		
	
	
	
	
	
| 
								 | 
							
								void setup ()
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								   //--- These lines set-up output at pins #9 and #10 for push-pull and are implemented in hardware ---//
							 | 
						||
| 
								 | 
							
								   digitalWrite( 9, HIGH);  // set pin #9 to HIGH
							 | 
						||
| 
								 | 
							
								   pinMode ( 9, OUTPUT);   // pin #9 in output mode
							 | 
						||
| 
								 | 
							
								   digitalWrite(10, LOW );  // set pin #10 to LOW
							 | 
						||
| 
								 | 
							
								   pinMode (10, OUTPUT);   // pin #10 in output mode
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								   TCCR1A = 0;             // Timer/Counter Control Register 1A set to 0
							 | 
						||
| 
								 | 
							
								   TCCR1B = 0;             // Timer/Counter Control Register 1B set to 0
							 | 
						||
| 
								 | 
							
								   TCNT1  = 0;             // Set counter for timer #1 to 0
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								   uint16_t freq = 9000;
							 | 
						||
| 
								 | 
							
								   uint16_t tc = 8000000/freq;
							 | 
						||
| 
								 | 
							
								   //    2 -> 4000 kHz
							 | 
						||
| 
								 | 
							
								   //   20 ->  400 kHz
							 | 
						||
| 
								 | 
							
								   //  200 ->   40 kHz
							 | 
						||
| 
								 | 
							
								   //  800 ->   10 kHz
							 | 
						||
| 
								 | 
							
								   // 1600 ->    5 kHz
							 | 
						||
| 
								 | 
							
								   // 2000 ->    4 kHz
							 | 
						||
| 
								 | 
							
								   
							 | 
						||
| 
								 | 
							
								   ICR1 = tc;
							 | 
						||
| 
								 | 
							
								   OCR1A = tc/2;              // number of ticks for counter 1A -  Output Compare Register for A
							 | 
						||
| 
								 | 
							
								   OCR1B = tc/2;              // set number of ticks for counter 1B to the same value as for 1A -  Output Compare Register for B
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								   TCCR1A |= (0 << COM1A0);   // enable toggling output A - Compare Match Output A Mode bit set to 1/*
							 | 
						||
| 
								 | 
							
								   TCCR1A |= (1 << COM1A1);
							 | 
						||
| 
								 | 
							
								   TCCR1A |= (1 << COM1B0);   // enable toggling output B - Compare Match Output B Mode bit set to 1
							 | 
						||
| 
								 | 
							
								   TCCR1A |= (1 << COM1B1);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								   // Phase and Frequency correct PWM with TOP set by ICR1
							 | 
						||
| 
								 | 
							
								   TCCR1B |= (1 << WGM13);       // WGM=8
							 | 
						||
| 
								 | 
							
								   
							 | 
						||
| 
								 | 
							
								   TCCR1B &= ~((1 << CS12) | (1 << CS11) | (1 << CS10)); // Clear the three clock select bits
							 | 
						||
| 
								 | 
							
								   TCCR1B |= (1 << CS10); 
							 | 
						||
| 
								 | 
							
								     
							 | 
						||
| 
								 | 
							
								   // see https://ww1.microchip.com/downloads/aemDocuments/documents/MCU08/ProductDocuments/DataSheets/ATmega48A-PA-88A-PA-168A-PA-328-P-DS-DS40002061B.pdf
							 | 
						||
| 
								 | 
							
								   // sections 16, 17, 18 for details
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								   // f = fclk/(2*N*(1+OCR1A))
							 | 
						||
| 
								 | 
							
								   // N variable represents the prescale factor (1, 8, 32, 64, 128, 256, or 1024), set CS10/CS11/CS12 appropriately
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								   // find values of OCR1A for frequencies of interest
							 | 
						||
| 
								 | 
							
								   // consider using timer #2!   
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								void loop() {
							 | 
						||
| 
								 | 
							
								   // put your main code here, to run repeatedly:
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 |