This commit is contained in:
Alix Chagot 2023-03-21 15:57:44 +01:00
parent 164d0d54a9
commit 125c801a01
4 changed files with 81 additions and 13 deletions

View file

@ -11,7 +11,7 @@ typedef struct {
void MyTimer_Base_Init(MyTimer_Struct_Typedef * Timer);
void MyTimer_ActiveIT(TIM_TypeDef * Timer, char Prio, void (*IT_function) (void));
void MyTimer_PWM(TIM_TypeDef * Timer ,char Channel);
void MyTimer_DutyCycle(TIM_TypeDef * Timer, char Channel, unsigned short DutyCycle);
void MyTimer_DutyCycle(TIM_TypeDef * Timer, char Channel, unsigned char DutyCycle);
#define MyTimer_Base_Start(Tim) (Tim.Timer->CR1 |= TIM_CR1_CEN)
#define MyTimer_Base_Stop(Tim) (Tim.Timer->CR1 &= ~TIM_CR1_CEN)

View file

@ -1,4 +1,5 @@
#include "timer.h"
#include "gpio.h"
void plantage(void) {
while(1);
@ -57,40 +58,105 @@ void TIM4_IRQHandler(void) {
void MyTimer_PWM(TIM_TypeDef * Timer ,char Channel)
{
MyGPIO_Struct_TypeDef PWM_OUT;
PWM_OUT.GPIO_Conf = AltOut_Ppull;
switch (Channel) {
case 1:
case 2:
Timer->CCMR1 &= ~TIM_CCMR1_OC1M_0;
Timer->CCMR1 |= TIM_CCMR1_OC1M_1| TIM_CCMR1_OC1M_2;
Timer->CCMR1 |= (TIM_CCMR1_OC1M_1| TIM_CCMR1_OC1M_2);
break;
case 3:
case 4:
Timer->CCMR2 &= ~TIM_CCMR1_OC1M_0;
Timer->CCMR2 |= TIM_CCMR1_OC1M_1| TIM_CCMR1_OC1M_2;
Timer->CCMR2 |= (TIM_CCMR1_OC1M_1| TIM_CCMR1_OC1M_2);
break;
}
Timer->CCER |= (TIM_CCER_CC1E << (4*(Channel-1)));
}
void MyTimer_DutyCycle(TIM_TypeDef * Timer, char Channel, unsigned short DutyCycle)
{
if(Timer == TIM2)
{
PWM_OUT.GPIO = GPIOA;
switch (Channel) {
case 1:
Timer->CCR1 = DutyCycle;
PWM_OUT.GPIO_Pin = 0;
break;
case 2:
PWM_OUT.GPIO_Pin = 1;
break;
case 3:
PWM_OUT.GPIO_Pin = 2;
break;
case 4:
PWM_OUT.GPIO_Pin = 3;
break;
}
}
if(Timer == TIM3)
{
switch (Channel) {
case 1:
PWM_OUT.GPIO = GPIOA;
PWM_OUT.GPIO_Pin = 6;
break;
case 2:
PWM_OUT.GPIO = GPIOA;
PWM_OUT.GPIO_Pin = 7;
break;
case 3:
PWM_OUT.GPIO = GPIOB;
PWM_OUT.GPIO_Pin = 0;
break;
case 4:
PWM_OUT.GPIO = GPIOB;
PWM_OUT.GPIO_Pin = 1;
break;
}
}
if(Timer == TIM4)
{
PWM_OUT.GPIO = GPIOB;
switch (Channel) {
case 1:
PWM_OUT.GPIO_Pin = 6;
break;
case 2:
PWM_OUT.GPIO_Pin = 7;
break;
case 3:
PWM_OUT.GPIO_Pin = 8;
break;
case 4:
PWM_OUT.GPIO_Pin = 9;
break;
}
}
MyGPIO_Init(&PWM_OUT);
}
void MyTimer_DutyCycle(TIM_TypeDef * Timer, char Channel, unsigned char DutyCycle)
{
unsigned int RC;
RC = ((Timer->ARR)/100)*(DutyCycle);
switch (Channel) {
case 1:
Timer->CCR1 = RC;
break;
case 2:
Timer->CCR2 = DutyCycle;
Timer->CCR2 = RC;
break;
case 3:
Timer->CCR3 = DutyCycle;
Timer->CCR3 = RC;
break;
case 4:
Timer->CCR4 = DutyCycle;
Timer->CCR4 = RC;
break;
}
}

View file

@ -25,9 +25,11 @@ int main(void) {
MyGPIO_Init(&b1);
MyTimer_Base_Init(&t3);
MyTimer_ActiveIT(TIM3, 0, &changer_led);
MyTimer_PWM(TIM3, 1);
MyTimer_DutyCycle(TIM3,1,10);
MyTimer_Base_Start(t3);
do
{
/*if (MyGPIO_Read(GPIOC, 13) == 0x1) {

View file

@ -75,7 +75,7 @@
<OPTFL>
<tvExp>1</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<IsCurrentTarget>0</IsCurrentTarget>
<IsCurrentTarget>1</IsCurrentTarget>
</OPTFL>
<CpuCode>18</CpuCode>
<DebugOpt>
@ -125,7 +125,7 @@
<SetRegEntry>
<Number>0</Number>
<Key>DLGDARM</Key>
<Name>(1010=-1,-1,-1,-1,0)(1007=-1,-1,-1,-1,0)(1008=-1,-1,-1,-1,0)(1009=-1,-1,-1,-1,0)(100=-1,-1,-1,-1,0)(110=-1,-1,-1,-1,0)(111=-1,-1,-1,-1,0)(1011=-1,-1,-1,-1,0)(180=-1,-1,-1,-1,0)(120=920,358,1341,785,0)(121=-1,-1,-1,-1,0)(122=-1,-1,-1,-1,0)(123=-1,-1,-1,-1,0)(140=-1,-1,-1,-1,0)(240=105,137,504,482,0)(190=-1,-1,-1,-1,0)(200=-1,-1,-1,-1,0)(170=120,153,405,449,0)(130=-1,-1,-1,-1,0)(131=931,121,1525,872,0)(132=492,166,1086,917,0)(133=-1,-1,-1,-1,0)(160=-1,-1,-1,-1,0)(161=-1,-1,-1,-1,0)(162=-1,-1,-1,-1,0)(210=-1,-1,-1,-1,0)(211=-1,-1,-1,-1,0)(220=-1,-1,-1,-1,0)(221=-1,-1,-1,-1,0)(230=-1,-1,-1,-1,0)(234=-1,-1,-1,-1,0)(231=-1,-1,-1,-1,0)(232=-1,-1,-1,-1,0)(233=-1,-1,-1,-1,0)(150=-1,-1,-1,-1,0)(151=-1,-1,-1,-1,0)</Name>
<Name>(1010=-1,-1,-1,-1,0)(1007=-1,-1,-1,-1,0)(1008=-1,-1,-1,-1,0)(1009=-1,-1,-1,-1,0)(100=-1,-1,-1,-1,0)(110=-1,-1,-1,-1,0)(111=-1,-1,-1,-1,0)(1011=-1,-1,-1,-1,0)(180=-1,-1,-1,-1,0)(120=920,358,1341,785,0)(121=-1,-1,-1,-1,0)(122=-1,-1,-1,-1,0)(123=-1,-1,-1,-1,0)(140=-1,-1,-1,-1,0)(240=105,137,504,482,0)(190=-1,-1,-1,-1,0)(200=-1,-1,-1,-1,0)(170=120,153,405,449,0)(130=-1,-1,-1,-1,0)(131=931,121,1525,872,0)(132=973,161,1567,912,1)(133=-1,-1,-1,-1,0)(160=-1,-1,-1,-1,0)(161=-1,-1,-1,-1,0)(162=-1,-1,-1,-1,0)(210=-1,-1,-1,-1,0)(211=-1,-1,-1,-1,0)(220=-1,-1,-1,-1,0)(221=-1,-1,-1,-1,0)(230=-1,-1,-1,-1,0)(234=-1,-1,-1,-1,0)(231=-1,-1,-1,-1,0)(232=-1,-1,-1,-1,0)(233=-1,-1,-1,-1,0)(150=-1,-1,-1,-1,0)(151=-1,-1,-1,-1,0)</Name>
</SetRegEntry>
<SetRegEntry>
<Number>0</Number>
@ -245,7 +245,7 @@
<OPTFL>
<tvExp>1</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<IsCurrentTarget>1</IsCurrentTarget>
<IsCurrentTarget>0</IsCurrentTarget>
</OPTFL>
<CpuCode>18</CpuCode>
<DebugOpt>
@ -406,7 +406,7 @@
<GroupNumber>2</GroupNumber>
<FileNumber>2</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExp>1</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>..\Drivers\src\gpio.c</PathWithFileName>