diff --git a/driver/adc.c b/driver/adc.c index 0ddd5df..7259e4c 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 */ } @@ -32,13 +32,13 @@ 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<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/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/accelerometer.c b/implementation/accelerometer.c index d2b36d0..c34d128 100644 --- a/implementation/accelerometer.c +++ b/implementation/accelerometer.c @@ -11,7 +11,7 @@ void Init_accelerometre(void) MySPI_Clear_NSS(); //CS LOW MySPI_Send(0x2D);//Registre Auto_sleep + Write + measure a 1 - MySPI_Send(0x08);// désactive + MySPI_Send(0x08);// d�sactive MySPI_Set_NSS();//CS HIGH /* MySPI_Clear_NSS(); //CS LOW @@ -22,7 +22,7 @@ void Init_accelerometre(void) MySPI_Clear_NSS(); //CS LOW MySPI_Send(0X2C);//Registre power consuption + Write - MySPI_Send(0X1A);//Paramétrage hors low consumption + 100Hz output data rate + MySPI_Send(0X1A);//Param�trage hors low consumption + 100Hz output data rate MySPI_Set_NSS();//CS HIGH MySPI_Clear_NSS(); //CS LOW @@ -39,8 +39,8 @@ void Lecture_accelerometre(float* GX, float* GY, float* GZ) MySPI_Clear_NSS(); //CS LOW - MySPI_Send(0xF2);//lecture 1er registre accéléromètre + lecture multibytes - dataX = MySPI_Read()<<8; // données sur 13 (12 data + sign) bits passage 8 bit en PF + MySPI_Send(0xF2);//lecture 1er registre acc�l�rom�tre + lecture multibytes + dataX = MySPI_Read()<<8; // donn�es sur 13 (12 data + sign) bits passage 8 bit en PF dataX |= MySPI_Read(); //lecture SPI dataX &= 0x1FFF; //masquage au dessus du bit 13 if (dataX > 511) diff --git a/implementation/accelerometer.h b/implementation/accelerometer.h index 77b42e0..a3190e5 100644 --- a/implementation/accelerometer.h +++ b/implementation/accelerometer.h @@ -2,7 +2,7 @@ #define INC_ACCELEROMETER_H_ #include "../driver/MySPI.h" + void Init_accelerometre(void); void Lecture_accelerometre(float *GX, float* GY, float* GZ); - #endif diff --git a/implementation/battery.c b/implementation/battery.c new file mode 100644 index 0000000..0730561 --- /dev/null +++ b/implementation/battery.c @@ -0,0 +1,61 @@ +#include "battery.h" + +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) && isClose(oldAdc,data,50)) //pas de precision/10 % + { + return; + } + + oldAdc = data; + actualMinutes = rtcBattery.minutes; + float percentBattery = ((float)data)/MAX_BAT; + char batteryBar[13]="[__________]"; + char testChar[24]; + + 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); + 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..d593a80 --- /dev/null +++ b/implementation/battery.h @@ -0,0 +1,15 @@ +#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); +void getGauge(char gauge[], float percent); +void initBattery(void); +char isClose(uint32_t data, uint32_t compare, int precision); + +#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/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 1b8a6c2..f3e5a46 100644 --- a/keilproject/Source/Principale.c +++ b/keilproject/Source/Principale.c @@ -5,7 +5,7 @@ #include "rtc.h" #include "remote.h" #include "accelerometer.h" - +#include "battery.h" void initImplementation(void); @@ -36,4 +36,5 @@ void initImplementation(void) MyMotor_ChangeSpeed(0); MyMotor_ChangeDirection(HORAIRE); MyRTC_Init(); + initBattery(); }