LoginSignup
0
0

More than 1 year has passed since last update.

STM32L010のソフトウェアシリアルで文字を受信する STM32 RX 9600bps

Last updated at Posted at 2021-05-12

x Mbedのリビジョンは、125

目的
表示器等用のソフトウェアシリアルを作ってみた。

忙しい人よう
https://os.mbed.com/users/caa45040/code/serial_rx_test_010/


#include "mbed.h"

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

//Serial pc(USBTX, USBRX); // tx, rx
//Serial pc(SERIAL_TX, SERIAL_RX); //767
//Serial pc(PA_2, PA_3); //010
//Serial pc(PA_9, PA_10); //010

#define UART_DELAY 96 //  1/9600

DigitalOut TX(PA_2);
DigitalIn  RX(PA_3);
//DigitalOut TX(PA_9);
//DigitalIn  RX(PA_10);

char ch_hex_a_b[5];
char *ch_hex_a(int l_num)
{
    int a,b,c;

    int q=0;
if(l_num  < 0) { l_num = 0 - l_num; q = 1;} 

    b=DVI10(l_num);
    c=l_num-(b*10);
    l_num=b;
    a=DVI10(l_num);
    b=l_num-(a*10);

    ch_hex_a_b[0] = '0' + a;
    ch_hex_a_b[1] = '0' + b;
    ch_hex_a_b[2] = '0' + c;
    ch_hex_a_b[3] = 0;

if( q == 1 ) {ch_hex_a_b[0]='-';}

    return(ch_hex_a_b);
} //ch_hex_a


//仮想シリアルへの一文字出力 9600bps
int pc_putc(char ch) {

    TX=1;
    TX=0;//START
    wait_us(UART_DELAY);

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

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

    return(0);

} //pc_putc

//文字列の表示
int pc_printf(char *str1) {

    int ii = 0; //ループカウンター
    while(str1[ii]!=0){

        //一文字出力
        pc_putc(str1[ii]);ii++;

    } //while

    //戻り値
    return(0);
}

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

int a,b,c,d,e,f,g,h;
int bs;
int zt,at,bt,ct,dt,et,ft,gt,ht,it;
char *ret1="\r\n";


int pc_getc()
{

    //待ちループ
    while( RX == 1 ) {} 

//llllllllllllllllllllllllllllllllllllllll

    zt = t.read_us();

    wait_us(156-7-12);

    a=RX;
    at = t.read_us();

    wait_us(104-7-11);

    b=RX;
    bt = t.read_us();

    wait_us(104-7-11);

    c=RX;
    ct = t.read_us();

    wait_us(104-7-11);

    d=RX;
    dt = t.read_us();

    wait_us(104-7-10);

//hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh   

    e=RX;
    et = t.read_us();

    wait_us(104-7-11);

    f=RX;
    ft = t.read_us();

    wait_us(104-7-12);

    g=RX;
    gt = t.read_us();

    wait_us(104-7-11);

    h=RX;
    ht = t.read_us();

    wait_us(156-7-11);

    it = t.read_us();

//pc_printf(ret1);
//pc_printf("0    zt=");
//pc_printf(  ch_hex_a(zt)  );
//pc_printf(ret1);

//pc_printf("156  at=");
//pc_printf(  ch_hex_a( 156  - (at-zt) )  );
//pc_printf(ret1);

//pc_printf("260  bt=");
//pc_printf(  ch_hex_a( 260  - (bt-zt) )  );
//pc_printf(ret1);

//pc_printf("364  ct=");
//pc_printf(  ch_hex_a( 364  - (ct-zt) )  );
//pc_printf(ret1);

//pc_printf("468  dt=");
//pc_printf(  ch_hex_a( 468  - (dt-zt) )  );
//pc_printf(ret1);

//pc_printf("572  et=");
//pc_printf(  ch_hex_a( 572  - (et-zt) )  );
//pc_printf(ret1);

//pc_printf("677  ft=");
//pc_printf(  ch_hex_a( 677  - (ft-zt) )  );
//pc_printf(ret1);

//pc_printf("781  gt=");
//pc_printf(  ch_hex_a( 781  - (gt-zt) )  );
//pc_printf(ret1);

//pc_printf("885  ht=");
//pc_printf(  ch_hex_a( 885  - (ht-zt) )  );
//pc_printf(ret1);

//pc_printf("1041 it=");
//pc_printf(  ch_hex_a( 1041 - (it-zt) )  );
//pc_printf(ret1);

    //h=0; g=1; f=0; e=0; d=0; c=0; b=0; a=1;

    bs=h*128+g*64+f*32+e*16+d*8+c*4+b*2+a;

    //char str2[2]={ bs ,0};
    //pc_printf("     ch=[");
    //pc_printf(  str2  );
    //pc_printf(ret1);

    return(bs);
}//pc_getc

int main()
{
    RX.mode( PullUp );   //  内蔵プルアップを使う

    //タイマーの開始
    t.start();
    //pc.printf("Hello World!\r\n");
    //t.stop();

    char ch;

    while(1) {

        t.reset();

        ch = pc_getc();

        char str2[2]={ ch ,0};
        pc_printf("-----ch=[");
        pc_printf(  str2  );
        pc_printf(ret1);


        wait(1.0);
    }//while
}//main

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



timer_test_010_2.jpg

serial_test_010_2_3.jpg

serial_test_010_2_4.jpg

serial_test_010_2_5.jpg

serial_test_p1_3.jpg

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