完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
想利用C1C4端口采集两路ad值,但是第二路一直失败,请教各位
#include #define ADC1_DR_Address ((u32)0x40012400+0x4c) unsigned char ad[128];//²É¼¯µ½µÄ128¸öµãadÖµ unsigned char ad_[43];//·ÖÇø´¦ÀíºóµÄ43¸öÇøÓòad unsigned char ad_cl[43];//43¸öÇøÓò¶þÖµ»¯ºóÊý¾Ý unsigned char min_ad,max_ad,fa;//ad×î´óÖµ£¬×îСֵ,·§Öµ int i; char ss1=0,n1=0,ss2=0,n2=0; #define si1 GPIO_Pin_3 #define clk1 GPIO_Pin_2 #define si2 GPIO_Pin_4 #define clk2 GPIO_Pin_5 __IO uint32_t ADC_ConvertedValue[2]; //__IO u16 ADC_ConvertedValueLocal; void SamplingDelay(void) { int i=60; while(i--){} } void ccd_IOint_1(){ GPIO_InitTypeDef GPIO_Struct; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); GPIO_Struct.GPIO_Pin=GPIO_Pin_3|GPIO_Pin_2; //CCD1µÄsi ,clk¶Ë¿Ú C3 C2 GPIO_Struct.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Struct.GPIO_Mode=GPIO_Mode_Out_PP ; GPIO_Init(GPIOC,&GPIO_Struct); } void ccd_IOint_2(){ GPIO_InitTypeDef GPIO_Struct; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); GPIO_Struct.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_5; //CCD2µÄsi ,clk¶Ë¿ÚA4 A5 GPIO_Struct.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Struct.GPIO_Mode=GPIO_Mode_Out_PP ; GPIO_Init(GPIOA,&GPIO_Struct); } void ADC1_GPIO_Config(void)// CCDµÄAD¶Ë¿ÚÅäÖà { GPIO_InitTypeDef GPIO_InitStructure; /* Enable DMA clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); /* Enable ADC1 and GPIOC clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE); /* Configure PC.0 as analog input */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOC, &GPIO_InitStructure); //ccd1µÄAD²É¼¯ PC1,ÊäÈëʱ²»ÓÃÉèÖÃËÙÂÊ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOC, &GPIO_InitStructure); // ccd2µÄAD²É¼¯PC4,ÊäÈëʱ²»ÓÃÉèÖÃËÙÂÊ } /** * @brief ÅäÖÃADC1µÄ¹¤×÷ģʽΪMDAģʽ * @param ÎÞ * @retval ÎÞ */ void ADC1_Mode_Config(void) { DMA_InitTypeDef DMA_InitStructure; ADC_InitTypeDef ADC_InitStructure; /* DMA channel1 configuration */ DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; //ADCµØÖ· DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue; //ÄÚ´æµØÖ· DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 2; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //ÍâÉèµØÖ·¹Ì¶¨ DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //ÄÚ´æµØÖ·µÝÔö DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //°ë×Ö DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //Ñ»·´«Êä DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel1, &DMA_InitStructure); /* Enable DMA channel1 */ DMA_Cmd(DMA1_Channel1, ENABLE); /* ADC1 configuration */ ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //¶ÀÁ¢ADCģʽ ADC_InitStructure.ADC_ScanConvMode = ENABLE ; //¿ªÆôɨÃèģʽ ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //¿ªÆôÁ¬Ðøת»»Ä£Ê½£¬¼´²»Í£µØ½øÐÐADCת»» ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //²»Ê¹ÓÃÍⲿ´¥·¢×ª»» ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //²É¼¯Êý¾ÝÓÒ¶ÔÆë ADC_InitStructure.ADC_NbrOfChannel = 2; //Ҫת»»µÄͨµÀÊýÄ¿2 ADC_Init(ADC1, &ADC_InitStructure); /*ÅäÖÃADCʱÖÓ£¬ÎªPCLK2µÄ8·ÖƵ£¬¼´9MHz*/ RCC_ADCCLKConfig(RCC_PCLK2_Div8); /*ÅäÖÃADC1µÄͨµÀ11Ϊ55. 5¸ö²ÉÑùÖÜÆÚ£¬ÐòÁÐΪ1 */ ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 1, ADC_SampleTime_239Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 2, ADC_SampleTime_239Cycles5); /* Enable ADC1 DMA */ ADC_DMACmd(ADC1, ENABLE); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /*¸´Î»Ð£×¼¼Ä´æÆ÷ */ ADC_ResetCalibration(ADC1); /*µÈ´ýУ׼¼Ä´æÆ÷¸´Î»Íê³É */ // while(ADC_GetResetCalibrationStatus(ADC1)); /* ADCУ׼ */ ADC_StartCalibration(ADC1); // /* µÈ´ýУ׼Íê³É*/ while(ADC_GetCalibrationStatus(ADC1)); // /* ÓÉÓÚûÓвÉÓÃÍⲿ´¥·¢£¬ËùÒÔʹÓÃÈí¼þ´¥·¢ADCת»» */ ADC_SoftwareStartConvCmd(ADC1, ENABLE); } u16 ADC_Get_Value_1(void)//¶ÁÈ¡ADת»»ºóµÄÊýÖµ { u32 a1=0; ADC_SoftwareStartConvCmd(ADC1,ENABLE); while(DMA_GetFlagStatus(DMA1_FLAG_TC1)==RESET){} a1=ADC_ConvertedValue[0]; return a1; } u16 ADC_Get_Value_2(void)//¶ÁÈ¡ADת»»ºóµÄÊýÖµ { u32 a2=0; ADC_SoftwareStartConvCmd(ADC1,ENABLE); while(DMA_GetFlagStatus(DMA1_FLAG_TC1)==RESET){} a2=ADC_ConvertedValue[1]; return a2; } void ADC1_Init(void) { ADC1_GPIO_Config(); ADC1_Mode_Config(); } void ccd_int() { ccd_IOint_1(); ccd_IOint_2(); ADC1_Init(); } void ccd_1() { char ss1=0,n1=0,ss2=0,n2=0; GPIO_SetBits(GPIOC,si1); /* SI = 1 */ SamplingDelay(); GPIO_SetBits(GPIOC,clk1); SamplingDelay(); GPIO_ResetBits(GPIOC,si1); /* SI = 0 */ SamplingDelay(); SamplingDelay(); GPIO_ResetBits(GPIOC,clk1); for(i=0; i<128; i++) { SamplingDelay(); GPIO_SetBits(GPIOC,clk1); /* CLK = 1 */ SamplingDelay(); SamplingDelay(); ad[i] = (u8)(ADC_Get_Value_1()>>4); GPIO_ResetBits(GPIOC,clk1); /* CLK = 0 */ } SamplingDelay(); GPIO_SetBits(GPIOC,clk1); /* CLK = 1 */ SamplingDelay(); GPIO_ResetBits(GPIOC,clk1); /* CLK = 0 */ ad_[42]=(ad[126]+ad[127])/2; for(i=0;i<42;i++) //ºÏ²¢ÏñËصã { ad_[i]=(ad[3*i]+ad[3*i+1]+ad[3*i+2])/3; } max_ad =ad[0]; min_ad =ad[0]; for(i=0;i<42;i++) { if (ad_[i] > max_ad) max_ad =ad_[i]; if (ad_[i] < min_ad) min_ad =ad_[i]; } fa = (max_ad + min_ad)*0.5; // for(i=0;i<128;i++) // { if(ad[i] > fa){ ss1=ss1+ad[i];n1++;} // else { ss2=ss2+ad[i];n2++; } // } // ss1=ss1/n1; // ss2=ss2/n2; // fa=(ss1+ss2)/2; for(i=0;i<128;i++) { if(ad[i] > fa&&ad[i]-fa>fa*0.35){ ad[i]=1;} else { ad[i]=0; } } for(i=0;i<43;i++) { if(ad_[i] > fa&&ad_[i]-fa>fa*0.35) ad_cl[i]=1; else ad_cl[i]=0; } } void ccd_2() { char ss1=0,n1=0,ss2=0,n2=0; GPIO_SetBits(GPIOA,si2); /* SI = 1 */ SamplingDelay(); GPIO_SetBits(GPIOA,clk2); SamplingDelay(); GPIO_ResetBits(GPIOA,si2); /* SI = 0 */ SamplingDelay(); SamplingDelay(); GPIO_ResetBits(GPIOA,clk2); for(i=0; i<128; i++) { SamplingDelay(); GPIO_SetBits(GPIOA,clk2); /* CLK = 1 */ SamplingDelay(); SamplingDelay(); ad[i] = (u8)(ADC_Get_Value_2()>>4); GPIO_ResetBits(GPIOA,clk2); /* CLK = 0 */ } SamplingDelay(); GPIO_SetBits(GPIOA,clk2); /* CLK = 1 */ SamplingDelay(); GPIO_ResetBits(GPIOA,clk2); /* CLK = 0 */ ad_[42]=(ad[126]+ad[127])/2; for(i=0;i<42;i++) //ºÏ²¢ÏñËصã { ad_[i]=(ad[3*i]+ad[3*i+1]+ad[3*i+2])/3; } max_ad =ad[0]; min_ad =ad[0]; for(i=0;i<42;i++) { if (ad_[i] > max_ad) max_ad =ad_[i]; if (ad_[i] < min_ad) min_ad =ad_[i]; } fa = (max_ad + min_ad)*0.5; // for(i=0;i<128;i++) // { if(ad[i] > fa){ ss1=ss1+ad[i];n1++;} // else { ss2=ss2+ad[i];n2++; } // } // ss1=ss1/n1; // ss2=ss2/n2; // fa=(ss1+ss2)/2; for(i=0;i<128;i++) { if(ad[i] > fa&&ad[i]-fa>fa*0.35){ ad[i]=1;} else { ad[i]=0; } } for(i=0;i<43;i++) { if(ad_[i] > fa&&ad_[i]-fa>fa*0.35) ad_cl[i]=1; else ad_cl[i]=0; } } |
|
相关推荐
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
1980 浏览 1 评论
AD7686芯片不传输数据给STM32,但是手按住就会有数据。
1836 浏览 3 评论
4416 浏览 0 评论
如何解决MPU-9250与STM32通讯时,出现HAL_ERROR = 0x01U
1985 浏览 1 评论
hal库中i2c卡死在HAL_I2C_Master_Transmit
2489 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-19 10:28 , Processed in 0.422589 second(s), Total 42, Slave 35 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号