From bd7817ad0d080df70ae4e5dc30133070217fa63a Mon Sep 17 00:00:00 2001 From: gehadelkoumy Date: Wed, 12 Apr 2023 00:33:46 +0200 Subject: [PATCH] update PWM function, tested hardware -fixes #35 --- .../ARM-MCAL-core-peripheral-drivers@12.md | 1 + TIM2/include/TIM2_config.h | 6 +-- TIM2/include/TIM2_interface.h | 9 ++-- TIM2/include/TIM2_private.h | 2 +- TIM2/src/TIM2_program.c | 48 ++++++++++------- TIM2/src/main.c | 10 ++-- TIM3/include/TIM3_config.h | 4 +- TIM3/include/TIM3_interface.h | 7 +-- TIM3/include/TIM3_private.h | 4 +- TIM3/src/TIM3_program.c | 52 ++++++++++++------- TIM3/src/main.c | 10 ++-- 11 files changed, 94 insertions(+), 59 deletions(-) create mode 100644 Graduation Project Documentation/Embedded/ARM-MCAL-core-peripheral-drivers@12.md diff --git a/Graduation Project Documentation/Embedded/ARM-MCAL-core-peripheral-drivers@12.md b/Graduation Project Documentation/Embedded/ARM-MCAL-core-peripheral-drivers@12.md new file mode 100644 index 0000000..f711146 --- /dev/null +++ b/Graduation Project Documentation/Embedded/ARM-MCAL-core-peripheral-drivers@12.md @@ -0,0 +1 @@ +## test diff --git a/TIM2/include/TIM2_config.h b/TIM2/include/TIM2_config.h index 010d16c..055e540 100755 --- a/TIM2/include/TIM2_config.h +++ b/TIM2/include/TIM2_config.h @@ -1,6 +1,6 @@ /*****************************************/ /* Author : Gehad Elkoumy */ -/* Version : V01 */ +/* Version : V02 */ /* Date : 10 MAR 2023 */ /*****************************************/ @@ -9,11 +9,11 @@ /* Options : 0 -- upcounter 1 -- downcounter */ -#define CR1_DIR 0 +#define CR1_DIR 1 /* Options : 0 to 65535 */ #define TIM_PRESCALER 8 -#endif \ No newline at end of file +#endif diff --git a/TIM2/include/TIM2_interface.h b/TIM2/include/TIM2_interface.h index e539065..9fd90ab 100755 --- a/TIM2/include/TIM2_interface.h +++ b/TIM2/include/TIM2_interface.h @@ -1,6 +1,6 @@ /*****************************************/ /* Author : Gehad Elkoumy */ -/* Version : V01 */ +/* Version : V02 */ /* Date : 10 MAR 2023 */ /*****************************************/ @@ -8,7 +8,8 @@ #define TIM2_INTERFACE_H void MTIM2_voidInit (void); -void MTIM2_voidOutputPWM (u16 Copy_u16CompareValue); +void MTIM2_voidSetBusyWait(u32 Copy_u16Ticks); +void MTIM2_voidOutputPWM (u16 Copy_16CompareValue); #endif @@ -20,7 +21,7 @@ void MTIM2_voidOutputPWM (u16 Copy_u16CompareValue); PWM freq = Fclk/(PSC*ARR) PWM duty cycle = CCR / ARR -ex: if we want freq of pulses 1KHz then Fclk = 8MHZ , ARR = 1000 , PSC = 8 -so pwm freq = 8M/(8*1000) = 1KHZ +ex: if we want freq of 1KHz then Fclk = 8MHZ , ARR = 1000 , PSC = 8 + pwm freq = 8M/(8*1000) = 1KHZ */ diff --git a/TIM2/include/TIM2_private.h b/TIM2/include/TIM2_private.h index f7c5848..10d0143 100755 --- a/TIM2/include/TIM2_private.h +++ b/TIM2/include/TIM2_private.h @@ -1,6 +1,6 @@ /*****************************************/ /* Author : Gehad Elkoumy */ -/* Version : V01 */ +/* Version : V02 */ /* Date : 10 MAR 2023 */ /*****************************************/ diff --git a/TIM2/src/TIM2_program.c b/TIM2/src/TIM2_program.c index 7eede64..d1e9bc2 100755 --- a/TIM2/src/TIM2_program.c +++ b/TIM2/src/TIM2_program.c @@ -1,6 +1,6 @@ /*****************************************/ /* Author : Gehad Elkoumy */ -/* Version : V01 */ +/* Version : V02 */ /* Date : 10 MAR 2023 */ /*****************************************/ @@ -18,41 +18,53 @@ void MTIM2_voidInit (void) CLR_BIT(TIM2 -> CR1 , 4); TIM2->CR1 |= (CR1_DIR << 4); - - /*prescaler value*/ TIM2->PSC = TIM_PRESCALER; /*if there is interrupt then DIER_UIE must be enabled*/ + SET_BIT(TIM2 -> DIER , 0); -} - -void MTIM2_voidOutputPWM (u16 Copy_u16CompareValue) -{ /*enable channel*/ SET_BIT(TIM2->CCER , 12); - + /*enable pwm mode 1*/ CLR_BIT(TIM2->CCMR2 , 12); SET_BIT(TIM2->CCMR2 , 13); SET_BIT(TIM2->CCMR2 , 14); - + /*enable output compare -- update value after overflow or immediately*/ CLR_BIT(TIM2->CCMR2 , 11); //immediately - - + /*enable auto reload preload for PWM*/ SET_BIT(TIM2 -> CR1 , 7); - - - - /*desired value*/ - TIM2->ARR = 1000; - CCR4 = (TIM2->ARR) - (Copy_u16CompareValue); //channel4 - + + /*load desired value of ARR*/ + TIM2->ARR = 100; + /*enable counter*/ SET_BIT(TIM2 -> CR1 , 0); /*enable update generation*/ SET_BIT(TIM2 -> EGR , 0); + +} + +void MTIM2_voidSetBusyWait(u32 Copy_u16Ticks) +{ + /* Load ticks to ARR register */ + TIM2 -> ARR = Copy_u16Ticks; + /*start counter*/ + SET_BIT(TIM2 -> CR1 , 0); + /* Wait till flag is raised */ + while( (GET_BIT(TIM2 -> SR , 0)) == 0); + /*Stop counter*/ + CLR_BIT(TIM2 -> CR1 , 0); + /*Clear flag*/ + CLR_BIT(TIM2 -> SR , 0); } + +void MTIM2_voidOutputPWM (u16 Copy_16CompareValue) +{ + /*load the desired value -- channel4*/ + CCR4 = Copy_16CompareValue; +} diff --git a/TIM2/src/main.c b/TIM2/src/main.c index 5cb2e3e..d8616ae 100755 --- a/TIM2/src/main.c +++ b/TIM2/src/main.c @@ -1,8 +1,8 @@ -/* +//* * main.c * * Created on: Mar 10, 2023 - * Author: HP + * Author: Gehad */ #include"STD_TYPES.h" @@ -26,13 +26,17 @@ void main (void) /*mode must be alternative function push pull*/ MGPIO_VoidSetPinDirection(GPIOA,PIN3,OUTPUT_2MHZ_AFPP); + MGPIO_VoidSetPinDirection(GPIOA,PIN2,OUTPUT_2MHZ_PP); + //MGPIO_VoidSetPinDirection(GPIOA,PIN0,OUTPUT_2MHZ_PP); MTIM2_voidInit(); - MTIM2_voidOutputPWM(100); while(1) { + MGPIO_VoidSetPinValue(GPIOA , PIN2 ,HIGH); + MTIM2_voidOutputPWM(0); + } } diff --git a/TIM3/include/TIM3_config.h b/TIM3/include/TIM3_config.h index 81ca377..ff17fb2 100755 --- a/TIM3/include/TIM3_config.h +++ b/TIM3/include/TIM3_config.h @@ -1,6 +1,6 @@ /*****************************************/ /* Author : Gehad Elkoumy */ -/* Version : V01 */ +/* Version : V02 */ /* Date : 10 MAR 2023 */ /*****************************************/ @@ -16,4 +16,4 @@ #define TIM_PRESCALER 8 -#endif \ No newline at end of file +#endif diff --git a/TIM3/include/TIM3_interface.h b/TIM3/include/TIM3_interface.h index 5a34fbe..f330279 100755 --- a/TIM3/include/TIM3_interface.h +++ b/TIM3/include/TIM3_interface.h @@ -1,6 +1,6 @@ /*****************************************/ /* Author : Gehad Elkoumy */ -/* Version : V01 */ +/* Version : V02 */ /* Date : 10 MAR 2023 */ /*****************************************/ @@ -9,6 +9,7 @@ void MTIM3_voidInit (void); void MTIM3_voidOutputPWM (u16 Copy_u16CompareValue); +void MTIM3_voidSetBusyWait(u32 Copy_u16Ticks); #endif @@ -20,7 +21,7 @@ void MTIM3_voidOutputPWM (u16 Copy_u16CompareValue); PWM freq = Fclk/(PSC*ARR) PWM duty cycle = CCR / ARR -ex: if we want freq of pulses 1KHz then Fclk = 8MHZ , ARR = 1000 , PSC = 8 -so pwm freq = 8M/(8*1000) = 1KHZ +ex: if we want freq of 1KHz then Fclk = 8MHZ , ARR = 1000 , PSC = 8 + pwm freq = 8M/(8*1000) = 1KHZ */ diff --git a/TIM3/include/TIM3_private.h b/TIM3/include/TIM3_private.h index bf27c1e..feae4aa 100755 --- a/TIM3/include/TIM3_private.h +++ b/TIM3/include/TIM3_private.h @@ -1,6 +1,6 @@ /*****************************************/ /* Author : Gehad Elkoumy */ -/* Version : V01 */ +/* Version : V02 */ /* Date : 10 MAR 2023 */ /*****************************************/ @@ -38,4 +38,4 @@ typedef struct -#endif \ No newline at end of file +#endif diff --git a/TIM3/src/TIM3_program.c b/TIM3/src/TIM3_program.c index 2029d7f..194d1ce 100755 --- a/TIM3/src/TIM3_program.c +++ b/TIM3/src/TIM3_program.c @@ -1,6 +1,6 @@ /*****************************************/ /* Author : Gehad Elkoumy */ -/* Version : V01 */ +/* Version : V02 */ /* Date : 10 MAR 2023 */ /*****************************************/ @@ -18,41 +18,55 @@ void MTIM3_voidInit (void) CLR_BIT(TIM3 -> CR1 , 4); TIM3->CR1 |= (CR1_DIR << 4); - - /*prescaler value*/ TIM3->PSC = TIM_PRESCALER; /*if there is interrupt then DIER_UIE must be enabled*/ + SET_BIT(TIM3 -> DIER , 0); -} - -void MTIM3_voidOutputPWM (u16 Copy_u16CompareValue) -{ /*enable channel1*/ SET_BIT(TIM3->CCER , 0); - + /*enable pwm mode 1 , channel1*/ CLR_BIT(TIM3->CCMR1 , 4); SET_BIT(TIM3->CCMR1 , 5); SET_BIT(TIM3->CCMR1 , 6); - + /*enable output compare -- update value after overflow or immediately*/ CLR_BIT(TIM3->CCMR1 , 3); //immediately , channel1 - - + /*enable auto reload preload for PWM*/ SET_BIT(TIM3 -> CR1 , 7); - - - - /*desired value*/ - TIM3->ARR = 1000; - CCR1 = (TIM3->ARR) - (Copy_u16CompareValue); //channel1 - + + /*load desired value in ARR*/ + TIM3->ARR = 100; + /*enable counter*/ SET_BIT(TIM3 -> CR1 , 0); /*enable update generation*/ SET_BIT(TIM3 -> EGR , 0); - + +} + +void MTIM3_voidSetBusyWait(u32 Copy_u16Ticks) +{ + /* Load ticks to ARR register */ + TIM3 -> ARR = Copy_u16Ticks; + /*start counter*/ + SET_BIT(TIM3 -> CR1 , 0); + /* Wait till flag is raised */ + while( (GET_BIT(TIM3 -> SR , 0)) == 0); + /*Stop counter*/ + CLR_BIT(TIM3 -> CR1 , 0); + /*Clear flag*/ + CLR_BIT(TIM3 -> SR , 0); + +} + + +void MTIM3_voidOutputPWM (u16 Copy_u16CompareValue) +{ + /* load desired value -- channel 1 */ + CCR1 = Copy_u16CompareValue; } + diff --git a/TIM3/src/main.c b/TIM3/src/main.c index f502780..e882008 100755 --- a/TIM3/src/main.c +++ b/TIM3/src/main.c @@ -2,7 +2,7 @@ * main.c * * Created on: Mar 10, 2023 - * Author: HP + * Author: Gehad */ #include"STD_TYPES.h" @@ -24,15 +24,17 @@ void main (void) /*Enable Timer3 clock*/ RCC_voidEnableClock(RCC_APB1,1); - /*mode must be alternative function push pull*/ + /*mode must be alternative function push pull (not considered as GPIO)*/ MGPIO_VoidSetPinDirection(GPIOA,PIN6,OUTPUT_2MHZ_AFPP); + MGPIO_VoidSetPinDirection(GPIOA,PIN5,OUTPUT_2MHZ_PP); MTIM3_voidInit(); - MTIM3_voidOutputPWM(500); + while(1) { - + MTIM3_voidOutputPWM(0); + MGPIO_VoidSetPinValue(GPIOA,PIN5,HIGH); } }