How do you generate complementary PWM Outputs?

I would like to generate complementary PWM Outputs with adjustable dead time.

According to the STM32F401RE Microcontroller datasheet

http://www.st.com/web/catalog/mmc/FM141/SC1169/SS1577/LN1810/PF258797,

this is possible with Timer 1 (TIM1).

So far I have attempted to configure the timer myself using information available from the TIM HAL Driver from ST:

http://developer.mbed.org/users/dreschpe/code/mbed-F401/docs/4e95b79aa640/stm32f4xx__hal__tim__ex_8c.html

and looking through an example of someone using the driver:

https://petoknm.wordpress.com/2015/01/05/rotary-encoder-and-stm32/.

Obviously, I do not want to use a HAL sensor, but this is the closest example I can get to someone using the advanced features of the timers.

Thanks!

Damien

Edit: Here is the code I went with in the end:

void ConfigurePWM(float duty_us, float period_us){
unsigned int value;
float newVal; // Ensure power is turned on
// Grabbed from lines 54-57 of analogin_api.h, modified for PWM
// This turns on the clock to Ports A, B, and C
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIOBEN | RCC_AHB1ENR_GPIOCEN;
// This turns on the clock to the Time 1:
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; // Set the GPIO Ports properly:
// PWM1 is connected to PA_8
// PWM1N is connected to PA_7 // Set the PWM outputs to general output pins:
// This sets the PA_7 and PA_8 pins to Alternate Function Pins
value = 0x8000 + 0x20000;
GPIOA->MODER |= value; // Set the PWM outputs to high speed:
value = 0xC000 + 0x30000;
GPIOA->OSPEEDR |= value; // Set PWM as outputs to the pins:
value = GPIOA->AFR[];
// Reset the lowest four bits:
value &= 0xFFFFFFF0;
// Configure PA_8 to AF:
value |= 0x1;
GPIOA->AFR[] = value; value = GPIOA->AFR[];
// Reset the the 4 MSB:
value &= 0x0FFFFFFF;
// Configure PA_7 to AF:
value |= 0x10000000;
GPIOA->AFR[] = value; // Set pull down resistors to PWM outputs:
value = GPIOA->PUPDR;
// Clear the bits:
value &= ~(GPIO_PUPDR_PUPDR7 | GPIO_PUPDR_PUPDR8);
// Set to pull down:
value |= GPIO_PUPDR_PUPDR7_1 | GPIO_PUPDR_PUPDR8_1;
// Set the register:
GPIOA ->PUPDR = value; // Set the prescale value to 1:
TIM1->PSC = ; // *** TIM1 control register 1: TIMx_CR1 ***
value = ;
// [9:8] Set CKD bits to zero for clock division of 1
// [7] TIMx_ARR register is buffered, set the ARPE bit to 1:
// value |= 0x80;
// [6:5] Set CMS bits to zero for edge aligned mode
// [6:5] Set CMS bits to 10 for Center Aligned mode 2, up down mode with flags set when counter reaches the top.
//value |= TIM_CR1_CMS_1;
// [4] Set DIR bit to zero for upcounting
// [3] Set OPM bit to zero so that the counter is not stopped at update event
// [2] Set URS bit to zero so that anything can create an interrupt
// [1] Set UDIS bit to zero to generate an update event
// [0] Set the CEN bit to zero to disable the counter
// * Set the TIMx_CR1 Register: *
TIM1->CR1 |= value; // *** TIM1 control register 2: TIMx_CR2 ***
value = ;
// [14] Set OIS4 bit to zero, the idle state of OC4 output
// [13] Set OIS3N bit to zero, the idle state of OC3N output
// [12] Set OIS3 bit to zero, the idle state of OC3 output
// [11] Set OIS2N bit to zero, the idle state of OC2N output
// [10] Set OIS2 bit to zero, the idle state of OC2 output
// [9] Set OIS1N bit to zero, the idle state of OC1N output
// [8] Set OIS1 bit to zero, the idle state of OC1 output
// [7] Set TI1S bit to zero, connecting only CH1 pin to TI1 input
// [6:4] Set to 111: The OC4REF signal is used as trigger output (TRGO)
// value |= TIM_CR2_MMS_2 | TIM_CR2_MMS_1 | TIM_CR2_MMS_0;
// value |= TIM_CR2_MMS_1 | TIM_CR2_MMS_0;
// [3] Set CCDS bit to zero, request sent when CCx event occurs
// [2] Set CCUS bit to 1, capture/compare control bits are updated by setting the COMG bit or when a rising edge occurs on TRGI
// value |= 0x4;
// [0] Set CCPC bit to 1, CCxE, CCxNE and OCxM are update on a commutation event, or rising edge on TRGI
// value |= 0x1;
// * Set the TIMx_CR2 Register: *
TIM1->CR2 = value; // *** TIM1 Auto Reload Register: ARR ***
value = ;
// [15:0] Set ARR bits to the frequency to be loaded in:
newVal = ceil(period_us/PWMSTEP_US);
value = (unsigned int) newVal;
// * Set the TIMx_ARR Register:
TIM1->ARR = value; // *** TIM1 capture/compare register 1: CCR1 ***
value = ;
// [15:0] Set the capture compare value to the duty cycle:
newVal = ceil(duty_us/PWMSTEP_US);
value = (unsigned int) newVal;
// * Set the TIMx_CCR1 Register:
TIM1->CCR1 = value; // *** TIM1 capture/compare register 4: CCR4 ***
value = ;
// [15:0] Set the capture compare value to the duty cycle:
newVal = ceil(duty_us/2.0f/PWMSTEP_US);
value = (unsigned int) newVal;
// * Set the TIMx_CCR1 Register:
TIM1->CCR4 = TIM1->ARR - CH4SHIFT; // *** TIM1 capture/compare mode register 2: CCMR2
value = ;
// [15] Set OC4CE bit to 0, OC4Ref is not affected by the ETRF input
// [14-12] Set the OC4M bits to '110', PWM mode 1, which is what we want I think.
value |= TIM_CCMR2_OC4M_2 | TIM_CCMR2_OC4M_1;
// [11] Set the OC4PE bit to 1, meaning read/write operations to the preload event require an update event.
value |= 0x800;
// [10] Set the OC4FE bit to 0, the output compare fast enable is disabled
// [9:8] Set the CC4S bits to 0, the channel is configured as an output.
// * Set the TIMx_CCMR2 Register: *
TIM1->CCMR2 = value; // *** TIM1 capture/compare mode register 1: CCMR1
value = ;
// [7] Set OC1CE bit to 0, OC1Ref is not affected by the ETRF input
// [6-4] Set the OC1M bits to '110', PWM mode 1, which is what we want I think.
value |= TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1;
// [3] Set the OC1PE bit to 1, meaning read/write operations to the preload event require an update event.
value |= 0x8;
// [2] Set the OC1FE bit to 0, the output compare fast enable is disabled
// [1:0] Set the CC1S bits to 0, the channel is configured as an output.
// * Set the TIMx_CCMR1 Register: *
TIM1->CCMR1 = value; // *** TIM1 capture/compare enable register: CCER
value = ;
// [15:4] - Don't care:
// [3] Set CC1NP bit to zero for active high.
// [2] Set CC1NE bit to 0, to de-activate the OC1N signal
// value |= 0x4;
// [1] Set the CC1P bit to zero for active high.
// [0] Set the CC1E bit to 1, to de-activate the OC1 signal
// value |= 0x1;
// * Set the TIM1_CCER Register: *
TIM1->CCER = value; // *** TIM1 break and dead-time register: BDTR
value = ;
// [15] Set MOE bit to 1 to enable the OC and OCN outputs
value |= 0x8000;
// [11] Set the OSSR bit such that the ouputs are forced to their idle mode when not running
//value |= TIM_BDTR_OSSR;
// [10] Set OSSI bit such that the outputs are forced to their idle mode when MOE = 0
value |= TIM_BDTR_OSSI;
// * Set the TIM1_BDTR register:
TIM1->BDTR = value; // *** TIM1 DMA/Interrupt enable register: DIER
value = ;
// [2] Set the CC1IE bit to 1, to trigger an interrupt when counter 1 has a match - which should be half way through the duty cycle.
value |= TIM_DIER_CC4IE;
// Set the TIM1_DIER register:
TIM1->DIER |= value; // Set the UG bit in the EGR register to kick things off:
value = ;
TIM1->EGR = value; // Configure the interrupt:
NVIC_SetVector(TIM1_CC_IRQn, (uint32_t)&TIM1_CC_IRQHandler);
NVIC_EnableIRQ(TIM1_CC_IRQn); return; }

STM32F4 How do you generate complementary PWM Outputs?的更多相关文章

  1. Generate stabilized PWM signals

    A standard technique for generating analog voltages using µCs is to use a PWM output and filter the ...

  2. Two PWM outputs from MCU combine to form a monotonic 16-bits DAC

    http://www.edn.com/design/analog/4329365/Combine-two-8-bit-outputs-to-make-one-16-bit-DAC

  3. Renesas M16C/6X -- Simple PWM Signal Generation Using DMA

    1. Requirements To generate a PWM output, we need to create a train of pulses with constant period a ...

  4. how to generate an analog output from a in-built pwm of Atmega 32AVR microcontrloller?

    how to generate an analog output from a in-built pwm of Atmega 32AVR microcontrloller? you need a re ...

  5. M451 PWM对照数据手册分析

    PWM_T Struct Reference Control Register » Pulse Width Modulation Controller(PWM)   typedef struct { ...

  6. 说说M451例程之PWM的寄存器讲解

    M451提供了两路PWM发生器.每路PWM支持6通道PWM输出或输入捕捉.有一个12位的预分频器把时钟源分频后输入给16位的计数器,另外还有一个16位的比较器.PWM计数器支持向上,向下,上下计数方式 ...

  7. STM32 Timer : Base Timer, Input Capture, PWM, Output Compare

    http://www.cs.indiana.edu/~geobrown/book.pdf An example of a basic timer is illustrated in Figure 10 ...

  8. STM32F4 -- How to use the DMA burst feature

    Bits 15:13 Reserved, must be kept at reset value. Bits 12:8 DBL[4:0]: DMA burst length This 5-bit ve ...

  9. STM32学习日志--使用DMA功能自动更新PWM的输出

    /******************************************************************************* 编译环境: EWARM V5.30 硬 ...

随机推荐

  1. ios TextField限制输入两位小数

    只需要实现textField的这个代理方法就可以实现 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange: ...

  2. Floyd判圈算法 UVA 11549 - Calculator Conundrum

    题意:给定一个数k,每次计算k的平方,然后截取最高的n位,然后不断重复这两个步骤,问这样可以得到的最大的数是多少? Floyd判圈算法:这个算法用在循环问题中,例如这个题目中,在不断重复中,一定有一个 ...

  3. RPM Database

    RPM Database RPM 不仅在安装.升级.卸载方面工作出色,而且在查询和验证方面也表现非凡.你很久前安装了一个数据库软件,但现在忘记了它的版本号,也不知道它的说明文档的位置,可以通过 RPM ...

  4. 在 Linux 上找出并解决程序错误的主要方法【转】

    转自:https://www.ibm.com/developerworks/cn/linux/sdk/l-debug/index.html 本文讨论了四种调试 Linux 程序的情况.在第 1 种情况 ...

  5. linux下常用FTP命令 上传下载文件【转】

    1. 连接ftp服务器 格式:ftp [hostname| ip-address]a)在linux命令行下输入: ftp 192.168.1.1 b)服务器询问你用户名和密码,分别输入用户名和相应密码 ...

  6. casperjs 知乎登陆

    phantom.casperTest = true; phantom.outputEncoding="utf-8"; var fs = require('fs'); var cas ...

  7. Java 语言多态性

    https://www.ibm.com/developerworks/cn/java/java-language-polymorphism/index.html 定义多态性 多态性是面向对象编程中的一 ...

  8. Android 5.0 行为变更

    Android 5.0 除了提供诸多新特性和功能外,还对系统和 API 行为做出了各种变更.本文重点介绍您应该了解并在开发应用时加以考虑的一些主要变更. 如果您之前发布过 Android 应用,请注意 ...

  9. NOIp 2018 提高组

    T1铺设道路 传送门 题目描述 春春是一名道路工程师,负责铺设一条长度为 $ n $ 的道路. 铺设道路的主要工作是填平下陷的地表.整段道路可以看作是 $ n $ 块首尾相连的区域,一开始,第 ii ...

  10. read命令

    read命令从键盘读取变量的值,通常用在shell脚本中与用户进行交互的场合. 该命令可以一次读取多个变量的值,变量和输入的值都需要使用空格隔开.在read命令后面,如果没有指定变量名,读取的数据将被 ...