diff --git a/keil_project/MDK-ARM/Project.uvoptx b/keil_project/MDK-ARM/Project.uvoptx index a4f5a25..58b97a9 100644 --- a/keil_project/MDK-ARM/Project.uvoptx +++ b/keil_project/MDK-ARM/Project.uvoptx @@ -568,42 +568,6 @@ 2 2 1 - 1 - 0 - 0 - ..\Services\Chrono.c - Chrono.c - 0 - 0 - - - 2 - 3 - 1 - 0 - 0 - 0 - ..\Services\alimentation.c - alimentation.c - 0 - 0 - - - 2 - 4 - 1 - 0 - 0 - 0 - ..\Services\accelerometer.c - accelerometer.c - 0 - 0 - - - 2 - 5 - 1 0 0 0 @@ -614,19 +578,7 @@ 2 - 6 - 1 - 0 - 0 - 0 - ..\Services\emetteur_rf.c - emetteur_rf.c - 0 - 0 - - - 2 - 7 + 3 1 0 0 @@ -638,7 +590,7 @@ 2 - 8 + 4 1 0 0 @@ -650,7 +602,7 @@ 2 - 9 + 5 1 0 0 @@ -662,7 +614,7 @@ 2 - 10 + 6 1 0 0 @@ -672,6 +624,42 @@ 0 0 + + 2 + 7 + 1 + 0 + 0 + 0 + ..\Services\Accelerometer.c + Accelerometer.c + 0 + 0 + + + 2 + 8 + 1 + 0 + 0 + 0 + ..\Services\Alimentation.c + Alimentation.c + 0 + 0 + + + 2 + 9 + 1 + 0 + 0 + 0 + ..\Services\RFOutput.c + RFOutput.c + 0 + 0 + @@ -680,18 +668,6 @@ 0 0 0 - - 3 - 11 - 1 - 0 - 0 - 0 - ..\MyDrivers\MyTimer.c - MyTimer.c - 0 - 0 - @@ -702,7 +678,7 @@ 0 4 - 12 + 10 1 0 0 @@ -714,7 +690,7 @@ 4 - 13 + 11 1 0 0 @@ -726,7 +702,7 @@ 4 - 14 + 12 1 0 0 @@ -738,7 +714,7 @@ 4 - 15 + 13 1 0 0 @@ -750,7 +726,7 @@ 4 - 16 + 14 1 0 0 @@ -762,7 +738,7 @@ 4 - 17 + 15 1 0 0 @@ -774,7 +750,7 @@ 4 - 18 + 16 1 0 0 @@ -794,7 +770,7 @@ 0 5 - 19 + 17 5 0 0 @@ -814,7 +790,7 @@ 0 6 - 20 + 18 1 0 0 @@ -834,7 +810,7 @@ 0 7 - 21 + 19 2 0 0 diff --git a/keil_project/MDK-ARM/Project.uvprojx b/keil_project/MDK-ARM/Project.uvprojx index 4c9116f..aea7bbf 100644 --- a/keil_project/MDK-ARM/Project.uvprojx +++ b/keil_project/MDK-ARM/Project.uvprojx @@ -185,6 +185,7 @@ 0 0 0 + 0 0 0 8 @@ -351,7 +352,7 @@ 0 0 0 - 0 + 4 @@ -392,31 +393,11 @@ User Services - - Chrono.c - 1 - ..\Services\Chrono.c - - - alimentation.c - 1 - ..\Services\alimentation.c - - - accelerometer.c - 1 - ..\Services\accelerometer.c - DcMotor.c 1 ..\Services\DcMotor.c - - emetteur_rf.c - 1 - ..\Services\emetteur_rf.c - IncrEncoder.c 1 @@ -437,17 +418,25 @@ 1 ..\Services\Servo.c + + Accelerometer.c + 1 + ..\Services\Accelerometer.c + + + Alimentation.c + 1 + ..\Services\Alimentation.c + + + RFOutput.c + 1 + ..\Services\RFOutput.c + MyDrivers - - - MyTimer.c - 1 - ..\MyDrivers\MyTimer.c - - Drivers/STM32f1xx_LL_Driver @@ -703,6 +692,7 @@ 0 0 0 + 0 0 0 8 @@ -869,7 +859,7 @@ 0 0 0 - 0 + 4 @@ -910,31 +900,11 @@ User Services - - Chrono.c - 1 - ..\Services\Chrono.c - - - alimentation.c - 1 - ..\Services\alimentation.c - - - accelerometer.c - 1 - ..\Services\accelerometer.c - DcMotor.c 1 ..\Services\DcMotor.c - - emetteur_rf.c - 1 - ..\Services\emetteur_rf.c - IncrEncoder.c 1 @@ -955,17 +925,25 @@ 1 ..\Services\Servo.c + + Accelerometer.c + 1 + ..\Services\Accelerometer.c + + + Alimentation.c + 1 + ..\Services\Alimentation.c + + + RFOutput.c + 1 + ..\Services\RFOutput.c + MyDrivers - - - MyTimer.c - 1 - ..\MyDrivers\MyTimer.c - - Drivers/STM32f1xx_LL_Driver @@ -1058,4 +1036,19 @@ + + + + <Project Info> + + + + + + 0 + 1 + + + + diff --git a/keil_project/MyDrivers/MyTimer.c b/keil_project/MyDrivers/MyTimer.c deleted file mode 100644 index e51d696..0000000 --- a/keil_project/MyDrivers/MyTimer.c +++ /dev/null @@ -1,182 +0,0 @@ -// TOUT A FAIRE !! // - -/* - indispensable pour pouvoir adresser les registres des périphériques. - Rem : OBLIGATION d'utiliser les définitions utiles contenues dans ce fichier (ex : TIM_CR1_CEN, RCC_APB1ENR_TIM2EN ...) - pour une meilleure lisibilité du code. - - Pour les masques, utiliser également les définitions proposée - Rappel : pour mettre à 1 , reg = reg | Mask (ou Mask est le représente le ou les bits à positionner à 1) - pour mettre à 0 , reg = reg&~ Mask (ou Mask est le représente le ou les bits à positionner à 0) - -*/ - -#include "MyTimer.h" -#include "stm32f1xx_ll_bus.h" // Pour l'activation des horloges -#include "stm32f1xx_ll_tim.h" - - -// variable pointeur de fonction permettant de mémoriser le callback à appeler depuis -// le handler d'IT -void (*Ptr_ItFct_TIM1)(void); -void (*Ptr_ItFct_TIM2)(void); -void (*Ptr_ItFct_TIM3)(void); -void (*Ptr_ItFct_TIM4)(void); - - - - -/** - * @brief Active l'horloge et règle l'ARR et le PSC du timer visé - * @note Fonction à lancer avant toute autre. Le timer n'est pas encore lancé (voir MyTimerStart) - * @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4 - * int Arr : valeur à placer dans ARR - * int Psc : valeur à placer dans PSC - * @retval None - */ -void MyTimer_Conf(TIM_TypeDef * Timer,int Arr, int Psc) -{ - LL_TIM_InitTypeDef My_LL_Tim_Init_Struct; - - // Validation horloge locale - if (Timer==TIM1) LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM1); - else if (Timer==TIM2) LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2); - else if (Timer==TIM3) LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM3); - else LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM4); - - // chargement structure Arr, Psc, Up Count - My_LL_Tim_Init_Struct.Autoreload=Arr; - My_LL_Tim_Init_Struct.Prescaler=Psc; - My_LL_Tim_Init_Struct.ClockDivision=LL_TIM_CLOCKDIVISION_DIV1; - My_LL_Tim_Init_Struct.CounterMode=LL_TIM_COUNTERMODE_UP; - My_LL_Tim_Init_Struct.RepetitionCounter=0; - - LL_TIM_Init(Timer,&My_LL_Tim_Init_Struct); - - - // Blocage IT - LL_TIM_DisableIT_UPDATE(Timer); - - - // Blocage Timer - LL_TIM_DisableCounter(Timer); - - - -} - - -/** - * @brief Démarre le timer considéré - * @note - * @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4 - * @retval None - */ -void MyTimer_Start(TIM_TypeDef * Timer) -{ - LL_TIM_EnableCounter(Timer); -} - -/** - * @brief Arrêt le timer considéré - * @note - * @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4 - * @retval None - */ -void MyTimer_Stop(TIM_TypeDef * Timer) -{ - LL_TIM_DisableCounter(Timer); -} - - -/** - * @brief Configure le Timer considéré en interruption sur débordement. - * @note A ce stade, les interruptions ne sont pas validés (voir MyTimer_IT_Enable ) - * @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4 - * void (*IT_function) (void) : nom (adresse) de la fonction à lancer sur interruption - * int Prio : priorité associée à l'interruption - * @retval None - */ -void MyTimer_IT_Conf(TIM_TypeDef * Timer, void (*IT_function) (void),int Prio) -{ - // affectation de la fonction - if (Timer==TIM1) Ptr_ItFct_TIM1=IT_function; - else if (Timer==TIM2) Ptr_ItFct_TIM2=IT_function; - else if (Timer==TIM3) Ptr_ItFct_TIM3=IT_function; - else Ptr_ItFct_TIM4=IT_function; - - - // Blocage IT (il faudra la débloquer voir fct suivante) - LL_TIM_DisableIT_UPDATE(Timer); - - // validation du canal NVIC - IRQn_Type TIM_irq; - - if (Timer==TIM1) TIM_irq=TIM1_UP_IRQn; - else if (Timer==TIM2) TIM_irq=TIM2_IRQn; - else if (Timer==TIM3) TIM_irq=TIM3_IRQn; - else TIM_irq=TIM4_IRQn; - - NVIC_SetPriority(TIM_irq, Prio); - NVIC_EnableIRQ(TIM_irq); - - -} - - -/** - * @brief Autorise les interruptions - * @note - * @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4 - * @retval None - */ -void MyTimer_IT_Enable(TIM_TypeDef * Timer) -{ - LL_TIM_EnableIT_UPDATE(Timer); -} - - -/** - * @brief Interdit les interruptions - * @note - * @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4 - * @retval None - */ -void MyTimer_IT_Disable(TIM_TypeDef * Timer) -{ - LL_TIM_DisableIT_UPDATE(Timer); -} - - -/* -============ LES INTERRUPTIONS ================================= - -*/ - -void TIM1_UP_IRQHandler(void) -{ - // rabaisser le flag d'IT - LL_TIM_ClearFlag_UPDATE(TIM1); - (*Ptr_ItFct_TIM1)(); -} - -void TIM2_IRQHandler(void) -{ - // rabaisser le flag d'IT - LL_TIM_ClearFlag_UPDATE(TIM2); - (*Ptr_ItFct_TIM2)(); -} - -void TIM3_IRQHandler(void) -{ - // rabaisser le flag d'IT - LL_TIM_ClearFlag_UPDATE(TIM3); - (*Ptr_ItFct_TIM3)(); -} - -void TIM4_IRQHandler(void) -{ - // rabaisser le flag d'IT - LL_TIM_ClearFlag_UPDATE(TIM4); - (*Ptr_ItFct_TIM4)(); -} diff --git a/keil_project/MyDrivers/MyTimer.h b/keil_project/MyDrivers/MyTimer.h deleted file mode 100644 index f2fa06c..0000000 --- a/keil_project/MyDrivers/MyTimer.h +++ /dev/null @@ -1,70 +0,0 @@ -// RIEN A MODIFIER // - -#ifndef MY_TIMER_H -#define MY_TIMER_H - -/* -Driver pour Timer 1 à 4 du STM32F103RB - -*/ - -#include "stm32f103xb.h" - -/** - * @brief Active l'horloge et règle l'ARR et le PSC du timer visé - * @note Fonction à lancer avant toute autre. Le timer n'est pas encore lancé (voir MyTimerStart) - * @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4 - * int Arr : valeur à placer dans ARR - * int Psc : valeur à placer dans PSC - * @retval None - */ -void MyTimer_Conf(TIM_TypeDef * Timer,int Arr, int Psc); - - -/** - * @brief Démarre le timer considéré - * @note - * @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4 - * @retval None - */ -void MyTimer_Start(TIM_TypeDef * Timer); - - -/** - * @brief Arrêt le timer considéré - * @note - * @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4 - * @retval None - */ -void MyTimer_Stop(TIM_TypeDef * Timer); - - -/** - * @brief Configure le Timer considéré en interruption sur débordement. - * @note A ce stade, les interruptions ne sont pas validés (voir MyTimer_IT_Enable ) - * @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4 - * void (*IT_function) (void) : nom (adresse) de la fonction à lancer sur interruption - * int Prio : priorité associée à l'interruption - * @retval None - */ -void MyTimer_IT_Conf(TIM_TypeDef * Timer, void (*IT_function) (void),int Prio); - - -/** - * @brief Autorise les interruptions - * @note - * @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4 - * @retval None - */ -void MyTimer_IT_Enable(TIM_TypeDef * Timer); - - -/** - * @brief Interdit les interruptions - * @note - * @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4 - * @retval None - */ -void MyTimer_IT_Disable(TIM_TypeDef * Timer); - -#endif diff --git a/keil_project/MyDrivers/MyUart.c b/keil_project/MyDrivers/MyUart.c deleted file mode 100644 index b907c68..0000000 --- a/keil_project/MyDrivers/MyUart.c +++ /dev/null @@ -1,67 +0,0 @@ -#include "MyUart.h" -#include "stm32f1xx_ll_bus.h" -#include "stm32f1xx_ll_usart.h" -#include "stm32f1xx_ll_gpio.h" - -void MyUart_Conf(USART_TypeDef * uart_port, int baudrate){ - - LL_USART_InitTypeDef My_LL_Usart_Init_Struct; - - if (uart_port==USART1) { - LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART1); - LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA); - LL_GPIO_InitTypeDef tx; - tx.Mode = LL_GPIO_MODE_ALTERNATE; - tx.OutputType = LL_GPIO_OUTPUT_PUSHPULL; - tx.Speed = LL_GPIO_SPEED_FREQ_LOW; - tx.Pin = LL_GPIO_PIN_9; - LL_GPIO_Init(GPIOA, &tx); - } - if (uart_port==USART2){ - LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_USART2); - LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA); - LL_GPIO_InitTypeDef tx; - tx.Mode = LL_GPIO_MODE_ALTERNATE; - tx.OutputType = LL_GPIO_OUTPUT_PUSHPULL; - tx.Pull = LL_GPIO_PULL_UP; - tx.Speed = LL_GPIO_SPEED_FREQ_LOW; - tx.Pin = LL_GPIO_PIN_2; - LL_GPIO_Init(GPIOA, &tx); - } - if (uart_port==USART3){ - LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_USART3); - LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOC); - LL_GPIO_InitTypeDef tx; - tx.Mode = LL_GPIO_MODE_ALTERNATE; - tx.OutputType = LL_GPIO_OUTPUT_PUSHPULL; - tx.Pull = LL_GPIO_PULL_UP; - tx.Speed = LL_GPIO_SPEED_FREQ_LOW; - tx.Pin = LL_GPIO_PIN_10; - LL_GPIO_Init(GPIOC, &tx); - } - - My_LL_Usart_Init_Struct.BaudRate = baudrate; - My_LL_Usart_Init_Struct.DataWidth = LL_USART_DATAWIDTH_8B ; - My_LL_Usart_Init_Struct.HardwareFlowControl = LL_USART_HWCONTROL_NONE; - My_LL_Usart_Init_Struct.OverSampling = LL_USART_OVERSAMPLING_16; - My_LL_Usart_Init_Struct.Parity = LL_USART_PARITY_NONE; - My_LL_Usart_Init_Struct.StopBits = LL_USART_STOPBITS_1; - My_LL_Usart_Init_Struct.TransferDirection = LL_USART_DIRECTION_TX_RX ; - - LL_USART_Init(uart_port,&My_LL_Usart_Init_Struct); - LL_USART_Enable(uart_port); - - /*int periph_speed; - if (uart_port==USART1) periph_speed = 36000000; - if (uart_port==USART2) periph_speed = 72000000; - if (uart_port==USART3) periph_speed = 72000000; - - LL_USART_SetBaudRate(uart_port, periph_speed, baudrate); -*/} - -void MyUart_send_bytes(USART_TypeDef * uart_port,char* buf, int len){ - for(int i = 0; i < len; i++){ - LL_USART_TransmitData8(uart_port, buf[i]); - while(!LL_USART_IsActiveFlag_TXE(uart_port)); - } -} diff --git a/keil_project/MyDrivers/MyUart.h b/keil_project/MyDrivers/MyUart.h deleted file mode 100644 index 6641c92..0000000 --- a/keil_project/MyDrivers/MyUart.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef MYUART_H -#define MYUART_H - -#include "stm32f1xx_ll_usart.h" - -void MyUart_Conf(USART_TypeDef * uart_port, int baudrate); - -void MyUart_send_bytes(USART_TypeDef * uart_port,char* buf, int len); - -#endif \ No newline at end of file diff --git a/keil_project/Services/accelerometer.c b/keil_project/Services/Accelerometer.c similarity index 69% rename from keil_project/Services/accelerometer.c rename to keil_project/Services/Accelerometer.c index 0e9be97..ac4bcb7 100644 --- a/keil_project/Services/accelerometer.c +++ b/keil_project/Services/Accelerometer.c @@ -1,9 +1,6 @@ -#include "accelerometer.h" -double x; -double y; -double angle; +#include "Accelerometer.h" -void accelero_init(void){ +void ACCELEROMETER_Init(void){ RCC -> CFGR |= (0x1<<15); RCC-> CFGR &= ~ (0x1<<14); LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_ADC1); @@ -23,7 +20,6 @@ void accelero_init(void){ pc1.Mode = LL_GPIO_MODE_ANALOG; LL_GPIO_Init(GPIOA, &pc1); - adc.DataAlignment = LL_ADC_DATA_ALIGN_RIGHT; adc.SequencersScanMode = LL_ADC_SEQ_SCAN_DISABLE; @@ -35,55 +31,37 @@ void accelero_init(void){ adcReg.ContinuousMode = LL_ADC_REG_CONV_SINGLE; adcReg.DMATransfer = LL_ADC_REG_DMA_TRANSFER_NONE; - LL_ADC_REG_Init(ADC1, &adcReg); - - /*LL_ADC_SetChannelSamplingTime(ADC2, LL_ADC_CHANNEL_10, LL_ADC_SAMPLINGTIME_1CYCLE_5); - LL_ADC_SetChannelSamplingTime(ADC2, LL_ADC_CHANNEL_11, LL_ADC_SAMPLINGTIME_1CYCLE_5); - */ - LL_ADC_Enable(ADC1); - - //LL_ADC_EnableIT_EOS(ADC1); - //wait 0,2 µs, calibration is advised - //LL_ADC_StartCalibration(ADC1); } - -double accelero_get_x(void){ +double ACCELEROMETER_GetX(void){ LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_1, LL_ADC_CHANNEL_1); LL_ADC_REG_StartConversionSWStart(ADC1); - while (LL_ADC_IsActiveFlag_EOS(ADC1) != 1){ - //__asm__"nope"; - } + while (LL_ADC_IsActiveFlag_EOS(ADC1) != 1); double x= LL_ADC_REG_ReadConversionData12(ADC1); return x; } -double accelero_get_y(void){ +double ACCELEROMETER_GetY(void){ LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_1, LL_ADC_CHANNEL_11); LL_ADC_REG_StartConversionSWStart(ADC1); - while (LL_ADC_IsActiveFlag_EOS(ADC1) != 1){ - //__asm__"nope"; - } + while (LL_ADC_IsActiveFlag_EOS(ADC1) != 1); double y = LL_ADC_REG_ReadConversionData12(ADC1); return y; } - -int accelero_angle_bon(void){ - x = accelero_get_x(); - y = accelero_get_y(); - angle = x/y; +int ACCELEROMETER_AngleGood(void){ + double x = ACCELEROMETER_GetX(); + double y = ACCELEROMETER_GetY(); + double angle = x/y; if (angle>tan(0.698132)){ return 0; }else { return 1; } - //le flag EOC n'est jamais mis à un .... // Soit la conversion est mal faite soit on n'utilise pas bien la simulation //soit on n'utilise pas bien isActiveFlag dans la boucle } - diff --git a/keil_project/Services/accelerometer.h b/keil_project/Services/Accelerometer.h similarity index 80% rename from keil_project/Services/accelerometer.h rename to keil_project/Services/Accelerometer.h index 8389738..d05ce28 100644 --- a/keil_project/Services/accelerometer.h +++ b/keil_project/Services/Accelerometer.h @@ -18,13 +18,13 @@ Les fonctions qui gèrent les IO (ajout par rapport à l'activité 1) =======================================================================================*/ -void accelero_init(void); +void ACCELEROMETER_Init(void); -double accelero_get_x(void); +double ACCELEROMETER_GetX(void); -double accelero_get_y(void); +double ACCELEROMETER_GetY(void); -int accelero_angle_bon(void); +int ACCELEROMETER_AngleGood(void); #endif diff --git a/keil_project/Services/Alimentation.c b/keil_project/Services/Alimentation.c new file mode 100644 index 0000000..3e4d704 --- /dev/null +++ b/keil_project/Services/Alimentation.c @@ -0,0 +1,53 @@ +#include "Alimentation.h" + + +void ALIMENTATION_Init(void){ + RCC -> CFGR |= (0x1<<15); + RCC-> CFGR &= ~ (0x1<<14); + LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_ADC1); + LL_APB1_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOC); + + LL_GPIO_InitTypeDef pc2; + LL_ADC_InitTypeDef adc; + LL_ADC_REG_InitTypeDef adcReg; + + LL_GPIO_StructInit(&pc2); + pc2.Pin = LL_GPIO_PIN_0; + pc2.Mode = LL_GPIO_MODE_ANALOG; + LL_GPIO_Init(GPIOC, &pc2); + + + adc.DataAlignment = LL_ADC_DATA_ALIGN_RIGHT; + adc.SequencersScanMode = LL_ADC_SEQ_SCAN_DISABLE; + LL_ADC_Init(ADC1, &adc); + + adcReg.TriggerSource = LL_ADC_REG_TRIG_SOFTWARE ; + adcReg.SequencerLength = LL_ADC_REG_SEQ_SCAN_DISABLE; + adcReg.SequencerDiscont = LL_ADC_REG_SEQ_DISCONT_DISABLE; + adcReg.ContinuousMode = LL_ADC_REG_CONV_SINGLE; + adcReg.DMATransfer = LL_ADC_REG_DMA_TRANSFER_NONE; + + + LL_ADC_REG_Init(ADC1, &adcReg); + LL_ADC_Enable(ADC1); +} + +float ALIMENTATION_GetBatteryLevel(void){ + float u2; + float battery_level; + LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_1, LL_ADC_CHANNEL_12); + LL_ADC_REG_StartConversionSWStart(ADC1); + while (LL_ADC_IsActiveFlag_EOS(ADC1) != 1); + battery_level = LL_ADC_REG_ReadConversionData12(ADC1); + u2 = (battery_level*3.3)/4095.0; + float level = u2*13.0; + return level; +} + +int ALIMENTATION_IsLevelEnough(void){ + if (ALIMENTATION_GetBatteryLevel()<(0.8*12)){ + return 0; + }else{ + return 1; + } +} diff --git a/keil_project/Services/alimentation.h b/keil_project/Services/Alimentation.h similarity index 83% rename from keil_project/Services/alimentation.h rename to keil_project/Services/Alimentation.h index 503541f..7effb3d 100644 --- a/keil_project/Services/alimentation.h +++ b/keil_project/Services/Alimentation.h @@ -18,9 +18,9 @@ Les fonctions qui gèrent les IO (ajout par rapport à l'activité 1) =======================================================================================*/ -void alimentation_init(void); -float get_battery_level(void); -int is_level_enough(void); +void ALIMENTATION_Init(void); +float ALIMENTATION_GetBatteryLevel(void); +int ALIMENTATION_IsLevelEnough(void); #endif diff --git a/keil_project/Services/Chrono.c b/keil_project/Services/Chrono.c deleted file mode 100644 index a77a4df..0000000 --- a/keil_project/Services/Chrono.c +++ /dev/null @@ -1,182 +0,0 @@ -// A COMPLETER - -/* -Service permettant de chornométrer jusqu'à 59mn 59s 99 1/100 -Utilise un timer au choix (TIMER1 à TIMER4). -Utilise la lib MyTimers.h /.c -*/ - - - -#include "Chrono.h" -#include "MyTimer.h" -#include "stm32f1xx_ll_gpio.h" -#include "stm32f1xx_ll_bus.h" - -// variable privée de type Time qui mémorise la durée mesurée -static Time Chrono_Time; // rem : static rend la visibilité de la variable Chrono_Time limitée à ce fichier - -// variable privée qui mémorise pour le module le timer utilisé par le module -static TIM_TypeDef * Chrono_Timer=TIM1; // init par défaut au cas où l'utilisateur ne lance pas Chrono_Conf avant toute autre fct. - -// déclaration callback appelé toute les 10ms -void Chrono_Task_10ms(void); - -int running = 0; - -/** - * @brief Configure le chronomètre. - * @note A lancer avant toute autre fonction. - * @param Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4 - * @retval None - */ -void Chrono_Conf(TIM_TypeDef * Timer) -{ - // Reset Time - Chrono_Time.Hund=0; - Chrono_Time.Sec=0; - Chrono_Time.Min=0; - - // Fixation du Timer - Chrono_Timer=Timer; - - Chrono_Conf_io(); - // Réglage Timer pour un débordement à 10ms - MyTimer_Conf(Chrono_Timer,999, 719); - - // Réglage interruption du Timer avec callback : Chrono_Task_10ms() - MyTimer_IT_Conf(Chrono_Timer, Chrono_Task_10ms,3); - - // Validation IT - MyTimer_IT_Enable(Chrono_Timer); - - -} - - -/** - * @brief Démarre le chronomètre. - * @note si la durée dépasse 59mn 59sec 99 Hund, elle est remise à zéro et repart - * @param Aucun - * @retval Aucun - */ -void Chrono_Start(void) -{ - MyTimer_Start(Chrono_Timer); - running = 1; -} - - -/** - * @brief Arrête le chronomètre. - * @note - * @param Aucun - * @retval Aucun - */ -void Chrono_Stop(void) -{ - MyTimer_Stop(Chrono_Timer); - running = 0; -} - - -/** - * @brief Remet le chronomètre à 0 - * @note - * @param Aucun - * @retval Aucun - */ -void Chrono_Reset(void) -{ - // Arrêt Chrono - MyTimer_Stop(Chrono_Timer); - - // Reset Time - Chrono_Time.Hund=0; - Chrono_Time.Sec=0; - Chrono_Time.Min=0; -} - - -/** - * @brief Renvoie l'adresse de la variable Time privée gérée dans le module Chrono.c - * @note - * @param Aucun - * @retval adresse de la variable Time - */ -Time * Chrono_Read(void) -{ - return &Chrono_Time; -} - - - - -/** - * @brief incrémente la variable privée Chron_Time modulo 60mn - * @note - * @param Aucun - * @retval Aucun - */ -void Chrono_Task_10ms(void) -{ - Chrono_Time.Hund++; - if (Chrono_Time.Hund==100) - { - Chrono_Time.Sec++; - Chrono_Time.Hund=0; - } - if (Chrono_Time.Sec==60) - { - Chrono_Time.Min++; - Chrono_Time.Sec=0; - } - if (Chrono_Time.Min==60) - { - Chrono_Time.Hund=0; - } - if ( Chrono_Time.Sec % 2) { - LL_GPIO_SetOutputPin(GPIOC,LL_GPIO_PIN_10); - } else { - LL_GPIO_ResetOutputPin(GPIOC,LL_GPIO_PIN_10); - } - -} - -/** - * @brief configurer les 3 IO pour recevoir les 2 BP et la LED - */ -void Chrono_Conf_io(void) { - LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOC); - - LL_GPIO_InitTypeDef button_rouge_conf; - button_rouge_conf.Mode = LL_GPIO_MODE_FLOATING; - button_rouge_conf.Pin = LL_GPIO_PIN_8; - LL_GPIO_Init(GPIOC, &button_rouge_conf); - - - LL_GPIO_InitTypeDef button_bleu_conf; - button_bleu_conf.Mode = LL_GPIO_MODE_FLOATING; - button_bleu_conf.Pin = LL_GPIO_PIN_13; - LL_GPIO_Init(GPIOC, &button_bleu_conf); - - LL_GPIO_InitTypeDef led; - led.Mode = LL_GPIO_MODE_OUTPUT; - led.OutputType = LL_GPIO_OUTPUT_OPENDRAIN; - led.Speed = LL_GPIO_SPEED_FREQ_LOW; - led.Pin = LL_GPIO_PIN_10; - LL_GPIO_Init(GPIOC, &led); -} - -void Chrono_Background(void){ - if(LL_GPIO_IsInputPinSet(GPIOC, LL_GPIO_PIN_8)) { - if(running) { - Chrono_Stop(); - } else { - Chrono_Start(); - } - } - if(!(LL_GPIO_IsInputPinSet(GPIOC, LL_GPIO_PIN_13))) { // negative logic for blue button - Chrono_Reset(); - } -} diff --git a/keil_project/Services/Chrono.h b/keil_project/Services/Chrono.h deleted file mode 100644 index 4afdaa6..0000000 --- a/keil_project/Services/Chrono.h +++ /dev/null @@ -1,107 +0,0 @@ -// RIEN A MODIFIER // - -#ifndef CHRONO_H -#define CHRONO_H - -/* -Service permettant de chornométrer jusqu'à 59mn 59s 99 1/100 -Utilise un timer au choix (TIMER1 à TIMER4). -Utilise la lib MyTimers.h /.c -*/ - -#include "stm32f103xb.h" - -/* ===================================================================================== -Les fonctions qui gèrent les IO (ajout par rapport à l'activité 1) -=======================================================================================*/ - - -#define PinStart LL_GPIO_PIN_8 -#define PinStop LL_GPIO_PIN_6 -// à compléter pour la pin reset du chronometre -#define PinLED LL_GPIO_PIN_10 -#define GPIO_Pin GPIOC -#define GPIO_LED GPIOC - - - - - - -/** - * @brief polle les 3 entrées et gènère les actions à faire - * @note Fct à lancer en tâche de fond (non bloquante) - * @param None - * @retval None - */ - -void Chrono_Background(void); - -//=======================================================================================// - - - - -// Type de donnée utilisé dans le module - -typedef struct { - char Hund; - char Sec; - char Min; -} Time; - - - - -/** - * @brief Configure le chronomètre. - * @note A lancer avant toute autre fonction. - * @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4 - * @retval None - */ -void Chrono_Conf(TIM_TypeDef * Timer); - - - - - - -/** - * @brief Démarre le chronomètre. - * @note si la durée dépasse 59mn 59sec 99 cent, elle est remise à zéro et repart - * @param Aucun - * @retval Aucun - */ -void Chrono_Start(void); - - -/** - * @brief Arrête le chronomètre. - * @note - * @param Aucun - * @retval Aucun - */ -void Chrono_Stop(void); - - -/** - * @brief Remet le chronomètre à 0 - * @note - * @param Aucun - * @retval Aucun - */ -void Chrono_Reset(void); - - -/** - * @brief Renvoie l'adresse de la variable Time privée gérée dans le module Chrono.c - * @note - * @param Aucun - * @retval adresse de la variable Time - */ -Time * Chrono_Read(void); - -void Chrono_Conf_io(void); - -void Chrono_Background(void); -#endif diff --git a/keil_project/Services/IncrEncoder.c b/keil_project/Services/IncrEncoder.c index bc9f795..b9a8b9b 100644 --- a/keil_project/Services/IncrEncoder.c +++ b/keil_project/Services/IncrEncoder.c @@ -1,9 +1,17 @@ +#include +#include + #include "IncrEncoder.h" #include "stm32f1xx_ll_gpio.h" #include "stm32f1xx_ll_bus.h" #include "stm32f1xx_ll_exti.h" #include "stm32f1xx_ll_tim.h" +#define RESOLUTION 1 +#define ANGLE_DEBUT 45 +#define INCR_ENCODER_MID_VALUE 719 + + int index_passed = 0; int counts_per_revolution = 360; @@ -45,9 +53,9 @@ void INCR_ENCODER_Init(void){ encoder_init_struct.IC1Filter = LL_TIM_IC_FILTER_FDIV1 ; encoder_init_struct.IC2Filter = LL_TIM_IC_FILTER_FDIV1 ; - LL_TIM_ENCODER_Init(TIM3, &encoder_init_struct); + LL_TIM_ENCODER_Init(TIM3, &encoder_init_struct); - LL_TIM_EnableCounter(TIM3); + LL_TIM_EnableCounter(TIM3); LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA); LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_AFIO); @@ -78,6 +86,7 @@ void INCR_ENCODER_Init(void){ } void EXTI9_5_IRQHandler(void){ + index_passed = 1; // reset counter = encoder position to 0 position LL_TIM_WriteReg(TIM3,CNT,0); @@ -94,5 +103,15 @@ int INCR_ENCODER_IsAbsolute(void) int INCR_ENCODER_GetAngle(void) { - return LL_TIM_ReadReg(TIM3,CNT); + int counter_value = LL_TIM_ReadReg(TIM3, CNT); + float vabs = abs(counter_value-INCR_ENCODER_MID_VALUE); + float vIEAngleDebut = INCR_ENCODER_MID_VALUE -(ANGLE_DEBUT*4); + float nbIncrements = 90/RESOLUTION; + + if(vabs > vIEAngleDebut) + { + return 0; + }else{ + return 90 - RESOLUTION*floor(vabs/(vIEAngleDebut/nbIncrements) ) ; + } }; diff --git a/keil_project/Services/IncrEncoder.h b/keil_project/Services/IncrEncoder.h index 3640e9f..7767b5e 100644 --- a/keil_project/Services/IncrEncoder.h +++ b/keil_project/Services/IncrEncoder.h @@ -1,5 +1,5 @@ #ifndef INCR_ENCODER -//codé par Kévin Cavailles et Jasper Güldenstein + /** */ void INCR_ENCODER_Init(void); diff --git a/keil_project/Services/emetteur_rf.c b/keil_project/Services/RFOutput.c similarity index 67% rename from keil_project/Services/emetteur_rf.c rename to keil_project/Services/RFOutput.c index 31e5a1c..42f2813 100644 --- a/keil_project/Services/emetteur_rf.c +++ b/keil_project/Services/RFOutput.c @@ -1,6 +1,9 @@ -#include "emetteur_rf.h" +#include "RFOutput.h" +#include -void emetteur_rf_init(void){ +char RF_OUTPUT_buf[100]; + +void RF_OUTPUT_Init(void){ LL_USART_InitTypeDef My_LL_Usart_Init_Struct; @@ -34,19 +37,19 @@ void emetteur_rf_init(void){ LL_USART_Init(USART1,&My_LL_Usart_Init_Struct); LL_USART_Enable(USART1); - /*int periph_speed; - if (uart_port==USART1) periph_speed = 36000000; - if (uart_port==USART2) periph_speed = 72000000; - if (uart_port==USART3) periph_speed = 72000000; + } - LL_USART_SetBaudRate(uart_port, periph_speed, baudrate); -*/} - -void emetteur_send_bytes(USART_TypeDef * uart_port,char* buf, int len){ +void RF_OUTPUT_SendBytes(char* buf, int len){ + + LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_11); for(int i = 0; i < len; i++){ - LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_11); - LL_USART_TransmitData8(uart_port, buf[i]); - while(!LL_USART_IsActiveFlag_TXE(uart_port)); - LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_11); + LL_USART_TransmitData8(USART1, buf[i]); + while(!LL_USART_IsActiveFlag_TXE(USART1)); } + LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_11); +} + +void RF_OUTPUT_SendMessage(int rouli_bon, int alimentation_bon, float angle_voile){ + int len = sprintf(RF_OUTPUT_buf, "Alim bon: %d, Rouli bon: %d, Angle de voile: %2f\r\n", alimentation_bon, rouli_bon, angle_voile); + RF_OUTPUT_SendBytes(RF_OUTPUT_buf, len); } diff --git a/keil_project/Services/emetteur_rf.h b/keil_project/Services/RFOutput.h similarity index 56% rename from keil_project/Services/emetteur_rf.h rename to keil_project/Services/RFOutput.h index 295fe4b..f9285df 100644 --- a/keil_project/Services/emetteur_rf.h +++ b/keil_project/Services/RFOutput.h @@ -7,8 +7,9 @@ #include "stm32f1xx_ll_usart.h" #include "stm32f1xx_ll_gpio.h" -void emetteur_rf_init(void); -void emetteur_send_bytes(USART_TypeDef * uart_port,char* buf, int len); +void RF_OUTPUT_Init(void); +void RF_OUTPUT_SendBytes(char* buf, int len); +void RF_OUTPUT_SendMessage(int rouli_bon, int alimentation_bon, float angle_voile); diff --git a/keil_project/Services/alimentation.c b/keil_project/Services/alimentation.c deleted file mode 100644 index e54e5df..0000000 --- a/keil_project/Services/alimentation.c +++ /dev/null @@ -1,63 +0,0 @@ -#include "alimentation.h" - - -void alimentation_init(void){ - RCC -> CFGR |= (0x1<<15); - RCC-> CFGR &= ~ (0x1<<14); - LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_ADC1); - LL_APB1_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOC); - - LL_GPIO_InitTypeDef pc2; - LL_ADC_InitTypeDef adc; - LL_ADC_REG_InitTypeDef adcReg; - - LL_GPIO_StructInit(&pc2); - pc2.Pin = LL_GPIO_PIN_0; - pc2.Mode = LL_GPIO_MODE_ANALOG; - LL_GPIO_Init(GPIOC, &pc2); - - - adc.DataAlignment = LL_ADC_DATA_ALIGN_RIGHT; - adc.SequencersScanMode = LL_ADC_SEQ_SCAN_DISABLE; - LL_ADC_Init(ADC1, &adc); - - adcReg.TriggerSource = LL_ADC_REG_TRIG_SOFTWARE ; - adcReg.SequencerLength = LL_ADC_REG_SEQ_SCAN_DISABLE; - adcReg.SequencerDiscont = LL_ADC_REG_SEQ_DISCONT_DISABLE; - adcReg.ContinuousMode = LL_ADC_REG_CONV_SINGLE; - adcReg.DMATransfer = LL_ADC_REG_DMA_TRANSFER_NONE; - - - LL_ADC_REG_Init(ADC1, &adcReg); - - /*LL_ADC_SetChannelSamplingTime(ADC2, LL_ADC_CHANNEL_10, LL_ADC_SAMPLINGTIME_1CYCLE_5); - LL_ADC_SetChannelSamplingTime(ADC2, LL_ADC_CHANNEL_11, LL_ADC_SAMPLINGTIME_1CYCLE_5); - - */ - LL_ADC_Enable(ADC1); - - //LL_ADC_EnableIT_EOS(ADC1); - //wait 0,2 µs, calibration is advised - //LL_ADC_StartCalibration(ADC1); - } -float u2; -unsigned int niv_batterie; - float battery_level; - float get_battery_level(void){ - LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_1, LL_ADC_CHANNEL_12); - LL_ADC_REG_StartConversionSWStart(ADC1); - while (LL_ADC_IsActiveFlag_EOS(ADC1) != 1); - battery_level = LL_ADC_REG_ReadConversionData12(ADC1); - u2 = (battery_level*3.3)/4095.0; - float level = u2*13.0; - return level; -} - - int is_level_enough(void){ - if (get_battery_level()<(0.8*12)){ - return 0; - }else{ - return 1; - } - } - \ No newline at end of file diff --git a/keil_project/Src/main.c b/keil_project/Src/main.c index 46d750c..8655e79 100644 --- a/keil_project/Src/main.c +++ b/keil_project/Src/main.c @@ -21,62 +21,89 @@ #include "stm32f1xx_ll_system.h" // utile dans la fonction SystemClock_Config #include "stm32f1xx_ll_bus.h" + #include "RFInput.h" #include "IncrEncoder.h" #include "DcMotor.h" #include "Sail.h" -#include "alimentation.h" -#include "accelerometer.h" +#include "Alimentation.h" +#include "Accelerometer.h" +#include "RFOutput.h" + +#define CONTROL_LOOP_PERIOD 250 +#define MSG_TRANSFER_PERIOD 3000 void SystemClock_Config(void); /* Private functions ---------------------------------------------------------*/ +// global variables for monitoring in keil +int counter = 0; +int battery_level_good = 0; +int angle_roulis_good = 0; +int angle_sail = 0; +int RF_Input_Duty = 0; +int TX_Flag = 0, CONTROL_LOOP_Flag = 0; + +char wait_for_girouette[] = "En attente d'initialisation de la girouette\r\n"; + /** * @brief Main program * @param None * @retval None */ - -int val = 0; -int val2 = 0; -int counter = 0; -float level; -int level_enough; -double xx,yy; -int bon; int main(void) { /* Configure the system clock to 72 MHz */ - SystemClock_Config(); - - //alimentation_init(); + SystemClock_Config(); + ALIMENTATION_Init(); + ACCELEROMETER_Init(); RF_INPUT_Init(); DC_MOTOR_Init(); SAIL_Init(); - accelero_init(); + RF_OUTPUT_Init(); + + while(!INCR_ENCODER_IsAbsolute()) + { + RF_OUTPUT_SendBytes(wait_for_girouette, sizeof(wait_for_girouette)); + LL_mDelay(500); + } - /* Infinite loop */ while (1) { - LL_mDelay(100); - //counter = (counter + 1) % 100; - //DC_MOTOR_SetSpeed(counter); - //val = RF_INPUT_GetPeriodUs(); - //val2 = RF_INPUT_GetDutyTimeUs(); - //SAIL_SetAngle(counter); - //level = get_battery_level(); - //level_enough = is_level_enough(); - xx = accelero_get_x(); - //yy = accelero_get_y(); - //bon = accelero_angle_bon(); + if(CONTROL_LOOP_Flag){ + battery_level_good = ALIMENTATION_IsLevelEnough(); + angle_roulis_good = ACCELEROMETER_AngleGood(); + + if(!angle_roulis_good){ + SAIL_SetAngle(90); + DC_MOTOR_SetSpeed(0); + }else{ + angle_sail = INCR_ENCODER_GetAngle(); + SAIL_SetAngle(angle_sail/2); + RF_Input_Duty = RF_INPUT_GetDutyTimeRelative(); + DC_MOTOR_SetSpeed(RF_Input_Duty); + } + CONTROL_LOOP_Flag = 0; + } + if(TX_Flag){ + RF_OUTPUT_SendMessage(angle_roulis_good, battery_level_good, angle_sail); + TX_Flag = 0; + } } } - - - - +void SysTick_Handler(void) +{ + + if(counter % CONTROL_LOOP_PERIOD == 0){ + CONTROL_LOOP_Flag = 1; + } + if(counter % MSG_TRANSFER_PERIOD == 0){ + TX_Flag = 1; + } + counter = (counter+1) % (CONTROL_LOOP_PERIOD*MSG_TRANSFER_PERIOD) ; +} /**