jeudi 26 février 2015

PWM example for STM32F4 Discovery

/*PWM example for STM32F4 Discovery@author     OUESLATI SABRI@email        sabrioueslati1@gmail.com@ide        Keil uVision 5 -- IAR Embedded workbech   i will set the PWM using the timer 4 that is located on PD12   and we will see its signal on the diode PD12*/


#include "stm32f4xx.h"

#include "stm32f4xx_rcc.h"

#include "stm32f4xx_gpio.h"

#include "stm32f4xx_tim.h"


void InitializeTimer(void){ 

 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);  

  TIM_TimeBaseInitTypeDef timerInitStructure;  

  timerInitStructure.TIM_Prescaler = 40000;  

  timerInitStructure.TIM_CounterMode = TIM_CounterMode_Down; 

   timerInitStructure.TIM_Period = 1050;    

timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;  

  timerInitStructure.TIM_RepetitionCounter = 0; 

   TIM_TimeBaseInit(TIM4, &timerInitStructure);   

 TIM_Cmd(TIM4, ENABLE);

}

void InitializePWMChannel()

  TIM_OCInitTypeDef outputChannelInit = {0,};

    outputChannelInit.TIM_OCMode = TIM_OCMode_PWM1;

    outputChannelInit.TIM_Pulse = 524; 

   outputChannelInit.TIM_OutputState = TIM_OutputState_Enable; 

   outputChannelInit.TIM_OCPolarity = TIM_OCPolarity_High;


    TIM_OC1Init(TIM4, &outputChannelInit);   

 TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);


    GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4);

}

void InitializeLEDs() {   

 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);


    GPIO_InitTypeDef gpioStructure;  

  gpioStructure.GPIO_Pin = GPIO_Pin_12;  

  gpioStructure.GPIO_Mode = GPIO_Mode_AF; 

   gpioStructure.GPIO_Speed = GPIO_Speed_50MHz; 

   GPIO_Init(GPIOD, &gpioStructure);

}

int main()  {    

InitializeLEDs(); 

   InitializeTimer();   

 InitializePWMChannel();  

  for (;;)    {    }

}


Discovery kit for STM32F407/417 lines

Key Features

  • STM32F407VGT6 microcontroller featuring 32-bit ARM Cortex-M4F core, 1 MB Flash, 192 KB RAM in an LQFP100 package
  • On-board ST-LINK/V2 with selection mode switch to use the kit as a standalone ST-LINK/V2 (with SWD connector for programming and debugging)
  • Board power supply: through USB bus or from an external 5 V supply voltage
  • External application power supply: 3 V and 5 V
  • LIS302DL or LIS3DSH ST MEMS 3-axis accelerometer
  • MP45DT02, ST MEMS audio sensor, omni-directional digital microphone
  • CS43L22, audio DAC with integrated class D speaker driver
  • Eight LEDs:
  • LD1 (red/green) for USB communication
  • LD2 (red) for 3.3 V power on
  • Four user LEDs, LD3 (orange), LD4 (green), LD5 (red) and LD6 (blue)
  • 2 USB OTG LEDs LD7 (green) VBus and LD8 (red) over-current
  • Two push buttons (user and reset)
  • USB OTG FS with micro-AB connector
  • Extension header for all LQFP100 I/Os for quick connection to prototyping board and easy probing


lundi 16 février 2015

GPIO on the STM32


General Purpose Input Output (GPIO)
The STM32 is well served with general purpose
IO pins, having typically 80 bidirectional IO pins.
The IO pins are arranged as five ports each
having 16 IO lines.

Configuration Registers
The STM32 has four configuration registers for each of the ports.

Port mode register – GPIOx_MODER
Output type register – GPIOx_OTYPER
Speed register – GPIOx_OSPEEDR
Pull-up/Pull-down register – GPIOx_PUPDR

Each of these registers is 32-bits wide although not all of the bits are used in all of the registers.

  • GPIO_Mode: Mode of pins operation
    • GPIO_Mode_IN: Set pin to input
    • GPIO_Mode_OUT: Set pin to be an output
    • GPIO_Mode_AF: Set pin to alternating function (to use with peripheral ex. SPI, USART, etc)
    • GPIO_Mode_AN: Set pin to be an analog (ADC or DAC)

  • GPIO_OType: Mode for pin’s output type
    • GPIO_OType_PP: Output type is push-pull
    • GPIO_OType_OD: Output type is open drain

  • GPIO_PuPd: Select pull resistors or disable it
    • GPIO_PuPd_UP: Enable pull up resistor
    • GPIO_PuPd_DOWN: Enable pull down resistor
    • GPIO_PuPd_NOPULL: Disable pull resistor

  • GPIO_Speed: Select GPIO speed
    • GPIO_Speed_100MHz
    • GPIO_Speed_50MHz
    • GPIO_Speed_25MHz
    • GPIO_Speed_2MHz




 EXEMPLE :