ADC Fonctionnel

This commit is contained in:
Clement Marce 2023-04-07 13:16:18 +02:00
parent 8d1127512c
commit 8fc1457802
6 changed files with 106 additions and 115 deletions

View file

@ -11,9 +11,11 @@ typedef struct
} MyADC_Struct_TypeDef;
void MyADC_Base_Init(MyADC_Struct_TypeDef * ADC, void (*fct)(void));
void MyADC_Base_Init(MyADC_Struct_TypeDef * ADC);
void MyADC_Base_Start(ADC_TypeDef * ADC);
void MyADC_Base_Stop(ADC_TypeDef * ADC);
void MyADC_Base_Interuption(ADC_TypeDef * ADC);
int MyADC_Base_Result (MyADC_Struct_TypeDef * ADC);
void MyADC_Init_Periph (void (*fct)(void));
#endif

View file

@ -4,63 +4,57 @@
void (*PtrfctADC)(void); //Déclaration du pointeur de fonction ADC
//----------------------INIT--------------------//
void MyADC_Base_Init(MyADC_Struct_TypeDef * ADC, void (*fct)(void)){
//---------------------INIT-------------------//
void MyADC_Base_Init(MyADC_Struct_TypeDef * ADC){
MyGPIO_Struct_TypeDef * GPIO_ADC; //Déclaration du GPIO de l'ADC
MyGPIO_Struct_TypeDef * GPIO_ADC; //Déclaration du GPIO de l'ADC
PtrfctADC=fct; //Affectation du pointeur de fonction ADC
//Division par 6 de la clock (72MHz) pour l'ADC (14MHz max)
RCC->CFGR |= RCC_CFGR_ADCPRE_DIV6;
RCC->CFGR |= RCC_CFGR_ADCPRE_DIV6; //Division par 6 de la clock (72MHz) pour l'ADC (12MHz)
RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; //Start clock ADC1
//Affectation du channel PC0 pour l'ADC
GPIO_ADC->GPIO = GPIOC;
GPIO_ADC->GPIO = GPIOC; //Initialisation du GPIO de l'ADC
GPIO_ADC->GPIO_Conf = In_Analog;
GPIO_ADC->GPIO_Pin = 0;
MyGPIO_Init(GPIO_ADC);
//Sart et choix de ADC1 ou ADC2
MyADC_Base_Start(ADC->ADC);
MyADC_Base_Interuption(ADC->ADC);
ADC1->SQR1 &= ADC_SQR1_L; //fixe le nombre de conversion à 1
ADC1->SQR3|= ADC->Channel; //indique la voie à convertir
ADC1->CR2 |= ADC_CR2_EXTTRIG; //activation du trigger externe
ADC1->CR2 |= ADC_CR2_EXTSEL; //event externe choisis : SWSTART
MyADC_Base_Start(ADC->ADC); //Sart ADC1 et Horloge ADC1
}
//-------------------------------------------------------------------//
//----------------------------- FONCTIONS ---------------------------//
//-------------------------------------------------------------------//
//---------------------START--------------------//
//--------------------START-------------------//
void MyADC_Base_Start(ADC_TypeDef * ADC){
if(ADC == ADC1){
ADC1->CR2 |= ADC_CR2_ADON; //Enable ADC1
RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; //Clock enable ADC1
} else if (ADC == ADC2){
ADC2->CR2 |= ADC_CR2_ADON; //Enable ADC2
RCC->APB2ENR |= RCC_APB2ENR_ADC2EN; //Clock enable ADC2
}
ADC1->CR2 |= ADC_CR2_ADON;
}
//---------------------STOP--------------------//
void MyADC_Base_Stop(ADC_TypeDef * ADC){
if(ADC == ADC1){
ADC1->CR2 &= ~ADC_CR2_ADON;
}else if(ADC == ADC2){
ADC2->CR2 &= ~ADC_CR2_ADON;
}
}
//-----------------INTERRUPTION-------------//
//------------------INTERRUPTION--------------//
void MyADC_Base_Interuption(ADC_TypeDef * ADC){
ADC->CR1 |= ADC_CR1_EOCIE; //Interruption active
NVIC->ISER[0] |= (0x1<<ADC1_2_IRQn); //Interruption active au niveau du coeur
//GESTION PRIO ??????????????
//Activation du trigger externe
ADC->CR1 |= ADC_CR1_EOCIE; //Interruption de l'ADC autorisée
NVIC->ISER[0] |= (0x1<<ADC1_2_IRQn); //Interruption active au niveau NVIC
NVIC->IP[ADC1_2_IRQn] |= 1<<4; //Affectation du niveau de priorité
}
//--------------------HANDLER---------------//
//--------------------HANDLER-----------------//
void ADC1_2_IRQHandler (void) {
PtrfctADC();
ADC1->SR &= ~(1<<1); //RAZ du flag end of conversion
(*PtrfctADC)(); //Appel de la fonction pointée par le pointeur fonction ADC
MyADC_Base_Start(ADC1);
ADC1->SR &= ~ADC_SR_EOC; //RAZ du flag end of conversion
}
//--------------------DATA--------------------//
int MyADC_Base_Result (MyADC_Struct_TypeDef * ADC){
return ADC1->DR & ~((0x0F)<<12); //Retour de la conversion de l'ADC
}
//-------------------POINTEUR-----------------//
void MyADC_Init_Periph (void (*fct)(void)){
PtrfctADC=fct; //Affectation du pointeur de fonction ADC
}

View file

@ -2,35 +2,47 @@
//---------------------FONCTION D'INITIALISATION-----------------//
void MyGPIO_Init ( MyGPIO_Struct_TypeDef * GPIOStructPtr){
void MyGPIO_Init ( MyGPIO_Struct_TypeDef * GPIOStructPtr )
{
//INITIALISATION DE LA CLOCK CORRESPONDANT AU GPIO A, B ou C
if (GPIOStructPtr->GPIO == GPIOA) {
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
} else if (GPIOStructPtr->GPIO == GPIOB) {
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN;
} else if (GPIOStructPtr->GPIO == GPIOC) {
RCC->APB2ENR |= RCC_APB2ENR_IOPCEN;
} else if (GPIOStructPtr->GPIO == GPIOD) {
RCC->APB2ENR |= RCC_APB2ENR_IOPDEN;
/* Activation of the GPIO port specific clock */
if (GPIOStructPtr->GPIO == GPIOA)
{
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
}
else if (GPIOStructPtr->GPIO == GPIOB)
{
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN;
}
else if (GPIOStructPtr->GPIO == GPIOC)
{
RCC->APB2ENR |= RCC_APB2ENR_IOPCEN;
}
else if (GPIOStructPtr->GPIO == GPIOD)
{
RCC->APB2ENR |= RCC_APB2ENR_IOPDEN;
}
/* Reset, and then configuration writing of the selected GPIO Pin */
if(GPIOStructPtr->GPIO_Pin <= 8)
{
GPIOStructPtr->GPIO->CRL &= ~0xF<<(4*(GPIOStructPtr->GPIO_Pin));
GPIOStructPtr->GPIO->CRL |= (GPIOStructPtr->GPIO_Conf)<<(4*(GPIOStructPtr->GPIO_Pin));
}
else
{
GPIOStructPtr->GPIO->CRH &= ~0xF<<(4*((GPIOStructPtr->GPIO_Pin)%8));
GPIOStructPtr->GPIO->CRH |= (GPIOStructPtr->GPIO_Conf)<<(4*((GPIOStructPtr->GPIO_Pin)%8));
}
//CONFIGURATION DE LA PIN UTILISEE (voir table20 p9.1)
if(GPIOStructPtr->GPIO_Pin < 8){
GPIOStructPtr->GPIO->CRL &= ~(0xF << (4 * GPIOStructPtr->GPIO_Pin));
GPIOStructPtr->GPIO->CRL |= (GPIOStructPtr->GPIO_Conf << (4 * GPIOStructPtr->GPIO_Pin));
if(GPIOStructPtr->GPIO_Conf == (char)In_PullDown)
{
GPIOStructPtr->GPIO->ODR &= ~(0x1<<(GPIOStructPtr->GPIO_Pin));
}
else {
GPIOStructPtr->GPIO->CRH &= ~(0xF << (4 * GPIOStructPtr->GPIO_Pin - 8));
GPIOStructPtr->GPIO->CRH |= (GPIOStructPtr->GPIO_Conf << (4 * GPIOStructPtr->GPIO_Pin - 8));
}
//CONFIGURATION DE L'ODR EN ENTREE (pull up et down uniquement)
if(GPIOStructPtr->GPIO_Conf == In_PullUp){
GPIOStructPtr->GPIO->ODR = 0x1;
}
else if(GPIOStructPtr->GPIO_Conf == In_PullDown){
GPIOStructPtr->GPIO->ODR = 0x0;
else if(GPIOStructPtr->GPIO_Conf == (char)In_PullUp)
{
GPIOStructPtr->GPIO->ODR |= 0x1<<(GPIOStructPtr->GPIO_Pin);
}
}

View file

@ -55,6 +55,6 @@ void MyTimer_PWM( MyTimer_Struct_TypeDef * Timer, uint16_t cycle){
//------------------------HANDLER--------------------//
void TIM2_IRQHandler (void)
{
TIM2->SR &= ~(1<<0);
Ptrfct();
TIM2->SR &= ~(1<<0);
}

View file

@ -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=297,604,718,1031,0)(121=-1,-1,-1,-1,0)(122=546,289,967,716,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=-1,-1,-1,-1,0)(130=-1,-1,-1,-1,0)(131=708,449,1302,1200,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=374,206,750,763,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=419,236,840,663,0)(121=-1,-1,-1,-1,0)(122=546,286,967,713,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=-1,-1,-1,-1,0)(130=-1,-1,-1,-1,0)(131=708,0,1302,751,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=308,316,824,669,0)(231=-1,-1,-1,-1,0)(232=-1,-1,-1,-1,0)(233=-1,-1,-1,-1,0)(150=1008,49,1611,800,0)(151=-1,-1,-1,-1,0)</Name>
</SetRegEntry>
<SetRegEntry>
<Number>0</Number>
@ -147,50 +147,34 @@
<Bp>
<Number>0</Number>
<Type>0</Type>
<LineNumber>9</LineNumber>
<LineNumber>57</LineNumber>
<EnabledFlag>1</EnabledFlag>
<Address>0</Address>
<Address>134219272</Address>
<ByteObject>0</ByteObject>
<HtxType>0</HtxType>
<ManyObjects>0</ManyObjects>
<SizeOfObject>0</SizeOfObject>
<BreakByAccess>0</BreakByAccess>
<BreakIfRCount>0</BreakIfRCount>
<BreakIfRCount>1</BreakIfRCount>
<Filename>..\Drivers\Sources\Driver_Timer.c</Filename>
<ExecCommand></ExecCommand>
<Expression></Expression>
<Expression>\\GPIO_Test\../Drivers/Sources/Driver_Timer.c\57</Expression>
</Bp>
<Bp>
<Number>1</Number>
<Type>0</Type>
<LineNumber>36</LineNumber>
<LineNumber>58</LineNumber>
<EnabledFlag>1</EnabledFlag>
<Address>0</Address>
<Address>134219274</Address>
<ByteObject>0</ByteObject>
<HtxType>0</HtxType>
<ManyObjects>0</ManyObjects>
<SizeOfObject>0</SizeOfObject>
<BreakByAccess>0</BreakByAccess>
<BreakIfRCount>0</BreakIfRCount>
<Filename>.\Sources\Main.c</Filename>
<ExecCommand></ExecCommand>
<Expression></Expression>
</Bp>
<Bp>
<Number>2</Number>
<Type>0</Type>
<LineNumber>59</LineNumber>
<EnabledFlag>1</EnabledFlag>
<Address>0</Address>
<ByteObject>0</ByteObject>
<HtxType>0</HtxType>
<ManyObjects>0</ManyObjects>
<SizeOfObject>0</SizeOfObject>
<BreakByAccess>0</BreakByAccess>
<BreakIfRCount>0</BreakIfRCount>
<BreakIfRCount>1</BreakIfRCount>
<Filename>..\Drivers\Sources\Driver_Timer.c</Filename>
<ExecCommand></ExecCommand>
<Expression></Expression>
<Expression>\\GPIO_Test\../Drivers/Sources/Driver_Timer.c\58</Expression>
</Bp>
</Breakpoint>
<WatchWindow1>
@ -206,7 +190,7 @@
<DebugFlag>
<trace>0</trace>
<periodic>1</periodic>
<aLwin>1</aLwin>
<aLwin>0</aLwin>
<aCover>0</aCover>
<aSer1>0</aSer1>
<aSer2>0</aSer2>
@ -249,12 +233,6 @@
<SecondString>FF000000000000000000000000000000E0FFEF400100000000000000000000000000000028504F5254412026203078303030303030323029203E3E2035000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000001000000000000000000F03F1700000000000000000000000000000000000000DA040008</SecondString>
</Wi>
</LogicAnalyzers>
<SystemViewers>
<Entry>
<Name>System Viewer\GPIOA</Name>
<WinId>35905</WinId>
</Entry>
</SystemViewers>
<DebugDescription>
<Enable>1</Enable>
<EnableFlashSeq>0</EnableFlashSeq>

View file

@ -1,10 +1,15 @@
#include "Driver_GPIO.h"
#include "Driver_Timer.h"
#include "Driver_ADC.h"
void Timer_interup(void);
void ADC_interup(void);
void ADC_interrup(void);
int main (void){
//Déclaration ADC de Test
MyADC_Struct_TypeDef ADCTEST;
//Déclaration d'un Timer 500 ms
MyTimer_Struct_TypeDef TIM500ms;
//Init Timer 2 et Test
@ -14,24 +19,23 @@ int main (void){
MyTimer_Base_Init(&TIM500ms, Timer_interup);
TIM2->DIER |= 1<< 0 ; //INTERRUPTION PERIPH
//TIM2->DIER |= TIM_DIER_UIE ;
TIM2->DIER |= TIM_DIER_UIE ;
NVIC->ISER[0] |= 1<<TIM2_IRQn ; //INTERRUPTION COEUR
NVIC->IP[TIM2_IRQn] = 2<< 4 ;
MyTimer_Base_Start(TIM500ms.Timer);
MyTimer_PWM(&TIM500ms, 50);
//Boucle infinie de la réponse de la LED à l'état BP
while(1){
//TEST ADC//
ADCTEST.ADC = ADC1;
ADCTEST.Channel = 0;
MyADC_Base_Init(&ADCTEST);
MyADC_Base_Interuption(ADCTEST.ADC);
MyADC_Init_Periph(ADC_interrup);
while(1){
MyADC_Base_Start(ADCTEST.ADC);
}
//TEST ADC
}
@ -42,7 +46,8 @@ void Timer_interup(void)
}
//Interruption du programme par trigger de l'ADC
void ADC_interrup(void)
void ADC_interrup()
{
}