void InitADC()
{
ADC1->CR1 = 0x20 //漏告分频系数为4 单次转换模式 先不启动ADC转换
ADC1->CR2 = 0x08 //非外部触发 数据右对齐 禁止扫描模式
ADC1->CSR = 0x03 //禁止转换结束中断 设置转换通道为AIN3
ADC1->TDRL|= 0x08 //禁止AIN3施密特触发器功能
ADC1->CR1 |= 0x01 //第一返虚明次写1是从低功耗模式唤醒
}
unsigned int GET_AD()
{
unsigned int temph=0
unsigned char templ=0
ADC1->CR1 |= 0x01 //启誉咐动AD转换
while(!(ADC1->CSR&0x80)) //等待转换结束
ADC1->CSR &= ~0x80 //清除转换结束标志
templ = ADC1->DRL
temph = ADC1->DRH //读出AD转换结果
temph = ConTOINT(temph,templ)
return(temph)
}
我给你个例程,我这是两个通道,一个是采集音频,然后彩灯随音乐节奏闪烁,同时采兄皮集电池电量。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的部分
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)