ADC fini avec interruption et UART à tester

This commit is contained in:
Noel Jumin 2023-03-27 16:49:41 +02:00
parent 7bd8d23d41
commit d97f0b4c7a
15 changed files with 216 additions and 79 deletions

View file

@ -10,5 +10,7 @@ typedef struct
} MyADC_Struct_TypeDef;
void MyADC_init(MyADC_Struct_TypeDef* myADC);
void MyADC_start_conversion(ADC_TypeDef* ADC);
void MyADC_ActiveIT (ADC_TypeDef * ADC, char Prio, void (*IT_function)(void));
int MyADC_result(ADC_TypeDef* ADC);
#endif

10
Drivers/Inc/UART.h Normal file
View file

@ -0,0 +1,10 @@
#ifndef MYUART_H
#define MYUART_H
#include "stm32f10x.h"
void MyUART_init(void);
void UART_send(char data); //à revoir
void MyUART_ActiveIT(char Prio, void (*IT_function)(void));
char UART_receive();
#endif

View file

@ -1,26 +1,10 @@
#include "ADC.h"
void (*adc_ptr_func)(void);
void MyADC_init(MyADC_Struct_TypeDef* myADC)
{
MyGPIO_Struct_TypeDef Port_ADC;
RCC->CFGR |= RCC_CFGR_ADCPRE_DIV6;
if(myADC->ADC==ADC1)
{
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(myADC->ADC==ADC2)
{
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
}
switch (myADC->channel)
{
case 0:
@ -120,18 +104,68 @@ void MyADC_init(MyADC_Struct_TypeDef* myADC)
MyGPIO_Init(&Port_ADC);
break;
}
}
int conversion(MyADC_Struct_TypeDef* myADC)
if(myADC->ADC==ADC1)
{
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
RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; // validation horloge ADC1
RCC->CFGR |= RCC_CFGR_ADCPRE_DIV6; // passage de l'horloge ADC1 à 12MHz
ADC1->CR2|= ADC_CR2_ADON; // démarrage ADC1
ADC1->SQR1&= ADC_SQR1_L; // fixe le nombre de conversion à 1
ADC1->SQR3|= myADC->channel; // indique la voie à convertir
ADC1->CR2 |= ADC_CR2_CAL; // dÈbut de la calibration
while ((ADC1->CR2 & ADC_CR2_CAL)); // attente de la fin de la calibration
}
else if(myADC->ADC==ADC2)
{
RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; // validation horloge ADC1
RCC->CFGR |= RCC_CFGR_ADCPRE_DIV6; // passage de l'horloge ADC1 à 12MHz
ADC2->CR2|= ADC_CR2_ADON; // démarrage ADC1
ADC2->SQR1&= ADC_SQR1_L; // fixe le nombre de conversion à 1
ADC2->SQR3|= myADC->channel; // indique la voie à convertir
ADC2->CR2 |= ADC_CR2_CAL; // dÈbut de la calibration
while ((ADC2->CR2 & ADC_CR2_CAL)); // attente de la fin de la calibration
}
}
void MyADC_start_conversion(ADC_TypeDef* ADC)
{
if(ADC==ADC1)
{
ADC1->CR2 |= ADC_CR2_ADON; // lancement de la conversion
}
else if(ADC==ADC2)
{
ADC2->CR2 |= ADC_CR2_ADON; // lancement de la conversion
}
}
void ADC1_2_IRQHandler(void)
{
ADC1->SR &= ~ADC_SR_EOC ;
ADC2->SR &= ~ADC_SR_EOC ;
if(adc_ptr_func!=0)
{
(*adc_ptr_func)();
}
}
void MyADC_ActiveIT (ADC_TypeDef * ADC, char Prio, void (*IT_function)(void))
{
ADC->CR1 |= ADC_CR1_EOCIE;
adc_ptr_func=IT_function;
NVIC->ISER[0] |= 0x01<<ADC1_2_IRQn;
NVIC->IP[ADC1_2_IRQn] |= Prio << 4;
}
int MyADC_result(ADC_TypeDef* ADC)
{
if (ADC==ADC1)
{
return ADC1->DR & ~((0x0F) << 12); // retour de la conversion
}
else if(ADC==ADC2)
{
return ADC2->DR & ~((0x0F) << 12); // retour de la conversion
}
}

View file

@ -5,10 +5,10 @@ debordement stock
fixer val prescaler+ autoreload(equivalent resolution)
demarrage timer => CEN=1*/
void (*ptr1_func)(void);
void (*ptr2_func)(void);
void (*ptr3_func)(void);
void (*ptr4_func)(void);
void (*tim_ptr1_func)(void);
void (*tim_ptr2_func)(void);
void (*tim_ptr3_func)(void);
void (*tim_ptr4_func)(void);
void MyTimer_Base_Init ( MyTimer_Struct_TypeDef * Timer)
{
@ -39,22 +39,22 @@ void MyTimer_ActiveIT (TIM_TypeDef * Timer, char Prio, void (*IT_function)(void)
if(Timer==TIM1)
{
num_tim=TIM1_UP_IRQn;
ptr1_func=IT_function;
tim_ptr1_func=IT_function;
}
else if(Timer==TIM2)
{
num_tim=TIM2_IRQn;
ptr2_func=IT_function;
tim_ptr2_func=IT_function;
}
else if(Timer==TIM3)
{
num_tim=TIM3_IRQn;
ptr3_func=IT_function;
tim_ptr3_func=IT_function;
}
else if(Timer==TIM4)
{
num_tim=TIM4_IRQn;
ptr4_func=IT_function;
tim_ptr4_func=IT_function;
}
NVIC->ISER[0] |= 0x01<<num_tim;
NVIC->IP[num_tim] |= Prio << 4;
@ -63,36 +63,36 @@ void MyTimer_ActiveIT (TIM_TypeDef * Timer, char Prio, void (*IT_function)(void)
void TIM2_IRQHandler (void)
{
if(ptr2_func!=0)
if(tim_ptr2_func!=0)
{
(*ptr2_func)();
(*tim_ptr2_func)();
}
TIM2->SR &= ~(1<<0) ;
}
void TIM3_IRQHandler (void)
{
if(ptr3_func!=0)
if(tim_ptr3_func!=0)
{
(*ptr3_func)();
(*tim_ptr3_func)();
}
TIM3->SR &= ~(1<<0) ;
}
void TIM4_IRQHandler (void)
{
if(ptr4_func!=0)
if(tim_ptr4_func!=0)
{
(*ptr4_func)();
(*tim_ptr4_func)();
}
TIM4->SR &= ~(1<<0) ;
}
void TIM1_UP_IRQHandler (void) // à vérifier
{
if(ptr1_func!=0)
if(tim_ptr1_func!=0)
{
(*ptr1_func)();
(*tim_ptr1_func)();
}
TIM1->SR &= ~(1<<0) ;
}

44
Drivers/Src/UART.c Normal file
View file

@ -0,0 +1,44 @@
#include "UART.h"
void (*uart_ptr_func)(void);
char buffer[1000]={0};
void MyUART_init(void) // que pour du 9600 bauds
{
RCC->APB2ENR |= RCC_APB2ENR_USART1EN; // validation horloge USART1
USART1->CR1 |= USART_CR1_UE; // Activation de l'USART
USART1->CR1 &= ~USART_CR1_M; // Choix d'une taille de 8 bits de données
USART1->CR2 &= USART_CR2_STOP; // Choix d'un seul bit de stop
USART1->BRR |= 468 << 4; // Fixe le baud rate à 9600bps //valeurs trouvé p804
USART1->BRR |= 75; // Fixe le baud rate à 9600bps
USART1->CR1 |= USART_CR1_TE; // Envoi de la première trame d'attente
}
void UART_send(char data)
{
USART1->DR |= data; // Ecriture de la donnée dans le registre DR
while(!(USART1->SR & USART_SR_TC)) {} // Attente de la fin de transmission
}
void MyUART_ActiveIT(char Prio, void (*IT_function)(void))
{
USART1->CR1 |= USART_CR1_RXNEIE;
NVIC->ISER[1] |= 0x01<<(USART1_IRQn%32);
NVIC->IP[USART1_IRQn] |= Prio << 4;
}
void USART1_IRQHandler()
{
USART1->SR &= ~USART_SR_RXNE;
if(uart_ptr_func!=0)
{
(*uart_ptr_func)();
}
}
char UART_receive()
{
char carac;
carac = USART1->DR;
return carac;
}

View file

@ -1,30 +0,0 @@
#include "stm32f10x.h"
#include "GPIO.h"
#include "TIMER.h"
void Mafonction_IT (void);
MyGPIO_Struct_TypeDef PA5;
MyGPIO_Struct_TypeDef PC13;
MyTimer_Struct_TypeDef timer2;
//PA5 LED
//PC13 Bouton
int main ( void )
{
while(1)
{
}
}
void Mafonction_IT (void)
{
MyGPIO_Toggle(PA5.GPIO,5);
}

View file

@ -0,0 +1,38 @@
#include "stm32f10x.h"
#include "GPIO.h"
#include "TIMER.h"
#include "ADC.h"
#include "UART.h"
int val_adc=0;
MyGPIO_Struct_TypeDef PA5; //PA5 LED
MyGPIO_Struct_TypeDef PC13; //PC13 Bouton
MyTimer_Struct_TypeDef timer2;
MyADC_Struct_TypeDef myADC;
void Mafonction_IT (void);
void Mafonction_IT2(void);
int main ( void )
{
myADC.ADC=ADC1;
myADC.channel=2;
MyADC_init(&myADC);
MyADC_ActiveIT(myADC.ADC,0,&Mafonction_IT2);
MyADC_start_conversion(myADC.ADC);
while(1)
{
}
}
void Mafonction_IT (void)
{
MyGPIO_Toggle(PA5.GPIO,5);
}
void Mafonction_IT2 (void)
{
val_adc=MyADC_result(myADC.ADC);
}

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=733,260,1154,687,0)(121=-1,-1,-1,-1,0)(122=1345,565,1766,992,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=1189,292,1783,1043,1)(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=1184,109,1560,666,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=733,260,1154,687,0)(121=-1,-1,-1,-1,0)(122=1345,565,1766,992,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=335,254,929,1005,0)(132=-1,-1,-1,-1,0)(133=-1,-1,-1,-1,0)(160=1126,210,1574,624,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=298,316,901,1067,0)(151=-1,-1,-1,-1,0)</Name>
</SetRegEntry>
<SetRegEntry>
<Number>0</Number>
@ -138,7 +138,24 @@
<Name>UL2CM3(-S0 -C0 -P0 -FD20000000 -FC1000 -FN1 -FF0STM32F10x_128 -FS08000000 -FL020000 -FP0($$Device:STM32F103RB$Flash\STM32F10x_128.FLM))</Name>
</SetRegEntry>
</TargetDriverDllRegistry>
<Breakpoint/>
<Breakpoint>
<Bp>
<Number>0</Number>
<Type>0</Type>
<LineNumber>37</LineNumber>
<EnabledFlag>1</EnabledFlag>
<Address>134218114</Address>
<ByteObject>0</ByteObject>
<HtxType>0</HtxType>
<ManyObjects>0</ManyObjects>
<SizeOfObject>0</SizeOfObject>
<BreakByAccess>0</BreakByAccess>
<BreakIfRCount>1</BreakIfRCount>
<Filename>.\Sources\Principal.c</Filename>
<ExecCommand></ExecCommand>
<Expression>\\TP1\Sources/Principal.c\37</Expression>
</Bp>
</Breakpoint>
<Tracepoint>
<THDelay>0</THDelay>
</Tracepoint>
@ -147,7 +164,7 @@
<periodic>1</periodic>
<aLwin>1</aLwin>
<aCover>0</aCover>
<aSer1>0</aSer1>
<aSer1>1</aSer1>
<aSer2>0</aSer2>
<aPa>0</aPa>
<viewmode>1</viewmode>
@ -442,6 +459,18 @@
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
<File>
<GroupNumber>1</GroupNumber>
<FileNumber>5</FileNumber>
<FileType>1</FileType>
<tvExp>1</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>..\Drivers\Src\UART.c</PathWithFileName>
<FilenameWithoutPath>UART.c</FilenameWithoutPath>
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
</Group>
<Group>

View file

@ -403,6 +403,11 @@
<FileType>1</FileType>
<FilePath>..\Drivers\Src\ADC.c</FilePath>
</File>
<File>
<FileName>UART.c</FileName>
<FileType>1</FileType>
<FilePath>..\Drivers\Src\UART.c</FilePath>
</File>
</Files>
</Group>
<Group>
@ -810,6 +815,11 @@
<FileType>1</FileType>
<FilePath>..\Drivers\Src\ADC.c</FilePath>
</File>
<File>
<FileName>UART.c</FileName>
<FileType>1</FileType>
<FilePath>..\Drivers\Src\UART.c</FilePath>
</File>
</Files>
</Group>
<Group>