Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

ADC Measuring Sample with Maximum Volatage (CY8CKIT-044&059) Requrested

MotooTanaka
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

Dear Arath-san and for those who may be interested,

I created a sample project using CY8CKIT-044.

Request

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

What will be the code for:

When the ADC measure 4V display the label "Maximum Voltage" and display it

in Putty terminal.

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

Note: As it must handle 4V (or higher), I set J9 of CY8CKIT-044 to 3-2 (5V)

Also in the Design Wide Resources > System

I set VDDA, VDDD, VDDIO to 5.0

005-DWR_System.JPG

schematic

Note: To test the program, I connected a POT (5.0V, P2(0), GND)

003-schematic.JPG

pins

004-Pins.JPG

ADC Configuration

001-ADC_Config_1.JPG

002-ADC_Config_2.JPG

main.c

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

#include "project.h"

#include "stdio.h"

#define STR_BUF_LEN 64

char str[STR_BUF_LEN+1] ;

void print(char *str)

{

    UART_UartPutString(str) ;

}

void cls(void)

{

    print("\033c") ; /* reset */

    CyDelay(20) ;

    print("\033[2J") ; /* clear screen */

    CyDelay(20) ;

}

void splash(char *prog_name)

{

    cls() ;

    if (prog_name && *prog_name) {

        print(prog_name) ;

    }

    print(" (") ;

    print(__DATE__) ;

    print(" ") ;

    print(__TIME__) ;

    print(")\n\r") ;

}

void init_hardware(void)

{

    CyGlobalIntEnable; /* Enable global interrupts. */

    UART_Start() ;

 

    ADC_Start() ;

}

int16_t measure(void)

{

    int16_t raw_count ;

    int16_t mV ;

    int ch = 0 ;

 

    ADC_StartConvert() ;

    ADC_IsEndConversion(ADC_WAIT_FOR_RESULT) ;

    raw_count = ADC_GetResult16(ch) ;

    mV = ADC_CountsTo_mVolts(ch, raw_count) ;

    return( mV ) ;

}

void show_value(int16_t mV)

{

    int overflow_flag = 0 ;

    int negative = 0 ;

    if (mV >= 4000) {

        overflow_flag = 1 ;

    }

    if (mV < 0) {

        negative = 1 ;

        mV = -1 * mV ;

        print("-");

    }

    snprintf(str, STR_BUF_LEN, "%d.%03d ", mV/1000, mV%1000) ;

    print(str) ;

    if (overflow_flag) {

        print("Maximum Voltage") ;

    }

    print("\n\r") ;

}

 

int main(void)

{

    int16_t mV ;

 

    init_hardware() ;

 

    splash("CY8CKIT-044 ADC Test with limit Voltage") ;

    for(;;)

    {

        mV = measure() ;

        show_value(mV) ;

        CyDelay(1000) ;

    }

}

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

putty setup

006-putty-setup.JPG

putty output

007-putty.JPG

moto

P.S. BTW, you could post a question/request in this Code Example Space,

then you may not have to wait for a day. (As I was taking off, yesterday)

0 Likes
3 Replies
ArPa_4801991
Level 1
Level 1
First like received

Tanaka-san

I appreciate your help and fast response!

Muchas Gracias!

よろしくおねがいします

Arath

ArPa_4801991
Level 1
Level 1
First like received

One last question Tanaka-san

Will be the same code for CY8CKIT-059?

Only want to re confirm

Thanks

Arath

0 Likes

Dear Arath-san,

> Will be the same code for CY8CKIT-059?

LOL, NO.

Since you were referencing my Ryukoku Topic, I assumed that you were talking about PSoC 4.

Anyway, not a big difference(s), a version for CY8CKIT-059 here.

Schematic

010-schematic.JPG

Pins

011-Pins.JPG

main.c

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

#include "project.h"

#include "stdio.h"

#define STR_BUF_LEN 64

char str[STR_BUF_LEN+1] ;

void print(char *str)

{

    UART_PutString(str) ;

}

void cls(void)

{

    print("\033c") ; /* reset */

    CyDelay(20) ;

    print("\033[2J") ; /* clear screen */

    CyDelay(20) ;

}

void splash(char *prog_name)

{

    cls() ;

    if (prog_name && *prog_name) {

        print(prog_name) ;

    }

    print(" (") ;

    print(__DATE__) ;

    print(" ") ;

    print(__TIME__) ;

    print(")\n\r") ;

}

void init_hardware(void)

{

    CyGlobalIntEnable; /* Enable global interrupts. */

    UART_Start() ;

   

    ADC_Start() ;

}

int16_t measure(void)

{

    int16_t raw_count ;

    int16_t mV ;

   

    ADC_StartConvert() ;

    ADC_IsEndConversion(ADC_WAIT_FOR_RESULT) ;

    raw_count = ADC_GetResult16() ;

    mV = ADC_CountsTo_mVolts(raw_count) ;

    return( mV ) ;

}

void show_value(int16_t mV)

{

    int overflow_flag = 0 ;

    int negative = 0 ;

    if (mV >= 4000) {

        overflow_flag = 1 ;

    }

    if (mV < 0) {

        negative = 1 ;

        mV = -1 * mV ;

        print("-");

    }

    snprintf(str, STR_BUF_LEN, "%d.%03d ", mV/1000, mV%1000) ;

    print(str) ;

    if (overflow_flag) {

        print("Maximum Voltage") ;

    }

    print("\n\r") ;

}

   

int main(void)

{

    int16_t mV ;

   

    init_hardware() ;

   

    splash("CY8CKIT-059 ADC Test with limit Voltage") ;

    for(;;)

    {

        mV = measure() ;

        show_value(mV) ;

        CyDelay(1000) ;

    }

}

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

Best Regards,

21-Oct-2020

Motoo Tanaka

0 Likes