72 MHz dividing by 100 code

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

Re: 72 MHz dividing by 100 code

Post by ted » Wed Jan 03, 2018 5:17 am

I have disabled two lines and program is running ok, will this cause the problems ?

Code: Select all

// Use timer 2
HardwareTimer pwmtimer2(2);

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

void loop() {
   
}

stevestrong
Posts: 2063
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany
Contact:

Re: 72 MHz dividing by 100 code

Post by stevestrong » Wed Jan 03, 2018 9:40 am

Why have you disabled those lines? Do you have problems if you do so?
If not, I would recommend to leave those lines enabled, in case you wish to use the same sequence in other context they may be important.

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

Re: 72 MHz dividing by 100 code

Post by ted » Wed Jan 03, 2018 1:03 pm

No reason for disabling, I was learning what each line is doing. I very happy with this program and thank you for helping me. Now I am understanding PWM 95%
Will disabling those lines make microcontroller less busy ?
I guess not much.

stevestrong
Posts: 2063
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany
Contact:

Re: 72 MHz dividing by 100 code

Post by stevestrong » Wed Jan 03, 2018 3:23 pm

Those lines will be called once in the setup phase, so they will not reserve any further MCU resources.

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

Re: 72 MHz dividing by 100 code

Post by ted » Wed Jan 03, 2018 5:35 pm

After pointing, I looked at the program and I see that it is obvious.

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

Re: 72 MHz dividing by 100 code

Post by Pito » Wed Jan 03, 2018 5:46 pm

Unless you start to modulate the PWM with your data.. :)
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 » Wed Jan 03, 2018 7:41 pm

i didn't think about it, again thanks for mentioning that.
what is your image - aged sculpture ?

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

Re: 72 MHz dividing by 100 code

Post by ted » Wed Jan 03, 2018 8:10 pm

So I can modulate PWM by sine wave, but how ?

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

Re: 72 MHz dividing by 100 code

Post by ted » Wed Jan 03, 2018 9:08 pm

Which one I should choose ?

#1

Code: Select all

function y = sin3(x)
n = 1 : 1000;
for i = 1 : length(x)
    y(i) = x(i).*prod(1 - x(i)^2 ./ (n*pi).^2);
end
#2

Code: Select all

void setup() {
  Serial.begin(9600);
}

void loop() {
  for (int j = 0; j < 360; j++) {
   Serial.println(sin(j * (PI / 180)));
  }
}

#3

Code: Select all

+/*
+Plotting example
+
+In this example we generate a continous sine wave 
+on the arduino and send it out via the serial port.
+This signal can be observed via the serial plotter
+of the Arduino IDE.
+
+This example code is in the public domain.
+*/
+
+float frequency = 1.0f;
+float amplitude = 100.0f;
+
+void setup() {
+  // Initialize serial
+  Serial.begin(115200);
+}

void loop() {
+  unsigned long time = millis();
+  
+  // Compute basic sine wave signal
+  float x = time * 0.001f * 2 * PI * frequency;
+  float signal = amplitude * sin(x);
+  
+  // Adding some random noise to the signal
+  signal += random(-100, 101) * 0.0006f * amplitude;
+
+  // Send the signal over the serial
+  Serial.println(signal);
+
+  // Wait for 10ms to limit the update rate
+  delay(10);
+}
#4

Code: Select all

 int i;
  for(i=0;i<SAMPLES;i++)
  {
      val1[i] = 50 + amp * sin(stp * i);
      val2[i] = val1[i];
  }
}

void get_wave(int16 shift)

User avatar
mrburnette
Posts: 2210
Joined: Mon Apr 27, 2015 12:50 pm
Location: Greater Atlanta
Contact:

Re: 72 MHz dividing by 100 code

Post by mrburnette » Thu Jan 04, 2018 3:58 pm

ted wrote:
Wed Jan 03, 2018 1:03 pm
<...>
Will disabling those lines make microcontroller less busy ?
I guess not much.
There is a danger to my not responding to this statement as it may be read and interpreted by a non-hardware user as meaning that within the uC, disabling peripheral interfaces will/would make the uC run faster. This is not the case.

If one studies the STM32 architecture document, one will note that the microcontroller has internal buses that connect various subsystems. Disabling a subsystem "may" reduce the chip power usage, but generally subsystems that are not utilized are not initialized and configured, so power dissipation of non-used peripherals should be minimal.

What does make a microcontroller "busy" is the start-up/initialization code and then user code that exercises the various peripheral activities. Some peripheral devices such as PWM consume bandwidth when they are configured but afterward do not consume any user execution time unless something is changed by user code. Of course, when PWM is active, electrical power is consumed because that device is active and clocking to an output pin.

Image

Post Reply