forked from trocache/RefKEIL
Fin séance 2
This commit is contained in:
parent
56875f8dcb
commit
164d0d54a9
6 changed files with 164 additions and 7 deletions
19
ProjetsKEIL/Drivers/inc/timer.h
Normal file
19
ProjetsKEIL/Drivers/inc/timer.h
Normal file
|
@ -0,0 +1,19 @@
|
|||
#ifndef MYTIMER_H
|
||||
#define MYTIMER_H
|
||||
#include "stm32f10x.h"
|
||||
|
||||
typedef struct {
|
||||
TIM_TypeDef * Timer;
|
||||
unsigned short ARR;
|
||||
unsigned short PSC;
|
||||
} MyTimer_Struct_Typedef;
|
||||
|
||||
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);
|
||||
|
||||
#define MyTimer_Base_Start(Tim) (Tim.Timer->CR1 |= TIM_CR1_CEN)
|
||||
#define MyTimer_Base_Stop(Tim) (Tim.Timer->CR1 &= ~TIM_CR1_CEN)
|
||||
|
||||
#endif
|
|
@ -70,5 +70,10 @@ void MyGPIO_Reset(GPIO_TypeDef * GPIO, char GPIO_Pin) {
|
|||
}
|
||||
|
||||
void MyGPIO_Toggle(GPIO_TypeDef * GPIO, char GPIO_Pin) {
|
||||
|
||||
if (MyGPIO_Read(GPIO, GPIO_Pin) == 0x1) {
|
||||
MyGPIO_Reset(GPIO, GPIO_Pin);
|
||||
}
|
||||
else {
|
||||
MyGPIO_Set(GPIO, GPIO_Pin);
|
||||
}
|
||||
}
|
||||
|
|
96
ProjetsKEIL/Drivers/src/timer.c
Normal file
96
ProjetsKEIL/Drivers/src/timer.c
Normal file
|
@ -0,0 +1,96 @@
|
|||
#include "timer.h"
|
||||
|
||||
void plantage(void) {
|
||||
while(1);
|
||||
}
|
||||
|
||||
void (*IT_Tim2) (void) = plantage;
|
||||
void (*IT_Tim3) (void) = plantage;
|
||||
void (*IT_Tim4) (void) = plantage;
|
||||
|
||||
void MyTimer_Base_Init(MyTimer_Struct_Typedef * Timer) {
|
||||
if ((Timer->Timer) == TIM2)
|
||||
RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
|
||||
if ((Timer->Timer) == TIM3)
|
||||
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
|
||||
if ((Timer->Timer) == TIM4)
|
||||
RCC->APB1ENR |= RCC_APB1ENR_TIM4EN;
|
||||
|
||||
Timer->Timer->PSC = Timer->PSC;
|
||||
Timer->Timer->ARR = Timer->ARR;
|
||||
}
|
||||
|
||||
void MyTimer_ActiveIT(TIM_TypeDef * Timer, char Prio, void (*IT_function) (void)) {
|
||||
Timer->DIER |= TIM_DIER_UIE;
|
||||
|
||||
if (Timer == TIM2) {
|
||||
NVIC_EnableIRQ(TIM2_IRQn);
|
||||
NVIC_SetPriority(TIM2_IRQn, Prio);
|
||||
IT_Tim2 = IT_function;
|
||||
}
|
||||
if (Timer == TIM3) {
|
||||
NVIC_EnableIRQ(TIM3_IRQn);
|
||||
NVIC_SetPriority(TIM3_IRQn, Prio);
|
||||
IT_Tim3 = IT_function;
|
||||
}
|
||||
if (Timer == TIM4) {
|
||||
NVIC_EnableIRQ(TIM4_IRQn);
|
||||
NVIC_SetPriority(TIM4_IRQn, Prio);
|
||||
IT_Tim4 = IT_function;
|
||||
}
|
||||
}
|
||||
|
||||
void TIM2_IRQHandler(void) {
|
||||
TIM2->SR &= ~TIM_SR_UIF;
|
||||
(*IT_Tim2)();
|
||||
}
|
||||
|
||||
void TIM3_IRQHandler(void) {
|
||||
TIM3->SR &= ~TIM_SR_UIF;
|
||||
(*IT_Tim3)();
|
||||
}
|
||||
|
||||
void TIM4_IRQHandler(void) {
|
||||
TIM4->SR &= ~TIM_SR_UIF;
|
||||
(*IT_Tim4)();
|
||||
}
|
||||
|
||||
void MyTimer_PWM(TIM_TypeDef * Timer ,char Channel)
|
||||
{
|
||||
switch (Channel) {
|
||||
case 1:
|
||||
case 2:
|
||||
Timer->CCMR1 &= ~TIM_CCMR1_OC1M_0;
|
||||
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;
|
||||
break;
|
||||
}
|
||||
|
||||
Timer->CCER |= (TIM_CCER_CC1E << (4*(Channel-1)));
|
||||
}
|
||||
|
||||
void MyTimer_DutyCycle(TIM_TypeDef * Timer, char Channel, unsigned short DutyCycle)
|
||||
{
|
||||
switch (Channel) {
|
||||
case 1:
|
||||
Timer->CCR1 = DutyCycle;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
Timer->CCR2 = DutyCycle;
|
||||
break;
|
||||
|
||||
case 3:
|
||||
Timer->CCR3 = DutyCycle;
|
||||
break;
|
||||
|
||||
case 4:
|
||||
Timer->CCR4 = DutyCycle;
|
||||
break;
|
||||
}
|
||||
}
|
|
@ -1,9 +1,13 @@
|
|||
#include "stm32f10x.h"
|
||||
#include "gpio.h"
|
||||
#include "timer.h"
|
||||
|
||||
void changer_led(void);
|
||||
|
||||
int main(void) {
|
||||
MyGPIO_Struct_TypeDef led2;
|
||||
MyGPIO_Struct_TypeDef b1;
|
||||
MyTimer_Struct_Typedef t3;
|
||||
|
||||
MyGPIO_InitClock();
|
||||
|
||||
|
@ -13,17 +17,28 @@ int main(void) {
|
|||
b1.GPIO = GPIOC;
|
||||
b1.GPIO_Pin = 13;
|
||||
b1.GPIO_Conf = In_PullUp;
|
||||
t3.Timer = TIM3;
|
||||
t3.ARR = 7199;
|
||||
t3.PSC = 4999;
|
||||
|
||||
MyGPIO_Init(&led2);
|
||||
MyGPIO_Init(&b1);
|
||||
MyTimer_Base_Init(&t3);
|
||||
MyTimer_ActiveIT(TIM3, 0, &changer_led);
|
||||
|
||||
MyTimer_Base_Start(t3);
|
||||
|
||||
do
|
||||
{
|
||||
if (MyGPIO_Read(GPIOC, 13) == 0x1) {
|
||||
/*if (MyGPIO_Read(GPIOC, 13) == 0x1) {
|
||||
MyGPIO_Reset(GPIOA, 5);
|
||||
}
|
||||
else {
|
||||
MyGPIO_Set(GPIOA, 5);
|
||||
}
|
||||
}*/
|
||||
} while (1);
|
||||
}
|
||||
}
|
||||
|
||||
void changer_led(void) {
|
||||
MyGPIO_Toggle(GPIOA, 5);
|
||||
}
|
||||
|
|
|
@ -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=-1,-1,-1,-1,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=150,186,744,937,0)(132=-1,-1,-1,-1,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=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>
|
||||
</SetRegEntry>
|
||||
<SetRegEntry>
|
||||
<Number>0</Number>
|
||||
|
@ -414,6 +414,18 @@
|
|||
<RteFlg>0</RteFlg>
|
||||
<bShared>0</bShared>
|
||||
</File>
|
||||
<File>
|
||||
<GroupNumber>2</GroupNumber>
|
||||
<FileNumber>3</FileNumber>
|
||||
<FileType>1</FileType>
|
||||
<tvExp>1</tvExp>
|
||||
<tvExpOptDlg>0</tvExpOptDlg>
|
||||
<bDave2>0</bDave2>
|
||||
<PathWithFileName>..\Drivers\src\timer.c</PathWithFileName>
|
||||
<FilenameWithoutPath>timer.c</FilenameWithoutPath>
|
||||
<RteFlg>0</RteFlg>
|
||||
<bShared>0</bShared>
|
||||
</File>
|
||||
</Group>
|
||||
|
||||
<Group>
|
||||
|
@ -426,7 +438,7 @@
|
|||
|
||||
<Group>
|
||||
<GroupName>::Device</GroupName>
|
||||
<tvExp>0</tvExp>
|
||||
<tvExp>1</tvExp>
|
||||
<tvExpOptDlg>0</tvExpOptDlg>
|
||||
<cbSel>0</cbSel>
|
||||
<RteFlg>1</RteFlg>
|
||||
|
|
|
@ -339,7 +339,7 @@
|
|||
<MiscControls></MiscControls>
|
||||
<Define></Define>
|
||||
<Undefine></Undefine>
|
||||
<IncludePath>.\Include</IncludePath>
|
||||
<IncludePath>..\Drivers\inc;.\inc</IncludePath>
|
||||
</VariousControls>
|
||||
</Cads>
|
||||
<Aads>
|
||||
|
@ -398,6 +398,11 @@
|
|||
<FileType>1</FileType>
|
||||
<FilePath>..\Drivers\src\gpio.c</FilePath>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>timer.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>..\Drivers\src\timer.c</FilePath>
|
||||
</File>
|
||||
</Files>
|
||||
</Group>
|
||||
<Group>
|
||||
|
@ -800,6 +805,11 @@
|
|||
<FileType>1</FileType>
|
||||
<FilePath>..\Drivers\src\gpio.c</FilePath>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>timer.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>..\Drivers\src\timer.c</FilePath>
|
||||
</File>
|
||||
</Files>
|
||||
</Group>
|
||||
<Group>
|
||||
|
|
Loading…
Reference in a new issue