72 MHz dividing by 100 code

What are you developing?
stevestrong
Posts: 2060
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany
Contact:

Re: 72 MHz dividing by 100 code

Post by stevestrong » Tue Jan 02, 2018 5:18 pm

You can only set integer values for the timer registers, so floats will not work.

ted
Posts: 62
Joined: Sun Jul 16, 2017 9:57 pm

Re: 72 MHz dividing by 100 code

Post by ted » Tue Jan 02, 2018 5:22 pm

Version #1 is working after second time - copy and paste your code.
Frequency is 50 kHz, pulse with 25%.
I will play with numbers.
Big Thanks

ted
Posts: 62
Joined: Sun Jul 16, 2017 9:57 pm

Re: 72 MHz dividing by 100 code

Post by ted » Tue Jan 02, 2018 5:33 pm

Updated version #2 is also working, output - 1.4MHz

User avatar
Pito
Posts: 1738
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: 72 MHz dividing by 100 code

Post by Pito » Tue Jan 02, 2018 5:40 pm

I've updated the second source above again, and double-checked with my counter.
It seems the input clock is 72MHz and I need "100-1" to get exact 720kHz (720.045kHz here).. :lol:

Code: Select all

98  = 727.319 kHz
99  = 720.045 kHz
100 = 712.916 kHz
101 = 705.927 kHz
Last edited by Pito on Tue Jan 02, 2018 7:11 pm, edited 6 times in total.
Pukao Hats Cleaning Services Ltd.

ted
Posts: 62
Joined: Sun Jul 16, 2017 9:57 pm

Re: 72 MHz dividing by 100 code

Post by ted » Tue Jan 02, 2018 5:46 pm

I changed numbers in those two lines = nice squares, 720 kHz, 50% duty.

Code: Select all


pwmtimer.setOverflow(100);            // PWM Period width for 720kHz ?
  pwmtimer.setCompare(TIMER_CH2, 50);  // PWM High Pulse width is 50% duty (1:1)pwmtimer.setOverflow(100);            // PWM Period width for 720kHz ?
  pwmtimer.setCompare(TIMER_CH2, 50);  // PWM High Pulse width is 50% duty (1:1)
  
  

ted
Posts: 62
Joined: Sun Jul 16, 2017 9:57 pm

Re: 72 MHz dividing by 100 code

Post by ted » Tue Jan 02, 2018 5:51 pm

I am just curious can I divide this again by 100 and get 7.2 kHz on pin PA6 ?

User avatar
Pito
Posts: 1738
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: 72 MHz dividing by 100 code

Post by Pito » Tue Jan 02, 2018 5:58 pm

get 7.2 kHz on pin PA6 ?
Here you are Sir:

Code: Select all

HardwareTimer pwmtimer(3);

void setup() {
  pinMode(PA6, PWM);
  pwmtimer.pause();
  pwmtimer.setPrescaleFactor(1);       // Timer input clock Prescaler = 1 (= 72MHz input ?)
  pwmtimer.setOverflow(10000-1);            // PWM Period width for 7200 Hz ?
  pwmtimer.setCompare(TIMER_CH1, 5000);  // PWM High Pulse width is 50% duty (1:1)
  pwmtimer.refresh();
  pwmtimer.resume();
}

void loop() {
}
7200,5 Hz on PA6 here :)
You may combine both to get 720k/7k on PA7/63.
Mind - the timer/pwm output pins on BluePill have got their own dedicated TimerX/ChannelY - see the BluePill Pins Layout picture for the Timers/Channels allocation.
Last edited by Pito on Tue Jan 02, 2018 7:12 pm, edited 10 times in total.
Pukao Hats Cleaning Services Ltd.

ted
Posts: 62
Joined: Sun Jul 16, 2017 9:57 pm

Re: 72 MHz dividing by 100 code

Post by ted » Tue Jan 02, 2018 6:00 pm

The second updated version is perfect

ted
Posts: 62
Joined: Sun Jul 16, 2017 9:57 pm

Re: 72 MHz dividing by 100 code

Post by ted » Tue Jan 02, 2018 6:04 pm

Thanks
Can I have both frequencies, PA7 =720 kHz and PA6 = 7.2kHz ?

User avatar
Pito
Posts: 1738
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: 72 MHz dividing by 100 code

Post by Pito » Tue Jan 02, 2018 6:06 pm

Yes, but PA7 = 720kHz and PA3 = 7,2kHz - just merge the above c-sources together - also read my previous post.

Code: Select all

HardwareTimer pwmtimer3(3);
HardwareTimer pwmtimer2(2);

void setup() {
  pinMode(PA7, PWM);
  pwmtimer3.pause();
  pwmtimer3.setPrescaleFactor(1);       // Timer input clock Prescaler = 1 (= 72MHz input ?)
  pwmtimer3.setOverflow(100-1);            // PWM Period width for 720kHz ?
  pwmtimer3.setCompare(TIMER_CH2, 50);  // PWM High Pulse width is 50% duty (1:1)
  pwmtimer3.refresh();
  pwmtimer3.resume();

  pinMode(PA3, PWM);
  pwmtimer2.pause();
  pwmtimer2.setPrescaleFactor(1);       // Timer input clock Prescaler = 1 (= 72MHz input ?)
  pwmtimer2.setOverflow(10000-1);            // PWM Period width for 7,2kHz ?
  pwmtimer2.setCompare(TIMER_CH4, 5000);  // PWM High Pulse width is 50% duty (1:1)
  pwmtimer2.refresh();
  pwmtimer2.resume();
}

void loop() {
}
Last edited by Pito on Tue Jan 02, 2018 6:19 pm, edited 2 times in total.
Pukao Hats Cleaning Services Ltd.

Post Reply