2012-01-26 08:58 AM
i am beginner about programming on stm32l discovery board. this is my first time programming on stm microcontrollers. I can open led. close led. i can use lcd on it. but i want to do that.
i have int i=0; and i want to increase value of it every 1 second or 100 ms and i want to show i value on lcd. Showing on lcd not problem but. using tiimer problem.. i did somethings but it is not working . finally i decided to write my problem here. thanks. #how-to-use-timer-on-stm32l2012-01-27 09:33 AM
2012-01-27 12:03 PM
I did what you sad. it worked. But I need Timer using . beacuse i need 3 timer and their handlers . but now. i couldn't do anything on timer. I looked a lot examples. anycode isn't working. all corrupt, missing.
I found an example about TIM9 but it is only working on pin. i need function. But thanks for systick reply.2012-01-30 04:11 AM
can anyone post a timer example.
2012-01-31 01:16 PM
I did what you sad. it worked. But I need Timer using . beacuse i need 3 timer and their handlers . but now. i couldn't do anything on timer. I looked a lot examples. anycode isn't working. all corrupt, missing.
I found an example about TIM9 but it is only working on pin. i need function. The STM32L can't be that different to the other STM32 parts, it should be a matter of enabling the TIM_Update interrupt, and providing a simple service routine. Why don't you paste in some of the examples that are supposedly not working, and we'll see if there are any obvious coding mistakes or issues. You also don't specify your development environment, which might be helpful if you want some click-n-run examples.
2012-01-31 04:13 PM
2012-01-31 04:15 PM
2012-01-31 05:16 PM
Something like this, perhaps
#include ''stm32l1xx.h''
#include ''discover_board.h''
#include ''stm32l1xx_tim.h''
static volatile uint32_t TimingDelay;
void TimingDelay_Decrement(void);
int kutay = 0;
int tick_sayici = 0;
void GPIO_Configuration(void);
int main(void)
{
GPIO_Configuration();
/* Setup SysTick Timer for 1 second interrupts */
SysTick_Config(SystemCoreClock / 1); // Cannot exceed 16,777,215
/* Set SysTick Preemption Priority, it's a system handler rather than a regular interrupt */
NVIC_SetPriority(SysTick_IRQn, 0x04);
GPIO_HIGH(GPIOB,GPIO_Pin_6); // PB.06 BLUE
GPIO_HIGH(GPIOB,GPIO_Pin_7); // PB.07 GREEN
while (1)
{
}
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); // Only important for remapping
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
// PB.06 BLUE LED, PB.07 GREEN LED
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void Delay(uint32_t nTime)
{
TimingDelay = nTime;
while(TimingDelay != 0);
}
// Called from SysTick_Handler
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00){
TimingDelay--;}
tick_sayici++;
if (tick_sayici >= 60) // Minute
tick_sayici = 0;
if (tick_sayici % 2)
{
GPIO_LOW(GPIOB,GPIO_Pin_7);
}
else
{
GPIO_HIGH(GPIOB,GPIO_Pin_7);
}
}
// This would normally exist in stm32l1xx_it.c, or equivalent
void SysTick_Handler(void)
{
TimingDelay_Decrement();
}
2012-01-31 05:54 PM
Or this for SysTick + TIM2, you might have to play with the #include's
#include ''stm32l1xx.h'' // low power mcu
#include ''discover_board.h''
#include ''stm32l1xx_tim.h''
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void RCC_Configuration(void);
void TIM2_Configuration(void);
void SysTick_Configuration(void);
int main(void)
{
RCC_Configuration();
NVIC_Configuration();
GPIO_Configuration();
GPIO_HIGH(GPIOB,GPIO_Pin_6); // PB.06 BLUE
GPIO_HIGH(GPIOB,GPIO_Pin_7); // PB.07 GREEN
SysTick_Configuration();
TIM2_Configuration();
while(1); // Infinite loop, main() must not exit
}
void RCC_Configuration(void)
{
/* Enable the GPIOs Clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
/* Enable APB1 clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_PWR,ENABLE);
/* Enable SYSCFG */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
/* Allow access to the RTC */
PWR_RTCAccessCmd(ENABLE);
/* Reset Backup Domain */
RCC_RTCResetCmd(ENABLE);
RCC_RTCResetCmd(DISABLE);
/*!<
LSE
Enable */
RCC_LSEConfig(RCC_LSE_ON);
/*!< Wait till LSE is ready */
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) {}
/*!< LCD Clock Source Selection */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel
=
TIM2_IRQn
;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority
=
2
;
NVIC_InitStructure.NVIC_IRQChannelSubPriority
=
1
;
NVIC_InitStructure.NVIC_IRQChannelCmd
=
ENABLE
;
NVIC_Init(&NVIC_InitStructure);
/* Set SysTick Preemption Priority, it's a system handler rather than a regular interrupt */
NVIC_SetPriority(SysTick_IRQn, 0x04);
}
void SysTick_Configuration()
{
RCC_ClocksTypeDef RCC_Clocks;
RCC_GetClocksFreq(&RCC_Clocks);
SysTick_Config(RCC_Clocks.HCLK_Frequency / 1); // 1 Hz
}
void TIM2_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_DeInit(TIM2);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Prescaler
=
16000
- 1; // 16 MHz /
16000
= 1 KHz
TIM_TimeBaseStructure.TIM_Period
=
1000
- 1; // 1 KHz /
1000
= 1 Hz;
TIM_TimeBaseStructure.TIM_ClockDivision
=
0
;
TIM_TimeBaseStructure.TIM_CounterMode
=
TIM_CounterMode_Up
;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* Enable TIM2 Update Interrupt */
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
/* Enable TIM2 */
TIM_Cmd(TIM2,ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// PB.06 BLUE LED, PB.07 GREEN LED
GPIO_InitStructure.GPIO_Pin
=
GPIO_Pin_6
| GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode
=
GPIO_Mode_OUT
;
GPIO_InitStructure.GPIO_OType
=
GPIO_OType_PP
;
GPIO_InitStructure.GPIO_PuPd
=
GPIO_PuPd_UP
;
GPIO_InitStructure.GPIO_Speed
=
GPIO_Speed_10MHz
;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
// These would normally exist in stm32l1xx_it.c, or equivalent
void SysTick_Handler(void)
{
static int
SYSTICK_Tick
=
0
;
SYSTICK_Tick++;
if (SYSTICK_Tick >= 60) // 1 Minute
SYSTICK_Tick = 0;
if (SYSTICK_Tick % 2) // Toggle PB.07 GREEN LED
{
GPIO_LOW(GPIOB,GPIO_Pin_7);
}
else
{
GPIO_HIGH(GPIOB,GPIO_Pin_7);
}
}
void TIM2_IRQHandler(void)
{
static int TIM2_Tick = 0;
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
TIM2_Tick++;
if (TIM2_Tick >= 60) // 1 Second
TIM2_Tick = 0;
if (TIM2_Tick % 2) // Toggle PB.06 BLUE LED
{
GPIO_LOW(GPIOB,GPIO_Pin_6);
}
else
{
GPIO_HIGH(GPIOB,GPIO_Pin_6);
}
}
}