PWMのレジスタ書き換えについて

Tip / ログイン to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
fuki_949671
Level 3
Level 3
10 replies posted 5 replies posted 10 questions asked

PWMコンポーネントのPeriodとCMP Valueの書き換えについて質問があります。

例えば、起動直後に

Periodに999、CMP Valueに899と設定するとDuty比で1対9のパルスになります。

二回目に、同じ値の書き込みを行うと、Duty比で5対5のパルスになり設定値通りのパルスになりません。

希望のDuty比に設定するには、どうすれば良いでしょうか?

0 件の賞賛
1 解決策
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

こんばんは、

お書きになっている現象から、PWM 設定は period : compare = 10 : 5 になっているのではないかと思います。

003-pwm-config.JPG

実験の為に CY8CKIT-059 で以下のようなサンプルを作ってみました。

胆は config_pwm() という関数で、

PWM を止めて、設定、PWM のカウントを0にする、PWMを再スタート (Enable)

という処理をしているところではないでしょうか。

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

void config_pwm(uint16_t period, uint16_t compare)

{

    PWM_Stop() ;

    PWM_WritePeriod(period) ;

    PWM_WriteCompare(compare) ;

    PWM_WriteCounter(0) ;

    PWM_Enable() ;

}

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

UART からの入力処理は自作の tty_utils を使用して手を抜いています。

Re: tty_utils a utility sample for CLI type program

schematic

001-schematic.JPG

pins

002-pins.JPG

main.c

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

#include "project.h"

#include "stdio.h"

#include "tty_utils.h"

#include "string.h"

#define CMD_LEN 16

uint16_t period = 999 ;

uint16_t compare = 899 ;

void usage(void)

{

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

    print("pwm <period> <compare>\n\r") ;

    print("period <peroid>\n\r") ;

    print("compare <compare>\n\r") ;

    print("\n\r") ;

}

void report(void)

{

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

    print("PWM : ") ;

    snprintf(str, STR_BUF_LEN, "period:  %d ", period) ;

    print(str) ;

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

    print(str) ;

}

void config_pwm(uint16_t period, uint16_t compare)

{

    PWM_Stop() ;

    PWM_WritePeriod(period) ;

    PWM_WriteCompare(compare) ;

    PWM_WriteCounter(0) ;

    PWM_Enable() ;

}

void do_command(void)

{

    char cmd[16] ;

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

    if (strcmp(cmd, "pwm") == 0) {

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

    } else if (strcmp(cmd, "period") == 0) {

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

    } else if (strcmp(cmd, "compare") == 0) {

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

    } else if (strcmp(cmd, "report") == 0) {

        report() ;

    } else {

        usage() ;

    }

    config_pwm(period, compare) ;

}

void init_hardware(void)

{

    CyGlobalIntEnable; /* Enable global interrupts. */

  

    tty_init() ;

    splash("PWM Duty Test on 5LP") ;

  

    PWM_Init() ;

    config_pwm(period, compare) ;

}

int main(void)

{

   init_hardware() ;

    prompt() ;

    for(;;) {

        if (get_line()) {

            do_command() ;

            prompt() ;

        }

    }

}

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

プログラムを実行したときの Tera Term の様子は

000-TeraTerm-log.JPG

起動直後の波形 period: 999 compare: 899

IMG_4181.JPG

compare 499にした場合

IMG_4182.JPG

compare 99 にした場合

IMG_4183.JPG

オシロの写真、踊っていてすみません。

というように、

pwm <periodvalue> <compare_value>

period <period_value>

compare <compare_value>

と入力して波形を変えられますので

遊んでみてください。

moto

元の投稿で解決策を見る

0 件の賞賛
2 返答(返信)
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

こんばんは、

お書きになっている現象から、PWM 設定は period : compare = 10 : 5 になっているのではないかと思います。

003-pwm-config.JPG

実験の為に CY8CKIT-059 で以下のようなサンプルを作ってみました。

胆は config_pwm() という関数で、

PWM を止めて、設定、PWM のカウントを0にする、PWMを再スタート (Enable)

という処理をしているところではないでしょうか。

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

void config_pwm(uint16_t period, uint16_t compare)

{

    PWM_Stop() ;

    PWM_WritePeriod(period) ;

    PWM_WriteCompare(compare) ;

    PWM_WriteCounter(0) ;

    PWM_Enable() ;

}

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

UART からの入力処理は自作の tty_utils を使用して手を抜いています。

Re: tty_utils a utility sample for CLI type program

schematic

001-schematic.JPG

pins

002-pins.JPG

main.c

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

#include "project.h"

#include "stdio.h"

#include "tty_utils.h"

#include "string.h"

#define CMD_LEN 16

uint16_t period = 999 ;

uint16_t compare = 899 ;

void usage(void)

{

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

    print("pwm <period> <compare>\n\r") ;

    print("period <peroid>\n\r") ;

    print("compare <compare>\n\r") ;

    print("\n\r") ;

}

void report(void)

{

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

    print("PWM : ") ;

    snprintf(str, STR_BUF_LEN, "period:  %d ", period) ;

    print(str) ;

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

    print(str) ;

}

void config_pwm(uint16_t period, uint16_t compare)

{

    PWM_Stop() ;

    PWM_WritePeriod(period) ;

    PWM_WriteCompare(compare) ;

    PWM_WriteCounter(0) ;

    PWM_Enable() ;

}

void do_command(void)

{

    char cmd[16] ;

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

    if (strcmp(cmd, "pwm") == 0) {

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

    } else if (strcmp(cmd, "period") == 0) {

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

    } else if (strcmp(cmd, "compare") == 0) {

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

    } else if (strcmp(cmd, "report") == 0) {

        report() ;

    } else {

        usage() ;

    }

    config_pwm(period, compare) ;

}

void init_hardware(void)

{

    CyGlobalIntEnable; /* Enable global interrupts. */

  

    tty_init() ;

    splash("PWM Duty Test on 5LP") ;

  

    PWM_Init() ;

    config_pwm(period, compare) ;

}

int main(void)

{

   init_hardware() ;

    prompt() ;

    for(;;) {

        if (get_line()) {

            do_command() ;

            prompt() ;

        }

    }

}

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

プログラムを実行したときの Tera Term の様子は

000-TeraTerm-log.JPG

起動直後の波形 period: 999 compare: 899

IMG_4181.JPG

compare 499にした場合

IMG_4182.JPG

compare 99 にした場合

IMG_4183.JPG

オシロの写真、踊っていてすみません。

というように、

pwm <periodvalue> <compare_value>

period <period_value>

compare <compare_value>

と入力して波形を変えられますので

遊んでみてください。

moto

0 件の賞賛

ご返信ありがとうございます。

PWM のカウントを0にする処理を追加し、希望の動作をするようになりました。

初歩的な質問に対し、いつも分かりやすいご丁寧なお返事ありがとうございます。