久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费
標題:
stm32學習之adc驅動
[打印本頁]
作者:
liuqq
時間:
2015-5-21 23:21
標題:
stm32學習之adc驅動
/*******************************************************************************************************
文件名稱:ADC驅動
編寫版本:V0.1
編寫日期:2012.10.25
********************************************************************************************************/
#include "ADC.h"
char adcRunFlag=0;
void AD_GPIO(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PB9輸出
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; //PE0,1輸出
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PD7擴展板之舵機部分
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
//所有其他懸空IO口設為下拉輸入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; //PA14
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; //PB8,9
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PC10,14,15
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PD0~7
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; //PE0,1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
}
void AD_Init(void)
{
AD_GPIO();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, DISABLE);
/* ADC1 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 reset calibaration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibaration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
difCount=3;
ExtendPort = GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_8);
ExtendServo = GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_7);
}
int AD_Get(u8 channel)
{
int count;
int ADC1ConvertedValue;
GPIO_InitTypeDef GPIO_InitStructure;
adcRunFlag++;
if((channel==14)&&(StateEx1))
{
StateEx1=0;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
if((channel==15)&&(StateEx2))
{
StateEx2=0;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
/* ADC1 regular channels configuration */
ADC_RegularChannelConfig(ADC1, ChannelArr[channel], 1, ADC_SampleTime_71Cycles5);
ADC_ClearFlag(ADC1, ADC_FLAG_EOC);
ADC_Cmd(ADC1, ENABLE);
count=0;
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC))
{
count++;
if(count==100)
{
adcRunFlag=0;
return ADCLast;
}
}
ADC1ConvertedValue = ADC_GetConversionValue(ADC1);
ADCLast=ADC1ConvertedValue;
adcRunFlag--;
return ADC1ConvertedValue;
}
int analog(u8 port) //1~12,13~20,21~28
{
if(port<1||port>36)
return -1;
else
{
if(port<13) //AD1-12
{
return AD_Get(port);
}
else if(port<21) //13-20沒有AD功能
{
return -1;
}
else if(port<29) //AD21-28
{
if(ExtendPort)
{
//4051選擇
Select4051(port-21);
return AD_Get(15);
}
else
{
return -1;
}
}
else if(port<37) //AD29-36
{
if(ExtendPort)
{
//4051選擇
Select4051(port-29);
return AD_Get(14);
}
else
{
return -1;
}
}
else
{
return -1;
}
}
}
int battery(void)
{
return AD_Get(16);
}
void Select4051(u8 ch)
{
switch(ch)
{
case 0:
Set4051A();
Clr4051B();
Set4051C();
break;
case 1:
Set4051A();
Set4051B();
Set4051C();
break;
case 2:
Clr4051A();
Clr4051B();
Set4051C();
break;
case 3:
Clr4051A();
Set4051B();
Set4051C();
break;
case 4:
Clr4051A();
Clr4051B();
Clr4051C();
break;
case 5:
Set4051A();
Set4051B();
Clr4051C();
break;
case 6:
Set4051A();
Clr4051B();
Clr4051C();
break;
case 7:
Clr4051A();
Set4051B();
Clr4051C();
break;
default:
break;
}
}
void SysTick_Handler_bat_callback(void)
{
if(battery()<2300)
LED_R(1);
else
LED_R(0);
}
復制代碼
歡迎光臨 (http://www.zg4o1577.cn/bbs/)
Powered by Discuz! X3.1
主站蜘蛛池模板:
国产精品福利视频
|
国精产品一品二品国精在线观看
|
911影院
|
av在线一区二区三区
|
日本精品视频
|
精品国产一区一区二区三亚瑟
|
精品毛片
|
请别相信他免费喜剧电影在线观看
|
亚洲国产二区
|
波波电影院一区二区三区
|
www.888www看片
|
日韩电影在线一区
|
黄页网址在线观看
|
在线观看视频91
|
国产精品99久久久久久www
|
91精品久久久久久久99
|
一区二区在线
|
欧美激情久久久
|
国产一区二区三区在线
|
亚洲在线一区二区
|
国产1区
|
国产香蕉视频在线播放
|
能看的av
|
www.国产日本
|
美日韩免费视频
|
国产原创在线观看
|
草久久久
|
精彩视频一区二区三区
|
国产农村妇女毛片精品久久麻豆
|
色婷婷综合久久久中字幕精品久久
|
免费一级毛片
|
伊人网站
|
久久久久国产精品一区
|
欧美日韩不卡合集视频
|
在线一区二区三区
|
欧美成人一区二区三区
|
中文字幕一区二区三区在线观看
|
黄色免费观看
|
黄色一级大片在线免费看产
|
玖玖免费
|
日韩1区
|