💾
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

Was this helpful?

  1. EC Course
  2. Sample code

Code Templates

Example Code Structure for main.c

/**
******************************************************************************
* @course   	Embedded Controller- HGU
* @author	iiLAB
* @mod		2023-12-05 by YKKIM
* @brief	Example Code Structure
* 
******************************************************************************
*/

#include “ecSTM32F411.h”	// includes all necessary library

// Initialization
void setup()
{
	// RCC Configuration (PLL: 84MHz)
	RCC_PLL_init();
    
	// SysTick Configuration (delay_ms)
	SysTick_init();
    
	// GPIO Configuration
	GPIO_init(GPIOA, LED_PIN, OUTPUT);
	GPIO_otype(GPIOA, LED_PIN, EC_PUSH_PULL);
	GPIO_ospeed(GPIOA, LED_PIN, EC_HIGH);
	GPIO_write(GPIOA, LED_PIN, LOW);
    
	// External Interrupt Configuration
	GPIO_init(GPIOC, BUTTON_PIN, INPUT);
	GPIO_pupd(GPIOC, BUTTON_PIN, EC_PD);
	EXTI_init(GPIOC, BUTTON_PIN, FALL, 0);
    
	// Timer Configuration
	TIM_init(TIM2, 1);
    
	// Timer IRQ Interrupt Configuration
	TIM_UI_init(TIM4, 1);
    
	// PWM Configuration
	PWM_init(PA_1);
	PWM_period(PA_1, 1);
	PWM_duty(PA_1, 0.f);
    
	// Stepper Motor Configuration
	Stepper_init(GPIOB, 10, GPIOB, 4, GPIOB, 5, GPIOB, 3);
	Stepper_setSpeed(5);
    
	// Ultrasonic Configuration
	// Trigger: PWM
	PWM_init(PA_6);
	PWM_period_us(PA_6, 50000);
	PWM_pulsewidth_us(PA_6, 10);
    
	// ECHO: Input Capture Configurtion
	ICAP_init(PB_6);
	ICAP_counter_us(PB_6, 10);
	ICAP_setup(PB_6, 1, IC_RISE);
	ICAP_setup(PB_6, 2, IC_FALL);
    
	// ADC Configuration
	PinName_t seqCHn[2] = {PB_0, PB_1};
	ADC_init(PB_1);
	ADC_init(PB_0)
	ADC_trigger(seqCHn, 2);
    
	// Injected ADC Configuration
	PinName_t seqCHn[2] = {PB_0, PB_1};
	JADC_init(PB_1);
	JADC_init(PB_0);
	JADC_sequence(seqCHn, 2);
    
	// UART Configuration
	UART1_init();
	UART1_baud(BAUD_9600);
	UART2_init();
	UART2_baud(BAUD_38400);
}

// Main - Polling
void main()
{
	setup();
	while(1)
	{
	// polling logic goes here
	}
}


// For Interrupts,
// 1. Set the priority of each type of interrupt handler
// 2. Make sure interrupts is as short as possible. 
// 3. For a periodic interrupt, check the calculation time within the interrupt handler. It should not go beyond the interrupt period

void TIM4_IRQHanlder(void){   // TIM1~TIM8
	if(is_UIF(TIM4)){
		// Periodic tasks. Such as
		// Sensor read
		// motor controller out, etc 
		// make it as short as possible
        
		// clear UIF
		clear_UIF(TIM4);
	}
    
	if(is_CCIF(TIM4, 1)){
		// Capture value
        
		// clear CCIF
		clear_CCIF(TIM4, 1);
	}
}

void SysTick_Handler(void){
	// Periodic tasks. Such as time measurement etc. 
	// make it as short as possible
}

void EXTI15_10_IRQHandler(void){   // EXTI0~15
	if(is_pending_EXTI(BUTTON_PIN)){
		/* USER CODE BEGIN EXTI0_IRQn 0 */
		/* USER CODE END EXTI0_IRQn 0 */
		/* USER CODE BEGIN EXTI0_IRQn 1 */
		/* USER CODE END EXTI0_IRQn 1 */
		
		// Clear pending
		clear_pending_EXTI(BUTTON_PIN);
	}
}

void ADC_IRQHandler(void){
	// ADC Overflow flag
	if(is_ADC_OVR()){
		clear_ADC_OVR();
	}
    
	// ADC finishing sequence
	if(is_ADC_EOC()){
		// Periodic ADC acquisition. 
		// Configure sampling rate with the period of
		// triggering Timer
	}

	// JADC finishing sequence
	if(is_ADC_JEOC()){
		// Periodic JADC acquisition. 
		// Configure sampling rate with the period of
		// triggering Timer
        
		// clear ADC JEOC
		clear_ADC_JEOC();
	}
}

void USART1_IRQHandler(void){   // USART1~USART2
	if(is_USART_RXNE(USART1)){
		// USART interrupt call whenever data is received
	}
}

PreviousSample codeNextHardware

Last updated 8 months ago

Was this helpful?