PS2 游戏双轴摇杆传感器模块由采用原装优质金属 PS2 摇杆电位器制 作,具有(X,Y)2 轴模拟输出,在设计上把 X,Y,Z 轴的电路都单独引出,可以直接用杜邦线连接到MCU。
![image.png](//file.elecfans.com/web2/M00/4F/C5/pYYBAGLEBmiASV1QAAF0K54w2S4321.png)
CH32V307PS2具有丰富大的片上资源,本次仅使用ADC1的通道0.
#include "adc.h"
void ADC_Function_Init(void)
{
ADC_InitTypeDef ADC_InitStructure={0};
GPIO_InitTypeDef GPIO_InitStructure={0};
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE );
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE );
RCC_ADCCLKConfig(RCC_PCLK2_Div8);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_DeInit(ADC1);
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);
ADC_Cmd(ADC1, ENABLE);
ADC_BufferCmd(ADC1, DISABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
Calibrattion_Val = Get_CalibrationValue(ADC1);
ADC_BufferCmd(ADC1, ENABLE);
ADC_TempSensorVrefintCmd(ENABLE);
}
int main(void)
{
u16 ADC_val;
Delay_Init();
USART_Printf_Init(115200);
printf("系统时钟:%d\r\n",SystemCoreClock);
ADC_Function_Init();
while(1)
{
ADC_val = Get_ADC_Average( ADC_Channel_0, 10 );
Delay_Ms(500);
ADC_val = Get_ConversionVal(ADC_val);
printf( "遥控数据%04d\r\n", ADC_val);
Delay_Ms(2);
}
}
![image.png](//file.elecfans.com/web2/M00/4F/2D/poYBAGLEDJiAG867AAFmVQruRwY175.png)
![7A9904BF4824DBA3B4F075234DFD50A8.jpg](//file.elecfans.com/web2/M00/4F/C7/pYYBAGLEDMqAD8tWAA8CYZtBDws702.jpg)
|