新人求助,stm8l151k4 ad采样的问题
2个回答
展开全部
//初始化函数
void ADC_Periph_Init(void)
{
u8 idx;
CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE);//使能ADC时钟
CLK_PeripheralClockConfig(CLK_Peripheral_DMA1, ENABLE);//使能DMA1时钟
GPIO_Init(GPIOB,GPIO_Pin_5,GPIO_Mode_In_FL_No_IT);//浮空输入
ADC_Cmd(ADC1,ENABLE);//打开ADC
for (idx = 0;idx < 50;idx++);//昌茄adc上电需要一段时间
ADC_VrefintCmd(ENABLE);//使能内部参考电压
ADC_DMACmd(ADC1,ENABLE);//使用ADC的DMA功能
//连续转换模式
ADC_Init(ADC1, ADC_ConversionMode_Continuous,ADC_Resolution_12Bit, ADC_Prescaler_1);
//ADC_SamplingTimeConfig(ADC1, ADC_Group_FastChannels, ADC_SamplingTime_192Cycles);
//ADC_SamplingTimeConfig(ADC1, ADC_Group_SlowChannels,ADC_SamplingTime_192Cycles);
ADC_SamplingTimeConfig(ADC1, ADC_Group_SlowChannels, ADC_SamplingTime_384Cycles);
ADC_ChannelCmd(ADC1, ADC_Channel_Vrefint, ENABLE);//内部参考电压
ADC_ChannelCmd(ADC1, ADC_Channel_13, ENABLE);//通道13 PB5
SYSCFG_REMAPDMAChannelConfig(REMAP_DMA1Channel_ADC1ToChannel0);
DMA_Init(DMA1_Channel0,//通道0
(uint32_t)&ADC_ConvertedValue,//缓冲顷迅渗区地址
(uint16_t)ADC1_DR_Address,//ADC数据寄存器地址
DMA_BUF_SIZE,//缓冲区大小
DMA_DIR_PeripheralToMemory,//外设到内存
DMA_Mode_Circular,//循环转换
DMA_MemoryIncMode_Inc,//内存地址自增
DMA_Priority_High,//优先级为高
DMA_MemoryDataSize_HalfWord);//内存数据是半字位宽
DMA_SetCurrDataCounter(DMA1_Channel0,DMA_BUF_SIZE);//设置转速的数据个数
DMA_ITConfig(DMA1_Channel0,DMA_ITx_TC, ENABLE);//使能传输完雀脊成中断
DMA_ITConfig(DMA1_Channel0,DMA_ITx_HT, DISABLE);
DMA_Cmd(DMA1_Channel0,ENABLE);//使能通道0
DMA_GlobalCmd(ENABLE);//打开DMA
ADC_SoftwareStartConv(ADC1);//software trigger
}
//DMA中断
INTERRUPT_HANDLER(DMA1_CHANNEL0_1_IRQHandler,2)
{uint16_t *p;
/* In order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction.
*/
if(DMA_GetITStatus(DMA1_IT_TC0) != RESET)
{
p=&ADC_ConvertedValue[0];
Data_Pack_handle(p);
DMA_ClearITPendingBit(DMA1_IT_TC0);
}
}
//数据打包函数。
void Data_Pack_handle(uint16_t *P_DMA_data)
{uint8_t i;
for(i=0;i<DMA_BUF_SIZE;i++)
adc_val[i] = *P_DMA_data++;
}
void main(void)
{uint8_t sum,i;
Sys_clk_config();
Port_Init();
ADC_Periph_Init();
//MWWDG_Init();//窗口看门狗初始化函数
enableInterrupts();//开中断
while (1)
{
test = search_form(ADC_ConvertedValue[3]);
temp_val = test-20;
}
连续采8个数,1000 1400 1000 1404 1001 1400 1000 1404 大概就是这样的采样结果,应该是800多才对呢。数据不是线性变化的。
void ADC_Periph_Init(void)
{
u8 idx;
CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE);//使能ADC时钟
CLK_PeripheralClockConfig(CLK_Peripheral_DMA1, ENABLE);//使能DMA1时钟
GPIO_Init(GPIOB,GPIO_Pin_5,GPIO_Mode_In_FL_No_IT);//浮空输入
ADC_Cmd(ADC1,ENABLE);//打开ADC
for (idx = 0;idx < 50;idx++);//昌茄adc上电需要一段时间
ADC_VrefintCmd(ENABLE);//使能内部参考电压
ADC_DMACmd(ADC1,ENABLE);//使用ADC的DMA功能
//连续转换模式
ADC_Init(ADC1, ADC_ConversionMode_Continuous,ADC_Resolution_12Bit, ADC_Prescaler_1);
//ADC_SamplingTimeConfig(ADC1, ADC_Group_FastChannels, ADC_SamplingTime_192Cycles);
//ADC_SamplingTimeConfig(ADC1, ADC_Group_SlowChannels,ADC_SamplingTime_192Cycles);
ADC_SamplingTimeConfig(ADC1, ADC_Group_SlowChannels, ADC_SamplingTime_384Cycles);
ADC_ChannelCmd(ADC1, ADC_Channel_Vrefint, ENABLE);//内部参考电压
ADC_ChannelCmd(ADC1, ADC_Channel_13, ENABLE);//通道13 PB5
SYSCFG_REMAPDMAChannelConfig(REMAP_DMA1Channel_ADC1ToChannel0);
DMA_Init(DMA1_Channel0,//通道0
(uint32_t)&ADC_ConvertedValue,//缓冲顷迅渗区地址
(uint16_t)ADC1_DR_Address,//ADC数据寄存器地址
DMA_BUF_SIZE,//缓冲区大小
DMA_DIR_PeripheralToMemory,//外设到内存
DMA_Mode_Circular,//循环转换
DMA_MemoryIncMode_Inc,//内存地址自增
DMA_Priority_High,//优先级为高
DMA_MemoryDataSize_HalfWord);//内存数据是半字位宽
DMA_SetCurrDataCounter(DMA1_Channel0,DMA_BUF_SIZE);//设置转速的数据个数
DMA_ITConfig(DMA1_Channel0,DMA_ITx_TC, ENABLE);//使能传输完雀脊成中断
DMA_ITConfig(DMA1_Channel0,DMA_ITx_HT, DISABLE);
DMA_Cmd(DMA1_Channel0,ENABLE);//使能通道0
DMA_GlobalCmd(ENABLE);//打开DMA
ADC_SoftwareStartConv(ADC1);//software trigger
}
//DMA中断
INTERRUPT_HANDLER(DMA1_CHANNEL0_1_IRQHandler,2)
{uint16_t *p;
/* In order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction.
*/
if(DMA_GetITStatus(DMA1_IT_TC0) != RESET)
{
p=&ADC_ConvertedValue[0];
Data_Pack_handle(p);
DMA_ClearITPendingBit(DMA1_IT_TC0);
}
}
//数据打包函数。
void Data_Pack_handle(uint16_t *P_DMA_data)
{uint8_t i;
for(i=0;i<DMA_BUF_SIZE;i++)
adc_val[i] = *P_DMA_data++;
}
void main(void)
{uint8_t sum,i;
Sys_clk_config();
Port_Init();
ADC_Periph_Init();
//MWWDG_Init();//窗口看门狗初始化函数
enableInterrupts();//开中断
while (1)
{
test = search_form(ADC_ConvertedValue[3]);
temp_val = test-20;
}
连续采8个数,1000 1400 1000 1404 1001 1400 1000 1404 大概就是这样的采样结果,应该是800多才对呢。数据不是线性变化的。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询