Overview
STM8S903K3 is an 8-bit microcontroller produced by STMicroelectronics, part of the STM8S series. This series of microcontrollers is popular in the market due to its high performance, robustness, and cost-effectiveness. The STM8S903K3 is especially suitable for a variety of industrial control and home appliance applications because it helps reduce system costs and shorten application development cycles.
Key Features:
- High Performance: Based on the next-generation STM8 core, it offers advanced processing capabilities.
- Low Power Consumption: Features multiple low-power modes, including Wait, Active Stop, and Stop modes, as well as the ability to individually disable peripheral clocks.
- High Integration: Integrates a true data EEPROM with up to 300k write/erase cycles, an internal clock oscillator, a watchdog timer, and power-on reset functionality.
- Clock Security System: Includes a clock monitor to ensure stable operation of the system.
- Interrupt Management: Equipped with a nested interrupt controller, supporting up to 32 interrupts and 28 external interrupts.
- Timers: Includes advanced control timers, general-purpose timers, and auto-wake-up timers.
- Communication Interfaces: Supports various communication protocols such as I2C, IrDA, LIN bus, SPI, UART/USART.
- Operating Voltage: 2.95 to 5.5 V, providing flexibility for different power environments.
- Temperature Range: Operating temperature range from -40°C to 85°C, making it suitable for a wide range of environmental conditions.
Pinout:
Block Diagram:
Application Examples:
Due to its high performance and low cost, the STM8S903K3 is widely used across various fields. For example, in electric vehicle controller designs, it serves as the main control chip for driving sensorless brushless DC motors, thereby reducing motor costs and enhancing the stability of the controller.
Programming STM8S903K3 with STVD and STVP
In this example, we will introduce the STM8S903K3 UART programming rutorial, based on ST Visual Develop and ST Visual Programmer, from environment configuration and project creation to compilation and testing.
Requried Tools
Hardware:
- STM8S903K3 Microcontroller
- ST-Link Debugger/Programmer
- Target Board (Optional)
- UART Serial Communication Interface (Optional)
Software:
- ST Visual Develop (STVD) (IDE tool for writing, compiling, and debugging)
- ST Visual Programmer (STVP) (programming tool)
- ST-Link USB Driver
- STM8 Cosmic Compiler Toolchain (build the project & generate the HEX file)
- Terminal Emulator Software (Optional)
Step-by-Step Process
1. Create New Workspace on ST Visual Develop
Open ST Visual Develop and create a new workspace. This will be the container for your project files and configurations.
2. Create New Workspace and Project
Inside the workspace, create a new project. This will serve as the main development environment where you will write and compile your code.
3.Setting Workspace Filename and Location
Specify the filename and location where the workspace will be saved. Choose an appropriate directory on your computer to store the project files.
4.New Project Toolchain STM8 Cosmic
Select the STM8 Cosmic toolchain for the project. The Cosmic toolchain is used to compile the code and generate the executable file for the STM8 microcontroller.
5. Select MCU STM8S903K3
In the project settings, choose the STM8S903K3 microcontroller (MCU) as the target device. This ensures that the project is configured correctly for the specific hardware you’re working with.
6. Adding UART Code to the main.c File
In the main.c file of your project, add the below UART code to enable serial communication. This includes configuring the UART settings and functions for transmitting and receiving data.
#include "stm8s903k3.h"
#include "stdio.h"
#define _COSMIC_
/*
Since different compilers (RAISONANCE, COSMIC, IAR) have slightly different parameter and return value conventions for putchar and getchar,
we use macros to define them accordingly. These macros are automatically added by the compiler.
*/
#ifdef _RAISONANCE_
#define PUTCHAR_PROTOTYPE int putchar (char c)
#define GETCHAR_PROTOTYPE int getchar (void)
#elif defined (_COSMIC_)
#define PUTCHAR_PROTOTYPE char putchar (char c)
#define GETCHAR_PROTOTYPE char getchar (void)
#else /* _IAR_ */
#define PUTCHAR_PROTOTYPE int putchar (int c)
#define GETCHAR_PROTOTYPE int getchar (void)
#endif /* _RAISONANCE_ */
// Function to send a character to UART1
PUTCHAR_PROTOTYPE
{
UART1_DR = c; // Send a character 'c' to UART1 data register
while (!(UART1_SR & 0x40)); // Wait for transmission to complete (TXE flag)
return (c);
}
// Function to receive a character from UART1
GETCHAR_PROTOTYPE
{
#ifdef _COSMIC_
char c = 0;
#else
int c = 0;
#endif
// Wait for new data to be received (RXNE flag)
while (!(UART1_SR & 0x20));
// Read received data
c = UART1_DR;
return (c);
}
// Clock initialization function
void CLK_Init(void)
{
// Enable the internal high-speed oscillator (HSI)
CLK_ICKR |= 0x01;
CLK_CKDIVR = 0x00; // No clock division (16MHz)
while (!(CLK_ICKR & 0x02)); // Wait for HSI to be ready
CLK_SWR = 0xE1; // Use HSI as the system clock source
}
// UART1 initialization function
void UART1_Init(void)
{
UART1_CR3 = 0x00; // Disable LIN mode, one stop bit, no SCK
UART1_CR2 = 0x00; // Disable TX interrupt, RX interrupt, and others
UART1_CR3 = 0x00; // Disable interrupts, no break frames
UART1_BRR2 = 0x02; // Set baud rate (9600)
UART1_BRR1 = 0x68; // Set baud rate (9600)
UART1_CR2 = 0x0C; // Enable TX and RX
}
// Simple delay function (not very accurate, could be improved with a timer)
void delay(unsigned int x)
{
unsigned int i, j;
for (i = x; i > 0; i--)
for (j = 300; j > 0; j--);
}
void main()
{
unsigned char c;
CLK_Init(); // Initialize system clock
UART1_Init(); // Initialize UART1
while(1)
{
printf("\n\rPlease enter a key: ");
while (!(UART1_SR & 0x20)); // Wait for data to be received (RXNE flag)
c = UART1_DR; // Read received character
printf("\n\rYou entered: %c.", c); // Send the received character back to the user
}
}
After adding the main.c code, we can add the stm8s903k3.h header file in the project’s “Include Files” folder. This file contains the necessary definitions and configurations for the STM8S903K3 MCU, allowing the compiler to recognize the device-specific registers and functions.
7. Build the Project
Once the code is written, build the project to compile the source code into machine code that the STM8S903K3 can understand. This step will check for errors and generate an output file (typically a .hex file).
8. Debug Target Swim ST-Link
Set up the debugging environment by selecting the ST-Link debugger for communication with the target MCU. The ST-Link is a USB-based programmer/debugger used to load code onto the STM8 microcontroller and debug it.
9. Edit Project Settings Commands in Post-Build
In the project settings, edit the post-build commands to automate any necessary steps that should occur after the build, such as copying the output file or performing additional processing.
10. Save Text File and Build the Project Again
Save the project and any text files containing configurations or build commands. After saving, rebuild the project to ensure all settings are properly applied and that the output file is generated.
11. Configure STM8S903K3 ST-LINK for ST Visual Programmer STM8
Set up the ST-Link with the STM8S903K3 microcontroller for programming. The ST-Link should be connected to both the computer and the STM8S903K3 to facilitate programming and debugging.
12. Programming Via ST Visual Programmer
Use the ST Visual Programmer software to open the uart.HEX
file generated by the build process. This file contains the compiled code for your STM8S903K3 microcontroller. Select the appropriate programming options in the ST Visual Programmer and program the microcontroller with the generated .hex
file.