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

PSoC™ 4

UlMo_4589691
Level 1
First like given
Level 1

Hello,

now I have a touch sensor and a LED. The touch sensor enables the LED. Now I want to preset the brightness of the LED. How can I do this?

0 Likes
1 Solution
MotooTanaka
Level 9
First comment on KBA 1000 replies posted 750 replies posted
Level 9

Hi,

It was my mistake, before setting period PWM_Start() or PWM_Init() must have been called.

Note: PWM_Init() is called at the first time PWM_Start() is called

003-pwm_start.JPG

And in the PWM_Init(), period is overwritten to the value set in the GUI.

So I should have written

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

PWM_Start() ;

PWM_Stop() ; /* before changing the period, PWM must be stopped */

PWM_WritePeriod(PWM_PERIOD) ;

PWM_WriteCompare(led_level) ;

PWM_Start() ;

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

To test if PWM_WritePeriod() really works,

I made following test program.

schematic

001-schematic.JPG

pins

002-pins.JPG

main.c

Note: To handle UART interactions I cheated by using my tty_utility

tty_utils a utility sample for CLI type program

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

#include "project.h"

#include "stdio.h"

#include "tty_utils.h"

#define PWM_PERIOD 1000 /* 65535 (0xFFFF) was too much for this purpose */

#define LED_OFF       0

#define LED_DIM      10 /* could be anywhere from 1 to 1000 */

uint16_t led_period = PWM_PERIOD ;

uint16_t led_level = LED_DIM ; /* set default to ON */

void config_pwm(uint16_t period, uint16_t compare) ;

typedef void (*func_ptr_type)(char *str) ;

void do_help(char *str) ;

void do_period(char *str) ;

void do_compare(char *str) ;

void do_pwm(char *str) ;

void do_status(char *str) ;

typedef struct _cmd_func_st {

    char *cmd ;

    func_ptr_type func ;

    char *description ;

} cmd_func_type ;

cmd_func_type cmd_list[] = {

    { "help",    do_help,    "Print Help Message" },

    { "period",  do_period,  "Set PWM Period value, period 1000" },

    { "compare", do_compare, "Set PWM Compare value, compare 10" },

    { "pwm",     do_pwm,     "Set PWM Config period and compare, pwm 1000 10" },

    { "status",  do_status,  "Show current setting" },

    { 0, 0, 0 }

} ;

void do_help(char *str)

{

    (void) str ;

    int i = 0 ;

   

    print("=== command ===\n\r") ;

    for (i = 0 ; cmd_list.cmd ; i++ ) {

        snprintf(str, STR_BUF_LEN, "%10s : ", cmd_list.cmd) ;

        print(str) ;

        print(cmd_list.description) ;

        print("\n\r") ;

    }

}

void do_period(char *str)

{

    sscanf(str, "%*s %hd", &led_period) ;

    config_pwm(led_period, led_level) ;

}

void do_compare(char *str)

{

    sscanf(str, "%*s %hd", &led_level) ;

    config_pwm(led_period, led_level) ;

}

void do_pwm(char *str)

{

    sscanf(str, "%*s %hd %hd", &led_period, &led_level) ;

    config_pwm(led_period, led_level) ;   

}

void do_status(char *str)

{

    uint16_t period, compare ;

    period = PWM_ReadPeriod() ;

    compare = PWM_ReadCompare() ;

    print("=== PWM Status ===\n\r") ;

    snprintf(str, STR_BUF_LEN, "Period: %hd\r\n", period) ;

    print(str) ;

    snprintf(str, STR_BUF_LEN, "Compare: %hd\r\n", compare) ;

    print(str) ;

}

void init_hardware(void)

{

    CyGlobalIntEnable; /* Enable global interrupts. */

    CapSense_1_Start() ;

   

    UART_Start() ;

    tty_init() ;

    splash("CapSense and LED PWM Test") ;

   

    PWM_Start() ;

}

void config_pwm(uint16_t period, uint16_t compare)

{

    PWM_Stop() ;

    PWM_WritePeriod(period) ;

    PWM_WriteCompare(compare) ;

    PWM_Start() ;

}

func_ptr_type get_func(char *cmd)

{

    int i ;

    for (i = 0 ; cmd_list.cmd ; i++ ) {

        if (strcmp(cmd, cmd_list.cmd) == 0) {

            break ;

        }

    }

    return( cmd_list.func ) ;

}

void do_command(char *str)

{

    char command[STR_BUF_LEN+1] ;

    func_ptr_type func ;

    sscanf(str, "%s", command) ;

    func = get_func(command) ;

    if (func) {

        func(str) ;

    } else {

        do_help(str) ;

    }

}

int main(void)

{  

    init_hardware() ;

   

    config_pwm(led_period, led_level) ;

    prompt() ;

   

    for(;;) {

        if (get_line()) {

            do_command(str) ;

            prompt() ;

        }

        if (!CapSense_1_IsBusy()) {

            CapSense_1_ProcessAllWidgets() ;

           

            if (CapSense_1_IsSensorActive(CapSense_1_BUTTON0_WDGT_ID, CapSense_1_BUTTON0_SNS0_ID)) {

                if (led_level == LED_OFF) {

                    led_level = LED_DIM ;

                } else {

                    led_level = LED_OFF ;

                }

                PWM_WriteCompare(led_level) ;

                CyDelay(500) ; /* cheap debouncing */

            }      

           

            CapSense_1_ScanAllWidgets() ;   

        }

    }

}

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

Tera Term log

001-tera_term_log.JPG

When the program started, as Period = 1000, Compare = 10 and the PWM Clock = 2MHz

About 2KHz pulse(s) are generated

TEK0000.jpg

When I entered "pwm 2000 1000" to the prompt (>)

Now frequency is 2KHz and as compre value is 1000 the duty is 50%

TEK0001.jpg

When I entered "pwm 3000 1000"

The frequency was about 668Hz and duty is about 30%

TEK0002.jpg

When I entered "pwm 1500 1000", frequency was about 1.336 kHz

and duty is about 67%.

TEK0003.jpg

So the bad news is that I had a bug in my previous program,

and the good news is PWM_WritePeriod() does its work 😉

moto

View solution in original post

0 Likes
10 Replies