0% found this document useful (0 votes)
25 views19 pages

Lab Report

Uploaded by

Toseeq Haider
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views19 pages

Lab Report

Uploaded by

Toseeq Haider
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

LAB TITLE:

STM32 TASKS WITH PROTEUS SIMULATION

SUBMITTED BY:
NS TOSEEQ HAIDER

SEMESTER:
5TH

SUBMITTED TO:
DO SIDRA AMIN

DATED:
28/10/2024
STM32:
INTRODUCTON:
STM32 is a family of 32-bit microcontroller integrated circuits by STMicroelectronics. It is a
popular family of microcontrollers that offer a wide range of performance, features, and
peripherals to cater to different application requirements. STM32 microcontrollers are
characterized by their high-performance computing capabilities and low power consumption.
They are used in a variety of applications, such as automotive, industrial, medical, and consumer
electronics.

SERIES:
The STM32 family consists of many series of microcontrollers: C0, F0, F1, F2, F3, F4, F7, G0,
G4, H5, H7, L0, L1, L4, L4+, L5, U0, U5, WBA, WB, WL. Each STM32 microcontroller series
is based upon a specific ARM-Cortex-M processor core.
Q-What is ARM-Cortex-M?
Ans= The ARM Cortex-M is a group of 32-bit RISC(reduced instruction set computers)
ARM processor cores licensed by ARM Limited. These cores are optimized for low-cost and
energy-efficient integrated circuits, which have been embedded in tens of billions of consumer
devices. Though they are most often the main component of microcontroller chips, sometimes
they are embedded inside other types of chips too.

stm32f103c8t6:
STM32F103C8T6 is part of the STM32F1 series of microcontrollers from
STMicroelectronics. The F1 series is known for its ARM Cortex-M3 core and is widely used in
various applications due to its balance of performance and power efficiency. The
STM32F103C8T6 is a popular variant in this series, often used in development boards like the
"Blue Pill."
APPLICATIONS:
STM32 is used in variety of applications including:

 Industrial control systems


 Human interface devices
 Smart metering
 Motor control
 Medical instruments
 Buildings and security (alarms, access control, power meters…)
 Consumer products (PC peripherals, GPS, gaming…)
 Less power using handheld devices
 Different types of robots
 Automation systems
PINOUT CONFIGURATION OF STM32 BLACK PILL :

TASK NO.1:
LED BLINKING SIMULATION IN PROTEUS:
PROJECT CREATION:
1-Open stm32cubeide
2-click on File
3-Then press new
4-Then STM32Project
5-Select the microcontroller
6-Give project name and finish it

PINS SETTING:
 Go to Pinout & Configuration
 Then go to RCC
 In mode of High Speed Clock select Crystal/Ceramic Resonator
 Select PA0 pin as GPIO_Output

CODE SNIPPET:
EXPLANATION:

We’ll explain the code line-by-line:

1. Infinite Loop (while (1)): This creates a loop that will run indefinitely. The code
inside this loop will continuously execute.
2. Set GPIO Pin Low (HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, 0)): This function
sets pin 0 on GPIO port A (GPIOA) to a low state (0). If an LED is connected to this pin, it
will turn off.
3. Delay (HAL_Delay(500)): This function introduces a pause for 500 milliseconds (or half
a second). During this time, the pin remains low.
4. Set GPIO Pin High (HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, 1)): This function
sets pin 0 on GPIOA to a high state (1). If an LED is connected to this pin, it will turn on.
5. Another Delay (HAL_Delay(500)): This introduces another pause for 500 milliseconds.
During this time, the pin remains high.

Q-What is GPIO?

Ans= At the most basic level, GPIO refers to a set of pins on your computer's mainboard or add-on card.
These pins can send or receive electrical signals, but they aren't designed for any specific purpose. This is
why they're called "general-purpose" IO. The GPIOs are shared across up to 5 ports GPIOA, GPIOB,
GPIOC, GPIOD, GPIOF each of them can host up to 16 I/O pins.

CIRCUIT DESIGN ON PROTEUS:

1-Open the proteus software

2-Create new project

3-Click on the P button


4-Write stm32f103c8t6 select it

5-Place microcontroller where you want to place

6-Give Power and ground to microcontroller naming as VDDA(power=+3.3V) and


VSSA(ground)

7-For power supply click then DC set +3.3V

8-Search for led-yellow

9-Connect one terminal of led to power supply of 3.3V and other to PA0 of stm32
SIMULATION:

TASK NO.2:
PWM TIMER WITH ADC USING POLLING AND DMA:
OVERVIEW:
In this task we’ve used ADC with Polling and DMA method for this purpose we’ve used two
GPIO pins GPIO Pin 0 and GPIO Pin 2 if we select GPIO Pin 0 we’ll get result in Polling
mode(Method in which CPU continuously checks (polls) a specific register to see if the ADC
conversion (or any other event) has completed) and by selecting GPIO Pin 2 we’ll get our result
in DMA Mode(The ADC peripheral sends data directly to memory (like ADC_VAL) without
involving the CPU in each data transfer).
PINS SETTING:
1-Go to Pinout & Configuration then select Analog then ADC1 and select IN7(7th input channel
of the ADC peripheral).
2-Go to Timers then select TIM2 select Internal Clock in Clock Source then select PWM
Generation CH2 from Channel2
3-Go to System Core then SYS and select SYsTick from Timebase Source

CODE SNIPPET:

EXPLANATION:
Infinite Loop:
This loop (while(1)) runs indefinitely, making the code inside repeat continuously.
If GPIO_PIN_0 is Set (Pressed):
 HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0): Checks if GPIO pin PB0 is pressed
(logic high).
 HAL_ADC_Start(&hadc1);: Starts a new ADC conversion for hadc1.
 HAL_ADC_PollForConversion(&hadc1, 1);: Waits until the ADC completes the
conversion (up to 1 ms).
 ADC_VAL = HAL_ADC_GetValue(&hadc1);: Reads the ADC result and stores it in
ADC_VAL.
 TIM2->CCR2 = (ADC_VAL << 4);: Shifts the ADC result left by 4 bits (multiplying
by 16) and uses it to set the PWM duty cycle of Timer 2, Channel 2.
 HAL_Delay(1);: Adds a 1 ms delay, slowing the loop slightly.
Else If GPIO_PIN_2 is Set (Pressed):
 HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2): Checks if GPIO pin PB2 is pressed
(logic high).
 HAL_ADC_Start_DMA(&hadc1, (uint32_t*)&ADC_VAL, 1);: Starts ADC
conversion in DMA (Direct Memory Access) mode, which transfers the result directly
into ADC_VAL without further CPU intervention. The 1 indicates that only one
conversion result is needed.
 HAL_Delay(1);: Adds a 1 ms delay for loop pacing.
Else (Neither GPIO_PIN_0 nor GPIO_PIN_2 is Pressed):
 HAL_ADC_Stop(&hadc1);: Stops any ongoing ADC conversion.
 HAL_ADC_Stop_DMA(&hadc1);: Stops any ongoing ADC-to-DMA transfers.

CALLBACK FUNCTION FOR DMA :


CODE SNIPPET:

EXPLANATION:
 This code is an interrupt callback function for when an ADC conversion completes
while using DMA mode on an STM32 microcontroller. It checks the state of a GPIO pin
(PB2), and if it’s set (pressed), it updates a PWM duty cycle based on the most recent
ADC result.
 hadc :The hadc parameter is a pointer to the ADC handle, which allows identifying
which ADC triggered the callback if there are multiple ADCs in use.
 If GPIO_PIN_2 is set(Pressed), the code inside the if block executes.
 ADC_VAL holds the most recent ADC conversion result, automatically updated by the
DMA.
 (ADC_VAL << 4): Shifts the ADC value left by 4 bits (essentially multiplying it by 16).
This scaling adjusts the ADC result to a higher range before setting it as the PWM duty
cycle.
 TIM2->CCR2: This register holds the compare value for Timer 2, Channel 2, which sets
the PWM duty cycle. Assigning the scaled ADC_VAL to TIM2->CCR2 updates the
PWM output duty cycle based on the ADC result.
CIRCUIT DESIGN ON PROTEUS:

APPARATUS:

In out task we’ve used the following apparatus:

1- Two LED’s(Yellow and Pink)


2- Three SPDT switches
3- One potentiometer(POT or Variable resistor of 10k)
4- One Virtual Terminal
5- One stm32f103c8t6

Q-What is SPDT?

Ans=SPDT switch is a single pole double throw switch that controls two dissimilar circuits with a single
input. It can switch between two circuits.

Q-What is Virtual Terminal?

Ans= In Proteus, a popular simulation software for electronics, the Virtual Terminal is a simulation tool
used to emulate a serial communication interface, such as a UART (Universal Asynchronous
Receiver/Transmitter). It allows users to send and receive data to and from the microcontroller (or other
serial devices) in a virtual environment, acting like a real terminal or serial monitor. This is useful for
debugging and observing data exchanges within your project without requiring physical hardware.
WIRING:

SIMULATION:
TASK NO.3:
SINGLE CHANNEL PWM GENERATION USING POLLING METHOD:
OVERVIEW:
In this task we’ll generate a 50Khz pulse by use of polling method. Duty cycle is defined in the
code and we can change its value from the code
Q-What is duty cycle?
Ans= A duty cycle or power cycle is the fraction of one period in which a signal or system is active. Duty
cycle is commonly expressed as a percentage or a ratio. A period is the time it takes for a signal to
complete an on-and-off cycle.

PINS SETTING:
1-Click on System Core then RCC then Crystal/Ceramic Resonator from HSE
2-Click on PA0 pin of stm32 and set it as GPIO_input
3-Click on Timers then select Internal Clock from Clock Source and select PWM Generation
CH1 from Channel1
4-Clock frequency is set to be 72Mhz

CODE SNIPPET:
EXPLANATION:
This code configures and controls PWM output on TIM1, Channel 1 of an STM32
microcontroller, based on the state of GPIOA pin 0. When GPIOA pin 0 is set (pressed), the
PWM signal starts; when it is reset (released), the PWM signal stops.
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);

 This line starts the PWM signal on Timer 1, Channel 1 using the HAL function.
However, the code will later manipulate this timer's settings directly, potentially
overriding this initial start.

TIM1->CR1 &= 0xFFFE;

 This line clears bit 0 of TIM1->CR1, disabling Timer 1. The hexadecimal mask
0xFFFE effectively sets bit 0 to 0, turning off the timer without altering other bits.

TIM1->PSC = 0;
TIM1->ARR = 1439;
TIM1->CCR1 = 720;

 TIM1->PSC = 0;: Sets the prescaler to 0, so the timer increments at the maximum
clock frequency (assuming no division).
 TIM1->ARR = 1439;: Sets the auto-reload register to 1439. The timer counts from 0
to 1439 before resetting, effectively setting the PWM period.
 TIM1->CCR1 = 720;: Sets the capture/compare register value to 720 for Channel 1.
This value sets the duty cycle; in this case, 720/1440 = 50%, meaning the PWM signal is
high for half of each cycle.

while ((GPIOA->IDR & 0x01) == 0);

 This line waits in a loop until bit 0 of GPIOA->IDR is set to 1, meaning GPIOA pin 0 is
pressed or goes high. This loop keeps the code in place until a button press is detected.

TIM1->CR1 |= 0x1;

 This line sets bit 0 of TIM1->CR1, enabling Timer 1, which starts or resumes the PWM
signal on Channel 1.

while ((GPIOA->IDR & 0x01) == 1);

 This loop waits until bit 0 of GPIOA->IDR is cleared (button released or GPIOA pin
0 goes low), keeping the PWM signal active while the button is pressed.
TIM1->CR1 &= 0xFFFE;

 This line clears bit 0 of TIM1->CR1, disabling Timer 1 and stopping the PWM signal on
Channel 1.

Q-What is prescalar?

Ans= In STM32 microcontrollers, a prescaler is a configuration setting for timers that controls the
frequency of the timer's clock input. It allows you to divide the main clock frequency by a specified factor,
effectively slowing down the timer’s counting speed. This is useful for generating different timing
intervals or PWM frequencies without needing to change the system clock.

Q-What is ARR?

Ans= In STM32 microcontrollers, ARR stands for Auto-Reload Register. This register is a key
component of the timer peripheral, and it plays a crucial role in determining the behavior of the timer,
especially for generating PWM signals and creating timing events.

Q-What is CCR?

Ans= In STM32 microcontrollers, CCR stands for Capture/Compare Register. When configured for
output compare mode (such as PWM generation), the CCR determines the point at which the timer output
(usually a PWM signal) is set high or low within a period defined by the Auto-Reload Register (ARR).

Q-What is Timer Overflow Period?

The timer overflow period is the time it takes for a timer to count from its initial value (usually 0) to its
maximum value (defined by the Auto-Reload Register or ARR) and then roll over back to 0.

CALCULATIONS:
Effective Timer Clock Frequency=Timer Clock Frequency/(Prescalar+1)
In our case Prescalar is set to be 0 so Effective Timer Clock Frequency will be
72Mhz/(0+1)=72Mhz.
Timer Overflow Period=(ARR+1)/Effective Timer Frequency
In our case ARR is set to be 1439(from 0 to 1439) so Timer Overflow Period is:
Timer Overflow Period=(1439+1)/72Mhz=20µs
Duty Cycle=(CCR/(ARR+1))x100
In our case ARR=1439 and CCR=720 so Duty Cycle will be:
Duty Cycle=(720/(1439+1))x100=50%
CIRCUIT DESIGN ON PROTEUS:

APPARATUS:

1-stm32f103c8

2-Pulse generator(High voltage=+3.3V with 50hz frequency)

3-Virtual terminal

WIRING:

SIMULATION:
TASK NO.4:
MULTICHANNEL ADC USING POLLING METHOD:
PINS SETTING:
1-Click on Analog then select the ADC1 then select IN1 and IN2
2-Go to parameter settings and select continuous mode enabled and set no of conversions 2
3- In Rank1 Cycles are 239.5 similarly in Rank2 Cycles are 239.5
4-PC13 Pin is selected as GPIO_output pin
5-Go to Connectivity then select USART1 and mode is asynchronous and baud rate is set to be
9600 from parameter settings.

CODE SNIPPET:

EXPLANATION:
HAL_UART_Transmit(&huart1, "ADC Conversion\r\n", 16, 20);

 This sends the string "ADC Conversion\r\n" via UART (Universal Asynchronous
Receiver-Transmitter) on the huart1 handle.
 The parameters specify the handle (&huart1), the data to be transmitted, the data length
(16 characters), and a timeout of 20 milliseconds.
 This message serves as a notification that an ADC conversion is about to take place.
HAL_ADC_Start(&hadc1);

 Initiates ADC conversion on the ADC handle hadc1.

HAL_ADC_PollForConversion(&hadc1, 20);

 Waits for the ADC conversion to complete, with a timeout of 20 milliseconds.


 After the ADC conversion is completed, the code proceeds to the next line.

ADC_val1 = HAL_ADC_GetValue(&hadc1);

 Reads the ADC conversion result from hadc1 and stores it in ADC_val1.

HAL_ADC_PollForConversion(&hadc1, 20);
ADC_val2 = HAL_ADC_GetValue(&hadc1);

 After polling and confirming the next ADC conversion is complete, the code reads
another ADC result and stores it in ADC_val2.
 This second polling and retrieval enable the reading of two ADC channels sequentially,
assuming the ADC is configured in scan mode.

HAL_ADC_Stop(&hadc1);

 Stops the ADC conversion process, putting it in an idle state.

print_char(ADC_val1);
print_char(ADC_val2);

 Calls the print_char function to transmit the values of ADC_val1 and ADC_val2 over
UART.
 The print_char function converts the numeric ADC values to a character array for
transmission.

HAL_Delay(500);

 Introduces a 500-millisecond delay before the next iteration of the loop.

2ND PART OF CODE:


uint8_t char_num_var[] = {"0000000000\r\n"};

 Creates an array of 12 characters (char_num_var), initialized with "0000000000\r\n".


This will hold up to a 10-digit number, followed by a newline (\r\n) for formatting.
 The \r\n (carriage return and newline) moves the cursor to a new line on the
display/terminal after the value is printed.
uint8_t i = 9;

 Sets i to 9, pointing to the last digit position in the char_num_var array where the least
significant digit will be stored.

while(num_var != 0)
{
char_num_var[i] = (num_var % 10) + 48;
num_var /= 10;
i--;
}

 This loop extracts digits from num_var by repeatedly taking the remainder (num_var %
10).
 (num_var % 10) + 48 converts the extracted digit to its ASCII equivalent (48 is the
ASCII code for '0').
 num_var /= 10 divides num_var by 10, effectively moving to the next higher digit in the
next iteration.
 i-- moves the index leftward in char_num_var, so the next digit is stored to the left of
the previous one.

char_num_var[i] = (num_var % 10) + 48;

 After the loop, if num_var was initially zero, this line ensures char_num_var represents
that zero. If num_var was not zero, it leaves any remaining positions in char_num_var as
zero, maintaining the fixed width.

HAL_UART_Transmit(&huart1, &char_num_var[0], 12, 20);

 Sends char_num_var over UART using HAL_UART_Transmit, with:


 &huart1 as the UART handle,
 &char_num_var[0] as the data to transmit,
 12 as the number of characters (including \r\n)
 and 20 as the transmission timeout in milliseconds.

CIRCUIT DESIGN ON PROTEUS:

APPARATUS:

1-Two HG potentiometers(each of 10k)

2-Two resistors(each of 1k)

3-One stm32f103c8

4-One virtual terminal


WIRING:

SIMULATION:

You might also like