cancel
Showing results for 
Search instead for 
Did you mean: 

TIM1 and USART1 Remap

mcallister
Associate II
Posted on January 11, 2008 at 16:23

TIM1 and USART1 Remap

4 REPLIES 4
mcallister
Associate II
Posted on May 17, 2011 at 12:18

I am using the Keil tools and REV B silicon of STM32F103RB.

In attempting to set up the TIM1 peripheral in output compare mode to generate 4 pulses, I had to enable the remapping of the USART1 peripheral before the output would work on TIM1 CH 2. (PA9)

GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE);

Prior to this, I had not set up the USART1 peripheral at all, as this USART is not used. This is how I configured the GPIO for port A:

// Configure Port A Alternate Function push-pull outputs

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 |

GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_Init(GPIOA, &GPIO_InitStructure);

It seems like the USART1 is ''connected'' to PA9 until I set the remap bit. Then it is connected to the TIM1 AF. Is there another way to do this so that I do not have to remap the USART1? I don't want to use the USART at all, and I want to be able to use PB6 & PB7 for I2C in the future.

Another problem I am having with this is that for the 4 output compares on TIM1, as soon as I enable the output compare for channel 2 (even before starting the timer), the output compare channel 2 goes high. The rest of the channels behave as intended, going high only after the output compare fires. I think this might be related to the problem mentioned above? I am using TIM1_OCMode_Active and TIM1_OCPolarity_High.

This is the initialization code for the timer timebase:

{

TIM1_TimeBaseInitTypeDef TIM1_TimeBaseStructure;

/* Time base configuration */

TIM1_TimeBaseStructure.TIM1_Period = 0xFFFF;

TIM1_TimeBaseStructure.TIM1_Prescaler = 8;

TIM1_TimeBaseStructure.TIM1_ClockDivision = 0x0;

TIM1_TimeBaseStructure.TIM1_CounterMode = TIM1_CounterMode_Up;

TIM1_TimeBaseInit(&TIM1_TimeBaseStructure);

}

The following code gets called at a periodic rate to set up the timer output compares:

{

TIM1_OCInitTypeDef TIM1_OCInitStructure;

TIM_Servos.Tim1_Done=0;

// Make sure the timer is disabled

TIM1_Cmd(DISABLE);

// Disable each output compare channel

TIM1_CCxCmd(TIM1_Channel_1,DISABLE);

TIM1_CCxCmd(TIM1_Channel_2,DISABLE);

TIM1_CCxCmd(TIM1_Channel_3,DISABLE);

TIM1_CCxCmd(TIM1_Channel_4,DISABLE);

// Reset the timer to zero

TIM1->CNT = 0x0000;

/* Output Compare Toggle Mode configuration: Channel1 */

TIM1_OCInitStructure.TIM1_OCMode = TIM1_OCMode_Active; // This says to set the pin to active on match

TIM1_OCInitStructure.TIM1_OCPolarity = TIM1_OCPolarity_High; // This sets the active state to high

TIM1_OCInitStructure.TIM1_Pulse = 0x0FFF;

TIM1_OC1Init(&TIM1_OCInitStructure); // Initialize OCMP1

TIM1_OCInitStructure.TIM1_Pulse = 0x0FFF;

TIM1_OC2Init(&TIM1_OCInitStructure);

TIM1_OCInitStructure.TIM1_Pulse = 0x0FFF;

TIM1_OC3Init(&TIM1_OCInitStructure);

TIM1_OCInitStructure.TIM1_Pulse = 0x0FFF;

TIM1_OC4Init(&TIM1_OCInitStructure);

// Force all of the output compare lines to low

GPIO_WriteBit (GPIOA, GPIO_Pin_9,Bit_RESET);

GPIO_WriteBit (GPIOA, GPIO_Pin_10,Bit_RESET);

GPIO_WriteBit (GPIOA, GPIO_Pin_11,Bit_RESET);

GPIO_WriteBit (GPIOA, GPIO_Pin_12,Bit_RESET);

// Enable each output compare channel

TIM1_CCxCmd(TIM1_Channel_1,ENABLE);

TIM1_CCxCmd(TIM1_Channel_2,ENABLE); // <-- When this line runs, the output compare 2 line (PA9) goes high immediately. The other channels do not behave tis way.

TIM1_CCxCmd(TIM1_Channel_3,ENABLE);

TIM1_CCxCmd(TIM1_Channel_4,ENABLE);

// Enable the capture/compare interrupts for each channel

TIM1_ITConfig(TIM1_IT_CC1 | TIM1_IT_CC2 | TIM1_IT_CC3 | TIM1_IT_CC4, ENABLE);

// Enable the timer, rising edge should happen 0x0FFF timer counts later

TIM1_Cmd(ENABLE);

}

Thanks for any help you can give.

ryan2399
Associate II
Posted on May 17, 2011 at 12:18

Are you enabling the USART1 peripheral?

i.e. APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

PA.9 is a GPIO after reset so if you need to redirect USART1 in order to use PA.9 with TIM1 then I suspect you've enabled USART1 at some point.

mcallister
Associate II
Posted on May 17, 2011 at 12:18

This was a problem, I was using

RCC_APB2PeriphClockCmd(RCC_APB2Periph_All,Enable)

However, I've fixed my code to only enable the peripherals I'm using with no change in the output. Since the original post, I've been able to get the other timers working in output compare as well, only TIM1 CH2 has this problem. As soon as I enable the output compare the pin goes high, even before the timer count is enabled.

Any other ideas?

Thanks.

mcallister
Associate II
Posted on May 17, 2011 at 12:18

Bump.

Still having this problem, just not using this output for now. Has anyone else seen this?

Thanks,

Justin