cancel
Showing results for 
Search instead for 
Did you mean: 

PSoC™ 6

prJ_3956181
New Contributor II

I HAVE REFERENCE TO INTERFACE HC-SRO4 TO PSOC4 PROTOTYPING KIT BUT I NEED TO HOW TO INTERFACE HC-SR04 ULTRASONIC SENSOR WITH PSOC6 AND HOW TO PROGRAM IT BECAUSE PDL(PERIPHERAL DEVICE LIBRARY) IS DIFFERENT FROM PSOC4 PROTOTYPING KIT AND HOW TO CONFIGURE COUNTER AND TIMER IN PSOC6?

0 Likes
1 Solution
MotooTanaka
Esteemed Contributor

Hi,

Although I'm not quite familiar with PSoC 6, I tried with CY8CKIT-062-WIFI-BW

IMG_3728.JPG

The schematic

000-schematic.JPG

Pin assignment

001-Pin-list.JPG

main.c

==================

#include "project.h"

#include <stdio.h>

/* https://keisan.casio.jp/exec/system/1231998943 */

#define MACH    346.513    /* Mach 1.0 at 25.0 */

volatile uint16_t duration = 0 ;

volatile int echo_flag = 0 ;

char str[128] ; /* print buffer */

void print(char *str)

{

    UART_PutString(str) ;

}

void Timer_Int_Handler(void)

{

    NVIC_ClearPendingIRQ(Timer_Echo_Int_cfg.intrSrc) ;

    NVIC_DisableIRQ((IRQn_Type)Timer_Echo_Int_cfg.intrSrc) ;

    duration = Timer_Echo_GetCapture() ;

    Timer_Echo_SetCounter(0) ;

    PWM_Trigger_Disable() ;

    echo_flag = 1 ;

}

void init_hardware(void)

{

    __enable_irq(); /* Enable global interrupts. */

  

    /* Enable CM4.  CY_CORTEX_M4_APPL_ADDR must be updated if CM4 memory layout is changed. */

//    Cy_SysEnableCM4(CY_CORTEX_M4_APPL_ADDR); // we don't use CM4 this time

    UART_Start() ;

  

    Cy_SysInt_Init(&Timer_Echo_Int_cfg, Timer_Int_Handler) ;

    NVIC_EnableIRQ((IRQn_Type)Timer_Echo_Int_cfg.intrSrc) ;

    Timer_Echo_Start() ;

    PWM_Trigger_Start() ;

}

/**

* print_value

* Calculate distance from the duration

* Since the duration include both way of the trip

* to get the distance, the real duration is the half

* of the duration.

*

* distance = duration * MACH(m/s) * 100(cm) / (2 * 12000000(Hz)) ;

*

*/

void print_value(uint32_t duration)

{

    double distance = 0.0 ;

  

    distance = (double)(duration) * MACH / 240000.0 ;

    sprintf(str, "%d.%02dcm\n", (int)distance, (int)(100 * distance)%100) ;

    UART_PutString(str) ;

}

int main(void)

{

    init_hardware() ;

  

    sprintf(str, "HC-SR04 Test (%s %s)\n", __DATE__, __TIME__) ;

    print(str) ;

  

    for(;;)

    {

        if (echo_flag) {

            print_value(duration) ;

            echo_flag = 0 ;

            NVIC_EnableIRQ((IRQn_Type)Timer_Echo_Int_cfg.intrSrc) ;

            PWM_Trigger_Start() ;

        }

        CyDelay(500) ;

    }

}

==================

Design Wide Resources > Interrupts

006-DesignWideResources-Interrupts.JPG

Tera Term Log

002-TeraTerm-Log.JPG

Wave form at 5cm

004-5cm.JPG

Wave form at 10cm

003-10cm.JPG

Wave form at 15cm

005-15cm.JPG

I hope this can be a hint for you.

moto

View solution in original post

0 Likes
4 Replies
AnkitaS_51
Employee

Hi ,

Currently, we don't have a project for PSoC6 with HC-SRO4 .

But please check this Code Example for PSoC6  where frequency measurement  done using Timer Counter by counting the number of edges (rising, falling, or both) that occur within a known time interval:

https://www.cypress.com/documentation/code-examples/ce220692-psoc-6-mcu-frequency-measurement-using-...

This CE will be helpful in porting and understanding Timer Counter usage with PSoC6.

Thanks,

Ankita

0 Likes
prJ_3956181
New Contributor II

@Ankita Thanks for the help

0 Likes
MotooTanaka
Esteemed Contributor

Hi,

Although I'm not quite familiar with PSoC 6, I tried with CY8CKIT-062-WIFI-BW

IMG_3728.JPG

The schematic

000-schematic.JPG

Pin assignment

001-Pin-list.JPG

main.c

==================

#include "project.h"

#include <stdio.h>

/* https://keisan.casio.jp/exec/system/1231998943 */

#define MACH    346.513    /* Mach 1.0 at 25.0 */

volatile uint16_t duration = 0 ;

volatile int echo_flag = 0 ;

char str[128] ; /* print buffer */

void print(char *str)

{

    UART_PutString(str) ;

}

void Timer_Int_Handler(void)

{

    NVIC_ClearPendingIRQ(Timer_Echo_Int_cfg.intrSrc) ;

    NVIC_DisableIRQ((IRQn_Type)Timer_Echo_Int_cfg.intrSrc) ;

    duration = Timer_Echo_GetCapture() ;

    Timer_Echo_SetCounter(0) ;

    PWM_Trigger_Disable() ;

    echo_flag = 1 ;

}

void init_hardware(void)

{

    __enable_irq(); /* Enable global interrupts. */

  

    /* Enable CM4.  CY_CORTEX_M4_APPL_ADDR must be updated if CM4 memory layout is changed. */

//    Cy_SysEnableCM4(CY_CORTEX_M4_APPL_ADDR); // we don't use CM4 this time

    UART_Start() ;

  

    Cy_SysInt_Init(&Timer_Echo_Int_cfg, Timer_Int_Handler) ;

    NVIC_EnableIRQ((IRQn_Type)Timer_Echo_Int_cfg.intrSrc) ;

    Timer_Echo_Start() ;

    PWM_Trigger_Start() ;

}

/**

* print_value

* Calculate distance from the duration

* Since the duration include both way of the trip

* to get the distance, the real duration is the half

* of the duration.

*

* distance = duration * MACH(m/s) * 100(cm) / (2 * 12000000(Hz)) ;

*

*/

void print_value(uint32_t duration)

{

    double distance = 0.0 ;

  

    distance = (double)(duration) * MACH / 240000.0 ;

    sprintf(str, "%d.%02dcm\n", (int)distance, (int)(100 * distance)%100) ;

    UART_PutString(str) ;

}

int main(void)

{

    init_hardware() ;

  

    sprintf(str, "HC-SR04 Test (%s %s)\n", __DATE__, __TIME__) ;

    print(str) ;

  

    for(;;)

    {

        if (echo_flag) {

            print_value(duration) ;

            echo_flag = 0 ;

            NVIC_EnableIRQ((IRQn_Type)Timer_Echo_Int_cfg.intrSrc) ;

            PWM_Trigger_Start() ;

        }

        CyDelay(500) ;

    }

}

==================

Design Wide Resources > Interrupts

006-DesignWideResources-Interrupts.JPG

Tera Term Log

002-TeraTerm-Log.JPG

Wave form at 5cm

004-5cm.JPG

Wave form at 10cm

003-10cm.JPG

Wave form at 15cm

005-15cm.JPG

I hope this can be a hint for you.

moto

View solution in original post

0 Likes
prJ_3956181
New Contributor II

@Motoo Tanaka Thanks for the help i will try the above

Top labels