ADC获取滑块的值(8通道)

时间:2022-12-09 06:20:11

#include "TgcConfig.h"
#include "my_usb.h"

/*******************************************************************************
* Function Name : TgcAdcInitial
* Description : ADC1³õʼ»¯£¬8¹æÔòͨµÀ
* Input : ÎÞ
* Return : ÎÞ
*******************************************************************************/
void TgcAdcInitial(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_ADC1 , ENABLE ); //ʹÄÜADC1ͨµÀʱÖÓ

RCC_ADCCLKConfig(RCC_PCLK2_Div6); //ÉèÖÃADC·ÖƵÒò×Ó6 72M/6=12,ADC×î´óʱ¼ä²»Äܳ¬¹ý14M

//PA1 ×÷ΪģÄâͨµÀÊäÈëÒý½Å
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);

ADC_DeInit(ADC1); //¸´Î»ADC1

ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC¹¤×÷ģʽ:ADC1ºÍADC2¹¤×÷ÔÚ¶ÀÁ¢Ä£Ê½
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Êý¾ÝÓÒ¶ÔÆë
ADC_InitStructure.ADC_NbrOfChannel = 1; //˳Ðò½øÐйæÔòת»»µÄADCͨµÀµÄÊýÄ¿
ADC_Init(ADC1, &ADC_InitStructure); //¸ù¾ÝADC_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèADCxµÄ¼Ä´æÆ÷

ADC_Cmd(ADC1, ENABLE); //ʹÄÜÖ¸¶¨µÄADC1

ADC_ResetCalibration(ADC1); //ʹÄܸ´Î»Ð£×¼

while(ADC_GetResetCalibrationStatus(ADC1)); //µÈ´ý¸´Î»Ð£×¼½áÊø

ADC_StartCalibration(ADC1); //¿ªÆôADУ׼

while(ADC_GetCalibrationStatus(ADC1)); //µÈ´ýУ׼½áÊø

// ADC_SoftwareStartConvCmd(ADC1, ENABLE); //ʹÄÜÖ¸¶¨µÄADC1µÄÈí¼þת»»Æô¶¯¹¦ÄÜ

}

/*******************************************************************************
* Function Name : AdcGetData
* Description : »ñȡͨµÀÖµ
* Input : ͨµÀ
* Return : ÎÞ
*******************************************************************************/
int AdcGetData(int Channel)
{
int GetData;
//ÉèÖÃÖ¸¶¨ADCµÄ¹æÔò×éͨµÀ£¬Ò»¸öÐòÁУ¬²ÉÑùʱ¼ä
ADC_RegularChannelConfig(ADC1, Channel, 1, ADC_SampleTime_239Cycles5 ); //ADC1,ADCͨµÀ,²ÉÑùʱ¼äΪ239.5ÖÜÆÚ

ADC_SoftwareStartConvCmd(ADC1, ENABLE); //ʹÄÜÖ¸¶¨µÄADC1µÄÈí¼þת»»Æô¶¯¹¦ÄÜ

while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));//µÈ´ýת»»½áÊø

GetData = ADC_GetConversionValue(ADC1);

GetData = GetData * 100 / 4095; //Ó³Éäµ½0-100

return GetData; //·µ»Ø×î½üÒ»´ÎADC1¹æÔò×éµÄת»»½á¹û
}

/*******************************************************************************
* Function Name : TgcGetData
* Description : »ñȡÿ¸öͨµÀƽ¾ùÖµ£¬È¡10´Î
* Input : ͨµÀ
* Return : ƽ¾ùÖµ
*******************************************************************************/
int TgcGetData(int Channel)
{
int i;
int DataSum = 0;
for(i = 10; i > 0; i--)
DataSum = DataSum + AdcGetData(Channel);

return DataSum/10;
}

/*******************************************************************************
* Function Name : TgcSendUsb
* Description : ͨ¹ýUSB½«Ã¿¸öͨµÀÖµ·¢Ë͸øÉÏλ»ú
* Input : ÎÞ
* Return : ÎÞ
*******************************************************************************/
void TgcSendUsb(void)
{
uint8_t UsbDataSend[4];
UsbDataSend[0] = 0x02;
UsbDataSend[2] = 0x00;

if(TgcTimeCnt > 20)
{
TgcTimeCnt = 0;
//TGCͨµÀ0·¢ËÍ
UsbDataSend[1] = 0x00; //channel_0
UsbDataSend[3] = TgcGetData(TgcChannel_0);
if(UsbDataSend[3] != TgcOldData_0)
UsbSendData(UsbDataSend,4);
TgcOldData_0 = UsbDataSend[3];

//TGCͨµÀ1·¢ËÍ
UsbDataSend[1] = 0x01; //channel_1
UsbDataSend[3] = TgcGetData(TgcChannel_1);
if(UsbDataSend[3] != TgcOldData_1)
UsbSendData(UsbDataSend,4);
TgcOldData_1 = UsbDataSend[3];

//TGCͨµÀ2·¢ËÍ
UsbDataSend[1] = 0x02; //channel_2
UsbDataSend[3] = TgcGetData(TgcChannel_2);
if(UsbDataSend[3] != TgcOldData_2)
UsbSendData(UsbDataSend,4);
TgcOldData_2 = UsbDataSend[3];

//TGCͨµÀ3·¢ËÍ
UsbDataSend[1] = 0x03; //channel_3
UsbDataSend[3] = TgcGetData(TgcChannel_3);
if(UsbDataSend[3] != TgcOldData_3)
UsbSendData(UsbDataSend,4);
TgcOldData_3 = UsbDataSend[3];

//TGCͨµÀ4·¢ËÍ
UsbDataSend[1] = 0x04; //channel_4
UsbDataSend[3] = TgcGetData(TgcChannel_4);
if(UsbDataSend[3] != TgcOldData_4)
UsbSendData(UsbDataSend,4);
TgcOldData_4 = UsbDataSend[3];

//TGCͨµÀ5·¢ËÍ
UsbDataSend[1] = 0x05; //channel_5
UsbDataSend[3] = TgcGetData(TgcChannel_5);
if(UsbDataSend[3] != TgcOldData_5)
UsbSendData(UsbDataSend,4);
TgcOldData_5 = UsbDataSend[3];

//TGCͨµÀ6·¢ËÍ
UsbDataSend[1] = 0x06; //channel_6
UsbDataSend[3] = TgcGetData(TgcChannel_6);
if(UsbDataSend[3] != TgcOldData_6)
UsbSendData(UsbDataSend,4);
TgcOldData_6 = UsbDataSend[3];

//TGCͨµÀ7·¢ËÍ
UsbDataSend[1] = 0x07; //channel_7
UsbDataSend[3] = TgcGetData(TgcChannel_7);
if(UsbDataSend[3] != TgcOldData_7)
UsbSendData(UsbDataSend,4);
TgcOldData_7 = UsbDataSend[3];
}

if(Tgc1sCnt > 2000)
{
Tgc1sCnt = 0;
//TGCͨµÀ0·¢ËÍ
UsbDataSend[1] = 0x00; //channel_0
UsbDataSend[3] = TgcGetData(TgcChannel_0);
UsbSendData(UsbDataSend,4);
while(Tgc1sCnt < 20);
Tgc1sCnt = 0;
//TGCͨµÀ1·¢ËÍ
UsbDataSend[1] = 0x01; //channel_1
UsbDataSend[3] = TgcGetData(TgcChannel_1);
UsbSendData(UsbDataSend,4);
while(Tgc1sCnt < 20);
Tgc1sCnt = 0;
//TGCͨµÀ2·¢ËÍ
UsbDataSend[1] = 0x02; //channel_2
UsbDataSend[3] = TgcGetData(TgcChannel_2);
UsbSendData(UsbDataSend,4);
while(Tgc1sCnt < 20);
Tgc1sCnt = 0;
//TGCͨµÀ3·¢ËÍ
UsbDataSend[1] = 0x03; //channel_3
UsbDataSend[3] = TgcGetData(TgcChannel_3);
UsbSendData(UsbDataSend,4);
while(Tgc1sCnt < 20);
Tgc1sCnt = 0;
//TGCͨµÀ4·¢ËÍ
UsbDataSend[1] = 0x04; //channel_4
UsbDataSend[3] = TgcGetData(TgcChannel_4);
UsbSendData(UsbDataSend,4);
while(Tgc1sCnt < 20);
Tgc1sCnt = 0;
//TGCͨµÀ5·¢ËÍ
UsbDataSend[1] = 0x05; //channel_5
UsbDataSend[3] = TgcGetData(TgcChannel_5);
UsbSendData(UsbDataSend,4);
while(Tgc1sCnt < 20);
Tgc1sCnt = 0;
//TGCͨµÀ6·¢ËÍ
UsbDataSend[1] = 0x06; //channel_6
UsbDataSend[3] = TgcGetData(TgcChannel_6);
UsbSendData(UsbDataSend,4);
while(Tgc1sCnt < 20);
Tgc1sCnt = 0;
//TGCͨµÀ7·¢ËÍ
UsbDataSend[1] = 0x07; //channel_7
UsbDataSend[3] = TgcGetData(TgcChannel_7);
UsbSendData(UsbDataSend,4);
}
}