热流板传感器接一个放大器,放大100倍后,接到STM精英板上,ADC转换后数据一直不随热流的电压值变化而变化,下面是我的代码,大家帮忙看一下,ADC转换后,用DMA保存到内存里数组里,然后求平均值,再输出
热流版的输出范围是0-20mv,电压放大后输出0-2V电压值
int main(void)
{
u16 adcx;
u16 r;
float temp;
delay_init(); //Ñóê±oˉêy3õê¼»ˉ
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//éèÖÃÖD¶ÏóÅÏ輶·Ö×éÎa×é2£o2λÇàÕ¼óÅÏ輶£¬2λÏìó|óÅÏ輶
uart_init(115200); //′®¿ú3õê¼»ˉÎa115200
LED_Init(); //LED¶Ë¿ú3õê¼»ˉ
LCD_Init();
init_All_Periph();
POINT_COLOR=RED;//éèÖÃ×ÖìåÎaoìé«
LCD_ShowString(60,50,200,16,16,"Elite STM32");
LCD_ShowString(60,70,200,16,16,"ADC TEST");
LCD_ShowString(60,90,200,16,16,"ATOM@ALIENTEK");
LCD_ShowString(60,110,200,16,16,"2015/1/14");
//ÏÔê¾ìáê¾DÅÏ¢
POINT_COLOR=BLUE;//éèÖÃ×ÖìåÎaà¶é«
LCD_ShowString(60,130,200,16,16,"ADC_CH0_VAL:");
LCD_ShowString(60,150,200,16,16,"ADC_CH0_VOL:0.000V");
ADC_SoftwareStartConvCmd(ADC1,ENABLE);
DMA_Cmd(DMA1_Channel1, ENABLE);
while(1)
{
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
r=ADC_GetConversionValue(ADC1);
printf("r=%d
",r);
filter();
adcx=Get_AF_Value();
LCD_ShowxNum(156,130,adcx,4,16,0);//ÏÔê¾ADCμÄÖμ
temp=(float)adcx*(3.3/4096);
adcx=temp;
LCD_ShowxNum(156,150,adcx,1,16,0);//ÏÔê¾μçÑ1Öμ
temp-=adcx;
temp*=1000;
LCD_ShowxNum(172,150,temp,3,16,0X80);
LED0=!LED0;
delay_ms(250);
}
}
adc.c文件内容
# define M 10
# define N 1
vu16 AD_Value[M]; //AD×a»»½á1û
vu16 AF_Value; //ƽ¾ùÖμoóμĽá1û
void Adc_Init(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_ADC1 , ENABLE ); //ê1ÄüADC1í¨μàê±Öó
RCC_ADCCLKConfig(RCC_PCLK2_Div6); //éèÖÃADC·ÖÆμòò×ó6 72M/6=12,ADC×î′óê±¼ä2»Äü3¬1y14M
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //PA1 ×÷ÎaÄ£Äaí¨μàêäèëòy½Å pa1Îaèèá÷1í¨μà
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //Ä£Äaêäèëòy½Å
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_DeInit(ADC1); //¸′λADC1,½«íaéè ADC1 μÄè«2¿¼Ä′æÆ÷ÖØéèÎaè±ê¡Öμ
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC1¤×÷Ä£ê½:ADC1oíADC21¤×÷Ôú¶àá¢Ä£ê½
ADC_InitStructure.ADC_ScanConvMode = DISABLE; //Ä£êy×a»»1¤×÷Ôúμ¥í¨μàÄ£ê½
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //Ä£êy×a»»1¤×÷Ôúμ¥′Î×a»»Ä£ê½
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //×a»»óéèí¼t¶ø2»êÇía2¿′¥·¢Æô¶ˉ
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ADCêy¾Yóò¶ÔÆë
ADC_InitStructure.ADC_NbrOfChannel = 1; //Ë3Dò½øDD1æÔò×a»»μÄADCí¨μàμÄêyÄ¿
ADC_Init(ADC1, &ADC_InitStructure); //¸ù¾YADC_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèADCxμļÄ′æÆ÷
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_239Cycles5 ); //ADC1,ADCí¨μà,2éÑùê±¼äÎa239.5ÖüÆú
ADC_DMACmd(ADC1,ENABLE); //¿aÆôADC μÄDMAÖ§3Ö Dèòa¶àá¢ÅäÖÃDMAí¨μà
ADC_Cmd(ADC1, ENABLE); //ê1ÄüÖ¸¶¨μÄADC1
ADC_ResetCalibration(ADC1); //ê1Äü¸′λD£×¼
while(ADC_GetResetCalibrationStatus(ADC1)); //μè′y¸′λD£×¼½áêø
ADC_StartCalibration(ADC1); //¿aÆôADD£×¼
while(ADC_GetCalibrationStatus(ADC1)); //μè′yD£×¼½áêø
}
void DMA_Configration(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //ê1ÄüDMA′«êä
DMA_DeInit(DMA1_Channel1); //½«DMAμÄí¨μà1¼Ä′æÆ÷ÖØéèÎaè±ê¡Öμ
// DMA1_MEM_LEN=cndtr;
DMA_InitStructure.DMA_PeripheralBaseAddr =(u32)&ADC1->DR; //DMAíaéè»ùμØÖ·
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&AD_Value; //DMAÄú′æ»ùμØÖ·
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //êy¾Y′«êä·½Ïò£¬′óÄú′æ¶áè¡·¢Ëíμ½íaéè
DMA_InitStructure.DMA_BufferSize = M; //DMAí¨μàμÄDMA»o′æμÄ′óD¡
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //íaéèμØÖ·¼Ä′æÆ÷2»±ä
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //Äú′æμØÖ·¼Ä′æÆ÷μYÔö
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //êy¾Y¿í¶èÎa16λ
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //êy¾Y¿í¶èÎa16λ
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //1¤×÷ÔúÕy3£»o′æÄ£ê½
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; //DMAí¨μà xóμóDÖDóÅÏ輶
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //DMAí¨μàxûóDéèÖÃÎaÄú′æμ½Äú′æ′«êä
DMA_Init(DMA1_Channel1, &DMA_InitStructure); //¸ù¾YDMA_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉDMAμÄí¨μàUSART1_Tx_DMA_ChannelËù±êê¶μļÄ′æÆ÷
}
void GPIO_Configration()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //1ü½ÇòÔ¸′λμÄDÎê½½óμ½GPIOéÏ,ËùòÔòaóÃμ½íÆíìê½êä3ö
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
void init_All_Periph()
{
//RCC_Configuration();
GPIO_Configration();
Adc_Init();
DMA_Configration();
}
void filter()
{
int i=0;
int sum=0;
for(i=0;i<M;i++)
{
sum=sum+AD_Value[i];
printf("i=%d,ad_value=%d
",i,AD_Value[i]);
}
AF_Value=sum/M;
}
int Get_AF_Value()
{
return AF_Value;
}
把传感器电路和CPU的AD分开来测,
一般先测AD功能是否正常,
给输入管脚接一个合适的固定电压,然后单独调试AD,
正常了再接传感器,继续调。
我把PA1接3.3V电压,ADC的值也一直是1.653这个值,没有变化
可以确定是我程序的问题,我再看一下,
一周热门 更多>