123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- #include "includes.h"
- int g_iVbat=345;//345
- void ADCInit(void)
- {
-
- ADC_InitTypeDef ADC_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE);
-
- GPIO_InitStructure.GPIO_Pin =GPIO_Pin_0;
- GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AIN;
- GPIO_Init(GPIOA,&GPIO_InitStructure); //默认速度为两兆
- //VOL_ADC_PIN
- GPIO_InitStructure.GPIO_Pin =VOL_ADC_PIN;
- GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AIN;
- GPIO_Init(VOL_ADC_PORT,&GPIO_InitStructure); //默认速度为两兆
-
- //配置ADC的运行:
- ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //独立模式
- ADC_InitStructure.ADC_ScanConvMode =DISABLE; //连续多通道模式
- ADC_InitStructure.ADC_ContinuousConvMode =DISABLE;//ENABLE; //连续转换
- ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //转换不受外界决定
- ADC_InitStructure.ADC_DataAlign =ADC_DataAlign_Right; //右对齐
- ADC_InitStructure.ADC_NbrOfChannel =1; //扫描通道数
- ADC_Init(ADC1,&ADC_InitStructure);
- ADC_Cmd (ADC1,ENABLE); //使能或者失能指定的ADC
-
- }
- void KEYADCInit(void)
- {
- ADC_InitTypeDef ADC_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
- GPIO_InitStructure.GPIO_Pin =GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IPU;//先内部上拉模式给IO充能
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- DelayMs(100);
- GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AIN;
- GPIO_Init(GPIOA,&GPIO_InitStructure); //默认速度为两兆
- DelayMs(200);//让电平恢复正常
- //配置ADC的运行:
- ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //独立模式
- ADC_InitStructure.ADC_ScanConvMode =DISABLE; //连续多通道模式
- ADC_InitStructure.ADC_ContinuousConvMode =DISABLE;//ENABLE; //连续转换
- ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //转换不受外界决定
- ADC_InitStructure.ADC_DataAlign =ADC_DataAlign_Right; //右对齐
- ADC_InitStructure.ADC_NbrOfChannel =1; //扫描通道数
- ADC_Init(ADC2,&ADC_InitStructure);
- // ADC_RegularChannelConfig(ADC2,ADC_Channel_1, 1,ADC_SampleTime_1Cycles5); //通道X,采样时间为1.5周期,1代表规则通道第1个这个1是啥意思我不太清楚只有是1的时候我的ADC才正常。
- ADC_Cmd (ADC2,ENABLE); //使能或者失能指定的ADC
- ADC_SoftwareStartConvCmd(ADC2,ENABLE);//使能或者失能指定的ADC的软件转换启动功能
- }
- /***********************************************************************
- *获取电池电压
- 返回值: -1为无效 ; 正数表示电池电压放大100倍,如 382表示3.82V
- ***********************************************************************/
- int GetVbat(void)
- {
- char buf[30];
- static int Ct=0;
- static int siVbat[10]={0,0,0,0,0,0,0,0,0,0};
- int iVbat[10];
- int max[5];
- int sum;
- int i,j,k;
-
- int adc;
- int Vbat;
-
- ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_1Cycles5 );
- ADC_SoftwareStartConvCmd(ADC1,ENABLE);
- while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
- adc=ADC_GetConversionValue(ADC1);
- if(adc<100 && adc>4095)
- {
- printf("VDAC OVER");
- return -1;
- }
- //循环存储
- siVbat[Ct]=adc;
- if(++Ct>9)Ct=0;
- //printf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\r\n",siVbat[0],siVbat[1],siVbat[2],siVbat[3],siVbat[4],siVbat[5],siVbat[6],siVbat[7],siVbat[8],siVbat[9]);
- //滤波处理,排列取3个最大值,3个最大中去掉第一大,其余求平均
- //拷贝
- for(i=0;i<10;i++){
- iVbat[i]=siVbat[i];
- }
- for(i=0;i<4;i++){
- max[i]=0;
- for(j=0;j<10;j++){
- if(iVbat[j]>max[i]){
- max[i]=iVbat[j];
- k=j;
- }
- }
- iVbat[k]=0;
- }
- // printf("%d,%d,%d\r\n",max[0],max[1],max[2]);
- sum=max[1]+max[2];
- if(max[2]<200)
- {
- //printf("ERR3");
- return -1;
- }
- Vbat=sum*308/4096;
- return Vbat;
- }
- /*******************************************************
- *CheckVbat
- *检查电池电压,控制低压报警,
- *低压关机已由硬件监测
- ******************************************************/
- void CheckVbat(int Vbat)
- {
- static unsigned char sucCt=0;//提示“请充电”计数器
-
- //if(modemInitStatus==0)return;//模块初始化完成后才操作
- if(Vbat<MIN_PWR_LEVEL){//
- SlwTrace(INF,"Vbat low!shutdown now!",1);
- //关闭模块
- if(sutPocStatus.ModemPowerOn==1){
- // //发送机指令并等待7秒
- // cmdShutDown(1);
- }
- ModemSendAt("AT$MYPOWEROFF\r\n");
- //发送机指令并等待7秒
- sutProductPara.SleepFlag=1;
- SaveProductParaToFlash();
- cmdShutDown(1);
- SpeakerDisable();
- PWR_EN_LOW;
- while(1);
- }
- else if(Vbat<WARN_PWR_LEVEL){//提示请充电
- if(sucCt==60){
- MeSpeak(ENCODE_ASCII,"请充电");
- LCDBackLight(1);
- LcdOnTimeCt=0;
- SlwTrace(INF, "Charge bat!!",1);
- }
- if(++sucCt>180)sucCt=0;
-
- }else sucCt=0;
-
- //printf("Vbat=====%d\r\n",g_iVbat);~
-
- }
- void BatterProcess(void){
- //static char tick=0;
- int vbat;
- vbat=GetVbat();
- // printf("VBAT=====%d\r\n",vbat);
- if(vbat > 0) g_iVbat=vbat;
- // if(++tick<=3) return;
- // tick=0;
- CheckVbat(g_iVbat);
- }
- unsigned short getBat(void){
- unsigned short ret;
- ret=(g_iVbat-MIN_PWR_LEVEL) * 100 / (400-MIN_PWR_LEVEL);
- return ((ret>100)?100:ret);
- }
- int GetKeyVbat(void)
- {
- int adc;
- int Vbat;
- ADC_RegularChannelConfig(ADC2,ADC_Channel_1, 1,ADC_SampleTime_1Cycles5); //通道X,采样时间为1.5周期,1代表规则通道第1个这个1是啥意思我不太清楚只有是1的时候我的ADC才正常。
- ADC_SoftwareStartConvCmd(ADC2,ENABLE);
- while(ADC_GetFlagStatus(ADC2, ADC_FLAG_EOC) == RESET);
- adc=ADC_GetConversionValue(ADC2);
- if(adc<100 && adc>4095)
- {
- printf("VDAC OVER");
- return -1;
- }
-
- Vbat=adc*300/4096;
- return Vbat;
- }
- int GetPowerVbat(void)
- {
- int adc;
- int Vbat;
- ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_1Cycles5 );
- ADC_SoftwareStartConvCmd(ADC1,ENABLE);
-
- while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
-
- adc=ADC_GetConversionValue(ADC1);
- if(adc<100 && adc>4095)
- {
- printf("VDAC OVER");
- return -1;
- }
- //Vbat=adc*300/4096;
- Vbat=adc;
- return Vbat;
- }
|