Posted on Leave a comment

General Timer based on RTC using stm32f103rb

This timer uses stm32 internal rtc peripheral to display time.

The initialization code is generated using CUBEMX which is embedded inside the CUBE IDE.

/* USER CODE BEGIN WHILE */
	
	RTC_TimeTypeDef readTime;	// RTC Time structure
	RTC_DateTypeDef readDate;	// RTC Date structure
	uint8_t time_hou_var[2];
	uint8_t time_min_var[2];
	uint8_t time_sec_var[2];

	while (1)
	{


    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
		/*  function to read time from RTC shadow register */
		    HAL_RTC_GetTime(&hrtc, &readTime, RTC_FORMAT_BIN);

		/* function to read date from RTC shadow register	*/
		    HAL_RTC_GetDate(&hrtc, &readDate, RTC_FORMAT_BIN);

		    itoa(readTime.Hours,(char *)time_hou_var,10);
		    textPtr = ((uint8_t *)(time_hou_var));
		    LCD_Fill(0, 40, 240, (40 + 1)+30, WHITE);
		    WriteString(10,(40*1)+10,textPtr,RED);

		    textPtr = ((uint8_t *)":");
		    WriteString(40,(40*1)+10,textPtr,GREEN);

		    itoa(readTime.Minutes,(char *)time_min_var,10);
		    textPtr = ((uint8_t *)(time_min_var));
		  //  LCD_Fill(0, 40, 240, (40 + 1)+30, WHITE);
		    WriteString(45,(40*1)+10,textPtr,RED);

		    textPtr = ((uint8_t *)":");
		    WriteString(70,(40*1)+10,textPtr,GREEN);

		    itoa(readTime.Seconds,(char *)time_sec_var,10);
		    textPtr = ((uint8_t *)(time_sec_var));
		    //   LCD_Fill(0, 40, 240, (40 + 1)+30, WHITE);
		    WriteString(75,(40*1)+10,textPtr,RED);

		    HAL_Delay(1000);

		    if(HAL_GPIO_ReadPin (GPIOA, KEY1_Pin))
		    {
		    	// Set The LED ON!
		    	HAL_GPIO_WritePin(GPIOA, LED1_Pin, GPIO_PIN_SET);

		    }
		    else
		    {
		    	// Else .. Turn LED OFF!
		    	HAL_GPIO_WritePin(GPIOA, LED1_Pin, GPIO_PIN_RESET);
		    	//HAL_GPIO_WritePin(LCD_BL_EN_GPIO_Port, LCD_BL_EN_Pin, GPIO_PIN_RESET);
		    	HAL_GPIO_TogglePin(LCD_BL_EN_GPIO_Port, LCD_BL_EN_Pin);
		    }
}

The code uses itoa() function which needs stdlib.h header file.

itoa() function in C language converts the integer into ASCII digits which are stored in a buffer.

itoa( integar_to_be_converted, Buffer_to_store_conversion, Base_system);

You can choose the base system in itoa function. For conversion to decimal number system, you enter 10

for binary, you can write 2.

for hexadecimal, you can write 16.

and so on.

Posted on 3 Comments

Mini STM32 V3.0

This development board has an STM32F103RB microcontroller. In this development board,

  • 8 Mhz Crystal
  • 32.768 Khz Crystal
  • 2.8 inch TFT-LCD Touch screen.
    The screen is a TFT-LCD Panel driven by the ili9325 driver.
  • PL2303 USB to Serial IC
  • User Programmable USB port
  • battery holder
  • Potentiometer
  • JTAG Port for debug and programming
  • 2 User programmable LED (both Red Colour)
  • 2 User configurable Push button
  • Boot 0 Button
  • Reset Button
  • AMS117 3.3 V LDO

Mini STM32 Schematic Diagram

2.8 inch TFT LCD Schematic

You can develop a program for this board using Keil, IAR or STM32 Cube IDE

You can program the microcontroller by holding the BOOT0 button and then while holding the boot button pressing the RESET button.
Which enable you to program the microcontroller using UART.

You will need STM32 Cube Programmer to upload hex files into the microcontroller

You can also program the Board using the JTAG port.

You can use the below application note for that.

How to use the ST-LINK/V2-1 in STM32L476G-DISCO Board to program the STM32 on an external application board

How To / Guides

Posted on Leave a comment

How to redirect printf() to USART in STM32f103RB using STM32Cube IDE

Printf() function can be redirected to USART and also towards SWO.

Here you will see how to redirect printf() to USART in STM32f103RB

You need to rewrite this code in your main.c file

/*
* Function Name: _write
* Function Description: Redirect the printf() statement towards the UART using the HAL_UART_Transmit
Author: Abhay

*/
int _write(int fd, char* ptr, int len) {
    HAL_UART_Transmit(&huart1, (uint8_t *) ptr, len, HAL_MAX_DELAY);
    return len;
}

If you have created your project using STM32CubeMX or STM32 Cube IDE, then you can rewrite it in between USER CODE BEGIN 0 as shown

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
int _write(int fd, char* ptr, int len) {
    HAL_UART_Transmit(&huart1, (uint8_t *) ptr, len, HAL_MAX_DELAY);
    return len;
}
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */

  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */

  }
  /* USER CODE END 3 */
}

After adding the code, connect the uart to your laptop or computer using serial to USB adapter and open serial Terminal application like YAT.
Then you will be able to see printf message over there.

Posted on Leave a comment

How to use the ST-LINK/V2-1 in STM32L476G-DISCO Board to program the STM32 on an external application board

Here is the video showing all the steps.

ST-Link V2-1 on STM32L476G-Disco Board

To use the ST-LINK/V2-1 to program the STM32 on an external application board (out of the STM32L476VGT6 onboard), remove the two jumpers from CN3 as shown in the above figure in red, and connect the board to the CN4 software debug connector according to Table.
Make sure the jumpers JP6.3V3, and JP5.OFF are set.
JP3, must be ON if CN4 pin 5 (NRST) is used in the external application board.

PinCN4 FunctionJTAG PIN NumberJTAG NameNote
1VappVDD from ApplicationDo not Connect Vapp to JTAG.
2SWCLKSWD Clock9TCK
3GNDGround1 to 9GND
4SWDIOSWD data input/output7TMS
5NRSTRESET of target MCU15nSRST
6SWORESERVED

Note: Do not connect Vapp to JTAG on the external board unless you know about the power domain of the external board.
Power the board separately.

You can use STM32CubeProgrammer to read the memory and also write the hex file into the microcontroller.

Here I am using mini stm32 v3.0 as an example.

The mini stm32 v3.0 has an STM32F103RB microcontroller along with a JTAG interface for programming and debugging.