cancel
Showing results for 
Search instead for 
Did you mean: 

PSoC 4 MCU

RoFr_4201436
New Contributor II

I need to make this exercise in PSOC 4:

"Use an analog port to read from the KIT GROVE potentiometer. Confuse an ADC of a

channel, eight BITS, and 100 Hz sampling. Use the API generated by PSoC Creator and

breakpoint in the XXX_GetResult16 (..) function. Read and find the value in Volts (Vx), knowing

that Vref + = 3.3V and Vref- = 0V. Observe Equation 1 below, where X is the digital read value and Vx the

value in Volts read. Calculate the value of Vx and compare the result obtained in

XXX_CountsTo_mV (0, X).

Vref+ - Vref-    --------------- 2n – 1

Vx – Vref-   --------------- X

Vx = (X * (Vref+ - Vref-) / (2n - 1) ) – Vref-

Use the voltage value provided by the potentiometer to modulate the frequency at which the

Red LED flashes."

I did the pratice this way:

I used 1 analog input,1 digital output for the LED and 1 ADC.

pastedImage_1.png

Settings ADC:

pastedImage_2.png

pastedImage_3.png

And i leave my code this way:

#include "project.h"

int main(void)

{

    CyGlobalIntEnable; /* Enable global interrupts. */

   

  

    ADC_Init();

    ADC_StartConvert();

    ADC_IsEndConversion(ADC_WAIT_FOR_RESULT);

    int16 x = ADC_GetResult16(0);

    float32 y = ADC_CountsTo_mVolts(0, x);

   

    for(;;)

    {

        LED_Write(!LED_Read());

        CyDelay(1000 *y);

        ADC_StartConvert();

        ADC_IsEndConversion(ADC_WAIT_FOR_RESULT);

       

        x = ADC_GetResult16(0);

        y = ADC_CountsTo_mVolts(0, x);

    }

}

But my code is not working. Help me!

0 Likes
1 Solution
rola_264706
Honored Contributor II

I have a question what PSOC module are you using?  If you are using the CY5677 CySmart BLE 4.2 USB Dongle then the pins you selected are already used by other devices on the board .  You have to look at the schematic and pick pins that are not used on the board.  One of these pins goes to Switch on pin  P2-6 and the other pin goes to a pin that you may use.  Or are you using another Module.  It is best to give the most information about your Module so we can help you.

View solution in original post

0 Likes
5 Replies
MotooTanaka
Esteemed Contributor

Hi,

First quick answer (suggestion)

ADC_CountsTo_mVolt() returns int16_t not float.

So if you change

> float32 y

to

< int16_t y

and

> CyDelay(1000 * y) ;

to

< CyDelay(y) ;

I hope that your program will work as you expected.

Second, again you attached un-openable archive orz

Please do something like below to create an archive project

(1) Clean the project (sample is my project)

002-clean-project.JPG

(2) Archive using the menu Project > Archive Workspace/Project

003-archive-project.JPG

(3) Specify "Minimal" in the dialog and select "Archive"

004-minimum.JPG

Then you will get <your project>.zip and please attach that archive file.

Third, changing the delay in the main loop does not seem to be too elegant to me.

So I would do something like below

I would use PWM to control LED.

As the LED on my CY8CKIT-044 is active low, I connected LED to line_n,

if the LED on your board is active high connect it to line pin of PWM.

Schematic

001-schematic.JPG

main.c

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

#include "project.h"

#include "stdio.h"

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

void print(char *str)

{

    UART_UartPutString(str) ;

}

void init_hardware(void)

{

    CyGlobalIntEnable; /* Enable global interrupts. */

    UART_Start() ;

    PWM_Start() ;

    ADC_Start() ;

}

void splash(void)

{

    sprintf(str, "ADC -> PWM test (%s %s)\n", __DATE__, __TIME__) ;

    print(str) ;

}

float measure(void)

{

    int16_t adc_count ;

    int16_t mV ;

    int     ch = 0 ;

   

    ADC_StartConvert() ;

    ADC_IsEndConversion(ADC_WAIT_FOR_RESULT) ;

    adc_count = ADC_GetResult16(ch) ;

    mV = ADC_CountsTo_mVolts(ch, adc_count) ;

    if (mV < 0) {

        mV = 0 ;

    }

    return( mV ) ;

}

int main(void)

{

    uint16_t adc_ref_val = 3300 ;

    uint16_t mV = 0 ;

    uint16_t period = 10000 ;

    uint16_t compare = 0 ; /* pwm duty is compare / period */

   

    init_hardware() ;

    splash() ;

   

    PWM_WritePeriod(period) ;

   

    for(;;)

    {

        mV = measure() ;

        compare = (int)(period * mV / adc_ref_val) ; ;

        PWM_WriteCompare( compare ) ;

        sprintf(str, "POT = %d.%02d V\n", mV/1000, mV%1000) ;

        print(str) ;

        CyDelay(100) ;

    }

}

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

Tera Term log

000-TeraTerm-log.JPG

moto

0 Likes
RoFr_4201436
New Contributor II

Hey man, i was try change but not working in my PSOC.

The light of the LED keep on and never decrease.

Try to see my program.

Ahh... I'm not learn about the UART yet, so... Can you help me with the simple way?

0 Likes
MotooTanaka
Esteemed Contributor

Hi,

I modified your program like below,

at least it seems to be working on my CY8C4146LQI-S433 board.

Note: The mV value is 0 ~ 3300 or so, and if we feed it directly to the CyDelay()

although it is working, it's difficult for us to notice, so I changed it to

CyDelay(mV/30) ;

With this it was easier to see the result.

main.c

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

#include "project.h"

int main(void)

{

    int16_t adc_count ;

    int16_t mV = 0 ;

    int16_t ch = 0 ;

   

    CyGlobalIntEnable; /* Enable global interrupts. */

   

    ADC_Start() ;

   

    for(;;)

    {

        LED_Write(!LED_Read());

        ADC_StartConvert();

        ADC_IsEndConversion(ADC_WAIT_FOR_RESULT);

        adc_count = ADC_GetResult16(ch);

        mV = ADC_CountsTo_mVolts(ch, adc_count);

       

        CyDelay(mV/30);

    }

}

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

moto

0 Likes
rola_264706
Honored Contributor II

I have a question what PSOC module are you using?  If you are using the CY5677 CySmart BLE 4.2 USB Dongle then the pins you selected are already used by other devices on the board .  You have to look at the schematic and pick pins that are not used on the board.  One of these pins goes to Switch on pin  P2-6 and the other pin goes to a pin that you may use.  Or are you using another Module.  It is best to give the most information about your Module so we can help you.

View solution in original post

0 Likes
rola_264706
Honored Contributor II

Sir you still haven't answered my question on what PSOC module you are using.  This is important because the module design can stop your program from working.

0 Likes