Projet_Voilier/Services/Batterie.c
2025-11-26 21:00:06 +01:00

33 lines
No EOL
979 B
C

#include <stm32f10x.h>
#include <Batterie.h>
#include <GPIO.h>
#include <ADC.h>
#include <USART.h>
float d;
short v;
int p;
void MyBatterie_Init(void){
MyGPIO_Init(GPIOC,4,In_Analog); // Init PORT pour recevoir tension
MyGPIO_Init(GPIOA,2,AltOut_Ppull); // Init PORT UART2 TX pour envoyer pourcentage batterie
MyGPIO_Init(GPIOA,3,In_Floating);
MyADC_Init(ADC1,14); // Init ADC channel 14 pour convertir valeur
MyADC_ActiveIT(ADC1,15,&handler_ADC); // Init interruption quand conversion finie
My_USART_Init(USART2); // Init USART pour envoyer batterie
start_conversion(ADC1); // Premiere conversion
}
void handler_ADC (void){
d = ADC1->DR &~ (0xF << 12); // Retourne valeur numérique
d = d*3.3/4096;
d = d*13*100/12;
p = (int) d;
send_USART(USART2,(p/10) + '0'); // Envoie le chiffre des dizaines
send_USART(USART2,(p%10) + '0'); // Envoie le chiffre des unités
send_USART(USART2,'%');
start_conversion(ADC1); // Recommence la conversion pour le prochain
}