samedi 16 mars 2013

Blinky with STM32F4DISCOVERY board

In this tutorial I'm going to show you one of the most used IP in any ST microcontroller the GPIO. What you need to know witch could be obvious to some that we are actually going to program the microcontroller in the board and not the board!!

The microcontroller of the STM32F4DISCOVERY board is STM32F407VGT ==> Datasheet

I'm using coocox for developing and it's an eclipse based IDE very friendly and intuitive.

The GPIO IP:
As I said it stands for Gneral purpose Input Output,
Each of the GPIO pins can be configured by software as output (push-pull or open-drain,
with or without pull-up or pull-down), as input (floating, with or without pull-up or pull-down)
or as peripheral alternate function. Most of the GPIO pins are shared with digital or analog
alternate functions. All GPIOs are high-current-capable and have speed selection to better
manage internal noise, power consumption and electromagnetic emission.
The I/O configuration can be locked if needed by following a specific sequence in order to
avoid spurious writing to the I/Os registers.
Fast I/O handling allowing maximum I/O toggling up to 84 MHz.
(From datasheet)

There are 5 GPIOs in the STM32 microcontroller (GPIOA,GPIOB,GPIOC,GPIOD,GPIOE) every GPIO has 16 configurable pin and each has 7 registers:
Two are used to configure the sixteen port bits individually, (CRL,CRH)
two are used to read/write the sixteen port bits in parallel, (ODR,IDR)
two are used to set/reset the sixteen port bits individually, (BSRR,BRR)
and one is used to implement a “locking sequence” that is intended to prevent rogue code from accidentally modifying the port configuration (LCKR)

First start new project choose chip (ST-STM32F407VG) . Later on the repositry window will appear, we will do some blinking so we definitely need the GPIO, click on the GPIO check box, RCC, CMSIS boot and M4 CMSIS Core are automatically checked.



In the project tree double click on main.c

GPIO as output :

What are we going to do is to make the leds embedded on the board to blink together every 1sec but to do so we need to know to witch pin and witch GPIO these leds are connected in our STM32F4DISCOVERY board.

According to the board schematics in pag 6
The leds are connected to (PD12/PD13/PD14/PD15) the GPIO used is then GPIOD and the pins are (12, 13, 14, 15)

So what we need to do is explained in the following flowchart:

The program is :
/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"

GPIO_InitTypeDef GPIO_InitStructure;

void Delay(__IO uint32_t nCount);
/**
* @brief GPIO pin toggle program
* @param None
* @retval None
*/
void main(void)
{
/* GPIOD Periph clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

/* Configure PD12, PD13, PD14 and PD15 in output pushpull mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOD, &GPIO_InitStructure);

while (1)
{
/* LEDZ on */
GPIO_SetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
/* Insert delay */
Delay(0xFFFFF);
/* LEDZ off */
GPIO_ResetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
/* Insert delay */
Delay(0xFFFFF);
}
}

/**
* @brief Delay Function.
* @param nCount:specifies the Delay time length.
* @retval None
*/
void Delay(__IO uint32_t nCount)
{
while(nCount--)
{
}
}

GPIO as INPUT : 
Say that we need to read input from the outside world like buttons or switches we have to use the GPIO too.
In this case we are going to read the embedded button in the STM32F4DISCOVERY board.
 According to the board schematics this button is connected to the PA0 (GPIOA, pin 0).
What are we going to do is to set the leds on when the button is set and resetting them when the button is not set.

But we need to configure the GPIOA and the pin 0 as input before we can read data from it.

The program :


#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"

GPIO_InitTypeDef GPIO_InitStructure_Button;
GPIO_InitTypeDef GPIO_InitStructure;

void main(){
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//for buttons

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

/* Configure PD12, PD13, PD14 and PD15 in output pushpull mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure_Button.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure_Button.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure_Button.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA,&GPIO_InitStructure_Button);
 int i;
        while(1){
                i = GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0);
                GPIO_WriteBit(GPIOD,GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15,i);
        }
}







STM32F4DISCOVERY board

I got my STM32F4DISCOVERY board a few days ago and I'm so excited to try it.
This board is definitely one and unique board made by st for amateurs and professional to discover easily and with pleasure the strength of the STM32F4 series.

The board is really rich with features (copy past from the STM32F4DISCOVERY datasheet)
  1. STM32F407VGT6 microcontroller featuring 32-bit ARM Cortex-M4F core, 1 MB Flash, 192 KB RAM in an LQFP100 package
  2. 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)
  3. Board power supply: through USB bus or from an external 5 V supply voltage
  4. External application power supply: 3 V and 5 V
  5.  LIS302DL, ST MEMS motion sensor, 3-axis digital output accelerometer (Amazing !!!)
  6.   MP45DT02, ST MEMS audio sensor, omni-directional digital microphone
  7. CS43L22, audio DAC with integrated class D speaker driver
  8. Eight LEDs:LD1 (red/green) for USB communicationLD2 (red) for 3.3 V power onFour user LEDs, LD3 (orange), LD4 (green), LD5 (red) and LD6 (blue)2 USB OTG LEDs LD7 (green) VBus and LD8 (red) over-current
  9. Two push buttons (user and reset)
  10.  USB OTG FS with micro-AB connector (amazing for a microcontroller) 
  11. Extension header for all LQFP100 I/Os for quick connection to prototyping board and easy probing


This board can be used in many application with its DSP it can solve complex Digital signal functions used in filter computing plus it has the FPU (Floating point unit) that allows the STM32F4 to deal with floats up to 10exp-18 precision and that would be very useful in application that needs media processing or precision calculations.

I will post some tutorials talking about my experience with board.

Cheers ;)










dimanche 17 février 2013

Thoughts about my country

So what's wrong with Tunisia!! with such great talents spread all over the world with such an honorable history with such immense agriculture potential Tunisia falls in its greatness.
So many talents so many good minds so many charismatic people and so many diversity but it seems that all of this positive line qualities Tunisia doesn't seems to get use of it, but instead this diversity this democracy is just dividing us more and more.
About 12 million person are living now in Tunisia, but even with this relatively small number of population we have more than 200 categories of Tunisian people, salfi, nahthawi, cpr, pdp, joumhouri, masar, ili m3a te2sisii willi mouch m3ehom welli me3inouch bech itabe3,.....
It seems that our diversity is playing against us. It seems that our qualities is our enemies.
It seems that democracy is not the solution.

vendredi 8 février 2013

DC power supply


The need of static power supply

The need of power voltage supply is mandatory in any mechatronic application. For mobile mechatronic applications like robots, the power voltage supplier must be embedded with the application this can deliver many drawback like for example autonomy.
But when dealing with static mechatronic projects we can just get rid of the battery and use directly the domestic AC power supplier. because this supplier can deliver energy 24/7 with one drawback that this energy must be calibrated to be used in static mechatronic application witch are usually need a DC supply voltage.
In this article I'm going to show you how to create you own DC power supply.

Specifications 

INPUT : 220 V AC
OUTPUT: 5V DC / 1.5A

Principal functions:

 Our board change the 220V AC into 5V DC with a maximum operating current of 1.5A

Current protection : 


we need to protect the board from unpredictable current variation from the power supply or potential shunt that could be very dangerous and can harm our system. The obvious solution is to use a fuse.
The maximum needed current for mini mechatronic applications is 1A with a tolerance of 500mA so any current that exceeds 1.5A would be considered as dangerous to the board and the fuse must interfere. The choice is then a fuse with a normal operating current = 1.5A.


Galvanic isolation and Voltage decreasing

This part of the board is mainly responsible for decreasing the alternative voltage from 220V to 12V alternative current.
For this purpose we need a 220v 12V transformer that can assure both of the functions

Wave rectification


This part of the board is probably the most important, In fact in this part the alternative energy is transformed to a direct one. we need to eliminate the negative part of the wave or replace it with a positive wave.
For this purpose we need a full wave rectifier composed by 4 1N4001 Diodes:

Filtering and smoothing

The rectified wave still need to be more smoothed so I have to fill the gaps between the waves. To do so we need to add a capacitor that able to store voltage and release it in time to fill the gaps.
The specifications needed for our board are :
Vin :regulator input = 12V / Operating current = 1A
C = I * detlta(T)/delta(V) = 3333µF we take it as 4700µf or 2200µF

Voltage regulation

At this step we have a decent 12V DC but it's not really stable and we need to get the 5V DC To feed the electronics. For this purpose we have to add a 5V voltage regulator that can transform 12V to a stable 5v DC. The perfect choice is LM7805 witch is a 5V DC/DC regulator that is capable of supporting up to 1A witch is acceptable for the usual applications. It's recommended to add another filtering capacitor of 10mF in the output of the regulator to assure stabilization.


Integration of the hole design with Altium designer: