Skip to content

Commit

Permalink
update PWM function, tested hardware -fixes #35
Browse files Browse the repository at this point in the history
  • Loading branch information
gehadelkoumy committed Apr 11, 2023
1 parent 1a92c38 commit bd7817a
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## test
6 changes: 3 additions & 3 deletions TIM2/include/TIM2_config.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*****************************************/
/* Author : Gehad Elkoumy */
/* Version : V01 */
/* Version : V02 */
/* Date : 10 MAR 2023 */
/*****************************************/

Expand All @@ -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
#endif
9 changes: 5 additions & 4 deletions TIM2/include/TIM2_interface.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/*****************************************/
/* Author : Gehad Elkoumy */
/* Version : V01 */
/* Version : V02 */
/* Date : 10 MAR 2023 */
/*****************************************/

#ifndef TIM2_INTERFACE_H
#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

Expand All @@ -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
*/
2 changes: 1 addition & 1 deletion TIM2/include/TIM2_private.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*****************************************/
/* Author : Gehad Elkoumy */
/* Version : V01 */
/* Version : V02 */
/* Date : 10 MAR 2023 */
/*****************************************/

Expand Down
48 changes: 30 additions & 18 deletions TIM2/src/TIM2_program.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*****************************************/
/* Author : Gehad Elkoumy */
/* Version : V01 */
/* Version : V02 */
/* Date : 10 MAR 2023 */
/*****************************************/

Expand All @@ -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;
}
10 changes: 7 additions & 3 deletions TIM2/src/main.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
//*
* main.c
*
* Created on: Mar 10, 2023
* Author: HP
* Author: Gehad
*/

#include"STD_TYPES.h"
Expand All @@ -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);

}
}

4 changes: 2 additions & 2 deletions TIM3/include/TIM3_config.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*****************************************/
/* Author : Gehad Elkoumy */
/* Version : V01 */
/* Version : V02 */
/* Date : 10 MAR 2023 */
/*****************************************/

Expand All @@ -16,4 +16,4 @@
#define TIM_PRESCALER 8


#endif
#endif
7 changes: 4 additions & 3 deletions TIM3/include/TIM3_interface.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*****************************************/
/* Author : Gehad Elkoumy */
/* Version : V01 */
/* Version : V02 */
/* Date : 10 MAR 2023 */
/*****************************************/

Expand All @@ -9,6 +9,7 @@

void MTIM3_voidInit (void);
void MTIM3_voidOutputPWM (u16 Copy_u16CompareValue);
void MTIM3_voidSetBusyWait(u32 Copy_u16Ticks);

#endif

Expand All @@ -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
*/
4 changes: 2 additions & 2 deletions TIM3/include/TIM3_private.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*****************************************/
/* Author : Gehad Elkoumy */
/* Version : V01 */
/* Version : V02 */
/* Date : 10 MAR 2023 */
/*****************************************/

Expand Down Expand Up @@ -38,4 +38,4 @@ typedef struct



#endif
#endif
52 changes: 33 additions & 19 deletions TIM3/src/TIM3_program.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*****************************************/
/* Author : Gehad Elkoumy */
/* Version : V01 */
/* Version : V02 */
/* Date : 10 MAR 2023 */
/*****************************************/

Expand All @@ -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;
}

10 changes: 6 additions & 4 deletions TIM3/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* main.c
*
* Created on: Mar 10, 2023
* Author: HP
* Author: Gehad
*/

#include"STD_TYPES.h"
Expand All @@ -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);
}
}

0 comments on commit bd7817a

Please sign in to comment.