模塊共三個接口:紅線---VCC_5V;黑線---GND;黃線—PC5(ADC數據采樣)
將模塊通電,黃線接到ADC通道輸入端即可工作;將采樣得到的電壓值通過填入表格,進行線性化處理,得到線性化公式。通過公式,可將ADC采樣值轉化為距離值。實測,在10cm—30cm范圍內,較為準確,最大誤差在1cm。模塊對被測角度的靈敏度很高,同一位置,不同的角度,誤差可以超過5cm,最好的測量角度是90度。
51hei.png (27.52 KB, 下載次數: 70)
下載附件
2019-11-19 20:20 上傳
Bits A2D(AD的轉換位數)——————10
SupplyV(供電電壓,滿量程)————3.30
8個校驗數據的輸出電壓值
distance=(m/(A2D,AD的轉換位數)+b)-k
distance=(m/10+b)-k
10cm 11.55--11.66
15cm 16.15--16.38
16.5cm 17.46--17.92
18cm 20.01--20.13
19cm 19.44--19.81
20cm 20.01--20.15
22cm 21.56--21.82
23cm 23.20--23.66
24.5cm 23.96--24.23
28cm 27.39--27.93
單片機源程序如下:
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f10x_lib.h"
- #include "stdio.h"
- /* Private macro -------------------------------------------------------------*/
- #define countof(a) (sizeof(a) / sizeof(*(a)))
- /* Private typedef -----------------------------------------------------------*/
- #define TxBufferSize (countof(TxBuffer) - 1)
- /* Private define ------------------------------------------------------------*/
- u8 TxBuffer[] = "ADC Example1: ADC TO DMA TO UART1\r\n";
- u8 TxCounter = 0;
- /* Private define ------------------------------------------------------------*/
- #define ADC1_DR_Address ((u32)0x4001244C)
- /* Private variables ---------------------------------------------------------*/
- USART_InitTypeDef USART_InitStructure;
- ADC_InitTypeDef ADC_InitStructure;
- DMA_InitTypeDef DMA_InitStructure;
- vu16 ADC_ConvertedValue;
- ErrorStatus HSEStartUpStatus;
-
- /* Private function prototypes -----------------------------------------------*/
- void RCC_Configuration(void);
- void GPIO_Configuration(void);
- void NVIC_Configuration(void);
-
- /* Private functions ---------------------------------------------------------*/
- void Delay_us(unsigned short us)
- {
- unsigned short i;
- while(us--)
- {
- for(i=0;i<10;i++);
- }
- }
- void Delay_ms(unsigned short ms)
- {
- unsigned short i;
- while(ms--)
- {
- for(i=0;i<10000;i++);
- }
- }
- /**********發送數據**********/
- int fputc(int ch, FILE *f)
- {
- USART_SendData(USART1,(unsigned char)ch);//USART1可以換成USART2等
- while(!(USART1->SR&USART_FLAG_TXE));
- return(ch);
- }
- /**********接收數據**********/
- int GetKey(void)
- {
- while(!(USART1->SR&USART_FLAG_RXNE));
- return((int)(USART1->DR&0x1FF));
- }
- /*******************************************************************************
- * Function Name : main
- * Description : Main program
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- int main(void)
- {
- // unsigned char i=1;
- unsigned long Tmp_Dat=0,i=1;
- float distance=0;
- #ifdef DEBUG
- debug();
- #endif
- RCC_Configuration(); //系統時鐘配置
- NVIC_Configuration(); //NVIC配置
- GPIO_Configuration(); //GPIO配置
- /* USART1 configuration ------------------------------------------------------*/
- /* USART1 configured as follow:
- - BaudRate = 9600 baud
- - Word Length = 8 Bits
- - Two Stop Bit
- - Odd parity
- - Hardware flow control disabled (RTS and CTS signals)
- - Receive and transmit enabled
- - USART Clock disabled
- - USART CPOL: Clock is active low
- - USART CPHA: Data is captured on the second edge
- - USART LastBit: The clock pulse of the last data bit is not output to
- the SCLK pin
- */
- USART_InitStructure.USART_BaudRate = 9600;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_InitStructure.USART_Clock = USART_Clock_Disable;
- USART_InitStructure.USART_CPOL = USART_CPOL_Low;
- USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;
- USART_InitStructure.USART_LastBit = USART_LastBit_Disable;
- /* Configure the USART1 */
- USART_Init(USART1, &USART_InitStructure);
- /* Enable the USART Transmoit interrupt: this interrupt is generated when the
- USART1 transmit data register is empty */
- USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
- /* Enable the USART Receive interrupt: this interrupt is generated when the
- USART1 receive data register is not empty */
- USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
- /* Enable USART1 */
- USART_Cmd(USART1, ENABLE);
- /* DMA channel1 configuration ----------------------------------------------*/
- DMA_DeInit(DMA_Channel1);
- DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
- DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue;
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
- DMA_InitStructure.DMA_BufferSize = 1;
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
- DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
- 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(DMA_Channel1, &DMA_InitStructure);
-
- /* Enable DMA channel1 */
- DMA_Cmd(DMA_Channel1, ENABLE);
-
- /* ADC1 configuration ------------------------------------------------------*/
- ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
- ADC_InitStructure.ADC_ScanConvMode = ENABLE;
- ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
- ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
- ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
- ADC_InitStructure.ADC_NbrOfChannel = 1;
- ADC_Init(ADC1, &ADC_InitStructure);
- /* ADC1 regular channel1 configuration */
- ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 1, ADC_SampleTime_55Cycles5);
- ADC_DMACmd(ADC1, ENABLE); /* 使能ADC1 DMA */
- ADC_Cmd(ADC1, ENABLE); /* 使能 ADC1 */
- ADC_ResetCalibration(ADC1); /* 使能 ADC1 reset calibaration register */
- while(ADC_GetResetCalibrationStatus(ADC1)); /* Check the end of ADC1 reset calibration register */
- ADC_StartCalibration(ADC1); /* Start ADC1 calibaration */
- while(ADC_GetCalibrationStatus(ADC1)); /* Check the end of ADC1 calibration */
- ADC_SoftwareStartConvCmd(ADC1, ENABLE); /* Start ADC1 Software Conversion */
- for(i=0;i<TxBufferSize;i++)
- {
- /* Write one byte to the transmit data register */
-
- USART_SendData(USART1, TxBuffer[TxCounter++]);
- while (!(USART1->SR & USART_FLAG_TXE)); //等待緩沖區空
- while (!(USART1->SR & USART_FLAG_TC)); //等待發送完成
- }
- while (1)
- {
- Delay_ms(1000);
-
- Tmp_Dat = ADC_ConvertedValue;
-
- // distance = (1/(Tmp_Dat*0.0000228324+0.00140335))-4.0;
- distance = (1/(Tmp_Dat*0.0000272988-0.002268704))-4.0;
- // distance = (1/(Tmp_Dat*0.000147-0.00042))-4.0;
- printf("%ld\r\n",Tmp_Dat);
- printf("distance=%.2f\r\n",distance);
- Tmp_Dat = Tmp_Dat*3300/0x0fff;
- TxBuffer[0] = Tmp_Dat/1000+'0';
- TxBuffer[1] = '.';
- TxBuffer[2] = (Tmp_Dat%1000)/100+'0';
- TxBuffer[3] = (Tmp_Dat%100)/10+'0';
- TxBuffer[4] = Tmp_Dat%10+'0';
- TxBuffer[5] = 'V';
- USART_SendData(USART1, '[');
- while (!(USART1->SR & USART_FLAG_TXE)); //等待緩沖區空
- while (!(USART1->SR & USART_FLAG_TC)); //等待發送完成
- USART_SendData(USART1, TxBuffer[0]);
- while (!(USART1->SR & USART_FLAG_TXE)); //等待緩沖區空
- while (!(USART1->SR & USART_FLAG_TC)); //等待發送完成
- USART_SendData(USART1, TxBuffer[1]);
- while (!(USART1->SR & USART_FLAG_TXE)); //等待緩沖區空
- while (!(USART1->SR & USART_FLAG_TC)); //等待發送完成
- USART_SendData(USART1, TxBuffer[2]);
- while (!(USART1->SR & USART_FLAG_TXE)); //等待緩沖區空
- while (!(USART1->SR & USART_FLAG_TC)); //等待發送完成
- USART_SendData(USART1, TxBuffer[3]);
- while (!(USART1->SR & USART_FLAG_TXE)); //等待緩沖區空
- while (!(USART1->SR & USART_FLAG_TC)); //等待發送完成
- USART_SendData(USART1, TxBuffer[4]);
- while (!(USART1->SR & USART_FLAG_TXE)); //等待緩沖區空
- while (!(USART1->SR & USART_FLAG_TC)); //等待發送完成
- USART_SendData(USART1, TxBuffer[5]);
- while (!(USART1->SR & USART_FLAG_TXE)); //等待緩沖區空
- while (!(USART1->SR & USART_FLAG_TC)); //等待發送完成
- USART_SendData(USART1, ']');
- while (!(USART1->SR & USART_FLAG_TXE)); //等待緩沖區空
- while (!(USART1->SR & USART_FLAG_TC)); //等待發送完成
- printf("\r\n");
- }
- }
- /*******************************************************************************
- * Function Name : RCC_Configuration
- * Description : Configures the different system clocks.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void RCC_Configuration(void)
- {
- /* RCC system reset(for debug purpose) */
- RCC_DeInit();
- /* Enable HSE */
- RCC_HSEConfig(RCC_HSE_ON);
- /* Wait till HSE is ready */
- HSEStartUpStatus = RCC_WaitForHSEStartUp();
- if(HSEStartUpStatus == SUCCESS)
- {
- /* Enable Prefetch Buffer */
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
- /* Flash 2 wait state */
- FLASH_SetLatency(FLASH_Latency_2);
-
- /* HCLK = SYSCLK */
- RCC_HCLKConfig(RCC_SYSCLK_Div1);
-
- /* PCLK2 = HCLK */
- RCC_PCLK2Config(RCC_HCLK_Div1);
- /* PCLK1 = HCLK/2 */
- RCC_PCLK1Config(RCC_HCLK_Div2);
- /* ADCCLK = PCLK2/4 */
- RCC_ADCCLKConfig(RCC_PCLK2_Div4);
-
- /* PLLCLK = 8MHz * 7 = 56 MHz */
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_7);
- /* Enable PLL */
- RCC_PLLCmd(ENABLE);
- /* Wait till PLL is ready */
- while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
- {
- }
- /* Select PLL as system clock source */
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
- /* Wait till PLL is used as system clock source */
- while(RCC_GetSYSCLKSource() != 0x08)
- {
- }
- }
- /* Enable peripheral clocks --------------------------------------------------*/
- /* Enable DMA clock */
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA, ENABLE);
- /* Enable ADC1 and GPIOC clock */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE);
- /* Enable GPIOA and USART1 clocks */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
- }
- /*******************************************************************************
- * Function Name : GPIO_Configuration
- * Description : Configures the different GPIO ports.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /* Configure PC.05 (ADC Channel15) as analog input -------------------------*/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- /* Configure USART1 Tx (PA.09) as alternate function push-pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- // /* Configure USART1 Rx (PA.10) as input floating */
- // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- // GPIO_Init(GPIOA, &GPIO_InitStructure);
- //
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- }
- /*******************************************************************************
- * Function Name : NVIC_Configuration
- * Description : Configures Vector Table base location.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
紅外測距模塊.7z
(1.67 MB, 下載次數: 36)
2019-11-19 20:21 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|