100 kHz PWM signal with complementary signal

Post your cool example code here.
Post Reply
fari
Posts: 23
Joined: Sun Feb 26, 2017 12:01 pm

100 kHz PWM signal with complementary signal

Post by fari » Fri Sep 08, 2017 8:26 pm

It might be interesting to someone here.
The code below generate a 100kHz PWM signal on PA8 and its complementary signal on PB13.

Code: Select all


#define PWM_OUT PA8       //PWM output
#define PWM_OUT_COMP PB13 //complementary output
void setup(){

  // initialize digital pin PC13 as an output.
  pinMode(PC13, OUTPUT);
  HardwareTimer timer1 = HardwareTimer(1);
 
  timer1.setPrescaleFactor(1);
  
  timer1.setPeriod(10); //100kHz

  
  pinMode(PWM_OUT, PWM); //Ideally these would be done as bit wise as I dont know exactly whats being set here..
  pinMode(PWM_OUT_COMP, PWM);

  
  timer_dev *t = TIMER1; //refers t to Timer 8 memory location, how to read back?
  timer_reg_map r = t->regs;
   
  bitSet(r.adv->CCER,0); //this should enable complimentary outputs
  bitSet(r.adv->CCER,2);
  pwmWrite(PWM_OUT, 10); //



}

void loop(){

  // I am alive
   digitalWrite(PC13, HIGH);   // turn the LED on (HIGH is the voltage level)
  //digitalWrite(PA5, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(PC13, LOW);    // turn the LED off by making the voltage LOW
  //digitalWrite(PA5, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);            
}

User avatar
ahull
Posts: 1596
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: 100 kHz PWM signal with complementary signal

Post by ahull » Fri Sep 08, 2017 9:02 pm

That looks like yet another neat trick I wasn't previously aware of. Thanks for sharing. :D
- Andy Hull -

User avatar
RogerClark
Posts: 6880
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: 100 kHz PWM signal with complementary signal

Post by RogerClark » Fri Sep 08, 2017 9:37 pm

Interestng...

i was thinking of adding PWM frequency to the core.

There was a thread about the ability to set the freq a month or so ago..

Enabling the complememtary output could be a useful addition to that, but i presume the complementry pin for each timer is not user selectable.

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

Re: 100 kHz PWM signal with complementary signal

Post by dannyf » Sat Sep 09, 2017 11:25 am

rather than hardwire the period to 10, tie it to desired pwm frequency (F_PWM) and then the buss frequency (F_CPU or F_AHB or F_...).

then you have a piece of code that's much more generic.

fari
Posts: 23
Joined: Sun Feb 26, 2017 12:01 pm

Re: 100 kHz PWM signal with complementary signal

Post by fari » Mon Sep 11, 2017 10:04 am

That is true Roger!. The complementary outputs are given and can not be modified.
Timer one Pins:
- TIM1_CH1 pin (PA.08)
- TIM1_CH1N pin (PB.13)
- TIM1_CH2 pin (PE.11)
- TIM1_CH2N pin (PB.14)
- TIM1_CH3 pin (PA.10)
- TIM1_CH3N pin (PB.15)

sabra
Posts: 3
Joined: Wed Aug 02, 2017 7:24 pm

Re: 100 kHz PWM signal with complementary signal

Post by sabra » Tue Sep 26, 2017 11:58 am

Hello, I have a NUCLEO-F030R8 which I am using to generate a 20kHz SPWM signal on TIM1_CH1. Would it be possible to output the complement of this signal onto TIM1_CH1N within the Arduino IDE? It seems the library that fari is using is not compatible with the NUCLEO-F030R8 board, is this correct? Thanks!

victor_pv
Posts: 1643
Joined: Mon Apr 27, 2015 12:12 pm

Re: 100 kHz PWM signal with complementary signal

Post by victor_pv » Tue Sep 26, 2017 3:07 pm

That's right Sabra. There are 3 main cores for STM32 in Arduino:
Leaflabs libmaple based https://github.com/rogerclarkmelbourne/Arduino_STM32
GENERIC (based on STM HAL) https://github.com/danieleff/STM32GENERIC
STM own official (based on STM HAL) https://github.com/stm32duino


The code in this thread is for the libmaple based one, which only works on F1, some F3 and some F4 mcus
What you are using an F0, so you must be using one of the other cores. What you want to do may be possible, but just in a different way.

Post Reply