如何使用STM8S单片机的多通道AD转换
1个回答
展开全部
我给你个例程,我这是两个通道,一个是采集音频,然后彩灯随音乐节奏闪烁,同时采集电池电量。
void AD2_Config(void)
{
/* Init GPIO for ADC2 */
GPIO_Init(GPIOB, GPIO_PIN_6, GPIO_MODE_IN_FL_NO_IT);
/* De-Init ADC peripheral*/
ADC1_DeInit();
ADC1_ClearITPendingBit(ADC1_IT_EOC);
/* Init ADC1 peripheral */
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CHANNEL_6, ADC1_PRESSEL_FCPU_D18, \
ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL6,\
DISABLE);
/* Enable EOC interrupt */
//if(autoFm == 1)
ADC1_ITConfig(ADC1_IT_EOCIE,DISABLE);
//else
// ADC1_ITConfig(ADC1_IT_EOCIE,ENABLE);
/* Enable general interrupts */
/*Start Conversion */
ADC1_StartConversion();
}
void AD_Config(void)
{
/* Init GPIO for ADC2 */
GPIO_Init(GPIOB, GPIO_PIN_7, GPIO_MODE_IN_FL_NO_IT);
// GPIOD->ODR &= 0x8f;
/* De-Init ADC peripheral*/
ADC1_DeInit();
ADC1_ClearITPendingBit(ADC1_IT_EOC);
/* Init ADC1 peripheral */
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CHANNEL_7, ADC1_PRESSEL_FCPU_D18, \
ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL7,\
DISABLE);
/* Enable EOC interrupt */
// if(autoFm == 1)
// ADC1_ITConfig(ADC1_IT_EOCIE,DISABLE);
//else
ADC1_ITConfig(ADC1_IT_EOCIE,ENABLE);
/*Start Conversion */
ADC1_StartConversion();
}
main()
{
TIM2_Config();
TIM4_Config();
AD2_Config();
TIM1_Config();
enableInterrupts();
while(1)
{
if(ADChange >= 1)//这是个延时,多久进一次
{
AD2_Config();
while(RESET == ADC1_GetFlagStatus(ADC1_FLAG_EOC));
VoicADLeve = ADC1_GetConversionValue();
AD_Config();
ADChange = 0;
ADChange = 0;
}
}
}
在ADC中断里面
INTERRUPT_HANDLER(ADC2_IRQHandler, 22)
{
ADC1_ClearITPendingBit(ADC2_IT_EOC);
}
INTERRUPT_HANDLER(ADC1_IRQHandler, 22)
{
uint16_t temp;
temp = ADC1_GetConversionValue();//这是采集音频
if(temp < min_ad)
{
min_ad = temp;
}
if(temp > max_ad)
{
max_ad= temp;
}
ADC1_ClearITPendingBit(ADC1_IT_EOC);
}
这程序我只给了你ADC的部分
void AD2_Config(void)
{
/* Init GPIO for ADC2 */
GPIO_Init(GPIOB, GPIO_PIN_6, GPIO_MODE_IN_FL_NO_IT);
/* De-Init ADC peripheral*/
ADC1_DeInit();
ADC1_ClearITPendingBit(ADC1_IT_EOC);
/* Init ADC1 peripheral */
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CHANNEL_6, ADC1_PRESSEL_FCPU_D18, \
ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL6,\
DISABLE);
/* Enable EOC interrupt */
//if(autoFm == 1)
ADC1_ITConfig(ADC1_IT_EOCIE,DISABLE);
//else
// ADC1_ITConfig(ADC1_IT_EOCIE,ENABLE);
/* Enable general interrupts */
/*Start Conversion */
ADC1_StartConversion();
}
void AD_Config(void)
{
/* Init GPIO for ADC2 */
GPIO_Init(GPIOB, GPIO_PIN_7, GPIO_MODE_IN_FL_NO_IT);
// GPIOD->ODR &= 0x8f;
/* De-Init ADC peripheral*/
ADC1_DeInit();
ADC1_ClearITPendingBit(ADC1_IT_EOC);
/* Init ADC1 peripheral */
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CHANNEL_7, ADC1_PRESSEL_FCPU_D18, \
ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL7,\
DISABLE);
/* Enable EOC interrupt */
// if(autoFm == 1)
// ADC1_ITConfig(ADC1_IT_EOCIE,DISABLE);
//else
ADC1_ITConfig(ADC1_IT_EOCIE,ENABLE);
/*Start Conversion */
ADC1_StartConversion();
}
main()
{
TIM2_Config();
TIM4_Config();
AD2_Config();
TIM1_Config();
enableInterrupts();
while(1)
{
if(ADChange >= 1)//这是个延时,多久进一次
{
AD2_Config();
while(RESET == ADC1_GetFlagStatus(ADC1_FLAG_EOC));
VoicADLeve = ADC1_GetConversionValue();
AD_Config();
ADChange = 0;
ADChange = 0;
}
}
}
在ADC中断里面
INTERRUPT_HANDLER(ADC2_IRQHandler, 22)
{
ADC1_ClearITPendingBit(ADC2_IT_EOC);
}
INTERRUPT_HANDLER(ADC1_IRQHandler, 22)
{
uint16_t temp;
temp = ADC1_GetConversionValue();//这是采集音频
if(temp < min_ad)
{
min_ad = temp;
}
if(temp > max_ad)
{
max_ad= temp;
}
ADC1_ClearITPendingBit(ADC1_IT_EOC);
}
这程序我只给了你ADC的部分
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询