adc.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #include "includes.h"
  2. int g_iVbat=345;//345
  3. void ADCInit(void)
  4. {
  5. ADC_InitTypeDef ADC_InitStructure;
  6. GPIO_InitTypeDef GPIO_InitStructure;
  7. RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
  8. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE);
  9. //电压ADC检测
  10. GPIO_InitStructure.GPIO_Pin =GPIO_Pin_0;
  11. GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AIN;
  12. GPIO_Init(GPIOA,&GPIO_InitStructure); //默认速度为两兆
  13. //音量旋钮ADC检测
  14. //VOL_ADC_PIN
  15. GPIO_InitStructure.GPIO_Pin =VOL_ADC_PIN;
  16. GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AIN;
  17. GPIO_Init(VOL_ADC_PORT,&GPIO_InitStructure); //默认速度为两兆
  18. //配置ADC的运行:
  19. ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //独立模式
  20. ADC_InitStructure.ADC_ScanConvMode =DISABLE; //连续多通道模式
  21. ADC_InitStructure.ADC_ContinuousConvMode =DISABLE;//ENABLE; //连续转换
  22. ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //转换不受外界决定
  23. ADC_InitStructure.ADC_DataAlign =ADC_DataAlign_Right; //右对齐
  24. ADC_InitStructure.ADC_NbrOfChannel =1; //扫描通道数
  25. ADC_Init(ADC1,&ADC_InitStructure);
  26. ADC_Cmd (ADC1,ENABLE); //使能或者失能指定的ADC
  27. }
  28. void KEYADCInit(void)
  29. {
  30. ADC_InitTypeDef ADC_InitStructure;
  31. GPIO_InitTypeDef GPIO_InitStructure;
  32. RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2,ENABLE);
  33. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  34. //按键ADC 检测
  35. GPIO_InitStructure.GPIO_Pin =GPIO_Pin_1;
  36. GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IPU;//先内部上拉模式给IO充能
  37. GPIO_Init(GPIOA,&GPIO_InitStructure);
  38. DelayMs(100);
  39. GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AIN;
  40. GPIO_Init(GPIOA,&GPIO_InitStructure); //默认速度为两兆
  41. DelayMs(200);//让电平恢复正常
  42. //配置ADC的运行:
  43. ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //独立模式
  44. ADC_InitStructure.ADC_ScanConvMode =DISABLE; //连续多通道模式
  45. ADC_InitStructure.ADC_ContinuousConvMode =DISABLE;//ENABLE; //连续转换
  46. ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //转换不受外界决定
  47. ADC_InitStructure.ADC_DataAlign =ADC_DataAlign_Right; //右对齐
  48. ADC_InitStructure.ADC_NbrOfChannel =1; //扫描通道数
  49. ADC_Init(ADC2,&ADC_InitStructure);
  50. // ADC_RegularChannelConfig(ADC2,ADC_Channel_1, 1,ADC_SampleTime_1Cycles5); //通道X,采样时间为1.5周期,1代表规则通道第1个这个1是啥意思我不太清楚只有是1的时候我的ADC才正常。
  51. ADC_Cmd (ADC2,ENABLE); //使能或者失能指定的ADC
  52. ADC_SoftwareStartConvCmd(ADC2,ENABLE);//使能或者失能指定的ADC的软件转换启动功能
  53. }
  54. /***********************************************************************
  55. *获取电池电压
  56. 返回值: -1为无效 ; 正数表示电池电压放大100倍,如 382表示3.82V
  57. ***********************************************************************/
  58. int GetVbat(void)
  59. {
  60. char buf[30];
  61. static int Ct=0;
  62. static int siVbat[10]={0,0,0,0,0,0,0,0,0,0};
  63. int iVbat[10];
  64. int max[5];
  65. int sum;
  66. int i,j,k;
  67. int adc;
  68. int Vbat;
  69. ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_1Cycles5 );
  70. ADC_SoftwareStartConvCmd(ADC1,ENABLE);
  71. while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
  72. adc=ADC_GetConversionValue(ADC1);
  73. if(adc<100 && adc>4095)
  74. {
  75. printf("VDAC OVER");
  76. return -1;
  77. }
  78. //循环存储
  79. siVbat[Ct]=adc;
  80. if(++Ct>9)Ct=0;
  81. //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]);
  82. //滤波处理,排列取3个最大值,3个最大中去掉第一大,其余求平均
  83. //拷贝
  84. for(i=0;i<10;i++){
  85. iVbat[i]=siVbat[i];
  86. }
  87. for(i=0;i<4;i++){
  88. max[i]=0;
  89. for(j=0;j<10;j++){
  90. if(iVbat[j]>max[i]){
  91. max[i]=iVbat[j];
  92. k=j;
  93. }
  94. }
  95. iVbat[k]=0;
  96. }
  97. // printf("%d,%d,%d\r\n",max[0],max[1],max[2]);
  98. sum=max[1]+max[2];
  99. if(max[2]<200)
  100. {
  101. //printf("ERR3");
  102. return -1;
  103. }
  104. Vbat=sum*308/4096;
  105. return Vbat;
  106. }
  107. /*******************************************************
  108. *CheckVbat
  109. *检查电池电压,控制低压报警,
  110. *低压关机已由硬件监测
  111. ******************************************************/
  112. void CheckVbat(int Vbat)
  113. {
  114. static unsigned char sucCt=0;//提示“请充电”计数器
  115. //if(modemInitStatus==0)return;//模块初始化完成后才操作
  116. if(Vbat<MIN_PWR_LEVEL){//
  117. SlwTrace(INF,"Vbat low!shutdown now!",1);
  118. //关闭模块
  119. if(sutPocStatus.ModemPowerOn==1){
  120. // //发送机指令并等待7秒
  121. // cmdShutDown(1);
  122. }
  123. ModemSendAt("AT$MYPOWEROFF\r\n");
  124. //发送机指令并等待7秒
  125. sutProductPara.SleepFlag=1;
  126. SaveProductParaToFlash();
  127. cmdShutDown(1);
  128. SpeakerDisable();
  129. PWR_EN_LOW;
  130. while(1);
  131. }
  132. else if(Vbat<WARN_PWR_LEVEL){//提示请充电
  133. if(sucCt==60){
  134. MeSpeak(ENCODE_ASCII,"请充电");
  135. LCDBackLight(1);
  136. LcdOnTimeCt=0;
  137. SlwTrace(INF, "Charge bat!!",1);
  138. }
  139. if(++sucCt>180)sucCt=0;
  140. }else sucCt=0;
  141. //printf("Vbat=====%d\r\n",g_iVbat);~
  142. }
  143. void BatterProcess(void){
  144. //static char tick=0;
  145. int vbat;
  146. vbat=GetVbat();
  147. printf("volVBAT=====%d\r\n",GetPowerVbat());
  148. if(vbat > 0) g_iVbat=vbat;
  149. // if(++tick<=3) return;
  150. // tick=0;
  151. CheckVbat(g_iVbat);
  152. }
  153. unsigned short getBat(void){
  154. unsigned short ret;
  155. ret=(g_iVbat-MIN_PWR_LEVEL) * 100 / (400-MIN_PWR_LEVEL);
  156. return ((ret>100)?100:ret);
  157. }
  158. int GetKeyVbat(void)
  159. {
  160. int adc;
  161. int Vbat;
  162. ADC_RegularChannelConfig(ADC2,ADC_Channel_1, 1,ADC_SampleTime_1Cycles5); //通道X,采样时间为1.5周期,1代表规则通道第1个这个1是啥意思我不太清楚只有是1的时候我的ADC才正常。
  163. ADC_SoftwareStartConvCmd(ADC2,ENABLE);
  164. while(ADC_GetFlagStatus(ADC2, ADC_FLAG_EOC) == RESET);
  165. adc=ADC_GetConversionValue(ADC2);
  166. if(adc<100 && adc>4095)
  167. {
  168. printf("VDAC OVER");
  169. return -1;
  170. }
  171. Vbat=adc*300/4096;
  172. return Vbat;
  173. }
  174. int GetPowerVbat(void)
  175. {
  176. int adc;
  177. int Vbat;
  178. ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_1Cycles5 );
  179. ADC_SoftwareStartConvCmd(ADC1,ENABLE);
  180. while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
  181. adc=ADC_GetConversionValue(ADC1);
  182. if(adc<100 && adc>4095)
  183. {
  184. printf("VDAC OVER");
  185. return -1;
  186. }
  187. Vbat=adc;
  188. return Vbat;
  189. }
  190. char SetSpkLvProcess(char update)
  191. {
  192. short templv=0;
  193. static char LastTemplv=0;
  194. static unsigned char Lastnum=0;//0
  195. static short SpkCnt=0;
  196. unsigned char i=0;
  197. char buf[30];
  198. //4- 655
  199. const unsigned short VolArr[VOL_LEV_NUM]={100,200,410,820,1230,1640,2050,2460,3000,3500,4090};//,4090};
  200. templv=GetPowerVbat();
  201. LastTemplv=Lastnum;
  202. for(i=0;i<VOL_LEV_NUM;i++)
  203. {
  204. if(LastTemplv==0){
  205. if(LastTemplv==0)if(templv>VolArr[LastTemplv]+VOL_LEV_DEF)LastTemplv++;
  206. }else if(LastTemplv<VOL_LEV_NUM-1){
  207. if(templv>=VolArr[LastTemplv]+VOL_LEV_DEF)LastTemplv++;
  208. else if(templv<VolArr[LastTemplv-1]-VOL_LEV_DEF)LastTemplv--;
  209. }else {
  210. if(LastTemplv==VOL_LEV_NUM-1)if(templv<VolArr[LastTemplv-1]-VOL_LEV_DEF)LastTemplv--;
  211. }
  212. }
  213. // printf("templv=%d----num===%d\r\n",templv,LastTemplv);
  214. if(update==1) return LastTemplv;
  215. if(LastTemplv!=Lastnum||update==2){
  216. if(LastTemplv!=0)SpeakerEnable();
  217. printf("templv=%d----num===%d\r\n",templv,LastTemplv);
  218. if(update==2){
  219. Lastnum=LastTemplv;
  220. newPara.VolumLv=LastTemplv;
  221. //FMSetVolume(Lastnum,1);
  222. }
  223. else{
  224. if(sutUIstatus.Status==UIS_STANDBY)
  225. {
  226. //GuiDrawRect(26,110,106,124,1);//115
  227. if(LastTemplv<Lastnum)GuiClearRect(27+LastTemplv*10,110+1,105,123);
  228. //GuiFillRect(26,110+1,26+LastTemplv*10-1,124-1,COLOR_BLUE);// COLOR_BLUE
  229. snprintf(buf,sizeof(buf),"音量%02d",LastTemplv);
  230. GuiShowStr(44,110,buf,0x01,0);
  231. }
  232. Lastnum=LastTemplv;
  233. newPara.VolumLv=LastTemplv;
  234. //FMSetVolume(LastTemplv,1);
  235. }
  236. snprintf(buf,sizeof(buf),"音量 %d",Lastnum);
  237. printf(buf);
  238. snprintf(buf,sizeof(buf),"AT+CLVL=%d\r\n",Lastnum*10);//10
  239. ModemSendAt(buf);
  240. if(hgs_poc_type==POC_OWN){
  241. snprintf(buf,sizeof(buf),"AT+CRSL=%d\r\n",Lastnum*10);//10
  242. ModemSendAt(buf);
  243. }else {
  244. if(newPara.KeySound!=0){
  245. snprintf(buf,sizeof(buf),"AT+POC=tonevol=%d\r\n",Lastnum);
  246. ModemSendAt(buf);
  247. }
  248. snprintf(buf,sizeof(buf),"AT+POC=ttsvol=%d\r\n",(Lastnum));
  249. ModemSendAt(buf);
  250. }
  251. LCDBackLight(1);
  252. LcdOnTimeCt=0;
  253. SpkCnt=300;
  254. }
  255. if(SpkCnt>0){
  256. if((--SpkCnt==0)&&(sutUIstatus.Status==UIS_STANDBY)){
  257. GuiClearRect(44,110,100,126);
  258. // if(g_uslockflag==1){
  259. // GuiShowBmp(56,110,"lock.bmp");
  260. // }
  261. if(sutPocStatus.Speaker==0&&sutPocStatus.TTS==0) SpeakerDisable();
  262. }
  263. }
  264. return LastTemplv;
  265. }