Assignment_ARM
Assignment_ARM
HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12);
}
}
Part 2
/* USER CODE BEGIN WHILE */
while (1)
{
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_SET)//
{
HAL_Delay(100); //Debouncing Delay used to avoid Noise
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_SET)//
{
HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12);
}
}
Part 1
Part 2
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12);
}
Flow
H/W Specific HAL Specific
Switch(EXTI0 Source)--> EXTI0_IRQHandler --> HAL_GPIO_EXTI_IRQHandler() -->
Actual Funcion where we can Code HAL_GPIO_EXTI_Callback()
void EXTI0_IRQHandler(void)
{
/* USER CODE BEGIN EXTI0_IRQn 0 */
/* NOTE: This function should not be modified, when the callback is needed,
the HAL_GPIO_EXTI_Callback could be implemented in the user file
*/
}
PreScalar=16000
16MHZ/PreScalar = 1KHZ
Freq.= 1/Time
1KHZ = 1MS
1HZ = 1Sec
Part 1 For a Delay of 200 MS , if Prescalar is 16000 and APB Bus Freq is 16Mhz,
What is the count Required?
16000000/16000=1000=1Khz = 1MS * 200(count) = 200MS
Part 2 For a Delay of 500 MS , if Prescalar is 16000 and APB Bus Freq is 16Mhz,
What is the count Required?
16000000/16000=1000=1Khz = 1MS * 500(count) = 500MS
Part 3 For a Delay of 50 MS , if Prescalar is 16000 and APB Bus Freq is 16Mhz, What
is the count Required?
Part 4 For a Delay of 750 MS , if Prescalar is 16000 and APB Bus Freq is 16Mhz,
What is the count Required?
If Clock Freq for ABP1 Timer Bus is 32Mhz and we want a delay for 1MS (Tick) , what
will be the Prescalar?
If Clock Freq for ABP1 Timer Bus is 32Mhz and Prescalar is 8000 , what will be the
Delay or Tick?
32Mhz/8000 = 4 Khz
F= 1/T
T=1/F = 1/4Khz=1/4000 = 0.25MS
Configure NVIC
NVIC GUI --> TIMER2 GLOBAL INTERRUPT Enable
Action:Toggle the LED's when Timer time Elapsed or Timer Expire , also configure
SWD Feature?
*Note:*Timer peripherals are always clocked through APBx timer clocks not APBx
peripheral clocks.*
Most Important Formula
For STM32:
PWM Frequency = [Timer Frequency ] / [(Prescaler+1)*(Period+1)]
For Theory:
PWM Frequency = [Timer Frequency ] / [(Prescaler)*(Period)]
PWM Time = 1 / PWM Frequency
Duty Cycle= Ton/ Ton + Toff
If I want 10 kHz, then applying the formula with prescaler one and counter period
99 I have:
PreScalar=15
16MHZ/PreScalar+1 = 1MHZ
1MHZ=0.001MS=1MicroSec
PWM Frequency = [16 Mhz] / [(15+1)*(99+1)] = 10 kHz
Duty Cycle= Ton/ Ton + Toff
Action:Control the Toggoling Fequency of each LED at different rate, also configure
SWD Feature?
/* USER CODE BEGIN PV */
volatile uint32_t pulse1_value = 250; //to produce 4Hz
volatile uint32_t pulse2_value = 500; //to produce 2HZ
volatile uint32_t pulse3_value = 750; //to produce 1.33Hz
volatile uint32_t pulse4_value = 1000; //to produce 1Hz
volatile uint32_t ccr_content;
Configure NVIC
NVIC GUI --> TIMER2 GLOBAL INTERRUPT Enable
Action: Capture the Timing of Button Pressed and Display the same in STM STudio,
also configure SWD Feature?
Freq:16MHZ
Aim: 100 Sec
Prescalar=16000
16MHZ/PreScalar = 1KHZ
1KHZ= 1MS
STM Studio
How to watch a Real Time Value of Variable?
Step1: Go To File Menu
Step2: Click on Import Variables
Step3: File Selection of Executable File (<project_name>.elf)
.elf file will be there in Workspace_xxxx-><Project_NameFolder>->Debug Folder
Step4:Select The variables you want to view (Eg. counterVal,inputCaptureVal) and
Press Import Button and Close
Step5:Varibles will be there in Display Variable Tab
Step6: Select the Variable -> Right Click and send to VarViewer1
Step7: Diaplay VarViewer1 as Bar Graph
Action : Press the User Switch (PA0) to get Max Value of ADC in Single Shot
and Display the same in STM STudio, also configure SWD Feature?
Action : Press the User Switch (PA0) to get Max Value of ADC in Continous
Conversion Mode
and Display the same in STM STudio, also configure SWD Feature?
Q11 Write a Interrupt Driven Program(RTC_INT_A11) to Learn Real Time Clock and
Configure Pins
(LED) PD12 to GPIO_Output // Alarm Indication LED
(LED) PD13 to GPIO_Output // Second's LED
Configure NVIC
NVIC GUI --> RTC alarms A and B interrupt ENABLE
Calender Time:-
Hours : 23
Minute : 59
Second : 50
Calender Date:-
Weekday : Thursday
Month : January
Date : 28
Year : 2021
Alarm A:
Time: 00:00:00
Alarm Date : 29
Part 2
Alarm B:
Time: 00:00:10
Alarm Date :29 Turn on LED14 with Alarm B
memset(&sdatestructureget,0,sizeof(sdatestructureget));
memset(&stimestructureget,0,sizeof(stimestructureget));
/* Get the RTC current Time */
HAL_RTC_GetTime(&hrtc, &stimestructureget, RTC_FORMAT_BIN);
/* Get the RTC current Date */
HAL_RTC_GetDate(&hrtc, &sdatestructureget, RTC_FORMAT_BIN);
/* Display time Format : hh:mm:ss */
sprintf((char*)showtime,"%02d:%02d:%02d ",stimestructureget.Hours,
stimestructureget.Minutes, stimestructureget.Seconds);
printf("Current Time %s",showtime);
printf("Current Time %d",(uint8_t*)showtime);
memset(showtime,0,sizeof(showtime));
sprintf((char*)showtime,"%02d-%2d-%2d\r\n",sdatestructureget.Month,
sdatestructureget.Date, 2000 + sdatestructureget.Year);
printf("Current Date %s",showtime);
printf("Current Date %d",(uint8_t*)showtime);
Configure NVIC
NVIC GUI --> TIMER2 GLOBAL INTERRUPT Enable
50Period * x = 6000ms , x =120
Action: Observe the Counter Variable it will start from 0 to 59 [60 counts],
60Count * 100 Period = 6000 ms and 500ms HAL Delay,
total 6500ms as the IWDG freq. for STM32F3 is set to 40Khz ,
Pre Scalar is 64, 40/64=0.625 Khz = 1.6 ms * 4095 = 6552 ms ~ 6.5Sec,
you will observe the watchdog resest the system if unattended
(No HAL_IWDG_Refresh) for 6.5 sec
for STM32F3
Watch Dog Freq: 40Khz
Prescalar : 64
Effective Clock for WDG : 40Khz/ Prescalar =40/64=0.625 Khz = 1.6 ms * 4095 = 6552
ms ~ 6.5Sec
If we do not refresh IWDG within 6.5 Sec then it will reset the system
Configure NVIC
NVIC GUI --> UART4 GLOBAL INTERRUPT Enable
Interrupt
/* USER CODE BEGIN PV */
char rx;
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_UART_Receive_IT(&huart4, (uint8_t *)&rx, 1);
}
Part 2 Assignment
Example
Output : Hello
Output should only be displayed when user press enter key after typing some text
Hits:
Use Array, Increment Index
For Enter Key Press Event compare buf with '\r'
Q15 Write a Direct Memory Access (DMA) Driven Program(UART_DMA_A15) to Learn UART
Functionality via DMA and
Configure Pins
(LED) PD12 to GPIO_Output // Interrupt Confirmation LED
DMA Settings:
Press Add Button and Select- UART4_RX
Direction : Peripheral to Memory
DMA Request Setting:
Mode : Normal
SPI
8 Bit Register
RW
7 6 5 4 3 2 1 0
1 0 0 0 0 0 0 0 Read (0x80) | CMD
0 0 0 0 0 0 0 0 Write(0x00) | CMD
for Stm32F3
//PV
uint8_t ReadFlag=0x80;
uint8_t MultiByte=0x40;
uint8_t Return[2];
uint16_t Rx_x,Rx_y,Rx_z;
//Who am I Checking
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, RESET);//CS Pin Low
TxBuf[0]=0x0f|ReadFlag;//Read Address of Register
HAL_SPI_Transmit(&hspi1, TxBuf, 1, 50);
HAL_SPI_Receive(&hspi1, Return, 1, 50);
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, SET);//CS Pin High
//Configure Control Register 1 with Address 0x20
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, RESET);//CS Pin Low
TxBuf[0]=0x20;//Address of Register
TxBuf[1]=0x3F;//Data for Register
HAL_SPI_Transmit(&hspi1, TxBuf, 2, 50);
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, SET);//CS Pin High
//Configure Control Register 4 with Address 0x23
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, RESET);//CS Pin Low
TxBuf[0]=0x23;//Address of Register
TxBuf[1]=0x10;//Data for Register
HAL_SPI_Transmit(&hspi1, TxBuf, 2, 50);
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, SET);//CS Pin High
while(1)
{
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, RESET);//CS Pin Low
TxBuf[0]=0x28|ReadFlag|MultiByte;//Read Multi Byte Address of Register for X
HAL_SPI_Transmit(&hspi1, TxBuf, 1, 50);
HAL_SPI_Receive(&hspi1, Return, 2, 50);
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, SET);//CS Pin High
Rx_x= (Return[1]<<8)|Return[0]
Rx_y= (Return[1]<<8)|Return[0]
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, RESET);//CS Pin Low
TxBuf[0]=0x2C|ReadFlag|MultiByte;//Read Multi Byte Address of Register for Z
HAL_SPI_Transmit(&hspi1, TxBuf, 1, 50);
HAL_SPI_Receive(&hspi1, Return, 2, 50);
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, SET);//CS Pin High
Rx_z= (Return[1]<<8)|Return[0]
for Stm32F4
0x20 Control Register4
0x37 Value to be set in Control Register (for enabling XYZ Axis, Setting Feqn.
12.5Hz)
_________________________ _________________________
| ______________| |______________ |
| | I2C1 | | I2C1| |
| | | | | |
| | SCL(PB6)|______________________|(PB6)SCL | |
| | | | | |
| | | | | |
| | | | | |
| | SDA(PB9)|______________________|(PB9)SDA | |
| | | | | |
| |______________| |______________| |
| __ | | __ |
| |__| | | |__| |
| USER | | USER |
| GND|______________________|GND |
|_STM32F4 ________________| |_________________STM32F4_|
Q17. Write a I2C Program to Communicate between Two STM32F4 Boards and Send LED_ON
Info?
Configure Pins
(LED) PD12 to GPIO_Output
(LED) PD13 to GPIO_Output
(LED) PD14 to GPIO_Output
(LED) PD15 to GPIO_Output
(Button)PA0 to GPIO_Input
SDA(PB9)
SCL(PB6)
Configure Peripherals I2C (I2C1)
Mode : I2C
Master Feature
I2C Speed Mode : Fast Mode
I2C Clock : 400000 (400Khz)
FastMode DutyCycle : Tlow/Thigh =16/9
Slave Feature
Primary Address Length: 10bit
Primary Slave Address: 0x1D
}
HAL_Delay(100);//Delay just for better Tuning
}
}
}
#else
while(1)
{
switch(LedStatus)
{
case (1):
/* Turn ON LED1 */
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
break;
case (2):
/* Turn ON LED2 */
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_SET);
break;
case (3):
/* Turn ON LED3 */
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET);
break;
case (4):
/* Turn ON LED4 */
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_SET);
break;
default:
break;
}
}
/* USER CODE END 4 */
When acting as an I2C master in blocking mode, there are four API functions
available for communicating with a slave device:
HAL_I2C_Master_Transmit()
HAL_I2C_Master_Receive()
HAL_I2C_Mem_Write()
HAL_I2C_Mem_Read()