💾
EC
  • Introduction
  • EC Course
    • Syllabus
    • Preparation for EC
    • Tutorial
      • Tutorial: arduino-stm32
        • Tutorial: arduino-stm32 Installation
        • Tutorial: arduino-stm32 Part1
        • Tutorial: arduino-stm32 Part2
      • Tutorial: MDK uVision
        • Tutorial: Installing MDK uVision
        • Tutorial: Create a Project with uVision
        • Tutorial: Adding library header in uVision
        • Tutorial: Re-using Project Configuration
        • Debugging in uVision
      • Tutorial: PlatformIO in CLion
      • Tutorial: PlatformIO in VSCode
      • Tutorial: Repository Management
      • Tutorial: Managing library header files
      • Tutorial: PinName Configuration
      • Tutorial: Bitwise Macro
      • Tutorial: Custom initialization
      • Tutorial: Documentation
      • Tutorial: Creating Application API
      • Tutorial: 7-Segment Display
      • Tutorial: DC motor driver connection
      • Tutorial: USART with TeraTerm
      • Tutorial: Finite State Machine programming
      • Tutorial: Bluetooth
      • Tutorial: Zigbee with Nucleo board
    • LAB
      • LAB Report Template
      • LAB: Smart mini-fan with STM32-duino
      • LAB: Portable Fan with mbed
      • LAB: GPIO Digital InOut
      • LAB: GPIO Digital InOut 7-segment
      • LAB: EXTI & SysTick
      • LAB: Timer & PWM
      • LAB: Stepper Motor
      • LAB: Input Capture - Ultrasonic
      • LAB: USART - LED, Bluetooth
      • LAB: ADC - IR reflective sensor
      • LAB: Line Tracing RC Car
    • Sample code
      • Code Templates
    • Hardware
      • Nucleo-F411RE
      • LAB Hardware
        • Electronic Chips
        • HUINS Embedded Kit
    • Projects
      • Line Tracing Car Project
      • Design Project
        • Past Projects
      • Project Grading Criteria
    • Study Resource for MCU
      • Hexa-Decimal Table
      • Bitwise Op for Register
      • System Clock
      • Timer
      • USART
      • ADC
  • STM32 M4 Programming
    • Documentation
      • C++ API Docs
    • Firmware Library
      • PinName Configuration
      • GPIO Digital
      • RCC
      • EXTI_SysTick
      • TIMER
      • USART
    • Troubleshooting
    • mbed for STM32
      • Tutorial: mbed-Part 1
      • Tutorial: mbed - Part 2
      • Tutorial: mbed - Part 3
      • Using mbed API on uVision
    • mbed OS
  • Other Programming
    • Arduino
    • Socket Programming
      • Window Socket Programming
      • Arduino WiFi
    • Cube-MX
    • Github
    • Markdown
      • Example: API documentation
    • MATLAB
  • C Programming
    • C-Programming Lessons
      • Installing Visual Studio Community
        • Visual Studio Community 2022
      • Installing VS Code(Mac/Linux)
      • Creating Header Lib
      • Pointer
      • Array
      • 2D Array
      • Structure
      • Dynamic Alloc
      • Bitwise Operation
  • Numerical Programming
    • Syllabus
    • Preparation for NP
    • Math Review
    • Tutorial
      • TA Session Video
      • Tutorial: NP Library Header Files
      • Tutorial - Sine Taylor
      • Tutorial: Passing a Function, Function callback
      • Tutorial: Nonlinear solver
      • Tutorial: Differentiation
      • Tutorial: Integration
      • Tutorial: Matrix Structure
      • Tutorial: Eigenvalue problem
      • Tutorial: ODE-IVP
      • Tutorial: Curve Fitting
      • Tutorial: Create Github Repos of NP lib
      • Tutorial: Version Control in Github
      • Tutorial: Documentation with Markdown
      • Exercise: Version Control and Documentation
    • Example: MATLAB
    • Example: NP Library
    • Assignment
      • Assignment Factorial and Power
      • Assignment: Version Control and Documentation
    • Problem Bank
Powered by GitBook
On this page
  • Introduction
  • Requirement
  • Problem 0: STM-Arduino
  • Procedure
  • Problem 1: EC HAL library
  • Using HAL library
  • Example Code
  • Problem 2: Communicate MCU1-MCU2 using RS-232
  • Procedure
  • Configuration
  • Problem 3: Control DC Motor via Bluetooth
  • Bluetooth
  • DC Motor Driver
  • Procedure
  • Configuration
  • Reference
  • Troubleshooting
  • 1. Cannot find my bluetooth module in my PC

Was this helpful?

  1. EC Course
  2. LAB

LAB: USART - LED, Bluetooth

PreviousLAB: Input Capture - UltrasonicNextLAB: ADC - IR reflective sensor

Last updated 6 months ago

Was this helpful?

Date: 2023-09-26

Author/Partner:

Github: repository link

Demo Video: Youtube link

PDF version:

Introduction

In this lab, we will learn how to configure and use ‘USART(Universal synchronous asynchronous receiver transmitter)’ of MCU. Then, we will learn how to communicate between your PC and MCU and MCU to another MCU with wired serial communication.

  • Mission 1: Control LED(LD2) of each other MCU.

  • Mission 2: Run DC motors with Bluetooth

You must submit

  • LAB Report (*.md & *.pdf)

  • Zip source files(main*.c, ecRCC.h, ecGPIO.h, ecSysTick.c etc...).

    • Only the source files. Do not submit project files

Requirement

Hardware

  • MCU

    • NUCLEO-F411RE

  • Actuator/Sensor/Others:

    • DC motor, DC motor driver(L9110s),

    • Bluetooth Module(HC-06),

Software

  • Keil uVision, CMSIS, EC_HAL library

Problem 0: STM-Arduino

Procedure

  1. Create a new project under the directory \EC\LAB\LAB_USART_LED

  2. Follow the tutorial: U(S)ART (Universal Asynchronous/synchronous Receiver and Transmitter)

Problem 1: EC HAL library

Using HAL library

Change the file names as

  • ecUART2.c

  • ecUART2.h

Fill in empty spaces in the code.

You must update your header files located in the directory EC\include\.

ecUSART2.h

// Configuration UART 1, 2 using default pins
void UART1_init(void);
void UART2_init(void);	
void UART1_baud(uint32_t baud);
void UART2_baud(uint32_t baud);

// USART write & read
void USART1_write(uint8_t* buffer, uint32_t nBytes);
void USART2_write(uint8_t* buffer, uint32_t nBytes);
uint8_t USART1_read(void);										
uint8_t USART2_read(void);	

// RX Interrupt Flag USART1,2
uint32_t is_USART1_RXNE(void);
uint32_t is_USART2_RXNE(void);


Example Code

Example 1

#include "ecSTM32F4v2.h"
// #include "ecUART.h" 



static volatile uint8_t PC_Data = 0;
static volatile uint8_t BT_Data = 0;
uint8_t PC_string[]="Loop:\r\n";

void setup(void){
	RCC_PLL_init();
	SysTick_init();
	
	// USART2: USB serial init
	UART2_init();
	UART2_baud(BAUD_9600);

	// USART1: BT serial init 
	UART1_init();
	UART1_baud(BAUD_9600);
}

int main(void){	
	setup();
	printf("MCU Initialized\r\n");	
	while(1){
		// USART Receive: Use Interrupt only
		// USART Transmit:  Interrupt or Polling
		USART2_write(PC_string, 7);
		delay_ms(2000);        
	}
}

void USART2_IRQHandler(){          		// USART2 RX Interrupt : Recommended
	if(is_USART2_RXNE()){
		PC_Data = USART2_read();		// RX from UART2 (PC)
		USART2_write(&PC_Data,1);		// TX to USART2	 (PC)	 Echo of keyboard typing		
	}
}


void USART1_IRQHandler(){          		// USART2 RX Interrupt : Recommended
	if(is_USART1_RXNE()){
		BT_Data = USART1_read();		// RX from UART1 (BT)		
		printf("RX: %c \r\n",BT_Data); // TX to USART2(PC)
	}
}

Example 2

#include "ecSTM32F4v2.h"
// #include "ecUART.h" 

#define MAX_BUF 	10
#define END_CHAR 	13

static volatile uint8_t buffer[MAX_BUF]={0, };
static volatile uint8_t PC_string[MAX_BUF]={0, };
static volatile uint8_t PC_data = 0;

static volatile int idx = 0;
static volatile int bReceive =0;

void setup(void){
	RCC_PLL_init();
	SysTick_init();
	
	// USART2: USB serial init
	UART2_init();
	UART2_baud(BAUD_9600);

	// USART1: BT serial init 
	UART1_init();
	UART1_baud(BAUD_9600);
}

int main(void){	
	setup();
	printf("MCU Initialized\r\n");	
	
	while(1){
		if (bReceive == 1){
			printf("PC_string: %s\r\n", PC_string);				
			bReceive = 0;
		}
	}
}

void USART2_IRQHandler(){          		// USART2 RX Interrupt : Recommended
	if(is_USART2_RXNE()){
		PC_data = USART2_read();		// RX from UART2 (PC)
		USART2_write(&PC_data,1);		// TX to USART2	 (PC)	 Echo of keyboard typing
		
		// Creates a String from serial character receive				
		if(PC_data != END_CHAR && (idx < MAX_BUF)){	
			buffer[idx] = PC_data;
			idx++;
		}
		else if (PC_data== END_CHAR) {						
			bReceive = 1;
			// reset PC_string;
			memset(PC_string, 0, sizeof(char) * MAX_BUF);
			// copy to PC_string;
			memcpy(PC_string, buffer, sizeof(char) * idx);
			// reset buffer
			memset(buffer, 0, sizeof(char) * MAX_BUF);	
			idx = 0;
		}
		else{				//  if(idx >= MAX_BUF)			
			idx = 0;							
			// reset PC_string;
			memset(PC_string, 0, sizeof(char) * MAX_BUF);
			// reset buffer
			memset(buffer, 0, sizeof(char) * MAX_BUF);	// reset buffer
			printf("ERROR : Too long string\r\n");
		}
	}
}

General USART Setup

// General Setting
void setup(){
  RCC_PLL_init();
  
  // BT serial : specific RX/TX pins 
  USART_setting(USART1, PA_9,PA_10, BAUD_9600); 	// PA9 - RXD , PA10 - TXD
}

Problem 2: Communicate MCU1-MCU2 using RS-232

Procedure

1. Create a new project under the directory \repos\EC\LAB\LAB_USART_LED

  • The project name is “LAB_USART_LED”.

  • Create a new source files named as “LAB_USART_LED.c”

You MUST write your name on the source file inside the comment section.

2. Include your updated library in \repos\EC\include\ or \repos\EC\lib\ to your project.

  • ecUART2.h, ecUART2.c

  • Update ecSTM32F4v2.h

3. Connect each MCUs to each PC with USART 2 via USB cable (ST-Link)

  • MCU1 to PC1

  • MCU2 to PC2

4. Connect MCU1 to MCU2 with USART 1

  • connect RX/TX pins externally with jumper wires as

    • MCU1_TX to MCU2_RXD

    • MCU1_RX to MCU2_TX

    Connect the same GND for MCU1-MCU2

MUST connect the same GND pin for MCU1 and MCU2

  1. Send a message from PC_1 by typing keys on Teraterm. It should send that message from MCU_1 to MCU_2.

    Note that you have to press "Enter" to end the message.

  2. The received message by MCU_2 should be displayed on PC_2.

7. Turn other MCU's LED(LD2) On/OFF by sending text:

  • "L" for Turn OFF

  • "H" for Turn ON

Configuration

Type
Port - Pin
Configuration

System Clock

PLL 84MHz

USART2 : USB cable (ST-Link)

No Parity, 8-bit Data, 1-bit Stop bit, 38400 baud-rate

USART1 : MCU1 - MCU2

TXD: PA9 RXD: PA10

No Parity, 8-bit Data, 1-bit Stop bit, 38400 baud-rate

Digital Out: LD2

PA5

Code

Explain your source code with necessary comments.

// YOUR MAIN CODE ONLY
// YOUR CODE

Result

Experiment images and results

Show experiment images /results

Problem 3: Control DC Motor via Bluetooth

Bluetooth

Search for the bluetooth module specification sheet (HC-06) and study the pin configurations. The default PIN number is 1234.

Example of connecting to USART1

DC Motor Driver

Connect DC motor driver(L9110s) module pins to MCU as shown below.

DO NOT use MCU’s VCC to motor driver. You should use external voltage source.

  • A- IA: PWM pin (0~100% duty) for Motor A

  • A- IB: Direction Pin (Digital Out H or L) for Motor B

Procedure

1. Create a new project under the directory `\repos\EC\LAB\LAB_USART_Bluetooth

  • The project name is “LAB_USART_Bluetooth”.

  • Create a new source files named as “LAB_USART_Bluetooth.c”

You MUST write your name on the source file inside the comment section.

2. Include your updated library in \repos\EC\lib\ to your project.

  • ecGPIO.h, ecGPIO.c

  • ecRCC.h, ecRCC.c

  • ecUART.h, ecUART.c

  • ecTIM.h, ecTIM.c

3. Connect the MCU to PC via Bluetooth. Use USART 1

  • connect RX/TX pins as

    • MCU TXD - BLUE RXD

    • MCU RXD - BLUE TXD

  1. Check the Bluetooth connection by turning MCU's LED(LD2) On/OFF by sending text of "L0" or "L1" from PC.

  2. Run 2 DC motors(Left-wheel, Right-wheel) to steer.

    • Turn Left: MotorA / MotorB = (50 / 80%) duty

    • Turn Right: MotorA / MotorB = (80 / 50%) duty

    • Go straight: MotorA / MotorB = (80 / 80 %) duty

    • STOP: MotorA / MotorB = (0 / 0 %) duty

    You may use the key inputs as your preference for Left, Right, Straight.

    - Ex) ‘L’, ‘R’, 'U' 'S'

Configuration

Type
Port - Pin
Configuration

System Clock

PLL 84MHz

USART1 : MCU -Bluetooth

TXD: PA9 RXD: PA10

No Parity, 8-bit Data, 1-bit Stop bit, 9600 baud-rate

Digital Out: LD2

PA5

PWM (Motor A)

TIM2-Ch1

PWM period (2kHz~10kHz)

PWM (Motor B)

TIM2-Ch2

Code

Explain your source code with necessary comments.

// YOUR MAIN CODE ONLY
// YOUR CODE

Result

Experiment images and results

Show experiment images /results

Reference

Complete list of all references used (github, blog, paper, etc)

Troubleshooting

1. Cannot find my bluetooth module in my PC

Change your BT searching setting in your Window

Download

Your code goes here:

Add

image
image
image

Your code goes here:

Add

image
ecUART2_student.c ecUART2_student.h
ADD Code LINK such as github
demo video link
ADD Code LINK such as github
demo video link
Tutorial: arduino-stm32 Part2 | EC
Logo