STM32 interrupts and programming with GCC

Probably one of the key features of any microcontroller is the interrupt system. ARM Cortex-M3 microcontrollers may have up to 256 interrupted sources. The first 15 interrupt sources are called system exceptions. These exceptions arise within Cortex core like reset, NMI, hard fault and error, debug, and SystTick timer interrupt. In the exception table, they start from address 0x00000004 and are numbered from 1 to 15. There is no 0 number exception (FYI – the very top of exception table address is used to store the starting point of stack pointer): Each exception vector holds the four-byte address of the service routine that is called when an exception occurs. Exception table usually is located in startup code like this:

Continue reading

Programming STM32F10x I/O port pins

Previously we learned how to compile STM32VL Discovery projects that were included in the package. But to understand how to write our own programs, we need to get to some basics. I think the best place to start is the input and output system (I/O). Before we begin to write some code, let’s go through what’s inside STM32 ports. If you look into the STM32 reference manual, you’ll find that the I/O system is pretty flexible. Port pins can work in several modes: Input floating; Input pull-up; Input pull-down; Analog; Output open drain; Output push-pull; Alternate function push-pull; Alternate function open drain. Pins are organized as 16-bit ports that have their names like PORTA, PORTB, PORC, PORTD… Ports are 16-bit wide; they are controlled with 32-bit words. Individually each port pin can be configured to one of these functions. Additionally, each pin’s maximum speed can be set to one of the values: 2MHz, 10MHz, and 50MHz. STM32 I/Os are 5V tolerant. Anyway, the proper design should use 5 to 3.3V level converters.

Continue reading

Serial peripheral interface in AVR microcontrollers

Serial Peripheral Interface (SPI) is the fastest synchronous communication interface allowing data transfer speeds up to half of the core clock. If the AVR microcontroller is clocked at 16MHz, then the SPI clock may reach 8MHz in master mode. SPI communication interface is a standard way to talk to other peripherals around MCU like a flash, EEPROM, sensors, and even other microcontrollers. Generally speaking, devices communicate over the SPI interface using four wires MISO (Master In Slave Out), MOSI (Master Out Slave In), SCK (synchronization clock), and SS (Slave Select). Usually, if only one slave device is used, the SS line is omitted while the slave chip select pin is connected to the GND. However, this is a particular case. In all other cases SS pin has to be controlled manually in software – this isn’t handled automatically. If more slaves are connected to the SPI interface, there are options in selecting the suitable slave device: one is to use dedicated SS pins for each slave, or if the slave supports this, use the address byte in data packets to…

Continue reading

ST32MVLDiscovery project template for GCC

In this tutorial, we will set up a simple template for programming ST32 -Discovery board. For this, we will use the latest Code Sourcery and Eclipse IDE. To make things simpler, we will use ARM-based 32-bit MCU STM32F10x Standard Peripheral Library v3.5.0, that can be downloaded from ST site. Also, we will use STM32VLDiscovery firmware package for example, files. And why write our linker, startup, and make scripts. For this, we will use Michael Fischer’s example project (STM32Test.zip) for yagarto. We only need slight modifications to fit our needs. In this stage, we assume that you have set up Eclipse and Code Sourcery, and we can go further. First of all, create new C project in Eclipse File->New: Enter the project name, select the path where the project will be stored, and select Makefile project->Empty project in the Project type list. In Toolchain, list select Other Toolchain. This will create an empty project that will run make to compile the project.

Continue reading

ADC on Atmega328. Part 2

After we’ve learned how to perform simple ADC conversions on AVR microcontroller we can move forward with more complex tasks. AVR ADC module has many valuable features that make conversions more robust without occupying MCU resources. Imagine that we need to sample analog waveform or audio signal. It has to be done precisely at defined sampling frequency like 20kHz. The only way to do this correct is to use auto-triggering with exact time intervals. Why not pass counting task to a timer? Let’s write Timer0 auto-triggered ADC conversions with ADC complete interrupt service routine.

Continue reading

ADC on Atmega328. Part 1

Microcontrollers are meant to deal with digital information. They only understand ‘0’ and ‘1’ values. So what if we need to get some non-digital data into the microcontroller. The only way is to digitize or, to speak convert analog into digital. This is why almost all microcontrollers are featured with the ADC module. Among other electronic parts, the Atmega328 microcontroller also has 8 (or 6 in the PDIP package) ADC input channels. All these can be used to read an analog value that is within the reference voltage range. Let us see how this is easy.

Continue reading