From 1874f666d4a24ab17ee00c6686e5872242d5f72c Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Fri, 7 Apr 2023 15:28:30 +0200 Subject: [PATCH 1/4] Added battery and initBattery functions, using external variable uartCool --- driver/adc.c | 6 ++-- driver/adc.h | 2 +- implementation/accelerometer.c | 4 +-- implementation/accelerometer.h | 4 +-- implementation/battery.c | 26 +++++++++++++++ implementation/battery.h | 8 +++++ implementation/remote.c | 1 - keilproject/Source/Principale.c | 3 ++ keilproject/voilier.uvoptx | 56 +++++++++++++++++++++++++++++++-- keilproject/voilier.uvprojx | 20 ++++++++++++ 10 files changed, 118 insertions(+), 12 deletions(-) create mode 100644 implementation/battery.c create mode 100644 implementation/battery.h diff --git a/driver/adc.c b/driver/adc.c index 0ddd5df..4110fa0 100644 --- a/driver/adc.c +++ b/driver/adc.c @@ -1,8 +1,8 @@ #include "adc.h" -void (* pFncADC) (void); /* d�claration d�un pointeur de fonction */ +void (* pFncADC) (uint32_t); /* d�claration d�un pointeur de fonction */ -void MyADC_Init_Periph (void (* ptrFonction)(void)) +void MyADC_Init_Periph (void (* ptrFonction)(uint32_t)) { pFncADC = ptrFonction; /* affectation du pointeur */ } @@ -38,7 +38,7 @@ void MyADC_ActiveIT(ADC_TypeDef * ADC, uint8_t Prio) void ADC1_2_IRQHandler(void) { if (pFncADC != 0) - (*pFncADC) (); /* appel indirect de la fonction */ + (*pFncADC) (ADC1->DR); /* appel indirect de la fonction */ MyADC_Base_Start(ADC1); ADC1->SR &= ~ADC_SR_EOC; //Prochaine lecture pouvant �tre effectu�e. } diff --git a/driver/adc.h b/driver/adc.h index 849c0d7..caf77b6 100644 --- a/driver/adc.h +++ b/driver/adc.h @@ -23,7 +23,7 @@ typedef struct void MyADC_Init(MyADC_Struct_TypeDef * ADCStructPtr); void MyADC_ActiveIT(ADC_TypeDef * ADC, uint8_t Prio); -void MyADC_Init_Periph (void (* ptrFonction)(void)); +void MyADC_Init_Periph (void (* ptrFonction)(uint32_t)); MyGPIO_Struct_TypeDef GPIOFromADC(MyADC_Struct_TypeDef ADC); #define MyADC_Base_Start(ADC) (ADC->CR2 |= ADC_CR2_SWSTART) diff --git a/implementation/accelerometer.c b/implementation/accelerometer.c index 0bb3f14..a70849e 100644 --- a/implementation/accelerometer.c +++ b/implementation/accelerometer.c @@ -3,7 +3,7 @@ #include "MySPI.h" #include "gpio.h" -void accelerometre() +void accelerometre(void) { int dataX ; float GX ; @@ -54,4 +54,4 @@ void accelerometre() -} \ No newline at end of file +} diff --git a/implementation/accelerometer.h b/implementation/accelerometer.h index 1d8a585..e48952b 100644 --- a/implementation/accelerometer.h +++ b/implementation/accelerometer.h @@ -2,7 +2,7 @@ #define INC_ACCELEROMETER_H_ #include "../driver/MySPI.h" -void accelerometre(); +void accelerometre(void); -#endif \ No newline at end of file +#endif diff --git a/implementation/battery.c b/implementation/battery.c new file mode 100644 index 0000000..1e6a9b8 --- /dev/null +++ b/implementation/battery.c @@ -0,0 +1,26 @@ +#include "battery.h" +#include "adc.h" +#include "remote.h" + +extern MyUART_Struct_Typedef uartCool; + +void battery(uint32_t data) +{ + if(data >= 0x420) //0x429 -> approx. 12V + { + MyUART_Send(&uartCool,'h'); + } + else { + MyUART_Send(&uartCool,'l'); + } + +} + +void initBattery(void) +{ + MyADC_Init_Periph(battery); + MyADC_Struct_TypeDef adcBattery = {ADC1,10,cycles41d5}; + MyADC_Init(&adcBattery); + MyGPIO_Struct_TypeDef gpioBattery = {GPIOC,0,In_Analog};; + MyGPIO_Init(&gpioBattery); +} diff --git a/implementation/battery.h b/implementation/battery.h new file mode 100644 index 0000000..343b80b --- /dev/null +++ b/implementation/battery.h @@ -0,0 +1,8 @@ +#ifndef BATTERY_H +#define BATTERY_H +#include "stm32f10x.h" + +void battery(uint32_t data); +void initBattery(void); + +#endif diff --git a/implementation/remote.c b/implementation/remote.c index 5d16ae1..57317d6 100644 --- a/implementation/remote.c +++ b/implementation/remote.c @@ -6,7 +6,6 @@ MyUART_Struct_Typedef uartCool = {USART1,9600,lengthBit8,parityNone,stopBit1}; void remote(uint8_t data) { - MyUART_Send(&uartCool,data); int8_t signedData = (int8_t)data; if(signedData >= 0) { diff --git a/keilproject/Source/Principale.c b/keilproject/Source/Principale.c index 545b510..d4439c1 100644 --- a/keilproject/Source/Principale.c +++ b/keilproject/Source/Principale.c @@ -4,6 +4,8 @@ #include "motoreducteur.h" #include "rtc.h" #include "remote.h" +#include "battery.h" +#include "accelerometer.h" void initImplementation(void); @@ -27,4 +29,5 @@ void initImplementation(void) MyMotor_ChangeDirection(HORAIRE); MyRTC_Init(); accelerometre(); + initBattery(); } diff --git a/keilproject/voilier.uvoptx b/keilproject/voilier.uvoptx index 1daa6dd..9dc48d8 100644 --- a/keilproject/voilier.uvoptx +++ b/keilproject/voilier.uvoptx @@ -272,6 +272,26 @@ STLink\ST-LINKIII-KEIL_SWO.dll + + 0 + ARMRTXEVENTFLAGS + -L70 -Z18 -C0 -M0 -T1 + + + 0 + DLGTARM + (1010=-1,-1,-1,-1,0)(1007=-1,-1,-1,-1,0)(1008=-1,-1,-1,-1,0)(1009=-1,-1,-1,-1,0) + + + 0 + ARMDBGFLAGS + + + + 0 + DLGUARM + (105=-1,-1,-1,-1,0) + 0 ST-LINKIII-KEIL_SWO @@ -290,12 +310,12 @@ 0 0 - 0 + 1 0 0 0 0 - 0 + 1 0 0 0 @@ -326,6 +346,12 @@ + + + System Viewer\ADC1 + 35905 + + 1 0 @@ -414,7 +440,7 @@ 2 6 1 - 0 + 1 0 0 ..\driver\adc.c @@ -478,6 +504,30 @@ 0 0 + + 3 + 11 + 1 + 0 + 0 + 0 + ..\implementation\battery.c + battery.c + 0 + 0 + + + 3 + 12 + 1 + 0 + 0 + 0 + ..\implementation\accelerometer.c + accelerometer.c + 0 + 0 + diff --git a/keilproject/voilier.uvprojx b/keilproject/voilier.uvprojx index c74b758..7e45548 100644 --- a/keilproject/voilier.uvprojx +++ b/keilproject/voilier.uvprojx @@ -443,6 +443,16 @@ 1 ..\implementation\remote.c + + battery.c + 1 + ..\implementation\battery.c + + + accelerometer.c + 1 + ..\implementation\accelerometer.c + @@ -890,6 +900,16 @@ 1 ..\implementation\remote.c + + battery.c + 1 + ..\implementation\battery.c + + + accelerometer.c + 1 + ..\implementation\accelerometer.c + From 179f7b0f13236c32619896cf1d13008ee32377dd Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Mon, 10 Apr 2023 23:48:53 +0200 Subject: [PATCH 2/4] Added MyUART_SendArray function in uart.c, Modified battery function to send a battery gauge. Created MAX_BAT that defines the max level of the battery --- driver/uart.c | 11 +++++++++++ driver/uart.h | 1 + implementation/battery.c | 21 ++++++++++++++++----- implementation/battery.h | 1 + 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/driver/uart.c b/driver/uart.c index 051c3fa..c798880 100644 --- a/driver/uart.c +++ b/driver/uart.c @@ -106,6 +106,17 @@ void MyUART_InitGPIO(MyUART_Struct_Typedef * UARTStructPtr) MyGPIO_Init(&txd); } +int MyUART_SendArray(MyUART_Struct_Typedef *UART, uint8_t * data, int dataLength) +{ + int i; + for(i=0; iUART->DR = data[i]; + while (!(UART->UART->SR & USART_SR_TXE)); + } + return 0; +} + void MyUART_Send(MyUART_Struct_Typedef *UART, uint8_t data) { UART->UART->DR = data; diff --git a/driver/uart.h b/driver/uart.h index 262a4c5..d27238e 100644 --- a/driver/uart.h +++ b/driver/uart.h @@ -30,6 +30,7 @@ typedef struct { void MyUART_Init(MyUART_Struct_Typedef * UARTStructPtr); void MyUART_InitGPIO(MyUART_Struct_Typedef * UARTStructPtr); +int MyUART_SendArray(MyUART_Struct_Typedef *UART, uint8_t * data, int dataLength); void MyUART_Send(MyUART_Struct_Typedef *UART, uint8_t data); uint8_t MyUART_Receive(MyUART_Struct_Typedef *UART); void MyUART_Init_Periph (void (* ptrFonction)(uint8_t)); diff --git a/implementation/battery.c b/implementation/battery.c index 1e6a9b8..6c456b7 100644 --- a/implementation/battery.c +++ b/implementation/battery.c @@ -6,14 +6,25 @@ extern MyUART_Struct_Typedef uartCool; void battery(uint32_t data) { - if(data >= 0x420) //0x429 -> approx. 12V + int i; + float percentBattery = ((float)data)/MAX_BAT; + uint8_t batteryBar[12], batteryGauge=percentBattery*10; + if(batteryGauge>10) { - MyUART_Send(&uartCool,'h'); + batteryGauge = 10; } - else { - MyUART_Send(&uartCool,'l'); + batteryBar[0] = '['; + for(i=1; i<=(10-batteryGauge); i++) + { + batteryBar[i]='.'; } - + while(i<11) + { + batteryBar[i]='#'; + i++; + } + batteryBar[i]=']'; + MyUART_SendArray(&uartCool, batteryBar, 12); } void initBattery(void) diff --git a/implementation/battery.h b/implementation/battery.h index 343b80b..4170796 100644 --- a/implementation/battery.h +++ b/implementation/battery.h @@ -1,6 +1,7 @@ #ifndef BATTERY_H #define BATTERY_H #include "stm32f10x.h" +#define MAX_BAT 1145 void battery(uint32_t data); void initBattery(void); From 33d73b2c82fb8cbf00d8104636bbcc7a5dbb4399 Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Tue, 11 Apr 2023 11:28:42 +0200 Subject: [PATCH 3/4] Added MyRTC_Struct_TypeDef, modified battery function to show the time, and the percent --- driver/adc.c | 2 +- implementation/battery.c | 40 +++++++++++++++++++++------------ implementation/battery.h | 4 ++++ implementation/rtc.c | 16 ++++++------- implementation/rtc.h | 12 +++++++++- keilproject/Source/Principale.c | 2 +- 6 files changed, 51 insertions(+), 25 deletions(-) diff --git a/driver/adc.c b/driver/adc.c index 4110fa0..7259e4c 100644 --- a/driver/adc.c +++ b/driver/adc.c @@ -32,7 +32,7 @@ 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<ISER[0] |= (1<10) { batteryGauge = 10; } - batteryBar[0] = '['; - for(i=1; i<=(10-batteryGauge); i++) - { - batteryBar[i]='.'; - } - while(i<11) + char timerClock[8] = {'[','.','.','h','.','.',']',0x20}; + sprintf(timerClock+1,"%.*d",2,rtcBattery.hours); + timerClock[3]=':'; + sprintf(timerClock+4,"%.*d",2,rtcBattery.minutes); + timerClock[3]='\t'; + MyUART_SendArray(&uartCool, (uint8_t *)timerClock, 8); + + for(i=(10-batteryGauge); i<11; i++) { batteryBar[i]='#'; - i++; } - batteryBar[i]=']'; - MyUART_SendArray(&uartCool, batteryBar, 12); + MyUART_SendArray(&uartCool, batteryBar, 13); + + char batteryPercentString[3]; + sprintf(batteryPercentString,"%.*g",2,percentBattery*100); + batteryPercentString[2]='%'; + MyUART_SendArray(&uartCool, (uint8_t *)batteryPercentString, 6); + + MyUART_Send(&uartCool, '\n'); } void initBattery(void) @@ -32,6 +44,6 @@ void initBattery(void) MyADC_Init_Periph(battery); MyADC_Struct_TypeDef adcBattery = {ADC1,10,cycles41d5}; MyADC_Init(&adcBattery); - MyGPIO_Struct_TypeDef gpioBattery = {GPIOC,0,In_Analog};; + MyGPIO_Struct_TypeDef gpioBattery = {GPIOC,0,In_Analog}; MyGPIO_Init(&gpioBattery); } diff --git a/implementation/battery.h b/implementation/battery.h index 4170796..88b754b 100644 --- a/implementation/battery.h +++ b/implementation/battery.h @@ -1,6 +1,10 @@ #ifndef BATTERY_H #define BATTERY_H #include "stm32f10x.h" +#include "adc.h" +#include "remote.h" +#include "rtc.h" +#include #define MAX_BAT 1145 void battery(uint32_t data); diff --git a/implementation/rtc.c b/implementation/rtc.c index 8757e9b..59d318b 100644 --- a/implementation/rtc.c +++ b/implementation/rtc.c @@ -11,7 +11,7 @@ void MyRTC_Init(void) MyI2C_Init(I2C2, 15, IT_I2C_Err); } -void MyRTC_GetTime(int* sec, int* min, int* hour, int* day, int* date, int* month, int* year) +void MyRTC_GetTime(MyRTC_Struct_TypeDef * rtc) { MyI2C_RecSendData_Typedef data; char regCopy = 0; @@ -21,17 +21,17 @@ void MyRTC_GetTime(int* sec, int* min, int* hour, int* day, int* date, int* mont data.Nb_Data = 1; MyI2C_GetString(I2C2, 0x00, &data); - *sec = ((regCopy >> 4) & 0x07) * 10 + (regCopy & 0x0F); + rtc->seconds = ((regCopy >> 4) & 0x07) * 10 + (regCopy & 0x0F); MyI2C_GetString(I2C2, 0x01, &data); - *min = ((regCopy >> 4) & 0x07) * 10 + (regCopy & 0x0F); + rtc->minutes = ((regCopy >> 4) & 0x07) * 10 + (regCopy & 0x0F); MyI2C_GetString(I2C2, 0x02, &data); - *hour = 0; + rtc->hours = 0; MyI2C_GetString(I2C2, 0x03, &data); - *day = (regCopy & 0x07); + rtc->day = (regCopy & 0x07); MyI2C_GetString(I2C2, 0x04, &data); - *date = ((regCopy >> 4) & 0x03) * 10 + (regCopy & 0x0F); + rtc->date = ((regCopy >> 4) & 0x03) * 10 + (regCopy & 0x0F); MyI2C_GetString(I2C2, 0x05, &data); - *month = ((regCopy >> 4) & 0x01) * 10 + (regCopy & 0x0F); + rtc->month = ((regCopy >> 4) & 0x01) * 10 + (regCopy & 0x0F); MyI2C_GetString(I2C2, 0x06, &data); - *year = ((regCopy >> 4) & 0xF0) * 10 + (regCopy & 0x0F) + 2000; + rtc->year = ((regCopy >> 4) & 0xF0) * 10 + (regCopy & 0x0F) + 2000; } diff --git a/implementation/rtc.h b/implementation/rtc.h index dde573c..7e4deb2 100644 --- a/implementation/rtc.h +++ b/implementation/rtc.h @@ -3,7 +3,17 @@ #include "stm32f10x.h" #include "MyI2C.h" +typedef struct{ + int seconds; + int minutes; + int hours; + int day; + int date; + int month; + int year; +} MyRTC_Struct_TypeDef; + void MyRTC_Init(void); -void MyRTC_GetTime(int* sec, int* min, int* hour, int* day, int* date, int* month, int* year); +void MyRTC_GetTime(MyRTC_Struct_TypeDef * rtc); #endif diff --git a/keilproject/Source/Principale.c b/keilproject/Source/Principale.c index d4439c1..b1923c2 100644 --- a/keilproject/Source/Principale.c +++ b/keilproject/Source/Principale.c @@ -28,6 +28,6 @@ void initImplementation(void) MyMotor_ChangeSpeed(0); MyMotor_ChangeDirection(HORAIRE); MyRTC_Init(); - accelerometre(); initBattery(); + accelerometre(); } From b232a42eba8e3e194359e32b2cc37b7b4fda1596 Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Tue, 11 Apr 2023 14:38:54 +0200 Subject: [PATCH 4/4] Added isClose to test if the adc changes brutally, and getGauge which simplifies the battery bar. --- implementation/battery.c | 56 ++++++++++++++++++++++++---------------- implementation/battery.h | 2 ++ 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/implementation/battery.c b/implementation/battery.c index f3f5329..0730561 100644 --- a/implementation/battery.c +++ b/implementation/battery.c @@ -2,43 +2,55 @@ extern MyUART_Struct_Typedef uartCool; int actualMinutes =-1; +uint32_t oldAdc =0; void battery(uint32_t data) { MyRTC_Struct_TypeDef rtcBattery; MyRTC_GetTime(&rtcBattery); - if(actualMinutes == rtcBattery.minutes) + + if((actualMinutes == rtcBattery.minutes) && isClose(oldAdc,data,50)) //pas de precision/10 % { return; } + + oldAdc = data; actualMinutes = rtcBattery.minutes; float percentBattery = ((float)data)/MAX_BAT; - uint8_t batteryBar[13]={'[','.','.','.','.','.','.','.','.','.','.',']',0x20}, batteryGauge=percentBattery*10, i; - if(batteryGauge>10) - { - batteryGauge = 10; - } - char timerClock[8] = {'[','.','.','h','.','.',']',0x20}; - sprintf(timerClock+1,"%.*d",2,rtcBattery.hours); - timerClock[3]=':'; - sprintf(timerClock+4,"%.*d",2,rtcBattery.minutes); - timerClock[3]='\t'; - MyUART_SendArray(&uartCool, (uint8_t *)timerClock, 8); + char batteryBar[13]="[__________]"; + char testChar[24]; - for(i=(10-batteryGauge); i<11; i++) - { - batteryBar[i]='#'; - } - MyUART_SendArray(&uartCool, batteryBar, 13); - - char batteryPercentString[3]; - sprintf(batteryPercentString,"%.*g",2,percentBattery*100); - batteryPercentString[2]='%'; - MyUART_SendArray(&uartCool, (uint8_t *)batteryPercentString, 6); + getGauge(batteryBar, percentBattery); + sprintf(testChar,"[%.2d:%.2d] %s %.2d%%",rtcBattery.hours,rtcBattery.minutes,batteryBar,(int)(percentBattery*100)); + MyUART_SendArray(&uartCool, (uint8_t *)testChar, 24); MyUART_Send(&uartCool, '\n'); } +void getGauge(char gauge[], float percent) +{ + int i; + percent=percent*10; + if(percent>10) + { + percent = 10.0; + } + for(i=(10-percent)+1; i<11; i++) + { + gauge[i]='#'; + } + gauge[12]='\0'; +} + +char isClose(uint32_t data, uint32_t compare, int precision) +{ + if(data < precision) + { + return !(data >= compare+precision); + } + return !((data >= compare+precision) || (data <= compare-precision)); +} + void initBattery(void) { MyADC_Init_Periph(battery); diff --git a/implementation/battery.h b/implementation/battery.h index 88b754b..d593a80 100644 --- a/implementation/battery.h +++ b/implementation/battery.h @@ -8,6 +8,8 @@ #define MAX_BAT 1145 void battery(uint32_t data); +void getGauge(char gauge[], float percent); void initBattery(void); +char isClose(uint32_t data, uint32_t compare, int precision); #endif