cancel
Showing results for 
Search instead for 
Did you mean: 

Creating a square wave

thomaspfeffer9
Associate II
Posted on July 11, 2011 at 15:01

I'm an absolutly beginner in microcontrollers.

I want to create a simple square wave signal on STM32F103VB with

tim3, ch2, but nothing happens.

Can anybody tell me what I'm doing wrong?

Thanks

Tom

#define DEF_SPEED 140;

#define CCR1_Val  32768;

int main(void)

{

  GPIO_InitTypeDef GPIO_InitStructure;

  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

  TIM_OCInitTypeDef TIM_OCInitStructure;

  // RCC/System clocks configuration

  RCC_Config();

  // Enable Timer3 clock and release reset

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

  // map PC7 as TIM3-CH2

  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_7; //7

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // clock to 50MHz

  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;  // alternate function pull

  GPIO_Init(GPIOC, &GPIO_InitStructure);

  GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE); // ?!!!!!!!!!!

  // Time base configuration

  TIM_TimeBaseStructure.TIM_Period = 0xFF; // 8 bit resolution

  TIM_TimeBaseStructure.TIM_Prescaler = DEF_SPEED;       

  TIM_TimeBaseStructure.TIM_ClockDivision = 0;    

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

 

  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

  // Output Compare Toggle Mode configuration: Channel2

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;          

  TIM_OCInitStructure.TIM_Pulse = CCR1_Val;  

  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;

  TIM_OC2Init(TIM3, &TIM_OCInitStructure);

  TIM_Cmd(TIM3,ENABLE);

 

  while (1) {}

}

14 REPLIES 14
saudkhanis
Associate II
Posted on June 05, 2014 at 17:57

The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6Y5&d=%2Fa%2F0X0000000bqR%2FKSGp5OE3ZXhZUxxd.x4_4OuNf9f5HzJn1SW6CkWDsBE&asPdf=false
Posted on June 05, 2014 at 18:13

What board and chip are we talking about here? If it's an F100 on a VL-Discovery double check if the chip supports the specific timers

What other output do you see from your program?

Do you see output from the non-remapped pins?

You might need to change the JTAG remapping

   /* Disable the Serial Wire Jtag Debug Port SWJ-DP */

   GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE);

or

   /* JTAG-DP Disabled and SW-DP Enabled */

   GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
saudkhanis
Associate II
Posted on June 05, 2014 at 18:35

Sorry i forgot to mention im using STM32F100 Discovery board. I tried using TIM3 full remapping and i was able to get blinking LED (PC9). I thought it might be something to do with CH4 as PC9 is on CH4. With TIM3 partial remapping I was also able to get a pwm on PB1 which again is CH4. If I try to get the output on CH2 (TIM2) or CH1 (TIM3) I dont get the output.

Posted on June 05, 2014 at 19:09

/* Dual PWM Remap Demo PB3/PB4 - STM32 VL-Discovery - sourcer32@gmail.com */
#include ''stm32F10x.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
// clock for GPIO and AFIO (REMAP)
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
// clock for TIM2 and TIM3
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// PB3 TIM2_CH2
// PB4 TIM3_CH1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_FullRemap_TIM2, ENABLE); // Remapped to CH2 PB3
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); // Remapped to CH1 PB4
/* JTAG-DP Disabled and SW-DP Enabled */
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); // Off PB3/PB4 - REQUIRED
}
/**************************************************************************************/
void TIM2_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseStructure.TIM_Prescaler = (SystemCoreClock / 1000000) - 1; // 1 MHz
TIM_TimeBaseStructure.TIM_Period = 500 - 1; // 1 MHz / 500 = 2 KHz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
// Channel 2 configuration = PB.03 TIM2_CH2
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = (TIM_TimeBaseStructure.TIM_Period + 1) / 2; // 50% Duty
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
// turning on TIM2 and PWM outputs
TIM_Cmd(TIM2, ENABLE);
}
/**************************************************************************************/
void TIM3_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseStructure.TIM_Prescaler = (SystemCoreClock / 1000000) - 1; // 1 MHz
TIM_TimeBaseStructure.TIM_Period = 250 - 1; // 1 MHz / 250 = 2 KHz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
// Channel 1 configuration = PB.04 TIM3_CH1
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = (TIM_TimeBaseStructure.TIM_Period + 1) / 4; // 25% Duty
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
// turning on TIM3 and PWM outputs
TIM_Cmd(TIM3, ENABLE);
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
TIM2_Configuration();
TIM3_Configuration();
while(1);
}
/**************************************************************************************/
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf(''Wrong parameters value: file %s on line %d

'', file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
saudkhanis
Associate II
Posted on June 05, 2014 at 19:19

Thank you clive1! I will try this now.