72 MHz dividing by 100 code

What are you developing?
ted
Posts: 62
Joined: Sun Jul 16, 2017 9:57 pm

72 MHz dividing by 100 code

Post by ted » Mon Jan 01, 2018 1:59 am

STM32F103 is running at 72 MHz I believe the simple code can divide it by 100 which will give 0.72 MHz PWM output on pin, for example PA6. Maybe someone know how to write such code ?

Code: Select all


int outputPin = PA6; //output pin

void setup() {
  // put your setup code here, to run once:
  pinMode(outputPin, OUTPUT);
  //or ?  pinMode(out1, PWM);
  Serial.begin(9600);
  // ???
}

void loop() {
    // put your main code here, to run repeatedly:
                
 // 72 MHz dividing by 100 code ???
  
//  Serial.println(   ???);  
//  Serial.print (???);   //  print result of dividing, PWM on pin PA6
}


dannyf
Posts: 231
Joined: Wed May 11, 2016 4:29 pm

Re: 72 MHz dividing by 100 code

Post by dannyf » Mon Jan 01, 2018 5:15 pm

the simple code
which simple code?
Maybe someone know how to write such code ?
one typical process would be to look up the datasheet and figure out how the clock division works.

another typical process would be to beg someone else to write it for you.


User avatar
Pito
Posts: 1743
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 8:38 am

For example (I doubt this will work at exactly 720kHz, however), tested:

Code: Select all

HardwareTimer pwmtimer(3);

uint16_t maxduty, duty;  
uint32_t myperiod, mypulse;

void setup() {
  pwmtimer.pause();
  myperiod = 20; // PWM period in useconds, 20 -> freq = 50KHz
  maxduty = pwmtimer.setPeriod(myperiod);
  pwmtimer.refresh();
  pwmtimer.resume();
  pinMode(PA7, PWM);
}

void loop() {
mypulse =  5; // 0<=mypulse<=period, this is my High pulse length in useconds
duty = map((int32_t)mypulse, 0, (int32_t)myperiod, 0, (int32_t)maxduty);
pwmWrite(PA7, duty); // 0<=duty<=maxduty
while(1){};
}
You may try to access the timer registers directly to achieve faster results.
With a 72MHz input the timer resolution could be 13.88ns, thus you need 100 to load in (or something like that) to get 720kHz out.
Tested:

Code: Select all

HardwareTimer pwmtimer(3);

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

void loop() {
}
Last edited by Pito on Tue Jan 02, 2018 5:53 pm, edited 8 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 4:26 pm

What I should put in brackets serial ?

Code: Select all


#include <SPI.h>
int outputPin = PA6; //output pin

void setup() {
  // put your setup code here, to run once:
  pinMode(outputPin, OUTPUT);
  //or ?  pinMode(out1, PWM);
  Serial.begin(9600);
  // ???
}

void loop() {
  
  SPI.setClockDivider(SPI_CLOCK_DIV128); 
 Serial.println(   ???);  
//  Serial.print (???);   //  print result of dividing, PWM on pin PA6
}


/*
 SPI.setClockDivider(SPI_CLOCK_DIV16); // (72 MHz / 16 = 4.5MHz)
 The SPI1 works on APB2 (72 MHz). The maximum frequency that works the SPI1 is 72/2 =36 MHZ.
The SPI2 works on APB1 (36 MHz). The maximum frequency that works the SPI2 is 36/2 =18 MHZ. 

The available dividers are:
CODE: SELECT ALL
SPI_CLOCK_DIV2
SPI_CLOCK_DIV4
SPI_CLOCK_DIV8
SPI_CLOCK_DIV16
SPI_CLOCK_DIV32
SPI_CLOCK_DIV64
SPI_CLOCK_DIV128
SPI_CLOCK_DIV256

 */

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 4:35 pm

no tested - error
div_100_a:17: error: 'period' was not declared in this scope

duty = map((int32_t)mypulse, 0, (int32_t)period, 0, (int32_t)maxduty);

^

exit status 1
'period' was not declared in this scope

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 4:42 pm

You may try to access the timer registers directly to achieve faster results.
output on PA7 = 550 Hz

User avatar
Pito
Posts: 1743
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 4:54 pm

Updated the source - see above.
Also I've tested the first source and it works here on BluePill.
The second source under investigation.
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:05 pm

There is no reaction when I am changing the numbers in bracket

Code: Select all


pwmtimer.setPrescaleFactor(4);       // Timer input clock Prescaler = 1 (= 36MHz input ?)
  pwmtimer.setOverflow(0.5);            // PWM Period width for 720kHz ?
  
  

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:11 pm

Also I've tested the first source and it works here on BluePill.
When I am compiling first version I have mentioned error

Post Reply