PSOC4 LED Brightness control using two push button switch

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

cross mob
AbLe_4238186
Level 1
Level 1
Code Expert

Hello, I am new to PSOC development and currently I'm using PSoC 4200L CY8C4246AZI-L433 for a simple brightness control with two push button switch connected to its pin.

Just want to understand how to configure PSOC4 where I can send PWM from let say P1.4 as output to control LED brightness with two push button switch say from P1.0(Down ) and P1.1(UP)? Is there an example related to this.Thanks

BR,

Abe

0 Likes
1 Solution
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

I tried with following configuration

(1) Schematic

I assumed that "Up" and "Down" switches are active low (low when pushed)

If they are active high, please change the pin configs from pull-up to pull-down

and remove "NOTs" from the schematic.

Similarly, I assumed that "LED" is active-low, if not, please connect "LED" to "line" instead of "line_n".

000-schematic.JPG

(2) Up (pin config)

002-Up-config.JPG

(3) Down (pin config)

003-Down-config.JPG

(4) up_int (interrupt config)

004-up-int-config.JPG

(5) down_int (interrupt config)

005-down-int-config.JPG

(6) pin assignment list

001-pin-assign.JPG

(7) main.c

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

#include "project.h"

#define PWM_DELTA 1000

volatile int up_flag = 0 ;

volatile int down_flag = 0 ;

uint16_t pwm_period = 10000 ;

int      pwm_compare = 5000 ;

CY_ISR(up_isr)

{

    up_int_ClearPending() ;

    up_flag = 1 ;

}

CY_ISR(down_isr)

{

    down_int_ClearPending() ;

    down_flag = 1 ;

}

void pwm_up(void)

{

    pwm_compare += PWM_DELTA ;

    if (pwm_compare > pwm_period) {

        pwm_compare = pwm_period ;

    }

    PWM_Stop() ;

    PWM_WriteCompare(pwm_compare) ;

    PWM_WriteCounter(0) ;

    PWM_Enable() ;

}

void pwm_down(void)

{

    pwm_compare -= PWM_DELTA ;

    if (pwm_compare < 0) {

        pwm_compare = 0 ;

    }

    PWM_Stop() ;

    PWM_WriteCompare(pwm_compare) ;

    PWM_WriteCounter(0) ;

    PWM_Enable() ;

}

int main(void)

{

    CyGlobalIntEnable; /* Enable global interrupts. */

 

    up_int_ClearPending() ;

    up_int_StartEx(up_isr) ;

 

    down_int_ClearPending() ;

    down_int_StartEx(down_isr) ;

 

    PWM_WritePeriod(pwm_period) ;

    PWM_WriteCompare(pwm_compare) ;

    PWM_WriteCounter(0) ;

    PWM_Start() ;

     

    for(;;)

    {

        if (up_flag) {

            pwm_up() ;

            CyDelay(100) ;

            up_flag = 0 ;

        }

        if (down_flag) {

            pwm_down() ;

            CyDelay(100) ;

            down_flag = 0 ;

        }

    }

}

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

I could confirm that this project can be built,

but as I don't have a board with the specified MCU I could not test this.

When I copied this project and re-targeted to 4200M,

it worked with CY8CKIT-044 (LED was P2[6]).

moto

View solution in original post

2 Replies
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

Abe,

I would suggest to use a community component ButtonSw32 for reading buttons, and use a PSoC4 basic demo project (see Figure 5 of the Application Note provided):

ButtonSw32: button switch debouncer component

The readme.txt file provides installation instructions.

Once you make LED to on/off, it is will be easy to add PWM for brightness control.

/odissey1

ButtonSw_P4_basic_1b.png

0 Likes
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

I tried with following configuration

(1) Schematic

I assumed that "Up" and "Down" switches are active low (low when pushed)

If they are active high, please change the pin configs from pull-up to pull-down

and remove "NOTs" from the schematic.

Similarly, I assumed that "LED" is active-low, if not, please connect "LED" to "line" instead of "line_n".

000-schematic.JPG

(2) Up (pin config)

002-Up-config.JPG

(3) Down (pin config)

003-Down-config.JPG

(4) up_int (interrupt config)

004-up-int-config.JPG

(5) down_int (interrupt config)

005-down-int-config.JPG

(6) pin assignment list

001-pin-assign.JPG

(7) main.c

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

#include "project.h"

#define PWM_DELTA 1000

volatile int up_flag = 0 ;

volatile int down_flag = 0 ;

uint16_t pwm_period = 10000 ;

int      pwm_compare = 5000 ;

CY_ISR(up_isr)

{

    up_int_ClearPending() ;

    up_flag = 1 ;

}

CY_ISR(down_isr)

{

    down_int_ClearPending() ;

    down_flag = 1 ;

}

void pwm_up(void)

{

    pwm_compare += PWM_DELTA ;

    if (pwm_compare > pwm_period) {

        pwm_compare = pwm_period ;

    }

    PWM_Stop() ;

    PWM_WriteCompare(pwm_compare) ;

    PWM_WriteCounter(0) ;

    PWM_Enable() ;

}

void pwm_down(void)

{

    pwm_compare -= PWM_DELTA ;

    if (pwm_compare < 0) {

        pwm_compare = 0 ;

    }

    PWM_Stop() ;

    PWM_WriteCompare(pwm_compare) ;

    PWM_WriteCounter(0) ;

    PWM_Enable() ;

}

int main(void)

{

    CyGlobalIntEnable; /* Enable global interrupts. */

 

    up_int_ClearPending() ;

    up_int_StartEx(up_isr) ;

 

    down_int_ClearPending() ;

    down_int_StartEx(down_isr) ;

 

    PWM_WritePeriod(pwm_period) ;

    PWM_WriteCompare(pwm_compare) ;

    PWM_WriteCounter(0) ;

    PWM_Start() ;

     

    for(;;)

    {

        if (up_flag) {

            pwm_up() ;

            CyDelay(100) ;

            up_flag = 0 ;

        }

        if (down_flag) {

            pwm_down() ;

            CyDelay(100) ;

            down_flag = 0 ;

        }

    }

}

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

I could confirm that this project can be built,

but as I don't have a board with the specified MCU I could not test this.

When I copied this project and re-targeted to 4200M,

it worked with CY8CKIT-044 (LED was P2[6]).

moto