/
battery.c
111 lines (101 loc) · 4.03 KB
/
battery.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*============================================================================*
___ ___ ___ _ _ *
/ \ | _ ) | _ \ ___ | |__ ___ | |_ o O O *
| - | | _ \ | / / _ \ | '_ \ / _ \ | _| o *
|_|_| |___/ |_|_\ \___/ |_.__/ \___/ _\__| TS__[O] *
_|"""""|_|"""""|_|"""""|_|"""""|_|"""""|_|"""""|_|"""""| {======| *
"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'./o--000' *
* *
* Auto Balance Robot controller firmware for Nuvoton Cortex M4 series *
* *
* Written by T.L. Shen for Nuvoton Technology. *
* tlshen@nuvoton.com/tzulan611126@gmail.com *
* *
*============================================================================*
*/
#include <stdio.h>
#define BAT_FULL 12.5f
#define BAT_LOW 10.8f
#define BAT_TH 15
#ifdef M451
#include "M451Series.h"
#else
#include "DrvGPIO.h"
#endif
#include "battery.h"
#include "Def.h"
#ifdef M451
#define IO_STATE_BAT PB, BIT2
#define IO_BAT PB2
#else
#endif
/*---------------------------------------------------------------------------------------------------------*/
/* Define global variables and constants */
/*---------------------------------------------------------------------------------------------------------*/
volatile uint32_t g_u32AdcIntFlag;
static uint16_t AdcData;
static int8_t BatteryPercent=0;
/*---------------------------------------------------------------------------------------------------------*/
/* EADC interrupt handler */
/*---------------------------------------------------------------------------------------------------------*/
void ADC00_IRQHandler(void)
{
g_u32AdcIntFlag = 1;
EADC_CLR_INT_FLAG(EADC, 0x1); /* Clear the A/D ADINT0 interrupt flag */
}
void Battery_Init(void)
{
#ifdef M451
SYS_UnlockReg();
/* Enable EADC module clock */
CLK_EnableModuleClock(EADC_MODULE);
/* EADC clock source is 72MHz, set divider to 8, ADC clock is 72/8 MHz */
CLK_SetModuleClock(EADC_MODULE, 0, CLK_CLKDIV0_EADC(8));
SYS_LockReg();
/* Configure the GPB0 - GPB3 ADC analog input pins. */
SYS->GPB_MFPL &= ~SYS_GPB_MFPL_PB2MFP_Msk;
SYS->GPB_MFPL |= SYS_GPB_MFPL_PB2MFP_EADC_CH2;
GPIO_DISABLE_DIGITAL_PATH(PB, 0x4);
/* Set the ADC internal sampling time, input mode as single-end and enable the A/D converter */
EADC_Open(EADC, EADC_CTL_DIFFEN_SINGLE_END);
EADC_SetInternalSampleTime(EADC, 6);
/* Configure the sample module 0 for analog input channel 2 and software trigger source.*/
EADC_ConfigSampleModule(EADC, 0, EADC_SOFTWARE_TRIGGER, 2);
/* Clear the A/D ADINT0 interrupt flag for safe */
EADC_CLR_INT_FLAG(EADC, 0x1);
/* Enable the sample module 0 interrupt. */
EADC_ENABLE_INT(EADC, 0x1);//Enable sample module A/D ADINT0 interrupt.
EADC_ENABLE_SAMPLE_MODULE_INT(EADC, 0, 0x1);//Enable sample module 0 interrupt.
/* Reset the ADC interrupt indicator and trigger sample module 0 to start A/D conversion */
g_u32AdcIntFlag = 0;
/* Enable battery detect circuit (PA3=1)*/
GPIO_SetMode(PA, BIT3, GPIO_MODE_OUTPUT);
PA3=1;
#else
/* TBD.. */
#endif
}
bool CheckLowBattery()
{
if(GetBattery()<BAT_TH)
return true;
else
return false;
}
void UpdateBattery()
{
EADC_START_CONV(EADC, 0x1);
AdcData = EADC_GET_CONV_DATA(EADC, 0);
}
uint8_t GetBattery()
{
int8_t CurrentBattery;
CurrentBattery = ((AdcData-2700)*100/(3240-2700));
BatteryPercent = BatteryPercent*0.95f + CurrentBattery*0.05f;
//BatteryPercent = CurrentBattery;
if(BatteryPercent<0)
BatteryPercent = 0;
else if(BatteryPercent>100)
BatteryPercent = 100;
return BatteryPercent;
}