STM32G031 소프트웨어 직렬로 문자 수신 STM32 RX 9600bps

목적
표시기 등용 소프트웨어 시리얼을 만들어 보았다.


//#define in7 1 //uno
//#define in7      PB7  // 1pin
//#define in7      PA9  // 1pin
#define RX1      PA10 // 1pin
#define TX1      PA9  // 1pin


#define DW   digitalWrite

//#define UART_DELAY 832 //  1200bps ok 031
#define UART_DELAY 102   //  9600bps ok 031

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

char *ret1="\r\n";






int a,b,c,d,e,f,g,h;
int bs;
int zt,at,bt,ct,dt,et,ft,gt,ht,it;


int pc_getc()
{

String thisString9;

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

//llllllllllllllllllllllllllllllllllllllll

    zt = micros();

    delayMicroseconds(156-3);

    a=(digitalRead(RX1));
    at = micros();

    delayMicroseconds(104-3);

    b=(digitalRead(RX1));
    bt = micros();

    delayMicroseconds(104-4);

    c=(digitalRead(RX1));
    ct = micros();

    delayMicroseconds(104-4);

    d=(digitalRead(RX1));
    dt = micros();

    delayMicroseconds(104-3);

//hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh   

    e=(digitalRead(RX1));
    et = micros();

    delayMicroseconds(104-3);

    f=(digitalRead(RX1));
    ft = micros();

    delayMicroseconds(104-4);

    g=(digitalRead(RX1));
    gt = micros();

    delayMicroseconds(104-4);

    h=(digitalRead(RX1));
    ht = micros();

    delayMicroseconds(156-3);

    it = micros();

//pc_printf(ret1);
//pc_printf("0    zt=");
//  thisString9 = String(  (zt)  );
//  pc_printf( (char *)thisString9.c_str() );
//pc_printf(ret1);

//pc_printf("156  at=");
//  thisString9 = String(  156  - (at-zt)   );
//  pc_printf( (char *)thisString9.c_str() );
//pc_printf(ret1);

//pc_printf("260  bt=");
//  thisString9 = String(  260  - (bt-zt)   );
//  pc_printf( (char *)thisString9.c_str() );
//pc_printf(ret1);

//pc_printf("364  ct=");
//  thisString9 = String(  364  - (ct-zt)    );
//  pc_printf( (char *)thisString9.c_str() );
//pc_printf(ret1);

//pc_printf("468  dt=");
//  thisString9 = String(  468  - (dt-zt)    );
//  pc_printf( (char *)thisString9.c_str() );
//pc_printf(ret1);

//pc_printf("572  et=");
//  thisString9 = String(  572  - (et-zt)   );
//  pc_printf( (char *)thisString9.c_str() );
//pc_printf(ret1);

//pc_printf("677  ft=");
//  thisString9 = String(  677  - (ft-zt)    );
//  pc_printf( (char *)thisString9.c_str() );
//pc_printf(ret1);

//pc_printf("781  gt=");
//  thisString9 = String(  781  - (gt-zt)   );
//  pc_printf( (char *)thisString9.c_str() );
//pc_printf(ret1);

//pc_printf("885  ht=");
//  thisString9 = String(  885  - (ht-zt)   );
//  pc_printf( (char *)thisString9.c_str() );
//pc_printf(ret1);

//pc_printf("1041 it=");
//  thisString9 = String(  1041 - (it-zt)   );
//  pc_printf( (char *)thisString9.c_str() );
//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 q_st; //スタートタイム debug
int q_et; //エンドタイム   debug

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

  DW(TX1, HIGH);

    q_st =  micros(); //debug

  DW(TX1, LOW);//START
  delayMicroseconds(UART_DELAY);

  for(int ii=0;ii<8;ii++){
    DW(TX1, (ch>>ii)&1  );
    delayMicroseconds(UART_DELAY);
  }//for

  DW(TX1, HIGH);//Stop
  delayMicroseconds(UART_DELAY);

    q_et =  micros(); //debug 引いた数が0で1041usなら正解

  return(0);

}//pc_putc

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

    //文字の中身がゼロか
    while(*str1){

        //一文字出力
        pc_putc(*str1 ++);
        //Serial.print( *str1 ++ ); //uno

    } //while

    //戻り値
    return(0);
}//pc_printf

//0から999まで文字列に変換
char ch_hex_a_b[5];
char *ch_hex_a(int l_num)
{
  int a,b,c;

  b=l_num/10;
  c=l_num-(b*10); //1の桁
  a=b/10;         //100の桁
  b=b-(a*10);     //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;

  return(ch_hex_a_b);
}//ch_hex_a

//初期化
void setup()
{
  delay(3000);
  //ポートをhiにする 初期化
  pinMode(TX1, OUTPUT);
  DW(TX1, HIGH);
  delayMicroseconds(UART_DELAY*10);

  pinMode(RX1,INPUT_PULLUP);


//d  //pinMode(A3, OUTPUT);
//d  DW(TX1,HIGH);
//d  //pc_putc('A');
//d  delay(1000);
//d  DW(TX1,LOW);
//d  delay(1000);
//d  DW(TX1,HIGH);
//d  //pc_putc('A');
//d  delay(1000);
//d  DW(TX1,LOW);
//d  delay(1000);




  //Serial.begin(9600);

} //setup

  char ch;

//メインループ
void loop()
{

//  pc_putc('A');
//
//  String thisString2 = String(  (q_et-q_st)  );
//  pc_printf( (char *)thisString2.c_str() );
//  pc_printf("\r\n");

  ch = pc_getc();

  //ch = 'A';

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

  //1秒の待ち
  delay(1000);

} //loop





좋은 웹페이지 즐겨찾기