PWM LED brightness

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

cross mob
lock attach
Attachments are accessible only for community members.
Marcus_Hellen
Level 3
Level 3
10 replies posted 10 questions asked 10 likes given

Hello Everyone,

I am using cy8ckit-041-41xx Psoc 4S- Series Pioneer kit. I want to change led brightness via pwm. I am sharing my Topdesign and code. My goal is when I touch my touchpad over on X coordinate led brightness should change. For example, when X coordinate equal to 0, Led brightness sould be lower. When X coordinate equal to 100, Led brightness sould be highest. 

 

First thing which comes to my mind is : 

Xcord = CapSense_GetXYCoordinates(CapSense_TOUCHPAD_WDGT_ID);
Ycord = Xcord >>16;
Xcord = (uint16)Xcord;

PWM_WritePeriod(100* Xcord);
LED2_Write(LED_ON); 

but it did not work. 

Could you help me please?

Marcus

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,

 

In recent PSoC Creator GUI, we can not set compare value to period value, 

but to realize duty = 100%, we need to set compare value to period value (or greater).

Meantime, to realize duty = 0%, we need to set compare value to 0.

And these can be done within the software.

 

So I hacked the following test program with CY8CKIT-149 (CY8C4147AZI-S475)

Note: For your board, please change the target device to your device and change pins to fit your board.

 

When built and run, the serial output to Tera Term was something like

001-TeraTerm-log.JPG

When I entered "compare 0" the LED was total off (duty = 0%).

When I entered "compare 9999", which is period == compare,

and the LED was the brightest (duty = 100%)

When I entered "compare 10000", which is period < compare,

but program and PWM component accepted the value, but the brightness did not increase.

When I entered "compare 4999", which is period == 2 * compare, and LED was about half bright.

 

Followings are the brief of the project

Note: As usual, I cheated with using my tty_utils.

Schematic

002-schematic.JPG

Pins

003-Pins.JPG

main.c

#include "project.h"
#include "stdio.h"
#include "tty_utils.h"

char    cmd[STR_BUF_LEN+1] ;

void init_hardware(void)
{
    CyGlobalIntEnable; /* Enable global interrupts. */
    tty_init() ;
    PWM_Start() ;
}

void help(void)
{
    print("=== Usage ===\n\r") ;
    print("period  <value> : set PWM period to  <value>\n\r") ;
    print("compare <value> : set PWM compare to <value>\n\r") ;
    print("status          : print PWM status\n\r") ;
    print("help            : print this usage\n\r") ;
    print("=============\r\n") ;
}

void status_report(void)
{
    uint16_t period, compare ;
    period = PWM_ReadPeriod() ;
    compare = PWM_ReadCompare() ;
    snprintf(str, STR_BUF_LEN,
        "PWM: Period: %hu,  Compare: %hu\n\r",
        period, compare) ;
    print(str) ;
}

void set_pwm_period(uint16_t period)
{
    PWM_Stop() ;
    PWM_WritePeriod(period) ;
    PWM_WriteCounter(0) ;
    PWM_Enable() ;
    status_report() ;
}

void set_pwm_compare(uint16_t compare) 
{
    // PWM_Stop() // for compare PWM can be running
    PWM_WriteCompare(compare) ;
    PWM_WriteCounter(0) ; // When counter == 0 compare value gets updated
    // PWM_Enable() // for compare PWM can be running
    status_report() ;
}

int main(void)
{
    uint16_t period, compare ;
    
    init_hardware() ;
    
    period = PWM_ReadPeriod() ;
    compare = PWM_ReadCompare() ;
    
    splash("PWM TEST") ;
    help() ;
    status_report() ;
    
    prompt() ;

    for(;;) {
        if (get_line()) {
            sscanf(str, "%s", cmd) ;
            switch(cmd[0]) {
            case 'p': case 'P': // period
                sscanf(str, "%*s %hu", &period)  ;
                set_pwm_period(period) ;
                break ;
            case 'c': case 'C': // compare
                sscanf(str, "%*s %hu", &compare) ;
                set_pwm_compare(compare) ;
                break ;
            case 's': case 'S': // status report
                status_report() ;
                break ;
            default: 
                help() ;
                break ;
            }
            prompt() ;
        }
    }
}

 

moto

View solution in original post

3 Replies