Getting Started with STM32F103RC

Thread Starter

devjeetmandal

Joined May 7, 2017
48
Hello Everyone,
i am beginning with stm32f103rc and i am really confused about how to write program for blink led.
i am using keil uVision5.
please can anyone help me..
 

Thread Starter

devjeetmandal

Joined May 7, 2017
48
actually i have done nothing...
just set up keil so that i could write program
was reading user manual which was enough to confuse me
and was watching a video on stm32 cube mx
how to configure and use it.
was wondering y cant i just use keil.


was wondering how to set a pin as input or ouput
and how to make it high or low

as i have done LPC2148
where i need to set IOxDIR for setting input or output for a pin
and IOxSET or IOxCLR for high or low
and there were number of tutorials on internet for this


Now stm has nothing and evrything seems really confusing
 

MrChips

Joined Oct 2, 2009
34,809
Sorry, that is not correct. ST has everything you need.

Use STM32CubeMX to get started. Here you will set up your project, choose your MCU and create a code template for all the I/O functions that you need.

Then you go to Keil and fill in the main code, compile and run.

Look through your toolset for examples or go to the ST website.
If you are still stuck I will step you through.
 

Thread Starter

devjeetmandal

Joined May 7, 2017
48
okay i have done led blinking with stm32 cube mx by using HAL library
what if i don't want to use HAL library??

i want to build everything from core what should i do.??
 

MrChips

Joined Oct 2, 2009
34,809
okay i have done led blinking with stm32 cube mx by using HAL library
what if i don't want to use HAL library??

i want to build everything from core what should i do.??
You could write directly to the hardware registers but I would not recommend that.
Look through the examples and they show you how to call the libraries to set up the hardware.

Be aware that there are a number of things to be initialized before you can use any hardware feature. Miss one and the code wouldn't work.
 

simozz

Joined Jul 23, 2017
170
i am using keil uVision5.
It's a few years I don't use an STM32 micro-controllers but you may would take a look at openstm32 (free) : http://www.openstm32.org/HomePage
A friend of mine gave me a positive feedback about it.

Also, don't forget the STM32F1xx standard peripheral libraries.

The following is a code I used in my personal training with the STM32F4 discovery board for toggling a GPIO:

C:
#include <stm32f4xx.h>

void TIM1_config(void)
{
    /* Reset TM1 peripheral */
    RCC->APB2RSTR |= RCC_APB2RSTR_TIM1RST;
    RCC->APB2RSTR &= ~(RCC_APB2RSTR_TIM1RST);

    /* TIM1 CLOCK ENABLE*/
    RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;

    /* SET the autoreload value */
    TIM1->ARR &= 0xffff;

    /* TIM1 UP Counting & CLK Division x 2, downcount & autoreload enabled */
    TIM1->CR1 = TIM_CR1_CKD_0 | TIM_CR1_DIR | TIM_CR1_ARPE;

}

void delay_TM1(uint16_t CNTR_VALUE)
{
     /* SET the counter value */
     TIM1->CNT = CNTR_VALUE;
     /* Enable TIM1 - Turn it on*/
     TIM1->CR1 |= TIM_CR1_CEN;

     while(!TIM1->CNT)
        ;

     /* Turn off TM1 */
     TIM1->CR1 &= ~(TIM_CR1_CEN);
}

void GPIO_config(void)
{
    /* Reset GPIOD peripheral */
    RCC->AHB1RSTR |= RCC_AHB1RSTR_GPIODRST;
    RCC->AHB1RSTR &= ~(RCC_AHB1RSTR_GPIODRST);

    /* GPIOD CLOCK ENABLE*/
    RCC->AHB1ENR |= RCC_AHB1ENR_GPIODEN;

    /* MODER - General I/O purpose (Digital) Output */
    GPIOD->MODER = GPIO_MODER_MODER15_0;

    /* 100 MHz High speed or maximum high speed */
    GPIOD->OSPEEDR = GPIO_OSPEEDER_OSPEEDR15_0;

    /* Push Pull Output Stage */
    GPIOD->OTYPER = 0x0000;

    /* No pull-up or pull-down */
    GPIOD->PUPDR = 0x0000;
}

int main(void)
{
    TIM1_config();
    GPIO_config();

    while(1)
    {
       /* Set the pin to logic level 1 */
       GPIOD->BSRRL = 0x00008000;

       delay_TM1(0x00ff);
       /* Set the pin to logic level 0 */
       GPIOD->BSRRH = 0x00008000;

       delay_TM1(0x00ff);
    }

    return 0;
}
Even if it was not written for the micro-controller you are using, you may find it useful.
simozz
 
Last edited:

Thread Starter

devjeetmandal

Joined May 7, 2017
48
so i have been trying to blink led using stm32f103rc (without using HAL library)
i came up with this standard peripheral library and written a code based on that

C:
#include "stm32f10x.h"

void delay(int ms)
{
   int i,j;
   for (i=0;i<2048;i++)
   {
     for (j=0;j<ms;j++);
   }
}

int main(void)
{
   RCC_AHBPeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
   GPIO_InitTypeDef Init_GPIO;
   Init_GPIO.GPIO_Pin = GPIO_Pin_13;
   Init_GPIO.GPIO_Mode = GPIO_Mode_Out_PP;
   Init_GPIO.GPIO_Speed = GPIO_Speed_50MHz;

   GPIO_Init(GPIOC,&Init_GPIO);
   while (1)
   {
     GPIO_SetBits(GPIOC,GPIO_Pin_13);
    delay(1000);
    GPIO_ResetBits(GPIOC,GPIO_Pin_13);
    delay(1000);
   }
}

Now also i am not able to blink led on Port c 13.
what are the possible mistakes?
please help
thanks
 
Last edited:

MrChips

Joined Oct 2, 2009
34,809
Sorry, I do not have an STM32F103 device to test this.
I looked at your code and it appears to be correct. I tried it with an STM32F407 and it works, after enabling the correct peripheral clock.

I presume that you tested your setup using HAL and got it working.

Please post your fixes when you get this working.
 

Thread Starter

devjeetmandal

Joined May 7, 2017
48
Hello Everyone,
i have rectified all the mistakes and its working nicely.. thanks a lot for all your help.


i have done two mistakes.
1) by mistake i have inserted the startup file as medium density where as stm32f103rc is high density device.
2) the function i called RCC_AHBPeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); was a mistake
because i realized GPIO is not connected to AHB bus. it is connected to APB2 bus.
hence i checked out in stm32f10x_rcc.c and there was a function namely RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState);
done. so working code using standard pheripheral library is


C:
#include "stm32f10x.h"

void delay(int ms)
{
   int i,j;
   for (i=0;i<2048;i++)
   {
     for (j=0;j<ms;j++);
   }
}

GPIO_InitTypeDef Init_GPIO;

int main(void)
{
   
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
   
   
   Init_GPIO.GPIO_Pin = GPIO_Pin_13;
   Init_GPIO.GPIO_Mode = GPIO_Mode_Out_PP;
   Init_GPIO.GPIO_Speed = GPIO_Speed_50MHz;
   
   GPIO_Init(GPIOC,&Init_GPIO);
   while (1)
   {
     
     GPIO_SetBits(GPIOC,GPIO_Pin_13);
    delay(1000);
    GPIO_ResetBits(GPIOC,GPIO_Pin_13);
    delay(1000);
   }
}
 
Top