TA的每日心情 | 奋斗 2013-7-4 23:49 |
---|
签到天数: 21 天 连续签到: 1 天 [LV.4]偶尔看看III
|
楼主 |
发表于 2013-5-24 13:14:01
|
显示全部楼层
然后通过DAC,就可以获得各个电位器中间点的位置,从而获取摇杆位置。
下面是使用DMA来读取DAC的STM32代码:
void Joystick_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = Joystick_Address;
DMA_InitStructure.DMA_MemoryBaseAddr =(u32)Joystick_RCVTab;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 5;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
DMA_Cmd(DMA1_Channel1, ENABLE); //etootle: Enable DMA1 channel1
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 5;
ADC_Init(ADC1,&ADC_InitStructure);
ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_1,2,ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_2,3,ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_3,4,ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_4,5,ADC_SampleTime_239Cycles5);
ADC_DMACmd(ADC1,ENABLE);
ADC_Cmd(ADC1,ENABLE); //etootle: Enable ADC
ADC_ResetCalibration(ADC1); //etootle: ADC calibration
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
ADC_SoftwareStartConvCmd(ADC1,ENABLE); //etootle: ADC start
}
void Joystick_Show(void)
{
ETOOTLE_LCD_SETXY(0x05,0x00); //Check ARM ID
ETOOTLE_LCD_Show_Num((Joystick_RCVTab[1] & 0xff00)>>8);
ETOOTLE_LCD_Show_Num(Joystick_RCVTab[1] & 0xff);
ETOOTLE_LCD_SETXY(0x05,0x05); //Check ARM ID
ETOOTLE_LCD_Show_Num((Joystick_RCVTab[3] & 0xff00)>>8);
ETOOTLE_LCD_Show_Num(Joystick_RCVTab[3] & 0xff);
ETOOTLE_LCD_SETXY(0x06,0x00); //
ETOOTLE_LCD_Show_Num((Joystick_RCVTab[0] & 0xff00)>>8);
ETOOTLE_LCD_Show_Num(Joystick_RCVTab[0] & 0xff);
ETOOTLE_LCD_SETXY(0x06,0x05); //Check ARM ID
ETOOTLE_LCD_Show_Num((Joystick_RCVTab[4] & 0xff00)>>8);
ETOOTLE_LCD_Show_Num(Joystick_RCVTab[4] & 0xff);
}
void Button_Show(void)
{
u8 i;
Button_RCVTab[0]=GPIO_ReadInputDataBit(GPIOB,BUTTOM_LEFT);
Button_RCVTab[1]=GPIO_ReadInputDataBit(GPIOB,BUTTOM_SW1);
Button_RCVTab[2]=GPIO_ReadInputDataBit(GPIOB,BUTTOM_SW2);
Button_RCVTab[3]=GPIO_ReadInputDataBit(GPIOB,BUTTOM_SW3);
Button_RCVTab[4]=GPIO_ReadInputDataBit(GPIOB,BUTTOM_SW4);
Button_RCVTab[5]=GPIO_ReadInputDataBit(GPIOB,BUTTOM_RIGHT);
ETOOTLE_LCD_SETXY(0x07,0x01);
for(i=0;i<6;i++)
{
if((Button_RCVTab[i]& 0x01)==0) ETOOTLE_LCD_Show_Char('O');
else ETOOTLE_LCD_Show_Char('X');
ETOOTLE_LCD_Show_Char(' ');
}
}
|
|