久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2774|回復: 0
打印 上一主題 下一主題
收起左側

stm32103紅外測距模塊程序 串口打印電壓,距離

[復制鏈接]
跳轉到指定樓層
樓主
模塊共三個接口:紅線---VCC_5V;黑線---GND;黃線—PC5(ADC數據采樣)
將模塊通電,黃線接到ADC通道輸入端即可工作;將采樣得到的電壓值通過填入表格,進行線性化處理,得到線性化公式。通過公式,可將ADC采樣值轉化為距離值。實測,在10cm—30cm范圍內,較為準確,最大誤差在1cm。模塊對被測角度的靈敏度很高,同一位置,不同的角度,誤差可以超過5cm,最好的測量角度是90度。


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

單片機源程序如下:
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "stm32f10x_lib.h"
  3. #include "stdio.h"

  4. /* Private macro -------------------------------------------------------------*/
  5. #define countof(a)   (sizeof(a) / sizeof(*(a)))

  6. /* Private typedef -----------------------------------------------------------*/
  7. #define TxBufferSize   (countof(TxBuffer) - 1)

  8. /* Private define ------------------------------------------------------------*/
  9. u8 TxBuffer[] = "ADC Example1: ADC TO DMA TO UART1\r\n";
  10. u8 TxCounter = 0;

  11. /* Private define ------------------------------------------------------------*/
  12. #define ADC1_DR_Address    ((u32)0x4001244C)

  13. /* Private variables ---------------------------------------------------------*/
  14. USART_InitTypeDef USART_InitStructure;
  15. ADC_InitTypeDef ADC_InitStructure;
  16. DMA_InitTypeDef DMA_InitStructure;
  17. vu16 ADC_ConvertedValue;
  18. ErrorStatus HSEStartUpStatus;
  19.    
  20. /* Private function prototypes -----------------------------------------------*/
  21. void RCC_Configuration(void);
  22. void GPIO_Configuration(void);
  23. void NVIC_Configuration(void);
  24.   
  25. /* Private functions ---------------------------------------------------------*/
  26. void Delay_us(unsigned short us)
  27. {
  28.     unsigned short i;
  29.     while(us--)
  30.     {
  31.             for(i=0;i<10;i++);
  32.     }
  33. }

  34. void Delay_ms(unsigned short ms)
  35. {
  36.     unsigned short i;
  37.         while(ms--)
  38.     {
  39.         for(i=0;i<10000;i++);
  40.     }
  41. }

  42. /**********發送數據**********/
  43. int fputc(int ch, FILE *f)
  44. {
  45.     USART_SendData(USART1,(unsigned char)ch);//USART1可以換成USART2等
  46.         while(!(USART1->SR&USART_FLAG_TXE));
  47.     return(ch);
  48. }

  49. /**********接收數據**********/
  50. int GetKey(void)
  51. {
  52.     while(!(USART1->SR&USART_FLAG_RXNE));
  53.         return((int)(USART1->DR&0x1FF));
  54. }

  55. /*******************************************************************************
  56. * Function Name  : main
  57. * Description    : Main program
  58. * Input          : None
  59. * Output         : None
  60. * Return         : None
  61. *******************************************************************************/
  62. int main(void)
  63. {
  64. //    unsigned char i=1;
  65.     unsigned long Tmp_Dat=0,i=1;
  66.         float distance=0;
  67. #ifdef DEBUG
  68.     debug();
  69. #endif

  70.   RCC_Configuration();   //系統時鐘配置
  71.   NVIC_Configuration();   //NVIC配置
  72.   GPIO_Configuration();   //GPIO配置

  73.   /* USART1 configuration ------------------------------------------------------*/
  74.   /* USART1 configured as follow:
  75.         - BaudRate = 9600 baud  
  76.         - Word Length = 8 Bits
  77.         - Two Stop Bit
  78.         - Odd parity
  79.         - Hardware flow control disabled (RTS and CTS signals)
  80.         - Receive and transmit enabled
  81.         - USART Clock disabled
  82.         - USART CPOL: Clock is active low
  83.         - USART CPHA: Data is captured on the second edge
  84.         - USART LastBit: The clock pulse of the last data bit is not output to
  85.                          the SCLK pin
  86.   */
  87.   USART_InitStructure.USART_BaudRate = 9600;
  88.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  89.   USART_InitStructure.USART_StopBits = USART_StopBits_1;
  90.   USART_InitStructure.USART_Parity = USART_Parity_No;
  91.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  92.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  93.   USART_InitStructure.USART_Clock = USART_Clock_Disable;
  94.   USART_InitStructure.USART_CPOL = USART_CPOL_Low;
  95.   USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;
  96.   USART_InitStructure.USART_LastBit = USART_LastBit_Disable;

  97.   /* Configure the USART1 */
  98.   USART_Init(USART1, &USART_InitStructure);

  99. /* Enable the USART Transmoit interrupt: this interrupt is generated when the
  100.    USART1 transmit data register is empty */  
  101.   USART_ITConfig(USART1, USART_IT_TXE, ENABLE);

  102. /* Enable the USART Receive interrupt: this interrupt is generated when the
  103.    USART1 receive data register is not empty */
  104.   USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

  105.   /* Enable USART1 */
  106.   USART_Cmd(USART1, ENABLE);

  107.   /* DMA channel1 configuration ----------------------------------------------*/
  108.   DMA_DeInit(DMA_Channel1);
  109.   DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
  110.   DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue;
  111.   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  112.   DMA_InitStructure.DMA_BufferSize = 1;
  113.   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  114.   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
  115.   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  116.   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  117.   DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  118.   DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  119.   DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  120.   DMA_Init(DMA_Channel1, &DMA_InitStructure);
  121.   
  122.   /* Enable DMA channel1 */
  123.   DMA_Cmd(DMA_Channel1, ENABLE);
  124.      
  125.   /* ADC1 configuration ------------------------------------------------------*/
  126.   ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  127.   ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  128.   ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  129.   ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  130.   ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  131.   ADC_InitStructure.ADC_NbrOfChannel = 1;
  132.   ADC_Init(ADC1, &ADC_InitStructure);

  133.   /* ADC1 regular channel1 configuration */
  134.   ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 1, ADC_SampleTime_55Cycles5);
  135.   ADC_DMACmd(ADC1, ENABLE);   /* 使能ADC1 DMA */
  136.   ADC_Cmd(ADC1, ENABLE);     /* 使能 ADC1 */
  137.   ADC_ResetCalibration(ADC1);     /* 使能 ADC1 reset calibaration register */
  138.   while(ADC_GetResetCalibrationStatus(ADC1));     /* Check the end of ADC1 reset calibration register */
  139.   ADC_StartCalibration(ADC1);     /* Start ADC1 calibaration */
  140.   while(ADC_GetCalibrationStatus(ADC1));     /* Check the end of ADC1 calibration */
  141.   ADC_SoftwareStartConvCmd(ADC1, ENABLE);     /* Start ADC1 Software Conversion */


  142.   for(i=0;i<TxBufferSize;i++)
  143.   {
  144.    /* Write one byte to the transmit data register */
  145.       
  146.   USART_SendData(USART1, TxBuffer[TxCounter++]);
  147.   while (!(USART1->SR & USART_FLAG_TXE));   //等待緩沖區空
  148.   while (!(USART1->SR & USART_FLAG_TC));   //等待發送完成
  149.   }

  150.    while (1)
  151.   {   
  152.       Delay_ms(1000);
  153.    
  154.       Tmp_Dat =  ADC_ConvertedValue;
  155.       
  156. //      distance = (1/(Tmp_Dat*0.0000228324+0.00140335))-4.0;
  157.       distance = (1/(Tmp_Dat*0.0000272988-0.002268704))-4.0;
  158. //      distance = (1/(Tmp_Dat*0.000147-0.00042))-4.0;

  159.       printf("%ld\r\n",Tmp_Dat);
  160.       printf("distance=%.2f\r\n",distance);

  161.       Tmp_Dat = Tmp_Dat*3300/0x0fff;

  162.       TxBuffer[0] = Tmp_Dat/1000+'0';
  163.       TxBuffer[1] = '.';
  164.       TxBuffer[2] = (Tmp_Dat%1000)/100+'0';
  165.       TxBuffer[3] = (Tmp_Dat%100)/10+'0';
  166.       TxBuffer[4] = Tmp_Dat%10+'0';
  167.       TxBuffer[5] = 'V';

  168.       USART_SendData(USART1, '[');
  169.       while (!(USART1->SR & USART_FLAG_TXE));   //等待緩沖區空
  170.       while (!(USART1->SR & USART_FLAG_TC));   //等待發送完成

  171.       USART_SendData(USART1, TxBuffer[0]);
  172.       while (!(USART1->SR & USART_FLAG_TXE));   //等待緩沖區空
  173.       while (!(USART1->SR & USART_FLAG_TC));   //等待發送完成

  174.       USART_SendData(USART1, TxBuffer[1]);
  175.       while (!(USART1->SR & USART_FLAG_TXE));   //等待緩沖區空
  176.       while (!(USART1->SR & USART_FLAG_TC));   //等待發送完成

  177.       USART_SendData(USART1, TxBuffer[2]);
  178.       while (!(USART1->SR & USART_FLAG_TXE));   //等待緩沖區空
  179.       while (!(USART1->SR & USART_FLAG_TC));   //等待發送完成

  180.       USART_SendData(USART1, TxBuffer[3]);
  181.       while (!(USART1->SR & USART_FLAG_TXE));   //等待緩沖區空
  182.       while (!(USART1->SR & USART_FLAG_TC));   //等待發送完成

  183.       USART_SendData(USART1, TxBuffer[4]);
  184.       while (!(USART1->SR & USART_FLAG_TXE));   //等待緩沖區空
  185.       while (!(USART1->SR & USART_FLAG_TC));   //等待發送完成

  186.       USART_SendData(USART1, TxBuffer[5]);
  187.       while (!(USART1->SR & USART_FLAG_TXE));   //等待緩沖區空
  188.       while (!(USART1->SR & USART_FLAG_TC));   //等待發送完成

  189.       USART_SendData(USART1, ']');
  190.       while (!(USART1->SR & USART_FLAG_TXE));   //等待緩沖區空
  191.       while (!(USART1->SR & USART_FLAG_TC));   //等待發送完成
  192.       printf("\r\n");
  193.   }
  194. }

  195. /*******************************************************************************
  196. * Function Name  : RCC_Configuration
  197. * Description    : Configures the different system clocks.
  198. * Input          : None
  199. * Output         : None
  200. * Return         : None
  201. *******************************************************************************/
  202. void RCC_Configuration(void)
  203. {
  204.   /* RCC system reset(for debug purpose) */
  205.   RCC_DeInit();

  206.   /* Enable HSE */
  207.   RCC_HSEConfig(RCC_HSE_ON);

  208.   /* Wait till HSE is ready */
  209.   HSEStartUpStatus = RCC_WaitForHSEStartUp();

  210.   if(HSEStartUpStatus == SUCCESS)
  211.   {
  212.     /* Enable Prefetch Buffer */
  213.     FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

  214.     /* Flash 2 wait state */
  215.     FLASH_SetLatency(FLASH_Latency_2);
  216.   
  217.     /* HCLK = SYSCLK */
  218.     RCC_HCLKConfig(RCC_SYSCLK_Div1);
  219.   
  220.     /* PCLK2 = HCLK */
  221.     RCC_PCLK2Config(RCC_HCLK_Div1);

  222.     /* PCLK1 = HCLK/2 */
  223.     RCC_PCLK1Config(RCC_HCLK_Div2);

  224.     /* ADCCLK = PCLK2/4 */
  225.     RCC_ADCCLKConfig(RCC_PCLK2_Div4);
  226.   
  227.     /* PLLCLK = 8MHz * 7 = 56 MHz */
  228.     RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_7);

  229.     /* Enable PLL */
  230.     RCC_PLLCmd(ENABLE);

  231.     /* Wait till PLL is ready */
  232.     while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  233.     {
  234.     }

  235.     /* Select PLL as system clock source */
  236.     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

  237.     /* Wait till PLL is used as system clock source */
  238.     while(RCC_GetSYSCLKSource() != 0x08)
  239.     {
  240.     }
  241.   }

  242. /* Enable peripheral clocks --------------------------------------------------*/

  243.   /* Enable DMA clock */
  244.   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA, ENABLE);

  245.   /* Enable ADC1 and GPIOC clock */
  246.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE);

  247.   /* Enable GPIOA and USART1 clocks */
  248.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
  249. }

  250. /*******************************************************************************
  251. * Function Name  : GPIO_Configuration
  252. * Description    : Configures the different GPIO ports.
  253. * Input          : None
  254. * Output         : None
  255. * Return         : None
  256. *******************************************************************************/
  257. void GPIO_Configuration(void)
  258. {
  259.   GPIO_InitTypeDef GPIO_InitStructure;

  260.   /* Configure PC.05 (ADC Channel15) as analog input -------------------------*/
  261.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  262.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  263.   GPIO_Init(GPIOC, &GPIO_InitStructure);

  264.   /* Configure USART1 Tx (PA.09) as alternate function push-pull */
  265.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  266.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  267.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  268.   GPIO_Init(GPIOA, &GPIO_InitStructure);

  269. //  /* Configure USART1 Rx (PA.10) as input floating */
  270. //  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  271. //  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  272. //  GPIO_Init(GPIOA, &GPIO_InitStructure);
  273. //
  274.    
  275.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  276.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  277.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  278.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  279. }

  280. /*******************************************************************************
  281. * Function Name  : NVIC_Configuration
  282. * Description    : Configures Vector Table base location.
  283. * Input          : None
  284. * Output         : None
  285. * Return         : None
  286. *******************************************************************************/
  287. void NVIC_Configuration(void)
  288. {
  289.   NVIC_InitTypeDef NVIC_InitStructure;
  290. ……………………

  291. …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼

所有資料51hei提供下載:
紅外測距模塊.7z (1.67 MB, 下載次數: 36)



評分

參與人數 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 四季久久免费一区二区三区四区 | 免费a v网站 | 成人水多啪啪片 | 国产7777 | 粉嫩国产精品一区二区在线观看 | 国产免费一区二区三区 | 日韩欧美一区二区三区免费观看 | 手机看片在线播放 | 久久精品亚洲一区 | 国产精品亚洲视频 | 超碰在线国产 | 午夜丰满少妇一级毛片 | 视频一区在线观看 | 人人叉| 91精品久久久久久久久中文字幕 | 国产一区二区在线视频 | 影音先锋中文字幕在线观看 | 天天操天天玩 | 欧美寡妇偷汉性猛交 | 欧美亚洲一区二区三区 | 日韩成人av在线播放 | 久久久91精品国产一区二区三区 | 麻豆久久久 | 黄网站涩免费蜜桃网站 | 亚洲国产一区二区三区 | 中文字幕亚洲无线 | 亚洲三区在线观看 | 国产一级视频在线播放 | 国产高清精品网站 | 色综合视频 | 亚洲第一区久久 | 粉嫩一区二区三区四区公司1 | 久久精品视频免费观看 | 日韩一级欧美一级 | 国产一级片在线播放 | 国产一区二区影院 | 日韩高清一区 | 成人午夜毛片 | 56pao在线 | 91网站在线观看视频 | 欧美日韩不卡合集视频 |