stm32f407 TIM1 and TIM8 sync
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-03-14 7:18 AM
Hello I'm trying to synchronize TIM1 and TIM8 (slaves) via TIM2 (master).. I checked the manual and says it can be done (p. 554/1710).. Although TIM2 works fine, TIM1 and TIM8 are off..
Please check my code bellow and help me out!/* Includes ------------------------------------------------------------------*/
#include ''stm32f4xx.h''
#include ''stm32f4xx_rcc.h''
#include ''stm32f4xx_gpio.h''
#include ''stm32f4xx_tim.h''
#include ''misc.h''
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
/* Private function prototypes -----------------------------------------------*/
void
TIM_Config(
void
);
/* Private functions ---------------------------------------------------------*/
int
main(
void
)
{
TIM_Config();
/* TIM1 Peripheral Configuration ----------------------------------------*/
/* TIM1 Slave Configuration: PWM1 Mode */
TIM_TimeBaseStructure.TIM_Period = 2;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 1;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
/* Slave Mode selection: TIM1 */
TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_Gated);
TIM_SelectInputTrigger(TIM1, TIM_TS_ITR1);
/* TIM8 Peripheral Configuration ----------------------------------------*/
/* TIM8 Slave Configuration: PWM1 Mode */
TIM_TimeBaseStructure.TIM_Period = 2;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 1;
TIM_OC1Init(TIM8, &TIM_OCInitStructure);
/* Slave Mode selection: TIM8 */
TIM_SelectSlaveMode(TIM8, TIM_SlaveMode_Gated);
TIM_SelectInputTrigger(TIM8, TIM_TS_ITR1);
/* TIM2 Peripheral Configuration ----------------------------------------*/
/* Time Base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 255;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 4;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* Channel 1 Configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = 127;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
/* Master Mode selection */
TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);
/* Select the Master Slave Mode */
TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);
/* TIM2 counter enable */
TIM_Cmd(TIM2, ENABLE);
/* TIM enable counter */
TIM_Cmd(TIM1, ENABLE);
TIM_Cmd(TIM8, ENABLE);
/* Main Output Enable */
TIM_CtrlPWMOutputs(TIM2, ENABLE);
while
(1)
{}
}
void
TIM_Config(
void
)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIOA and GPIOC clocks enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOC, ENABLE);
/* TIM1 and TIM8 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_TIM8, ENABLE);
/* TIM2 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* GPIOA Configuration: TIM1 and TIM2 Channel1 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* GPIOC Configuration: TIM8 Channel1 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Connect TIM pins to AF1 */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_TIM2);
/* Connect TIM pins to AF3 */
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM8);
}
Thanks in advance
#discovery #stm32 #stm32f4
- Labels:
-
STM32F4 Series
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-03-14 8:56 AM
TIM_CtrlPWMOutputs(TIM2, ENABLE);
Don't need that for TIM2, critical for TIM1 and TIM8 (they're special)They also need the repetition count, set to zero.Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-03-14 9:10 AM
Thanks for the help but still nothing happens though TIM2 works fine..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-03-14 10:25 AM
Ok, I don't have my scope with me.
What board are you doing this on? And what signals do you expect to see (nature, frequency, etc)?Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-03-14 3:52 PM
I'm using stm32f407vgt6 discovery board and I want to start TIM1 and TIM8 simultaneously.. I used this as my template (
) which works fine but not when I change it for TIM1 and TIM8..- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-03-14 6:34 PM
So you expect the timers to output a continuous frequency, what frequency, or modulate?
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-03-15 4:08 AM
I want TIM1 and TIM8 to work at 42.5KHz, which I haven't fix it inside the code, but I don't think it's an issue. TIM2 works at 105.3KHz right now, checked with my oscilloscope.. My HSE frequency is at 25MHz, usually I change it to 80MHz (system_stm32f4xx.c)..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-03-15 5:59 PM
Really not seeing Gated mode being at all appropriate for that. Surely a Reset mode driven by a one-shot would be the way to phase align two timers.
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-03-15 6:02 PM
42.5 KHz is not cleanly divisible into 80 MHz, perhaps you'd want to try 85 MHz?
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-03-16 10:13 AM
I can't see how reset mode would help me out, since it just resets the two clocks (I'll check it out though)..
The frequency of HSE doesn't mean a lot.. It's the timer period and prescaller which define the appropriate frequency.. For example on my code (not this one i posted here) I have SYSCLK=168MHz and HSE=80MHz.. Then I wrote that#define FREQUENCY 42500 /* output frequency 42500 KHz */
/* Compute the value to be set in ARR register to generate the desired signal frequency */
TimerPeriod = ((SystemCoreClock/2) / FREQUENCY) - 1;
TIM_TimeBaseStructure.TIM_Period = TimerPeriod - 1;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
and the oscilloscope shows 5KHz..
