LoginSignup
0
0

More than 1 year has passed since last update.

STM32L010で周波数の測定を作てみた。(約10khzぐらいまで)(PWM測定利用)

Last updated at Posted at 2021-08-14

x mbed2のリビジョン143

目的
基板作成が落ち着いたから
内部タイマーを使いusでオン時間で周波数を測る。
ディティが50対50じゃないと測れない。
シリアルに出力する。
レジスター直読み込みで高速化した。

x  t.start()は、pwmの測定で利用されている

だいたい1khzを入力した

serial_counter_010_1_1.jpg

りろいろ改良 特にpwm測定


#include "mbed.h"

//10の割り算 0から1028までは、正しい。主に0から999
#define DVI10(n) ((n*205)>>11)

//1000の割り算 だいたい正しい
#define DVI1000(n) ((n>>10)+(((n>>10)*32)>>10))

#define HIGH 1
DigitalIn in1(PA_1);

DigitalOut led1(PA_4); //debug

RawSerial pc(PA_2, PA_3); //010 tx, rx

//タイマーの設定
Timer t;

//in1のオン時間をusで測る 今のところ引数は、無効
int pulseIn(int pin1,int pu1,int timeout1)
{
    t.reset();

    //while(in1 != 0) {}

    loop_ins:
    //                      1234567890123456
    if (   (GPIOA->IDR) & 0b0000000000000010   ) { goto loop_ins;}

    //while(in1 == 0) {}

    loop_s1:
    //                            1234567890123456
    if (     (~(GPIOA->IDR))  & 0b0000000000000010    ) { goto loop_s1;}

    //led1 = 1;//debug

    int sd = t.read_us();


    //while(in1 != 0) {}

    loop_in1:
    //                      1234567890123456
    if (   (GPIOA->IDR) & 0b0000000000000010   ) { goto loop_in1;}

    //led1 = 0;//debug

    return(t.read_us()-sd);

}//pulseIn

int main() {

    t.start();

    int pwmco2;
    int hz;

    while(1){

        //PWMでデータ取得
        pwmco2=pulseIn(7 ,HIGH,2000000);
//        printf("The time taken was %d seconds\r\n", pwmco2  );
//        printf("The time taken was %d seconds\r\n", DVI1000(pwmco2)  );

        pwmco2=pwmco2*2;
        if(pwmco2==0){pwmco2=1;}
        hz = 1000000/pwmco2;
//        printf("The time taken was %d hz\r\n", hz );
        printf("%d\r\n", hz );


        //0.3秒待つ
        wait_ms(300);

    }//while

}//main


改良まえ



#include "mbed.h"

//10の割り算 0から1028までは、正しい。主に0から999
#define DVI10(n) ((n*205)>>11)

//1000の割り算 だいたい正しい
#define DVI1000(n) ((n>>10)+(((n>>10)*32)>>10))

#define HIGH 1
DigitalIn in1(PA_1);

DigitalOut led1(PA_4); //debug

RawSerial pc(PA_2, PA_3); //010 tx, rx

//タイマーの設定
Timer t;

//in1のオン時間をusで測る 今のところ引数は、無効
int pulseIn(int pin1,int pu1,int timeout1)
{

    //while(in1 == 0) {}

    loop_s1:
    //                     1234567890123456
    if (   (GPIOA->IDR & 0b0000000000000010) == 0   ) { goto loop_s1;}

    //led1 = 1;//debug

    //t.start();
    t.reset();
    wait_us(37); //debug

    //while(in1 != 0) {}

    loop_in1:
    //                     1234567890123456
    if (   (GPIOA->IDR & 0b0000000000000010) != 0   ) { goto loop_in1;}

    //t.stop();

    //led1 = 0;//debug

    return(t.read_us());

}//pulseIn

int main() {

    t.start();

    int pwmco2;
    int hz;

    while(1){

        //PWMでデータ取得
        pwmco2=pulseIn(7 ,HIGH,2000000);
//        printf("The time taken was %d seconds\r\n", pwmco2  );
//        printf("The time taken was %d seconds\r\n", DVI1000(pwmco2)  );

        pwmco2=pwmco2*2;
        if(pwmco2==0){pwmco2=1;}
        hz = 1000000/pwmco2;
//        printf("The time taken was %d hz\r\n", hz );
        printf("%d\r\n", hz );


        //0.3秒待つ
        wait_ms(300);

    }//while

}//main






0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0