[DemoCode下载] 使用NUC240接超音波模塊HC_SR04,作距離偵測

[复制链接]
1564|5
 楼主| a_ziliu 发表于 2017-4-14 14:08 | 显示全部楼层 |阅读模式
NUC240 <-> HC_SR04模塊
VCC<->VCC
GND<->GND
PA1<->TRIG
PE1<->ECHO
NUC240 HC_SR04.zip (1.73 MB, 下载次数: 12)
玛尼玛尼哄 发表于 2017-4-14 19:15 | 显示全部楼层
玛尼玛尼哄 发表于 2017-4-14 19:15 | 显示全部楼层
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V2.00
  4. * $Revision: 3 $
  5. * $Date: 15/04/10 10:26a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    NUC230_240 Series GPIO Driver Sample Code
  7. *
  8. * @note
  9. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  10. ******************************************************************************/
  11. #include <stdio.h>
  12. #include "NUC230_240.h"


  13. #define PLL_CLOCK           72000000


  14. void SYS_Init(void)
  15. {
  16.     /*---------------------------------------------------------------------------------------------------------*/
  17.     /* Init System Clock                                                                                       */
  18.     /*---------------------------------------------------------------------------------------------------------*/

  19.     /* Enable Internal RC 22.1184MHz clock */
  20.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  21.     /* Waiting for Internal RC clock ready */
  22.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

  23.     /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
  24.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

  25.     /* Enable external XTAL 12MHz clock */
  26.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  27.     /* Waiting for external XTAL clock ready */
  28.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  29.     /* Set core clock as PLL_CLOCK from PLL */
  30.     CLK_SetCoreClock(PLL_CLOCK);

  31.     /* Enable UART module clock */
  32.     CLK_EnableModuleClock(UART0_MODULE);

  33.     /* Select UART module clock source */
  34.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));

  35.     /*---------------------------------------------------------------------------------------------------------*/
  36.     /* Init I/O Multi-function                                                                                 */
  37.     /*---------------------------------------------------------------------------------------------------------*/

  38.     /* Set GPB multi-function pins for UART0 RXD and TXD */
  39.     SYS->GPB_MFP &= ~(SYS_GPB_MFP_PB0_Msk | SYS_GPB_MFP_PB1_Msk);
  40.     SYS->GPB_MFP |= (SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD);

  41. }


  42. #define     MaxValue            0xFFFF
  43. unsigned int cap_buffer[256];
  44. unsigned int cap_edge[256];
  45. unsigned int cap_count;
  46. double length,time;
  47. void PWMB_IRQHandler(void)
  48. {
  49.         //1 Rising edge latch interrupt
  50.         if(PWM_GetCaptureIntFlag(PWMB, 3)==1)
  51.         {
  52.         PWM_ClearCaptureIntFlag(PWMB, 3, PWM_CAPTURE_INT_RISING_LATCH);
  53.         }
  54.         //2 Falling edge latch interrupt
  55.         if(PWM_GetCaptureIntFlag(PWMB, 3)==2)
  56.         {
  57.                 PWM_ClearCaptureIntFlag(PWMB, 3, PWM_CAPTURE_INT_FALLING_LATCH);
  58.         time=MaxValue - PWM_GET_CAPTURE_FALLING_DATA(PWMB, 3);
  59.                 /*
  60.                                 if(cap_count<256)
  61.                 {
  62.         PWM_ClearCaptureIntFlag(PWMB, 3, PWM_CAPTURE_INT_FALLING_LATCH);
  63.         cap_buffer[cap_count]=MaxValue - PWM_GET_CAPTURE_FALLING_DATA(PWMB, 3);

  64.         cap_count++;
  65.                 }
  66.                 */
  67.         }
  68.         if(PWM_GetCaptureIntFlag(PWMB, 3)==3)
  69.         {
  70.         while(1);
  71.         }
  72. }


  73. /*---------------------------------------------------------------------------------------------------------*/
  74. /* MAIN function                                                                                           */
  75. /*---------------------------------------------------------------------------------------------------------*/
  76. int main(void)
  77. {

  78.     /* Unlock protected registers */
  79.     SYS_UnlockReg();

  80.     /* Init System, peripheral clock and multi-function I/O */
  81.     SYS_Init();

  82.     /* Lock protected registers */
  83.     SYS_LockReg();
  84. #if 0
  85.          

  86.   /* Init UART0 for printf */
  87.     UART0_Init();

  88.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
  89.     printf("+-------------------------------------------------+\n");
  90.     printf("|    PB.2(Output) and PE.1(Input) Sample Code     |\n");
  91.     printf("+-------------------------------------------------+\n\n");
  92.     printf("  >> Please connect PB.2 and PE.1 first << \n");
  93.     printf("     Press any key to start test by using [Pin Data Input/Output Control] \n\n");
  94.     getchar();
  95.    
  96.     /* Configure PB.2 as Output mode and PE.1 as Input mode */
  97.     GPIO_SetMode(PB, BIT2, GPIO_PMD_OUTPUT);
  98.     GPIO_SetMode(PE, BIT1, GPIO_PMD_INPUT);

  99.     i32Err = 0;
  100.     printf("GPIO PB.2(output mode) connect to PE.1(input mode) ......");

  101.     /* Use Pin Data Input/Output Control to pull specified I/O or get I/O pin status */
  102.     /* Pull PB.2 to Low and check PE.1 status */
  103.     PB2 = 0;
  104.     if(PE1 != 0) {
  105.         i32Err = 1;
  106.     }

  107.     /* Pull PB.2 to High and check PE.1 status */
  108.     PB2 = 1;
  109.     if(PE1 != 1) {
  110.         i32Err = 1;
  111.     }

  112.     if(i32Err) {
  113.         printf("  [FAIL].\n");
  114.     } else {
  115.         printf("  [OK].\n");
  116.     }

  117.     /* Configure PB.2 and PE.1 to default Quasi-bidirectional mode */
  118.     GPIO_SetMode(PB, BIT2, GPIO_PMD_QUASI);
  119.     GPIO_SetMode(PE, BIT1, GPIO_PMD_QUASI);
  120. #endif
  121.                 GPIO_SetMode(PA, BIT0, GPIO_PMD_OUTPUT);
  122.                 SystemCoreClockUpdate();
  123.                 PA0=0;
  124.                 CLK_SysTickDelay(500);
  125.                   //Initial PWM7 Function Pin
  126.     SYS->GPE_MFP = (SYS->GPE_MFP & ~SYS_GPE_MFP_PE1_Msk) | SYS_GPE_MFP_PE1_PWM7;

  127.     /* Enable PWM module clock */
  128.     CLK_EnableModuleClock(PWM67_MODULE);


  129.     /* Select PWM module clock source */
  130.     CLK_SetModuleClock(PWM67_MODULE, CLK_CLKSEL2_PWM67_S_HXT, 0);

  131.     /* Reset PWMA channel4~channel7 */
  132.     SYS_ResetModule(PWM47_RST);

  133.     /* Enable Backward Compatible: write 1 to clear CFLRI0~3 and CRLRI0~3 */
  134.     PWMB->PBCR = 1;
  135.     PWMB->PCR |= PWM_PCR_CH3MOD_Msk;
  136.     PWM_SET_DIVIDER(PWMB, 3, PWM_CLK_DIV_4);                            //Set PWM1_CH3 Clock Source Divider
  137.     PWM_SET_PRESCALER(PWMB, 3, 2);                                      //Set PWM1_CH2 and PWM1_CH3 Prescaler
  138.     PWMB->CNR3 = MaxValue;

  139.     //Start Capture
  140.     PWM_EnableCapture(PWMB, 0x8);

  141.     PWM_EnableCaptureInt(PWMB, 3, PWM_CAPTURE_INT_FALLING_LATCH|PWM_CAPTURE_INT_RISING_LATCH);
  142.     NVIC_EnableIRQ(PWMB_IRQn);
  143.     PWM_Start(PWMB, 0x8);
  144.                
  145.                
  146.     while(1){

  147.                         PA0=1;       
  148.                         CLK_SysTickDelay(10);
  149.                         PA0=0;
  150.                         CLK_SysTickDelay(600000);
  151.                         length=time*34.0/2000.0;
  152.                         printf("length=%f\n\r",length);
  153.                        
  154.                 }
  155. }

  156. /*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/


玛尼玛尼哄 发表于 2017-4-14 19:16 | 显示全部楼层
该模块的工作原理为,先向TRIG脚输入至少10us的触发信号,该模块内部将发出 8 个 40kHz 周期电平并检测回波。一旦检测到有回波信号则ECHO输出高电平回响信号。回响信号的脉冲宽度与所测的距离成正比。由此通过发射信号到收到的回响信号时间间隔可以计算得到距离。公式: 距离=高电平时间*声速(340M/S)/2。
玛尼玛尼哄 发表于 2017-4-14 19:17 | 显示全部楼层
此模块共有4只引出脚,从左往右,第一脚为VCC,由于该模块工作电压为5V;第二只脚为TRIG,输入触发信号;第三只脚为ECHO,输出回响信号;第四只脚为接地脚。
天灵灵地灵灵 发表于 2017-4-14 20:51 | 显示全部楼层
楼上补刀的厉害,楼主什么资料都不给。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

100

主题

310

帖子

6

粉丝
快速回复 在线客服 返回列表 返回顶部