Program on STM32F100 (Extra Suplement)
For additional example to program the microcontroller STM32F100 of ARM Cortex M3, the following are listed some examples and explanations.
So let's start do it......
Modul used in this project is the STM32 VLDI (Value Line Discovery). Modul is quite simple and easy to use in any application. Modul with the size 43.18 mm x 84.6 mm is made up of STM32F100RBT6B, on board ST-LINK, two-output LED LD3 and LD4 (green and blue), and two push buttons (USER and RESET). STM32F100RBT6B that contained on modul has 128 KB of flash memory and 8 KB RAM memory. In addition, it can also be operated at a voltage level of 3.3V or 5V.
Figure 1. STM32 Value Line Discovery
While the pinout configuration of STM32F100 is shown by the following figure.
Figure 2. Pinout Configuration of STM32F100
Here is a sample program to control LED LD3 (PC9) based on input USER button (PA0). If the USER button is pressed, the LED will light from dim to bright, and if button not pressed, the LED will be "on-off-on-off" interchangeably with a 300ms delay time.
The first thing that needs to be included is library that is used, ie
#include "stm32f10x.h" // STM32F10x libraryThen, the next step is to create void "delay" to generate delay results.
#include "stm32f10x_gpio.h" // pin I/O library
#include "stm32f10x_rcc.h" // Reset and Clock Control Library
#include "stm32f10x_tim.h" // Timer Library
void delay(uint32_t tunda);
static __IO uint32_t waktu_tunda;
void delay(uint32_t tunda)
{waktu_tunda = tunda;
while(waktu_tunda != 0);}
void SysTick_Handler(void)
{if (waktu_tunda != 0)
{waktu_tunda --;}}
Before we initialize port I/O and Timer, we must activate RCC of GPIO and Timer, as shown by the following syntax.
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);After that, we can initialize port I/O and Timer, as follows.
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = 1000 - 1;
TIM_TimeBaseStructure.TIM_Prescaler = 240 - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV4;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE);
TIM_Cmd(TIM3, ENABLE);
From that initialization, we know that PC9 is used as output and PA0 is used as input. TIM3 channel 4 is used to generate PWM signal. While the result of PWM frequency is 24MHz/(240x1000)=100kHz.
If we seen in the datasheet, TIM3 channel 4 is placed on pin B1. While the LED that is used, is placed on PC9, so we need "remap" function. On the datasheet, PC9 can be converted (remapping) as TIM3 channel 4. The list of code program is
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig( GPIO_FullRemap_TIM3, ENABLE );
The code program is used to activate RCC of AFIO (Alternate-Function I/O) and to choose remap configuration that is used.
To generate a 10us delay time, we use this function.
if (SysTick_Config(SystemCoreClock / 100000)) while (1);If we need a 1ms delay time, we use a prescaler 1000.
Then, the syntax of LED control, inserted in while(1) loop, as follows.
while(1)If the button is pressed, the LED will light from dim to bright, and if button not pressed, the LED will be "on-off-on-off" (toggle) interchangeably with a 300ms delay time.
{ tombol = (GPIOA->IDR & 0x1);
if (tombol==1)
{ if (i<1000)
{TIM3->CCR4 = i;
delay(100);i++;}
else i=0;}
else
{ TIM3->CCR4 = 1000;
delay(30000);
TIM3->CCR4 = 0;
delay(30000);i=0;}}
for futher information http://stm32.kosyak.info/doc/index.html
http://micromouseusa.com/?p=296
Ok, I think my explanation is enough...