如何使用STM8S单片机的多通道AD转换
答案:1 悬赏:30 手机版
解决时间 2021-03-01 13:08
- 提问者网友:沉默菋噵
- 2021-02-28 22:56
如何使用STM8S单片机的多通道AD转换
最佳答案
- 五星知识达人网友:蕴藏春秋
- 2021-02-28 23:26
我给你个例程,我这是两个通道,一个是采集音频,然后彩灯随音乐节奏闪烁,同时采集电池电量。
void AD2_Config(void)
{
GPIO_Init(GPIOB, GPIO_PIN_6, GPIO_MODE_IN_FL_NO_IT);
ADC1_DeInit();
ADC1_ClearITPendingBit(ADC1_IT_EOC);
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CHANNEL_6, ADC1_PRESSEL_FCPU_D18, \
ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL6,\
DISABLE);
//if(autoFm == 1)
ADC1_ITConfig(ADC1_IT_EOCIE,DISABLE);
//else
// ADC1_ITConfig(ADC1_IT_EOCIE,ENABLE);
ADC1_StartConversion();
}
void AD_Config(void)
{
GPIO_Init(GPIOB, GPIO_PIN_7, GPIO_MODE_IN_FL_NO_IT);
// GPIOD->ODR &= 0x8f;
ADC1_DeInit();
ADC1_ClearITPendingBit(ADC1_IT_EOC);
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CHANNEL_7, ADC1_PRESSEL_FCPU_D18, \
ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL7,\
DISABLE);
// if(autoFm == 1)
// ADC1_ITConfig(ADC1_IT_EOCIE,DISABLE);
//else
ADC1_ITConfig(ADC1_IT_EOCIE,ENABLE);
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)
{
GPIO_Init(GPIOB, GPIO_PIN_6, GPIO_MODE_IN_FL_NO_IT);
ADC1_DeInit();
ADC1_ClearITPendingBit(ADC1_IT_EOC);
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CHANNEL_6, ADC1_PRESSEL_FCPU_D18, \
ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL6,\
DISABLE);
//if(autoFm == 1)
ADC1_ITConfig(ADC1_IT_EOCIE,DISABLE);
//else
// ADC1_ITConfig(ADC1_IT_EOCIE,ENABLE);
ADC1_StartConversion();
}
void AD_Config(void)
{
GPIO_Init(GPIOB, GPIO_PIN_7, GPIO_MODE_IN_FL_NO_IT);
// GPIOD->ODR &= 0x8f;
ADC1_DeInit();
ADC1_ClearITPendingBit(ADC1_IT_EOC);
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CHANNEL_7, ADC1_PRESSEL_FCPU_D18, \
ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL7,\
DISABLE);
// if(autoFm == 1)
// ADC1_ITConfig(ADC1_IT_EOCIE,DISABLE);
//else
ADC1_ITConfig(ADC1_IT_EOCIE,ENABLE);
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的部分
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯