diff --git a/Drivers/Inc/ADC.h b/Drivers/Inc/ADC.h index 235b60b..21bf348 100644 --- a/Drivers/Inc/ADC.h +++ b/Drivers/Inc/ADC.h @@ -1,36 +1,14 @@ -#ifndef MYTIMER_H -#define MYTIMER_H +#ifndef MYADC_H +#define MYADC_H #include "stm32f10x.h" +#include "GPIO.h" typedef struct { - TIM_TypeDef * Timer ; // TIM1 à TIM4 - unsigned short ARR; - unsigned short PSC; -}MyTimer_Struct_TypeDef; -/* -***************************************************************************************** -* @brief -* @param -> Paramètre sous forme d’une structure (son adresse) contenant les -informations de base -* @Note -> Fonction à lancer systématiquement avant d’aller plus en détail dans les -conf plus fines (PWM, codeur inc...) -************************************************************************************************* -*/ -void MyTimer_Base_Init (MyTimer_Struct_TypeDef * Timer); -/* -***************************************************************************************** -* @brief -* @param -> - TIM_TypeDef * Timer : Timer concerne - - char Prio : de 0 a 15 -* @Note -> La fonction MyTimer_Base_Init doit avoir ete lancee au prealable -************************************************************************************************* -*/ -void MyTimer_ActiveIT (TIM_TypeDef * Timer, char Prio, void (*IT_function)(void)); - - -#define MyTimer_Base_Start(Timer) (Timer->CR1 |= 0x01) -#define MyTimer_Base_Stop(Timer) (Timer->CR1 &= ~0x01) + ADC_TypeDef * ADC; + char channel; +} MyADC_Struct_TypeDef; +void MyADC_init(MyADC_Struct_TypeDef* myADC); #endif diff --git a/Drivers/Src/ADC.c b/Drivers/Src/ADC.c index 7b9f22b..18adebe 100644 --- a/Drivers/Src/ADC.c +++ b/Drivers/Src/ADC.c @@ -1,92 +1,137 @@ -#include "TIMER.h" +#include "ADC.h" -/*periode_timer=peridoe_horloge*prescaler*resolution -debordement stocké dans registre UIF -fixer val prescaler+ autoreload(equivalent resolution) -demarrage timer => CEN=1*/ - -void (*ptr_func)(void); - -void MyTimer_Base_Init ( MyTimer_Struct_TypeDef * Timer) +void MyADC_init(MyADC_Struct_TypeDef* myADC) { - if(Timer->Timer==TIM1) + MyGPIO_Struct_TypeDef Port_ADC; + RCC->CFGR |= RCC_CFGR_ADCPRE_DIV6; + if(myADC->ADC==ADC1) { - RCC->APB2ENR |= 0x01<<11; + RCC->APB2ENR |= 0x01<<9; + ADC1->CR2|= 0x01; // on active l'adc + ADC1->SQR1&= ~(0x0F<<20);// 1 convertion + ADC2->SQR3&= ~0x1F;// on met 0 la séquance 1 + ADC1->SQR3|= myADC->channel;// on indique le channel a convertir } - else if (Timer->Timer==TIM2) + else if(myADC->ADC==ADC2) { - RCC->APB1ENR |= 0x01; + RCC->APB2ENR |= 0x01<<8; + ADC2->CR2|= 0x01; // on active l'adc + ADC2->SQR1&= ~(0x0F<<20);// 1 convertion + ADC2->SQR3&= ~0x1F;// on met 0 la séquance 1 + ADC2->SQR3|= myADC->channel;// on indique le channel a convertir } - else if (Timer->Timer==TIM3) - { - RCC->APB1ENR |= 0x01<<1; - } - else if (Timer->Timer==TIM4) - { - RCC->APB1ENR |= 0x01<<2; - } - Timer->Timer->ARR=Timer->ARR; - Timer->Timer->PSC=Timer->PSC; -} - -void MyTimer_ActiveIT (TIM_TypeDef * Timer, char Prio, void (*IT_function)(void)) -{ - int num_tim; - ptr_func=IT_function; - Timer->DIER |= 0x01; - if(Timer==TIM1) - { - num_tim=TIM1_UP_IRQn; - } - else if(Timer==TIM2) - { - num_tim=TIM2_IRQn; - } - else if(Timer==TIM3) - { - num_tim=TIM3_IRQn; - } - else if(Timer==TIM4) - { - num_tim=TIM4_IRQn; - } - NVIC->ISER[0] |= 0x01<IP[num_tim] |= Prio << 4; + switch (myADC->channel) + { + case 0: + Port_ADC.GPIO=GPIOA; + Port_ADC.GPIO_Conf=In_Analog; + Port_ADC.GPIO_Pin=0; + MyGPIO_Init(&Port_ADC); + break; + case 1: + Port_ADC.GPIO=GPIOA; + Port_ADC.GPIO_Conf=In_Analog; + Port_ADC.GPIO_Pin=1; + MyGPIO_Init(&Port_ADC); + break; + case 2: + Port_ADC.GPIO=GPIOA; + Port_ADC.GPIO_Conf=In_Analog; + Port_ADC.GPIO_Pin=2; + MyGPIO_Init(&Port_ADC); + break; + case 3: + Port_ADC.GPIO=GPIOA; + Port_ADC.GPIO_Conf=In_Analog; + Port_ADC.GPIO_Pin=3; + MyGPIO_Init(&Port_ADC); + break; + case 4: + Port_ADC.GPIO=GPIOA; + Port_ADC.GPIO_Conf=In_Analog; + Port_ADC.GPIO_Pin=4; + MyGPIO_Init(&Port_ADC); + break; + case 5: + Port_ADC.GPIO=GPIOA; + Port_ADC.GPIO_Conf=In_Analog; + Port_ADC.GPIO_Pin=5; + MyGPIO_Init(&Port_ADC); + break; + case 6: + Port_ADC.GPIO=GPIOA; + Port_ADC.GPIO_Conf=In_Analog; + Port_ADC.GPIO_Pin=6; + MyGPIO_Init(&Port_ADC); + break; + case 7: + Port_ADC.GPIO=GPIOA; + Port_ADC.GPIO_Conf=In_Analog; + Port_ADC.GPIO_Pin=7; + MyGPIO_Init(&Port_ADC); + break; + case 8: + Port_ADC.GPIO=GPIOB; + Port_ADC.GPIO_Conf=In_Analog; + Port_ADC.GPIO_Pin=0; + MyGPIO_Init(&Port_ADC); + break; + case 9: + Port_ADC.GPIO=GPIOB; + Port_ADC.GPIO_Conf=In_Analog; + Port_ADC.GPIO_Pin=1; + MyGPIO_Init(&Port_ADC); + break; + case 10: + Port_ADC.GPIO=GPIOC; + Port_ADC.GPIO_Conf=In_Analog; + Port_ADC.GPIO_Pin=0; + MyGPIO_Init(&Port_ADC); + break; + case 11: + Port_ADC.GPIO=GPIOC; + Port_ADC.GPIO_Conf=In_Analog; + Port_ADC.GPIO_Pin=1; + MyGPIO_Init(&Port_ADC); + break; + case 12: + Port_ADC.GPIO=GPIOC; + Port_ADC.GPIO_Conf=In_Analog; + Port_ADC.GPIO_Pin=2; + MyGPIO_Init(&Port_ADC); + break; + case 13: + Port_ADC.GPIO=GPIOC; + Port_ADC.GPIO_Conf=In_Analog; + Port_ADC.GPIO_Pin=3; + MyGPIO_Init(&Port_ADC); + break; + case 14: + Port_ADC.GPIO=GPIOC; + Port_ADC.GPIO_Conf=In_Analog; + Port_ADC.GPIO_Pin=4; + MyGPIO_Init(&Port_ADC); + break; + case 15: + Port_ADC.GPIO=GPIOC; + Port_ADC.GPIO_Conf=In_Analog; + Port_ADC.GPIO_Pin=5; + MyGPIO_Init(&Port_ADC); + break; + } } -void TIM2_IRQHandler (void) -{ - if(ptr_func!=0) +int conversion(MyADC_Struct_TypeDef* myADC) { - (*ptr_func)(); - } - TIM2->SR &= ~(1<<0) ; + if() + { + + } + ADC1->CR2 |= ADC_CR2_ADON; // lancement de la conversion + While(!(ADC1->SR & ADC_SR_EOC) ) {} // attente de la fin de conversion + ADC1->SR &= ~ADC_SR_EOC; // validation de la conversion + return ADC1->DR & ~((0x0F) << 12); // retour de la conversion } -void TIM3_IRQHandler (void) -{ - if(ptr_func!=0) - { - (*ptr_func)(); - } - TIM3->SR &= ~(1<<0) ; -} -void TIM4_IRQHandler (void) -{ - if(ptr_func!=0) - { - (*ptr_func)(); - } - TIM4->SR &= ~(1<<0) ; -} - -void TIM1_UP_IRQHandler (void) // à vérifier -{ - if(ptr_func!=0) - { - (*ptr_func)(); - } - TIM1->SR &= ~(1<<0) ; -} diff --git a/Drivers/Src/TIMER.c b/Drivers/Src/TIMER.c index 7b9f22b..07c123f 100644 --- a/Drivers/Src/TIMER.c +++ b/Drivers/Src/TIMER.c @@ -5,7 +5,10 @@ debordement stock fixer val prescaler+ autoreload(equivalent resolution) demarrage timer => CEN=1*/ -void (*ptr_func)(void); +void (*ptr1_func)(void); +void (*ptr2_func)(void); +void (*ptr3_func)(void); +void (*ptr4_func)(void); void MyTimer_Base_Init ( MyTimer_Struct_TypeDef * Timer) { @@ -32,23 +35,26 @@ void MyTimer_Base_Init ( MyTimer_Struct_TypeDef * Timer) void MyTimer_ActiveIT (TIM_TypeDef * Timer, char Prio, void (*IT_function)(void)) { int num_tim; - ptr_func=IT_function; Timer->DIER |= 0x01; if(Timer==TIM1) { - num_tim=TIM1_UP_IRQn; + num_tim=TIM1_UP_IRQn; + ptr1_func=IT_function; } else if(Timer==TIM2) { num_tim=TIM2_IRQn; + ptr2_func=IT_function; } else if(Timer==TIM3) { num_tim=TIM3_IRQn; + ptr3_func=IT_function; } else if(Timer==TIM4) { num_tim=TIM4_IRQn; + ptr4_func=IT_function; } NVIC->ISER[0] |= 0x01<IP[num_tim] |= Prio << 4; @@ -57,36 +63,36 @@ void MyTimer_ActiveIT (TIM_TypeDef * Timer, char Prio, void (*IT_function)(void) void TIM2_IRQHandler (void) { - if(ptr_func!=0) + if(ptr2_func!=0) { - (*ptr_func)(); + (*ptr2_func)(); } TIM2->SR &= ~(1<<0) ; } void TIM3_IRQHandler (void) { - if(ptr_func!=0) + if(ptr3_func!=0) { - (*ptr_func)(); + (*ptr3_func)(); } TIM3->SR &= ~(1<<0) ; } void TIM4_IRQHandler (void) { - if(ptr_func!=0) + if(ptr4_func!=0) { - (*ptr_func)(); + (*ptr4_func)(); } TIM4->SR &= ~(1<<0) ; } void TIM1_UP_IRQHandler (void) // à vérifier { - if(ptr_func!=0) + if(ptr1_func!=0) { - (*ptr_func)(); + (*ptr1_func)(); } TIM1->SR &= ~(1<<0) ; } diff --git a/Projet_DevDrivers/Sources/Principal.c b/Projet_DevDrivers/Sources/Principal.c index 1cd5850..84c63ad 100644 --- a/Projet_DevDrivers/Sources/Principal.c +++ b/Projet_DevDrivers/Sources/Principal.c @@ -13,19 +13,11 @@ MyTimer_Struct_TypeDef timer2; int main ( void ) { - PA5.GPIO=GPIOA; - PA5.GPIO_Conf=Out_Ppull; - PA5.GPIO_Pin=5; - MyGPIO_Init(&PA5); - timer2.Timer=TIM2; - timer2.ARR=35999; //pour avoir 500ms - timer2.PSC=1000; - MyTimer_Base_Init(&timer2); - MyTimer_ActiveIT(timer2.Timer,1, &Mafonction_IT); - MyTimer_Base_Start(timer2.Timer); + while(1) { + } } diff --git a/Projet_DevDrivers/TP1.uvoptx b/Projet_DevDrivers/TP1.uvoptx index 6d8b9ef..ece801c 100644 --- a/Projet_DevDrivers/TP1.uvoptx +++ b/Projet_DevDrivers/TP1.uvoptx @@ -430,6 +430,18 @@ 0 0 + + 1 + 4 + 1 + 1 + 0 + 0 + ..\Drivers\Src\ADC.c + ADC.c + 0 + 0 + diff --git a/Projet_DevDrivers/TP1.uvprojx b/Projet_DevDrivers/TP1.uvprojx index dcb8b2b..9e4df6f 100644 --- a/Projet_DevDrivers/TP1.uvprojx +++ b/Projet_DevDrivers/TP1.uvprojx @@ -398,6 +398,11 @@ 1 ..\Drivers\Src\TIMER.c + + ADC.c + 1 + ..\Drivers\Src\ADC.c + @@ -800,6 +805,11 @@ 1 ..\Drivers\Src\TIMER.c + + ADC.c + 1 + ..\Drivers\Src\ADC.c +