1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

M5NanoC6のPWM信号を0から180に変換する(STM32L010)

Last updated at Posted at 2024-09-28

x 過去ログを見よ!!!
x 電子工作なのでArduino

前の記事

目的
M5NanoC6にサーボをつけて遊びたい
ソフトウェアシリアルに出力
1kHz 5%から95%を0から180に変換(とりあえず)(サブマイコンで)

o_coq463.jpg




//PWM_IN_1kPWM_OUT_50Hz_1500us_L010_1


#include "mbed.h"


DigitalOut TX(PA_4);
#define UART_DELAY 101 //  1/9600 98-105

//仮想シリアルへの一文字出力 9600bps
int pc_putc(char ch)
{
    TX=1;
    TX=0;//Start
    wait_us(UART_DELAY);

    for(int ii=0; ii<8; ii++) { //Data x 8
        TX=(ch>>ii)&1;
        wait_us(UART_DELAY);
    }; //for

    TX=1;//Stop
    wait_us(UART_DELAY);

    return(0);
} //pc_putc


//数字の表示 ただし約3000まで
int pc_print_i(int n){

    //int n; //変換元
    int i; //カウンター
    int a; //一時変数
    int m; //符号
    char buf[32];
    
    //符号を外す
    m = 0;//正の数
    if(n < 0) {n = 0 - n;m = 1;}
    
    //0の時、0を表示
    if (n == 0) {
        //printf("0");
        pc_putc('0');
        return 0;
    }//if

    //10の桁分づつ変換する
    i = 0;
    while (n > 0) {
            
        a = (n * 52429) >> 19; // n / 10;
        buf[i++] = n - (a * 10);
        n = a;
                        
    }//while

    //符号をつける
    if(m != 0){printf("-");}

    //逆から数字を表示する
    for (; i > 0; i--){
        pc_putc( '0' + buf[i - 1] );
    }//for

}//pc_print_i


//リターン付 数字の表示
int pc_println_i(int n){

    pc_print_i(n);
    pc_putc('\r');
    pc_putc('\n');

}//pc_println_i


#define HIGH 1
DigitalIn  ECHO(PA_5);
//タイマーの設定
Timer t;
//マスク       5432109876543210
int masuk = 0b0000000000100000;

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

    //t.reset();//タイマーの開始

    //timeout1 = 32000000;
    timeout1 = 100000;    //28ms
    //timeout1 = 1000;   //degbu

    //while(PA_5 == 0) {}

loop_s1:
    timeout1--;
    //                            5432109876543210
    if (     (~(GPIOA->IDR))  & masuk     ) {
        if ( timeout1 ) {
            goto loop_s1;
        }
    }


    //unsigned long sd = t.read_us();//測定開始

    //timeout1 = 32000000;
    timeout1 = 100000;     //28ms
    //timeout1 = 1000;   //degbu

    t.reset();//タイマーの開始

    //while(PA_5 != 0) {}

loop_in1:
    timeout1--;
    //                      5432109876543210
    if (   (GPIOA->IDR) & masuk   ) {
        if( timeout1 ) {
            goto loop_in1;
        }
    }


    return((int)t.read_us()/*-sd*/);//測定終了

}//pulseIn


//PWMのピンの設定
//PwmOut servo(A4); //PA_5   //010
//PwmOut servo(PA_4); //PA_5   //010
//PwmOut servo(PA_3); //PA3   //010

//メインルーチン
int main()
{

    //タイマーの開始
    t.start();

    int times; //測定時間

    TX=1; //ポートの初期化

    //周期の設定        
    //servo.period_us(1000); //1khz

    //間隔の設定
    //servo.pulsewidth_us(500); //50%

    //無限ループ
    while(1) {

        // PWMを受け取る 引数は、無効(空読み)
        times = pulseIn( 5, HIGH,2000000);
        // PWMを受け取る 引数は、無効
        times = pulseIn( 5, HIGH,2000000);

        //少し論理値よりズレるので補正
        times = ((times * (1024+5))>>10)-2;
        if( times <   50){times =  50;}
        if( times >  950){times = 950;}
        times = times - 50;
        times = times * 2;
        times = (times * 52429) >> 19; // n/10


        //ソフトウェアシリアルから距離を出力
        pc_println_i(times);

        //0.2秒の待ち
        wait_ms(200);

        //0.5秒の待ち
        //wait_ms(500);

    }//while

}//main


//容量削減
void error(const char* format, ...) {}



1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?