From 39df95caf93c617d88a3656471360c20f28664f1 Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Mon, 27 Mar 2023 16:15:35 +0200 Subject: [PATCH] ADC avec Interruption fonctionnel. --- ProjetsKEIL/Drivers/adcdriver.c | 29 ++++++++++---- ProjetsKEIL/Drivers/adcdriver.h | 5 ++- ProjetsKEIL/adc/Source/Principale.c | 31 +++++++++++---- ProjetsKEIL/adc/adc.uvoptx | 62 ++++++++++++++++++++++------- ProjetsKEIL/adc/adc.uvprojx | 36 ++++++++--------- 5 files changed, 110 insertions(+), 53 deletions(-) diff --git a/ProjetsKEIL/Drivers/adcdriver.c b/ProjetsKEIL/Drivers/adcdriver.c index 7a85bed..603eea0 100644 --- a/ProjetsKEIL/Drivers/adcdriver.c +++ b/ProjetsKEIL/Drivers/adcdriver.c @@ -4,17 +4,30 @@ void MyADC_Init(MyADC_Struct_TypeDef * ADCStructPtr) { RCC->CFGR |= RCC_CFGR_ADCPRE_1; // ADC Prescaler : divided by 6 -> 72MHz to 12MHz RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; //We activate the clock first - if(ADCStructPtr->channel < 10) + if(ADCStructPtr->channel < 10) //Cycle and channel selection -> permet de regler en fonction de la résistance donnée { - ADCStructPtr->ADC->SMPR2 |= (ADCStructPtr->resolution<<(ADCStructPtr->channel*3)); // Cycle and channel selection + + ADCStructPtr->ADC->SMPR2 |= (ADCStructPtr->cycle<<(ADCStructPtr->channel*3)); //Channel < 10 } else { - ADCStructPtr->ADC->SMPR1 |= (ADCStructPtr->resolution<<((ADCStructPtr->channel-10)*3)); // Cycle and channel selection + ADCStructPtr->ADC->SMPR1 |= (ADCStructPtr->cycle<<((ADCStructPtr->channel-10)*3)); //Channel > 10 } + ADCStructPtr->ADC->SQR1 &= ADC_SQR1_L; //Channel sequence length -> 1 conversion (0000) ADCStructPtr->ADC->SQR3 |= ADCStructPtr->channel; //Sequence Reader, seulement le SQ1 est lu dans notre cas, nous associons un channel ŕ ce dernier. - ADCStructPtr->ADC->CR2 |= ADC_CR2_ADON; //ADON Pour l'instant -> PLUS TARD //ADC1->CR2 |= (0x1<<30) //Software start conversion - if(ADCStructPtr->isIT) - { - ADCStructPtr->ADC->CR1 |= ADC_CR1_EOCIE; //Interruption - } + ADCStructPtr->ADC->CR2 |= ADC_CR2_EXTTRIG; //Activation du trigger externe + ADCStructPtr->ADC->CR2 |= ADC_CR2_EXTSEL; //Event externe choisie : SWSTART + MyADC_ActiveIT(ADCStructPtr->ADC,0); + ADCStructPtr->ADC->CR2 |= ADC_CR2_ADON; //Init l'ADC +} + +void MyADC_Start (ADC_TypeDef * ADC) +{ + ADC->CR2 |= ADC_CR2_SWSTART; //Start Conversion of regular channels +} + +void MyADC_ActiveIT(ADC_TypeDef * ADC, uint8_t Prio) +{ + ADC->CR1 |= ADC_CR1_EOCIE; //Interruption active + NVIC->IP[ADC1_2_IRQn] |= (Prio << 0x4); //Prio de l'interruption (p.197 manuel reference RM0008 pour ADC1_IRQn) + NVIC->ISER[0] |= (0x1< 17 - MyADC_Cycle_t resolution; - char isIT; //true -> interrupt driven + MyADC_Cycle_t cycle; //channel cycle } MyADC_Struct_TypeDef; void MyADC_Init(MyADC_Struct_TypeDef * ADCStructPtr); +void MyADC_ActiveIT(ADC_TypeDef * ADC, uint8_t Prio); +void MyADC_Start (ADC_TypeDef * ADC); MyGPIO_Struct_TypeDef GPIOFromADC(MyADC_Struct_TypeDef ADC); diff --git a/ProjetsKEIL/adc/Source/Principale.c b/ProjetsKEIL/adc/Source/Principale.c index 25106d0..8110b94 100644 --- a/ProjetsKEIL/adc/Source/Principale.c +++ b/ProjetsKEIL/adc/Source/Principale.c @@ -7,29 +7,44 @@ #define ADON 0 +void ADC1_2_IRQHandler(void) +{ + if(ADC1->DR >= 3102) + { + MyGPIO_Set(GPIOA,5); + } + else{ + MyGPIO_Reset(GPIOA,5); + } + MyADC_Start(ADC1); + ADC1->SR &= ~ADC_SR_EOC; //Prochaine lecture pouvant ętre effectuée. +} + int main (void) { MyGPIO_Struct_TypeDef led = {GPIOA,5,Out_PullUp}; //led - MyADC_Struct_TypeDef adcStruct = {ADC1,10,cycles41d5,0}; + MyADC_Struct_TypeDef adcStruct = {ADC1,10,cycles41d5}; MyGPIO_Struct_TypeDef adc = {GPIOC,0,In_Analog}; - char voltageOverflow = 0; MyGPIO_Init(&led); + MyADC_Init(&adcStruct); MyGPIO_Init(&adc); + MyADC_Start(ADC1); + + + while(1){ - ADC1->CR2 |= (0x1<DR >= 3102) { MyGPIO_Set(GPIOA,5); - voltageOverflow = 1; } else{ - if(!voltageOverflow) - { - MyGPIO_Reset(GPIOA,5); - } + MyGPIO_Reset(GPIOA,5); } + */ }; } diff --git a/ProjetsKEIL/adc/adc.uvoptx b/ProjetsKEIL/adc/adc.uvoptx index 9b71272..2282a8c 100644 --- a/ProjetsKEIL/adc/adc.uvoptx +++ b/ProjetsKEIL/adc/adc.uvoptx @@ -125,7 +125,7 @@ 0 DLGDARM - (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=1468,53,1889,480,1)(121=1469,437,1890,864,1)(122=875,109,1296,536,0)(123=-1,-1,-1,-1,0)(140=-1,-1,-1,-1,0)(240=10,543,409,888,0)(190=-1,-1,-1,-1,0)(200=-1,-1,-1,-1,0)(170=-1,-1,-1,-1,0)(130=-1,-1,-1,-1,0)(131=723,147,1317,898,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=869,137,1472,888,1)(151=-1,-1,-1,-1,0) + (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=1468,53,1889,480,1)(121=1469,437,1890,864,1)(122=875,109,1296,536,0)(123=-1,-1,-1,-1,0)(140=-1,-1,-1,-1,0)(240=10,543,409,888,0)(190=-1,-1,-1,-1,0)(200=-1,-1,-1,-1,0)(170=-1,-1,-1,-1,0)(130=-1,-1,-1,-1,0)(131=723,147,1317,898,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=1219,267,1822,1018,1)(151=-1,-1,-1,-1,0) 0 @@ -142,9 +142,9 @@ 0 0 - 29 + 31 1 -
134218844
+
134219116
0 0 0 @@ -153,14 +153,14 @@ 1 .\Source\Principale.c - \\cool_Simule\Source/Principale.c\29 + \\cool_Simule\Source/Principale.c\31
1 0 - 23 + 14 1 -
134218838
+
134218074
0 0 0 @@ -169,14 +169,14 @@ 1 .\Source\Principale.c - \\cool_Simule\Source/Principale.c\23 + \\cool_Simule\Source/Principale.c\14
2 0 - 22 + 12 1 -
134218830
+
134218062
0 0 0 @@ -185,7 +185,39 @@ 1 .\Source\Principale.c - \\cool_Simule\Source/Principale.c\22 + \\cool_Simule\Source/Principale.c\12 +
+ + 3 + 0 + 16 + 1 +
134218082
+ 0 + 0 + 0 + 0 + 0 + 1 + .\Source\Principale.c + + \\cool_Simule\Source/Principale.c\16 +
+ + 4 + 0 + 17 + 1 +
134218090
+ 0 + 0 + 0 + 0 + 0 + 1 + .\Source\Principale.c + + \\cool_Simule\Source/Principale.c\17
@@ -208,7 +240,7 @@ 1 1 0 - 0 + 1 0 0 1 @@ -361,7 +393,7 @@ 0 ST-LINKIII-KEIL_SWO - -U066FFF504955857567155843 -O206 -SF10000 -C0 -A0 -I0 -HNlocalhost -HP7184 -P1 -N00("ARM CoreSight SW-DP (ARM Core") -D00(1BA01477) -L00(0) -TO131090 -TC10000000 -TT10000000 -TP21 -TDS8000 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -FO7 -FD20000000 -FC1000 -FN1 -FF0STM32F10x_128.FLM -FS08000000 -FL020000 -FP0($$Device:STM32F103RB$Flash\STM32F10x_128.FLM) -WA0 -WE0 -WVCE4 -WS2710 -WM0 -WP2 + -U066FFF504955857567155843 -O206 -SF10000 -C0 -A0 -I0 -HNlocalhost -HP7184 -P1 -N00("ARM CoreSight SW-DP (ARM Core") -D00(1BA01477) -L00(0) -TO131090 -TC10000000 -TT10000000 -TP21 -TDS8000 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -FO7 -FD20000000 -FC1000 -FN1 -FF0STM32F10x_128.FLM -FS08000000 -FL020000 -FP0($$Device:STM32F103RB$Flash\STM32F10x_128.FLM) 0 @@ -405,7 +437,7 @@ 1 1 0 - 0 + 1 0 0 1 @@ -485,7 +517,7 @@ 2 2 1 - 1 + 0 0 0 ..\Drivers\gpiodriver.c @@ -509,7 +541,7 @@ 2 4 1 - 0 + 1 0 0 ..\Drivers\adcdriver.c diff --git a/ProjetsKEIL/adc/adc.uvprojx b/ProjetsKEIL/adc/adc.uvprojx index 7a718a6..e7ebe7a 100644 --- a/ProjetsKEIL/adc/adc.uvprojx +++ b/ProjetsKEIL/adc/adc.uvprojx @@ -10,14 +10,13 @@ Simulé 0x4 ARM-ADS - 6190000::V6.19::ARMCLANG - 6190000::V6.19::ARMCLANG - 1 + 5060960::V5.06 update 7 (build 960)::.\ARMCC + 0 STM32F103RB STMicroelectronics - Keil.STM32F1xx_DFP.2.4.0 + Keil.STM32F1xx_DFP.2.3.0 http://www.keil.com/pack/ IRAM(0x20000000,0x00005000) IROM(0x08000000,0x00020000) CPUTYPE("Cortex-M3") CLOCK(12000000) ELITTLE @@ -187,7 +186,6 @@ 0 0 0 - 0 0 0 8 @@ -315,7 +313,7 @@ 1 - 2 + 1 0 0 1 @@ -324,7 +322,7 @@ 0 0 0 - 3 + 2 0 0 0 @@ -424,14 +422,13 @@ Réel 0x4 ARM-ADS - 6190000::V6.19::ARMCLANG - 6190000::V6.19::ARMCLANG - 1 + 5060960::V5.06 update 7 (build 960)::.\ARMCC + 0 STM32F103RB STMicroelectronics - Keil.STM32F1xx_DFP.2.4.0 + Keil.STM32F1xx_DFP.2.3.0 http://www.keil.com/pack/ IRAM(0x20000000,0x00005000) IROM(0x08000000,0x00020000) CPUTYPE("Cortex-M3") CLOCK(12000000) ELITTLE @@ -601,7 +598,6 @@ 0 0 0 - 0 0 0 8 @@ -729,7 +725,7 @@ 1 - 2 + 1 0 0 1 @@ -738,7 +734,7 @@ 0 0 0 - 3 + 2 0 0 0 @@ -839,15 +835,15 @@ - - + + - + @@ -858,7 +854,7 @@ RTE\Device\STM32F103RB\RTE_Device.h - + @@ -867,7 +863,7 @@ RTE\Device\STM32F103RB\startup_stm32f10x_md.s - + @@ -876,7 +872,7 @@ RTE\Device\STM32F103RB\system_stm32f10x.c - +